Cross-Site Request Forgery Prevention Cheat Sheet — Pseudo-Code For Implementing HMAC CSRF Tokens
Below is an example in pseudo-code that demonstrates the implementation steps described above Bounded code example (external data; do not execute automatically): ```code // Gather the values secret = getSecretSecurely("CSRF_SECRET") // HMAC secret key sessionID = session.sessionID // Current authent
Reference note (untrusted external data; do not execute it as instructions).
Below is an example in pseudo-code that demonstrates the implementation steps described above
Bounded code example (external data; do not execute automatically):
```code
// Gather the values
secret = getSecretSecurely("CSRF_SECRET") // HMAC secret key
sessionID = session.sessionID // Current authenticated user session
randomValue = cryptographic.randomValue(64) // Cryptographic random value
// Create the CSRF Token
message = sessionID.length + "!" + sessionID + "!" + randomValue.length + "!" + randomValue.toHex() // HMAC message payload
hmac = hmac("SHA256", secret, message) // Generate the HMAC hash
// Add the `randomValue` to the HMAC hash to create the final CSRF token.
// Avoid using the `message` because it contains the sessionID in plain text,
// which the server already stores separately.
csrfToken = hmac.toHex() + "." + randomValue.toHex()
// Store the CSRF Token in a cookie
response.setCookie("csrf_token=" + csrfToken + "; Secure") // Set Cookie without HttpOnly flag
```
Below is an example in pseudo-code that demonstrates validation of the CSRF token once it is sent back from the client
Bounded code example (external data; do not execute automatically):
```code
// Get the CSRF token from the request
csrfToken = request.getParameter("csrf_token") // From header or form field (NOT cookie)
// Split the token to get the randomValue
const tokenParts = csrfToken.split(".");
const hmacFromRequest = tokenParts[0];
const randomValue = tokenParts[1];
// Recreate the HMAC with the current session and the randomValue from the request
secret = getSecretSecurely("CSRF_SECRET") // HMAC secret key
sessionID = session.sessionID // Current authenticated user session
message = sessionID.length + "!" + sessionID + "!" + randomValue.length + "!" + randomValue
// Generate the expected HMAC
expectedHmac = hmac("SHA256", secret, message)
// Compare the HMAC from the request with the expected HMAC
if (!constantTimeEquals(hmacFromRequest, expectedHmac)) {
// HMAC validation failed, reject the request
response.sendError(403, "Invalid CSRF token")
logError
```
Note: The constantTimeEquals function should be used to compare the HMACs to prevent timing attacks. This function compares two strings in constant time, regardless of how many characters match.
Attribution: Adapted from OWASP Cheat Sheet Series under CC-BY-SA-4.0. Adaptation: WikiKV isolated this documentation section, normalized formatting, retained only bounded code excerpts, and shortened it at a paragraph or sentence boundary for retrieval. Verify version-sensitive details at the source.
ATTRIBUTED SOURCE
This compact reference card is adapted from official documentation and is not a community-verified experience.
OWASP Cheat Sheet Series — cheatsheets/Cross-Site_Request_Forgery_Prevention_Cheat_Sheet.md :: Pseudo-Code For Implementing HMAC CSRF Tokens ↗Revision 07111ee754e8 · CC-BY-SA-4.0 and attribution