Skip to content

Commit

Permalink
Update rate limiting defaults and bring test coverage to 100%
Browse files Browse the repository at this point in the history
  • Loading branch information
robhudson committed May 22, 2024
1 parent 0531388 commit 4cf1b7a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
4 changes: 2 additions & 2 deletions csp/contrib/rate_limiting.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def build_policy(self, request, response):
replace = getattr(response, "_csp_replace", {})
nonce = getattr(request, "_csp_nonce", None)

policy = getattr(settings, "CONTENT_SECURITY_POLICY", {})
policy = getattr(settings, "CONTENT_SECURITY_POLICY", None)

if policy is None:
return ""
Expand All @@ -34,7 +34,7 @@ def build_policy_ro(self, request, response):
replace = getattr(response, "_csp_replace_ro", {})
nonce = getattr(request, "_csp_nonce", None)

policy = getattr(settings, "CONTENT_SECURITY_POLICY_REPORT_ONLY", {})
policy = getattr(settings, "CONTENT_SECURITY_POLICY_REPORT_ONLY", None)

if policy is None:
return ""
Expand Down
13 changes: 13 additions & 0 deletions csp/tests/test_contrib.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,19 @@ def test_report_percentage():
assert 400 <= times_seen <= 600


@override_settings(CONTENT_SECURITY_POLICY_REPORT_ONLY={"REPORT_PERCENTAGE": 10, "DIRECTIVES": {"report-uri": "x"}})
def test_report_percentage_report_only():
times_seen = 0
for _ in range(5000):
request = rf.get("/")
response = HttpResponse()
mw.process_response(request, response)
if "report-uri" in response[HEADER_REPORT_ONLY]:
times_seen += 1
# Roughly 10%
assert 400 <= times_seen <= 600


@override_settings(CONTENT_SECURITY_POLICY=None)
def test_no_csp():
request = rf.get("/")
Expand Down

0 comments on commit 4cf1b7a

Please sign in to comment.