← Recipes

阅读中文版本

Prevent CSRF with Double-Submit Cookie

Issue a CSRF token as a cookie and require it as a header so a cross-site form post without the header is rejected.

~20 minsecuritycsrfcookies

Task

Implement if (req.cookies.csrf !== req.headers['x-csrf-token']) return res.status(403).end(); to close a known attack surface before the next security review finds it.

When to use this

A penetration test report flags the missing if (req.cookies.csrf !== req.headers['x-csrf-token']) return res.status(403).end(); as a medium-severity finding on a public endpoint.

Prerequisites

  • A running application with the vulnerable endpoint
  • Access to security headers or middleware config

Steps

1. Configure if (req.cookies.csrf !== req.headers['x-csrf-token']) return res.status(403).end(); in the project configuration file with the recommended defaults.

if (req.cookies.csrf !== req.headers['x-csrf-token']) return res.status(403).end();

Expected output: The configuration is accepted and if (req.cookies.csrf !== req.headers['x-csrf-token']) return res.status(403).end(); runs without errors on the first invocation.

2. Run the check locally to verify if (req.cookies.csrf !== req.headers['x-csrf-token']) return res.status(403).end(); produces the expected result before pushing.

pnpm test

Expected output: Local run matches CI behavior; no surprises after push.

3. Add the gate to the CI pipeline so if (req.cookies.csrf !== req.headers['x-csrf-token']) return res.status(403).end(); blocks merge on failure.

- name: Run add csrf double submit cookie
  run: pnpm test

Expected output: Pull requests with failing if (req.cookies.csrf !== req.headers['x-csrf-token']) return res.status(403).end(); are blocked from merging.

Troubleshooting

Problem

The check passes locally but fails in CI

Solution

Pin dependency versions and the runtime to match CI exactly; reproduce with the same Docker image or act.

Problem

Configuration is rejected with a schema error

Solution

Check the tool version matches the config schema; older versions may not support newer fields.

Limitations

  • The gate only runs on events that trigger the workflow; locally pushed commits bypass it.
  • Configuration drift is possible if someone edits settings outside version control.

Close the attack surface

Start by measuring the problem. Issue a CSRF token as a cookie and require it as a header so a cross-site form post without the header is rejected. The most common mistake is adding the gate without first confirming it catches the issue you care about. Run if (req.cookies.csrf !== req.headers[‘x-csrf-token’]) return res.status(403).end(); against the current codebase and inspect the output before wiring it into CI.

if (req.cookies.csrf !== req.headers['x-csrf-token']) return res.status(403).end();

Verify and monitor

Once the gate is in place, make failures actionable. A failing if (req.cookies.csrf !== req.headers[‘x-csrf-token’]) return res.status(403).end(); should point to the exact file and line, not a generic error. Pin the tool version in the lockfile so a upstream release does not silently change behavior. Review the gate output in every pull request just like a test result.