iangreenleaf

iangreenleaf

Here’s the problem I’m working on: bots probing my site for vulnerabilities will try injecting special character sequences into params, like /articles/abc%DE~%C7%1FY, which becomes the binary <<97, 98, 99, 222, 126, 199, 31, 89>>, which is not a valid string.

No doubt this attack targets Oracle Server 2003 or something, I don’t know. It’s not going to cause any harm to my app, but it does end up triggering a Postgrex error because the invalid binary makes it all the way into the SELECT query before being rejected as invalid UTF-8.

I’d like to catch this earlier and return an appropriate 4xx error for invalid input rather than a 500 error when the DB query fails. Plug.Parsers has an option to validate UTF-8 in body and query params, so that a request like /articles/abc?a=b%DE~%C7%1FY will throw a relevant exception, but it seems like the path params aren’t checked in the same way.

I’m not sure how to attack this problem. I don’t want to add a check individually to every controller, since this is an application-wide need. Should the path params be run through the same parser checks as other params, or is there a reason they aren’t?

First 8 of 8 Posts Switch mode

NobbZ

NobbZ

You could add a plug which checks the :request_path. Something like this:

plug fn (conn, _opts) ->
  if String.valid?(conn.request_path) do
    conn
  else
    conn
    |> Plug.Conn.put_status(:im_a_teapot)
    |> Plug.Conn.halt()
  end
end

This is a quick draft based on the docs. You might want to adjust some parts of it, add some content, make it a module based plug or change the status code sent :smiley:

Also this code assumes, that the :request_path is already decoded at this point. If it is not you can use URI.decode/1 to do so.

iangreenleaf

iangreenleaf OP

Thanks! I realized I could modify your suggestion to use the Plug validation function and throw a relevant exception, mimicking the behavior of the other param parsing:

  plug :validate_path_utf8
  defp validate_path_utf8(conn, _opts) do
    conn.request_path
    |> URI.decode()
    |> Plug.Conn.Utils.validate_utf8!(Plug.BadRequestError, "path params")

    conn
  end

Phoenix handles this well with a 400 on production. Doing it in a plug feels a bit like a workaround, but it seems to be working well enough.

sribe

sribe

As to a different part of your question, I don’t think the standard requires the path to be valid UTF8. If I’m right, it wouldn’t be appropriate for the default behavior to require it–leaving it up to devs to check the path for stricter requirements would be right.

(After all, the path used to commonly be a path to a file on disk, and Windows would have supported paths in the local interpretation of 8-bit extension to ASCII…)

malaire

malaire

Current URL standard seems to be based on valid UTF-8 encoding.

sribe

sribe

I cede to your superior google-fu

iangreenleaf

iangreenleaf OP

Agreed, that’s how I read this sentence in Section 1.3:

Sequences of percent-encoded bytes, percent-decoded, should not cause UTF-8 decode without BOM or fail to return failure.

I imagine this is why Plug.Parsers offers the :validate_utf8 option and sets it to true by default. Most of the time, we should expect valid UTF-8 input, but there are situations where people might want to bypass it.

NobbZ

NobbZ

I have never heard of that standard, but instead used RFC 3986 as a reference, where they explicitely state, that the standard does not define any particular encoding, but uses US-ASCII throughout the document.

Section 2, 1. paragraph:

[…] This specification does not mandate any particular
character encoding for mapping between URI characters and the octets
used to store or transmit those characters. […]

malaire

malaire

These new “Living Standards” seem to be quite new. But W3C does say that for HTML the WHATWG standard is current standard:

HTML Standard is the current HTML standard. It obsoletes all other previously-published HTML specifications.

And that WHATWG HTML Standard refers to this URL Standard.

In Goals section the URL standard also says that one of the goals is to obsolete RFC 3986 and RFC 3987.

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
rahultumpala
Hello, I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New

We're in Beta

About us Mission Statement