Skip to content

Commit 31159e4

Browse files
committed
Log warning if report_only is enabled with no report_uri
1 parent 7989a94 commit 31159e4

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

lib/plug_content_security_policy.ex

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ defmodule PlugContentSecurityPolicy do
5757
field_value = Enum.map_join(config.directives, "; ", &convert_tuple/1) <> ";"
5858

5959
if config.report_only do
60+
_ =
61+
unless config.directives[:report_uri] do
62+
Logger.warn("#{__MODULE__}: `report_only` enabled but no `report_uri` specified")
63+
end
64+
6065
{@report_field, field_value}
6166
else
6267
{@default_field, field_value}

test/plug_content_security_policy_test.exs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,5 +105,25 @@ defmodule PlugContentSecurityPolicyTest do
105105

106106
assert log =~ "[warn]"
107107
end
108+
109+
test "logs warning if report_only is enabled with no report_uri directive ", %{conn: conn} do
110+
log =
111+
capture_log(fn ->
112+
PlugCSP.call(conn, %{directives: %{}, nonces_for: [], report_only: true})
113+
end)
114+
115+
assert log =~ "[warn]"
116+
117+
log =
118+
capture_log(fn ->
119+
PlugCSP.call(conn, %{
120+
directives: %{report_uri: "http://example.com"},
121+
nonces_for: [],
122+
report_only: true
123+
})
124+
end)
125+
126+
assert log == ""
127+
end
108128
end
109129
end

0 commit comments

Comments
 (0)