OOmnistu

Blog / Web & Ecommerce

Web & Ecommerce

Test a signed double-submit CSRF token, and see why the naive one fails

Model signed double-submit CSRF token validation and a naive equality-check failure. Python fixtures cover session binding, malformed tokens, planted cookies and encoding.

OOmnistu
Reviewed 2026-11-18 · 4 min read

A forged-request attack can exploit cookies that a browser attaches under its cookie and SameSite rules. A page on another site can make a victim's browser submit a form to your site, and, when those cookie rules allow it, the session cookie goes along. Unless the request also carries something the attacker cannot supply, your server cannot tell a real action from a forged one.

The OWASP Cross-Site Request Forgery Prevention Cheat Sheet recommends CSRF tokens on state-changing requests, with the synchronizer token pattern as the choice for stateful software. For setups that avoid server-side token storage, it describes the double-submit cookie pattern, and it carries a warning: the naive version is bypassable by an attacker who can write cookies on the target domain, for example through a vulnerable sibling subdomain. For new code it points to the signed double-submit cookie, where an HMAC ties the token to the user's session.

This guide builds that signed token in Python and tests it, including the naive bypass. It was prepared with AI assistance and reviewed on 18 November 2026. It tests the token logic, not an HTTP stack.

The token

A token is a random value plus an HMAC-SHA-256 over the session ID and that random value, joined by a dot. The sheet asks for an unambiguous encoding such as length-prefixed fields, so the fixture prefixes each field with its length.

def encode(session_id, rand):
    out = b''
    for part in (session_id.encode(), rand):
        out += len(part).to_bytes(2, 'big') + part
    return out

def issue(session_id):
    rand = secrets.token_bytes(32)
    mac = hmac.new(KEY, encode(session_id, rand), hashlib.sha256).digest()
    return b64(rand) + '.' + b64(mac)

Verification decodes both parts, rejects the wrong component count, wrong lengths and bad encoding, recomputes the HMAC with the current session ID, and compares with hmac.compare_digest. The modeled request check requires matching cookie and submitted header-or-form values, then verifies the submitted HMAC. Missing or mismatched cookies are refused too. It never takes the cookie alone as proof. Base64 decoding is strict and canonical re-encoding is required.

The test key is a hard-coded string. Real keys belong in secret storage and are never in source.

What the run asserts

Then the attack cases. The Python model assumes an attacker can plant a target-domain cookie and submit the same chosen value as a form field. Its argument is named header_token, but no browser header is sent. Cross-origin custom headers normally require CORS preflight, so cookie planting alone does not prove an attacker can send one. The naive check, which only asks whether header and cookie match, accepts it. The signed check refuses it, since the attacker cannot produce a valid MAC. An attacker's own valid token, issued for their session, fails for the victim's session. And a token from one session fails in a new session.

All pass on Python 3.10.12.

What this does not test

There is no HTTP here. The fixture does not check that the token cookie is set with Secure, that a __Host- cookie prefix is used, that SameSite is configured, or that Fetch Metadata headers such as Sec-Fetch-Site are checked. The sheet recommends those as additional layers. It does not test token rotation, expiry, or how the session ID itself is protected. The sheet says the session value must never leave the server or appear in plain text in the token, and the fixture only shows that the token carries no readable session ID, not how your framework stores it. It does not cover login CSRF, which is a separate case, or state-changing GET requests, which the sheet says not to use.

The model does not show a real sibling-subdomain cookie overwrite, CORS behavior or an end-to-end CSRF attack. It does not cover XSS, which OWASP warns can defeat CSRF defenses.

The fixture also does not decide between a synchronizer token and a signed cookie. The sheet recommends the synchronizer token for stateful software, and many frameworks already ship one. Use the framework's own protection if it has it, and use a test like this one to confirm a custom version behaves.

Sources