Skip to content

Commit ac94702

Browse files
committed
Validate headers in inform
1 parent b921cf8 commit ac94702

2 files changed

Lines changed: 26 additions & 0 deletions

File tree

lib/plug/conn.ex

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1436,6 +1436,10 @@ defmodule Plug.Conn do
14361436
end
14371437

14381438
defp adapter_inform(%Conn{adapter: {adapter, payload}}, status, headers) do
1439+
for {key, value} <- headers do
1440+
validate_header_key_value!(key, value)
1441+
end
1442+
14391443
adapter.inform(payload, status, headers)
14401444
end
14411445

test/plug/conn_test.exs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,28 @@ defmodule Plug.ConnTest do
446446
)
447447
end
448448

449+
test "inform/3 raises when invalid header value given" do
450+
value = "</style.css>\r\nBAR"
451+
452+
message =
453+
~S[value for header "link" contains control feed (\r), newline (\n) or null (\x00): ] <>
454+
inspect(value)
455+
456+
assert_raise Plug.Conn.InvalidHeaderError, message, fn ->
457+
inform(conn(:get, "/foo"), 103, [{"link", value}])
458+
end
459+
460+
value = "</style.css>\0BAR"
461+
462+
message =
463+
~S[value for header "link" contains control feed (\r), newline (\n) or null (\x00): ] <>
464+
inspect(value)
465+
466+
assert_raise Plug.Conn.InvalidHeaderError, message, fn ->
467+
inform(conn(:get, "/foo"), 103, [{"link", value}])
468+
end
469+
end
470+
449471
test "inform!/3 performs an informational request" do
450472
conn = conn(:get, "/foo") |> inform!(103, [{"link", "</style.css>; rel=preload; as=style"}])
451473
assert {103, [{"link", "</style.css>; rel=preload; as=style"}]} in sent_informs(conn)

0 commit comments

Comments
 (0)