fix: tighten Django signed cookie identify_regex to cut false positives - #427
Merged
Merged
Conversation
The identify_regex matched any two colon-separated alphanumeric segments with no minimum length, producing IdentifyOnly false positives on short values like abc:def, JSESSIONID:abcdef1234, and government app tokens (0000Qs-hTQlwlgXAYBlmgEFoQTR:1ffkg4vkm). Real django.core.signing output is always payload[:timestamp]:signature where the signature is a base64url HMAC of at least 27 chars (SHA-1, 27) or 43 chars (SHA-256, the default since Django 3.1). Anchor the regex to that structure: optional leading dot, base64url payload, optional 4-8 char base62 timestamp, and a 27+ char signature. Verified against real cookies from both Signer/TimestampSigner and SHA-1/ SHA-256 algorithms; all match, while the false positives are rejected.
Merged
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## dev #427 +/- ##
=======================================
Coverage 99.45% 99.45%
=======================================
Files 30 30
Lines 3102 3102
=======================================
Hits 3085 3085
Misses 17 17 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
DjangoSignedCookies.identify_regexmatched any two colon-separated alphanumeric segments with no minimum length:This produced
IdentifyOnlyfalse positives on short values likeabc:def,JSESSIONID:abcdef1234, and government app tokens such as0000Qs-hTQlwlgXAYBlmgEFoQTR:1ffkg4vkm. Every other module in the library enforces minimum lengths (Rails{32,}--{16,}, Telerik{32,}, Express{20,90}); this one did not.Fix
^\.?— optional leading dot (zlib-compressed payloads)[a-zA-Z0-9_-]+— base64url payload(?::[a-zA-Z0-9_-]{4,8})?— optional base62 timestamp segment (TimestampSigner/dumps):[a-zA-Z0-9_-]{27,}$— base64url HMAC signature (SHA-1 = 27 chars, SHA-256 = 43 chars)Why this is complete (no old/new format split)
django.core.signingonly ever emits two shapes, both:-separated:payload:signature(Signer) andpayload:timestamp:signature(TimestampSigner/dumps). The only historical variation is the hash algorithm — SHA-1 (legacy, pre-3.1) → 27-char signature, SHA-256 (default since Django 3.1) → 43-char signature — both covered by{27,}. The middle segment is a base62 timestamp (6 chars this era;{4,8}has wide headroom), never a second signature. Verified by generating real cookies with the installed Django acrossSigner/TimestampSignerand both algorithms — all match.Validation
Real Django cookies (all match): test-suite cookie, minimal
TimestampSigner, minimal plainSigner, empty session dump, SHA-1 variants.False positives (all rejected):
0000Qs-hTQlwlgXAYBlmgEFoQTR:1ffkg4vkm,abc:def,a:b,test:1234,JSESSIONID:abcdef1234,foo:bar:baz.Tests
test_django_identify_false_positivestotests/django_signedcookies_test.py.tests/django_signedcookies_test.pyandtests/carve_test.pypass unmodified (corrected a stale comment that mislabeled the timestamp segment).uv run pytest tests/django_signedcookies_test.py tests/carve_test.py→ 27 passed.