Skip to content

Commit

Permalink
Also validate headers on merge_resp_headers
Browse files Browse the repository at this point in the history
  • Loading branch information
José Valim committed Jul 29, 2017
1 parent 9c06cad commit 10c1147
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
9 changes: 5 additions & 4 deletions lib/plug/conn.ex
Original file line number Diff line number Diff line change
Expand Up @@ -658,11 +658,12 @@ defmodule Plug.Conn do
conn
end

def merge_resp_headers(%Conn{resp_headers: current} = conn, headers) do
def merge_resp_headers(%Conn{resp_headers: current, adapter: adapter} = conn, headers) do
headers =
Enum.reduce headers, current, fn
{key, value}, acc when is_binary(key) and is_binary(value) ->
List.keystore(acc, key, 0, {key, value})
Enum.reduce headers, current, fn {key, value}, acc when is_binary(key) and is_binary(value) ->
validate_header_key_if_test!(adapter, key)
validate_header_value!(key, value)
List.keystore(acc, key, 0, {key, value})
end
%{conn | resp_headers: headers}
end
Expand Down
10 changes: 10 additions & 0 deletions test/plug/conn_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,16 @@ defmodule Plug.ConnTest do
end
end

test "merge_resp_header/3 raises when invalid header value given" do
assert_raise Plug.Conn.InvalidHeaderError, ~S[value for header "x-sample" contains control feed (\r) or newline (\n): "value\rBAR"], fn ->
merge_resp_headers(conn(:get, "foo"), [{"x-sample", "value\rBAR"}])
end

assert_raise Plug.Conn.InvalidHeaderError, ~S[value for header "x-sample" contains control feed (\r) or newline (\n): "value\n\nBAR"], fn ->
merge_resp_headers(conn(:get, "foo"), [{"x-sample", "value\n\nBAR"}])
end
end

test "merge_resp_headers/3" do
conn1 = merge_resp_headers(conn(:head, "/foo"), %{"x-foo" => "bar", "x-bar" => "baz"})
assert get_resp_header(conn1, "x-foo") == ["bar"]
Expand Down

0 comments on commit 10c1147

Please sign in to comment.