What the agent does
The agent starts from how the app authenticates. In an API shaped like fastify/demo, package.json pins fastify 5 with @fastify/session, @fastify/cookie, @fastify/helmet, @fastify/cors, @fastify/rate-limit and @fastify/autoload, and src/plugins/external/session.ts registers the cookie session with httpOnly, sameSite: 'lax', secure from COOKIE_SECURED and a 30-minute maxAge. POST /api/auth/login in src/routes/api/auth/index.ts writes request.session.user and calls session.save(), and src/routes/api/autohooks.ts rejects any /api request without a session user. The browser carries that cookie automatically, which is exactly what a cross-site form or script can exploit on POST /api/tasks, PATCH /api/tasks/:id, DELETE /api/tasks/:id, POST /api/tasks/:id/assign, POST /api/tasks/:id/upload and PUT /api/users/update-password. There is no CSRF plugin in the dependency list.
It adds @fastify/csrf-protection and a src/plugins/external/csrf.ts that registers it with sessionPlugin: '@fastify/session', wrapped in fastify-plugin with dependencies: ['session'] so autoload orders it after the session plugin whatever the directory listing says. A new GET /api/csrf route returns reply.generateCsrf(), which stores the secret in the session and hands the token to the caller. In src/routes/api/autohooks.ts the agent adds a preHandler that calls fastify.csrfProtection for every method other than GET, HEAD and OPTIONS, so each route under routes/api is covered without editing them one by one, including the multipart upload route. Because server.ts sets Ajv to removeAdditional: 'all', a _csrf field in a JSON body would be stripped before the plugin sees it, so the pull request documents the x-csrf-token header as the way to send it.
The tests already route every authenticated call through injectWithLogin in test/helper.ts, which posts to /api/auth/login and reads the COOKIE_NAME cookie. The agent extends that helper to fetch /api/csrf with the session cookie and add the x-csrf-token header, so test/routes/api/tasks/tasks.test.ts and test/routes/api/users/users.test.ts keep passing unchanged. A new test/app/csrf.test.ts asserts that a POST /api/tasks with a valid session and no token is refused with 403, that one with a token from /api/csrf succeeds, and that a token minted in another session is refused. The node:test runner and assert style match the existing files.
Before pushing, the agent runs npm run lint (neostandard, so no semicolons), npm run build -- --noEmit and npm test against a MySQL started from docker-compose.yml, with RATE_LIMIT_MAX=4 as .env.example says tests need. That is the same sequence .github/workflows/ci.yml runs on Node 22 and 24, so the pull request's checks tell the same story the sandbox did.
Start it with one API call
POST https://api.phoenix.so/o/{org}/sessions with this body:
{
"agent": "codex",
"input": "Add CSRF protection to this Fastify API and open a pull request. Read package.json, src/server.ts, src/app.ts, src/plugins/external/session.ts, src/plugins/external/env.ts, src/routes/api/autohooks.ts, src/routes/api/auth/index.ts, src/routes/api/tasks/index.ts, src/routes/api/users/index.ts, test/helper.ts, test/routes/api/tasks/tasks.test.ts and .github/workflows/ci.yml first. Add @fastify/csrf-protection as src/plugins/external/csrf.ts registered with sessionPlugin '@fastify/session' and declared as depending on the session plugin. Add GET /api/csrf returning reply.generateCsrf(). In src/routes/api/autohooks.ts add a preHandler that runs fastify.csrfProtection for every method except GET, HEAD and OPTIONS. Extend the login and injectWithLogin helpers in test/helper.ts to fetch a token from /api/csrf and send it as the x-csrf-token header so the existing tests pass unchanged. Add test/app/csrf.test.ts: a POST /api/tasks with a session but no token is 403, with a token it succeeds, and with a token from another session it is 403. Use node:test and assert like the existing tests. Run npm run lint, npm run build -- --noEmit and npm test against MySQL from docker-compose.yml with RATE_LIMIT_MAX=4, and paste the test summary in the pull request. Do not change the session cookie settings or the login route.",
"resources": [
{
"url": "https://github.com/your-org/your-app"
}
]
}- 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.
- 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.
- 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 one new dependency in package.json and package-lock.json, a new src/plugins/external/csrf.ts, a token route, a few lines in src/routes/api/autohooks.ts, the extended test/helper.ts and a new test/app/csrf.test.ts. The description shows the node:test summary with the new cases listed and the c8 coverage table the test script prints.
On a repository with the demo's CI workflow, the pull request runs lint, the type check and the tests on both Node versions against the MySQL service, so a plugin registered before the session, or a route the hook misses, fails there before anyone merges.
A reviewer checks that the hook skips GET, that the login route is still reachable without a token, that the token is bound to the session rather than global, and that the upload route, whose body is multipart, is covered by the header path. Then they add the header to whichever frontend calls the API.
Run it on every API with cookie sessions
A security team that has found cookie-authenticated Fastify services without CSRF tokens starts one POST /o/{org}/sessions per repository with this prompt and that repository in resources. Each run reads its own session plugin and route tree, so a service that already uses @fastify/secure-session is wired with that sessionPlugin option instead, and each opens its own pull request.
Give the key a webhook_url and run.completed arrives per run with data.session_url, from which a small handler collects the pull request links into one tracking issue.
The use case this job follows, with its automation and trigger: Hand off a long task.
Why run it on Rush?
A CSRF change breaks every client at once if the token path is wrong, and the mistake that matters most, a plugin that never binds to the session because autoload registered it first, only shows when a real request is made. In a Rush sandbox the agent starts MySQL, migrates, seeds and runs the whole suite through the real app, so the pull request carries the test output rather than a promise.
The run is also where the reasoning is kept: every npm command, the failing attempt where the token header was missing, and the fix. That record stays with the pull request for anyone who asks later why the hook lives in autohooks.ts and not on each route.
Frequently asked questions
Why not put _csrf in the JSON body?
The app's Ajv options in server.ts set removeAdditional: 'all', so a _csrf property not declared in a route's body schema is removed before the plugin reads it. The header is read before body validation and needs no schema change, so the pull request uses x-csrf-token.
Does login need a token?
Not in this pull request: the hook runs on routes under routes/api after the session is in place, and the login route is excluded the same way the existing authentication hook excludes it. Protecting login against login CSRF is a follow-up the agent notes in the description.
Will the file upload route still work?
Yes. The token is read from the header, not from the multipart body, so POST /api/tasks/:id/upload is protected and the existing upload tests pass through the updated injectWithLogin helper.
Is the token tied to the user?
The secret lives in the session that @fastify/session stores, so a token minted in one session is refused in another. The new test for a token from a second session proves it. Logging out destroys the session and with it the token.