All jobs

Add CSRF Protection to a Node.js App

Yes. An agent enables token checks on every state-changing form in your Express app, adds the token to the forms that lack it, proves it with Cypress and opens a PR.

Run this on RushOpens the console with this prompt ready to send, in a Project that has your repository.

What the agent does

The agent reads how forms are served and posted before turning protection on. In an app like OWASP/NodeGoat, package.json depends on express 4, express-session, body-parser, swig through consolidate for templates and csurf, with Cypress and Grunt in devDependencies; server.js connects to MongoDB from config/env/all.js, registers bodyParser.json and bodyParser.urlencoded, then session with saveUninitialized and resave on, and has the CSRF lines present but commented out: app.use(csrf()) and a middleware that copies req.csrfToken() into res.locals.csrftoken. app/routes/index.js registers POST /login, /signup, /profile, /contributions, /benefits and /memos, the routes a forged form could hit.

The views are half ready. login.html, signup.html, profile.html and contributions.html already carry a hidden _csrf input bound to {{csrftoken}}, so with the middleware off they render an empty value. benefits.html posts to /benefits with only a userId and a date, and memos.html posts a textarea to /memos, and neither has the field. app/routes/error.js renders error-template.html with a 500 for any error, which is what a token failure would show today, since csurf raises an error with code EBADCSRFTOKEN.

The agent enables the two commented blocks in server.js after the session middleware and before the routes, adds the hidden _csrf input to the two forms in benefits.html and memos.html, and extends the error handler so an EBADCSRFTOKEN error answers 403 with the same template and a short message instead of a stack trace and a 500. It keeps the session options as they are, because the token is stored in the session and saveUninitialized already creates one on the first GET, and it leaves the GET search forms in allocations.html and research.html alone since they change nothing. The tutorial page app/views/tutorial/a8.html describes exactly this fix and stays as reference text.

Verification uses the repository's own suites. test/e2e/integration holds Cypress specs for every page, with cypress.json pointing at them and test/e2e/plugins/index.js reading the host and port from config/env/all.js; profile_spec.js counts nine inputs in the profile form, which the existing hidden field already includes. The agent starts MongoDB, runs node artifacts/db-reset.js as docker-compose.yml does, starts npm start, runs npm run test:ci, and adds a spec that submits /profile without the token and expects 403. It runs npx jshint . as lint.yml does, and sends a curl POST to /memos with a valid session cookie and no token to keep the refusal.

Start it with one API call

POST https://api.phoenix.so/o/{org}/sessions with this body:

{
  "agent": "claude",
  "input": "Add CSRF protection to this Express app and open a pull request. Read package.json, server.js, config/env/all.js, app/routes/index.js, app/routes/error.js, every template under app/views that contains a form, cypress.json, test/e2e/plugins/index.js and test/e2e/integration/profile_spec.js first. In server.js enable the commented csurf middleware after the session middleware and before the routes, and the middleware that exposes req.csrfToken() as res.locals.csrftoken. Add a hidden _csrf input bound to csrftoken to the forms in app/views/benefits.html and app/views/memos.html; leave GET search forms alone. In app/routes/error.js answer an EBADCSRFTOKEN error with status 403 and the error template instead of 500. Start MongoDB, run node artifacts/db-reset.js, start the app with npm start, and run npm run test:ci so every Cypress spec passes. Add a Cypress spec that signs in, posts to /profile without the token and expects 403. Run npx jshint . as .github/workflows/lint.yml does. Send a curl POST to /memos with a signed-in session cookie and no token and keep the 403. Paste the Cypress summary, the jshint result and the curl output in the pull request. Do not change the session options, the DAOs or the tutorial pages.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
  1. Get an API key. In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
  2. Send the request. POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
  3. Read what came back. Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

What comes back

What comes back is a pull request with two blocks uncommented in server.js, one hidden input each in app/views/benefits.html and app/views/memos.html, a 403 branch in app/routes/error.js, and a new Cypress spec under test/e2e/integration. The description pastes the Cypress run with every page spec passing, the jshint result, and a curl session showing a 403 for a POST without the token and the normal redirect for one with it.

The e2e-test.yml workflow starts MongoDB in Docker and runs the Cypress suite on the pull request across the Node versions in its matrix, so the new spec runs there too, and lint.yml runs the same jshint pass.

A reviewer checks that csrf() sits after session and bodyParser so the token can be read from the form body, that every POST form in app/views now carries the field, that a missing token answers 403 and not a stack trace, and that the GET forms and the DAOs are untouched.

Run it on every security finding on a form

A team that owns several Express apps starts one run per repository with POST /o/{org}/sessions, this prompt and the repository in resources. The agent reads each app's own middleware order and templates, so an app that renders with EJS gets the field in EJS syntax and an app with a JSON API gets a header-based token instead of a form field, and each run opens its own pull request with its own test output.

The key's webhook_url receives run.completed for each run it starts, with data.session_url for the pull request link.

The use case this job follows, with its automation and trigger: Hand off a long task.

Why run it on Rush?

CSRF protection fails in the details: a middleware registered before the session exists, a form someone forgot, an error handler that turns a refused token into a 500 page. In a Rush sandbox the agent starts MongoDB and the app, runs the Cypress suite that drives every page, and posts a forged request itself, so the pull request shows the refusal and the suite passing rather than a diff that looks right.

The same request works across every Express app the team maintains, each in its own sandbox with the test output and the forged request kept in the transcript.

Frequently asked questions

Why is the middleware placed after session and body-parser?

csurf stores its secret in the session and reads the token from req.body._csrf, so both must exist when it runs. server.js already registers bodyParser and then session before the commented block, and the agent enables the block in that position, before routes(app, db).

Which forms change?

Only benefits.html and memos.html gain the hidden _csrf input. The login, signup, profile and contributions forms already have one bound to csrftoken. The search forms in allocations.html and research.html use GET and change nothing, so they need no token.

What does a forged request get?

A 403 rendered with the existing error-template.html and a short message. Without the change in app/routes/error.js, csurf would raise EBADCSRFTOKEN into the generic handler and the user would see a 500 with a stack trace.

Do the existing Cypress specs still pass?

Yes. They sign in and submit forms through the browser, which now includes the hidden field, and profile_spec.js counts nine inputs in the profile form, which already included the field. The agent runs the whole suite with npm run test:ci and pastes the summary.

More Node.js jobs

The same job on other stacks

Keep reading