Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #231: report percentage of 100% should always report #236

Merged
merged 1 commit into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions csp/contrib/rate_limiting.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,14 @@ def build_policy(self, request: HttpRequest, response: HttpResponseBase) -> str:
return ""

report_percentage = policy.get("REPORT_PERCENTAGE", 100)
include_report_uri = random.randint(0, 100) < report_percentage
if not include_report_uri:
replace["report-uri"] = None
remove_report = random.randint(0, 99) >= report_percentage
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it's my brain but I find this way around harder to track in my head. Maybe worth a comment to clarify?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ha, that's interesting. I was trying to remove the if not include and simplify to if remove since the code block inside the conditional removes the reporting. But perhaps I'm overlooking something.

if remove_report:
replace.update(
{
"report-uri": None,
"report-to": None,
}
)

return build_policy(config=config, update=update, replace=replace, nonce=nonce)

Expand All @@ -46,8 +51,13 @@ def build_policy_ro(self, request: HttpRequest, response: HttpResponseBase) -> s
return ""

report_percentage = policy.get("REPORT_PERCENTAGE", 100)
include_report_uri = random.randint(0, 100) < report_percentage
if not include_report_uri:
replace["report-uri"] = None
remove_report = random.randint(0, 99) >= report_percentage
if remove_report:
replace.update(
{
"report-uri": None,
"report-to": None,
}
)

return build_policy(config=config, update=update, replace=replace, nonce=nonce, report_only=True)
16 changes: 16 additions & 0 deletions csp/tests/test_contrib.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,26 @@ def test_report_percentage() -> None:
mw.process_response(request, response)
if "report-uri" in response[HEADER]:
times_seen += 1
if "report-to" in response[HEADER]:
times_seen += 1
# Roughly 10%
assert 400 <= times_seen <= 600


@override_settings(CONTENT_SECURITY_POLICY={"REPORT_PERCENTAGE": 100, "DIRECTIVES": {"report-uri": "x"}})
def test_report_percentage_100() -> None:
times_seen = 0
for _ in range(1000):
request = rf.get("/")
response = HttpResponse()
mw.process_response(request, response)
if "report-uri" in response[HEADER]:
times_seen += 1
if "report-to" in response[HEADER]:
times_seen += 1
assert times_seen == 1000


@override_settings(CONTENT_SECURITY_POLICY_REPORT_ONLY={"REPORT_PERCENTAGE": 10, "DIRECTIVES": {"report-uri": "x"}})
def test_report_percentage_report_only() -> None:
times_seen = 0
Expand Down