Skip to content

Commit

Permalink
Fix issue with file uploads containing ;'s in filenames (#541)
Browse files Browse the repository at this point in the history
* Plug.Conn.Utils.params/1 allows ;'s in quoted params

Plug.Conn.Utils.params would split key=value parameters on ";", but ;'s are valid
inside of quoted params.

Now, we only split on ;' when they are not quoted.

* Redundant function head

Signed-off-by: José Valim <jose.valim@plataformatec.com.br>
  • Loading branch information
mveytsman authored and José Valim committed Apr 15, 2017
1 parent 6f24c7e commit aff88b6
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions lib/plug/conn/utils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ defmodule Plug.Conn.Utils do
end
end

@doc """
@doc ~S"""
Parses headers parameters.
Keys are case insensitive and downcased,
Expand All @@ -147,14 +147,20 @@ defmodule Plug.Conn.Utils do
iex> params("foo=BAR ; wat")
%{"foo" => "BAR"}
iex> params("foo=\"bar\"; baz=\"boing\"")
%{"foo" => "bar", "baz" => "boing"}
iex> params("foo=\"bar;\"; baz=\"boing\"")
%{"foo" => "bar;", "baz" => "boing"}
iex> params("=")
%{}
"""
@spec params(binary) :: params
def params(t) do
t
|> :binary.split(";", [:global])
|> split_unquoted(";")
|> Enum.reduce(%{}, &params/2)
end

Expand Down Expand Up @@ -293,4 +299,14 @@ defmodule Plug.Conn.Utils do

defp downcase_char(char) when char in @upper, do: char + 32
defp downcase_char(char), do: char

defp split_unquoted(bin, s, groups \\ [<<>>], quoted? \\ false)
defp split_unquoted(<<>>, _s, groups, _quoted?),
do: groups
defp split_unquoted(<<?", t :: binary>>, s, [g | groups], quoted?),
do: split_unquoted(t, s, [<<g :: binary,?">> | groups], !quoted?)
defp split_unquoted(<<h, t :: binary>>, s, groups, false) when <<h>> == s,
do: split_unquoted(t, s, [<<>> | groups], false)
defp split_unquoted(<<h, t :: binary>>, s, [g | groups], quoted?),
do: split_unquoted(t, s, [<<g :: binary, h>> | groups], quoted?)
end

0 comments on commit aff88b6

Please sign in to comment.