Get unstuck

Hint 1: Compare origins exactly

A suffix or substring match accepts app.example.evil.test. Compare the whole normalized origin.

Hint 2: Do not treat two missing tokens as equal

Check the trusted session token is a nonempty string before comparing it.

Reveal the reference patch and explanation
Fixedrequest-policy.mjsJavaScript
export function allowChange(request, session) {
  return Boolean(session
    && ['POST','PUT','PATCH','DELETE'].includes(request.method)
    && request.origin === 'https://app.example'
    && typeof session.csrfToken === 'string' && session.csrfToken.length > 0
    && request.csrfToken === session.csrfToken);
}

The decision is a conjunction. A valid session alone is insufficient; a trusted origin alone is insufficient; equality of two missing token values is insufficient. The supplied matching session-A-secret supports each declared mutation method. A wrong-session token fails because B-secret differs from A-secret. These booleans model the guard before persistence, so a real route must also prove false prevents its state change.

Compare it with your own fix, then rerun the tests. The editor is not changed when you open this explanation.

Inspect the actual test harness

The browser and the lab download run the same cases, including cases that catch common wrong fixes, such as comparing with one fixture value instead of the real rule. The download includes the complete test adapter and cryptographic fixtures.

Download cases.mjs
How the browser sandbox runs your code

Your code runs in QuickJS 2025-09-13 (quickjs-emscripten 0.32.0) inside a Web Worker, isolated from the page, with a 32 MiB memory limit and a 2.5-second time limit. The test cases and the pass or fail verdict run outside that sandbox, so changing built-ins such as Array.prototype.push cannot change the result. The lab download runs the same cases under Node.js, where your code runs directly.

The same as in current Node.js:

  • ES2025 syntax and built-ins, including Set methods such as intersection, iterator helpers, Promise.try, RegExp.escape and the RegExp v flag

Added to the sandbox so that it behaves like Node.js:

  • structuredClone, Array.fromAsync, queueMicrotask, atob and btoa
  • URL and URLSearchParams, parsed by your browser’s own URL implementation
  • TextEncoder and TextDecoder for UTF-8
  • Intl.Collator and a locale-aware String.prototype.localeCompare
  • console.log and related methods, whose output appears with your results

Not available in the browser sandbox:

  • Timers (setTimeout, setInterval), fetch and other network or storage APIs; the exercises are deterministic and offline
  • Node.js modules and globals: import, require, process and Buffer
  • Web Crypto (crypto.getRandomValues, crypto.subtle); each workshop supplies the cryptographic operations it needs
  • Intl other than Collator: toLocaleString and toLocaleDateString ignore the locale, so numbers have no digit grouping and dates use one fixed format in the device’s time zone
  • using declarations, Symbol.dispose and DisposableStack (explicit resource management)
  • Uint8Array.fromBase64 and toBase64, JSON.rawJSON, Atomics and Error.captureStackTrace

Use the lesson in your own system

This is the server policy function exercised with HTTP-shaped fixtures, not a browser attack against a live server. Use a framework CSRF implementation, HTTPS, appropriate cookie settings and unpredictable server-generated tokens in production. XSS can act within the trusted origin and needs separate controls.

Your next task: Find every state-changing route in your application. Which middleware applies this policy, and are any GET routes accidentally mutating data?

OWASP CSRF prevention

Related guide: Protect cookie-authenticated actions against CSRF