# CSRF token validation gaps

State-changing routes without CSRF checks, or tokens whose secret is recoverable from the token itself

_Area: Web authentication and account takeover. Seen in 2 disclosed reports; latest 2020-08-27. Updated 2026-11-21._

## How it works
A state-changing request is accepted without proof that it came from the user's own page, or the proof is forgeable. Two root causes appear: (1) a route simply never validates a CSRF token, so any cross-site request carrying the victim's cookies is honored; (2) the token is present but its secret is recoverable — Rails' `authenticity_token` is Base64 of `one_time_pad | (one_time_pad XOR session[:csrf_token])`, and the pad ships in cleartext, so XORing the halves yields the session CSRF secret used as the HMAC key for per-form tokens [E3](https://bugbountylibrary.com/r/h1%3A732415.md). The broken assumption is "the attacker cannot read or reconstruct the token."

## Where it shows up
- Comment, profile, settings or admin forms with no nonce/token field, or a token checked only on some methods [E1](https://bugbountylibrary.com/r/blog%3Asonarsource-com-blog-wordpress-csrf-to-rce.md).
- Sanitizer output later re-serialized: attributes re-concatenated with a different quote style than they were parsed with [E1](https://bugbountylibrary.com/r/blog%3Asonarsource-com-blog-wordpress-csrf-to-rce.md).
- Deep-link or URL handlers that build API URLs and POST bodies by string concatenation from user-controlled values [E2](https://bugbountylibrary.com/r/h1%3A1741430.md).
- Masked/encrypted tokens where the mask is transmitted alongside the masked value [E3](https://bugbountylibrary.com/r/h1%3A732415.md).
- Per-form token schemes keyed on a session secret that is itself exposed in any token [E3](https://bugbountylibrary.com/r/h1%3A732415.md).

## How to review
For every state-changing route, confirm a token is required and validated server-side before the action, and that the token is bound to the session and route. For token construction, the invariant: the secret used as HMAC key must not be derivable from any value the client receives. Decode the token, split it, and check whether any half alone or XOR of halves reveals the session secret [E3](https://bugbountylibrary.com/r/h1%3A732415.md). For sanitizers, the invariant: no re-serialization after sanitization may change quoting or escaping context [E1](https://bugbountylibrary.com/r/blog%3Asonarsource-com-blog-wordpress-csrf-to-rce.md). For URL/body builders, every user-controlled segment must be encoded for its exact position (path segment vs. body parameter) [E2](https://bugbountylibrary.com/r/h1%3A1741430.md).

## How finders spotted it
- Noticed the comment form had no CSRF protection, then observed admins without a valid nonce fall back to the more permissive `wp_filter_post_kses`; code-read `wp_rel_nofollow_callback` and saw attributes re-concatenated unescaped, with single-quoted input re-wrapped in double quotes enabling breakout [E1](https://bugbountylibrary.com/r/blog%3Asonarsource-com-blog-wordpress-csrf-to-rce.md).
- Reviewed the patch for a prior fix and saw token and relPath concatenated unencoded into URL and body, hypothesizing traversal and parameter injection [E2](https://bugbountylibrary.com/r/h1%3A1741430.md).
- Code-read the token construction and noticed the one-time pad is the first half of the token, making it non-secret and reversible to the session CSRF secret [E3](https://bugbountylibrary.com/r/h1%3A732415.md).

## Variants seen
- Missing token on a form, chained with a sanitizer quote mismatch to stored XSS [E1](https://bugbountylibrary.com/r/blog%3Asonarsource-com-blog-wordpress-csrf-to-rce.md).
- Unencoded deep-link values enabling path traversal plus body parameter injection to reach arbitrary endpoints [E2](https://bugbountylibrary.com/r/h1%3A1741430.md).
- Reversible masked token allowing forgery of per-form tokens for arbitrary routes [E3](https://bugbountylibrary.com/r/h1%3A732415.md).

## Impact and severity
E1 escalated CSRF to stored XSS, admin session takeover, theme-editor PHP write and RCE — critical because it chained a permissive sanitizer and reached code execution [E1](https://bugbountylibrary.com/r/blog%3Asonarsource-com-blog-wordpress-csrf-to-rce.md). E2 let a malicious link create an admin user, high severity via chained traversal and injection [E2](https://bugbountylibrary.com/r/h1%3A1741430.md). E3 alone bypasses per-form CSRF and chains with XSS on a form-less page to hit unrelated sensitive forms [E3](https://bugbountylibrary.com/r/h1%3A732415.md). Impact rises with each chained weakness and with how far the forged action reaches.

## Seen in real software
Open-source packages and projects, and products, where this pattern was found. If the target runs one, compare the version it runs with the fix.
- **Ruby on Rails**: The authenticity_token can be reversed and used to forge valid per_form_csrf_tokens for arbitrary routes (CVE-2020-8166). Report: https://bugbountylibrary.com/r/h1%3A732415.md
- **WordPress** (wp_rel_nofollow_callback) prior to version 5.1.1, fixed in 5.1.1: WordPress 5.1 CSRF to Remote Code Execution (CVE-2019-9787). Report: https://bugbountylibrary.com/r/blog%3Asonarsource-com-blog-wordpress-csrf-to-rce.md

## Evidence
- E1: https://bugbountylibrary.com/r/blog%3Asonarsource-com-blog-wordpress-csrf-to-rce.md
- E2: https://bugbountylibrary.com/r/h1%3A1741430.md
- E3: https://bugbountylibrary.com/r/h1%3A732415.md
