# Run a Security Audit on a Fastify API

Yes. An agent starts your Fastify API against its own MySQL, confirms each finding with a real request to the running app, and writes a report, changing no code.

[Run this on Rush](https://cloud.getrush.ai/run/security-audit/fastify/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

The agent starts from how the app is wired, not from a checklist. In a Fastify 5 service shaped like fastify/demo it reads src/app.ts first: @fastify/autoload registers everything under src/plugins/external before src/plugins/app, then the routes with autoHooks and cascadeHooks on. That order is the audit's map. src/plugins/external/env.ts declares the schema every other plugin reads from: MYSQL_* for knex, COOKIE_SECRET and COOKIE_NAME for @fastify/session, COOKIE_SECURED defaulting to true, RATE_LIMIT_MAX defaulting to 100 per minute, and UPLOAD_DIRNAME defaulting to uploads with a pattern that rejects two dots. The agent lists each setting, its default and which plugin consumes it, so a finding about a cookie flag points at the exact line.

Then it reads the controls one plugin at a time. cors.ts sets methods GET, POST, PUT and DELETE and nothing about origin, so the agent checks what @fastify/cors sends for Access-Control-Allow-Origin by default and whether that combines badly with a session cookie that is httpOnly, sameSite lax and lives 1,800,000 milliseconds. helmet.ts is registered with no options, so it confirms the default headers on a real response and whether the Swagger UI at /api/docs still renders under them. rate-limit.ts applies RATE_LIMIT_MAX per minute globally, src/routes/api/users/index.ts tightens update-password to 3 per minute, and the 404 handler in app.ts allows 3 misses per 500 ms. The login route in src/routes/api/auth/index.ts has no tighter limit of its own, and the agent measures that with a burst of wrong passwords rather than asserting it. multipart.ts caps one file at 1 MB and ten fields; static.ts serves the whole uploads directory under /uploads, and the agent checks whether that prefix needs a session, since the external plugin is registered outside the /api autohook that demands one.

Authentication and authorization get their own section. src/routes/api/autohooks.ts refuses any /api request without request.session.user except /api/auth/login. src/plugins/app/authorization.ts decorates isModerator and isAdmin, which read roles from the session, and the agent maps which task routes use them: delete needs admin, assign needs moderator, and PATCH /api/tasks/:id, POST /api/tasks/:id/upload and DELETE /api/tasks/:filename/image check only that a session exists. It then tests the obvious question with two seeded users, whether one user can rename or re-image another user's task, and records the responses. password-manager.ts hashes with scrypt at cost 65536, block size 8, parallelization 2 and a 16-byte salt and compares with timingSafeEqual; the login handler only runs the compare when the email exists, so the agent times a known and an unknown email and reports whether the difference is measurable. The absence of a logout route and the 30-minute cookie lifetime go in the same section.

The file paths get a careful read because they mix user input and disk. The upload route builds the stored name as the task id, an underscore and the client's filename, trusts the multipart mimetype header against a list of image/jpeg and image/png, and passes the name through safeJoin in src/plugins/app/file-manager.ts, which calls sanitize-filename. GET /api/tasks/:filename/image looks the name up in the tasks table before reply.sendFile, so the agent tries a traversal name, a name of another task's image and a mismatched content type, and reports what the running app did. To run all this it uses the repo's own setup: docker-compose.yml for MySQL 8.4, .env.example with CAN_SEED_DATABASE=1, npm run db:migrate and npm run db:seed, which creates users whose password the test helper shows, then node dist/server.js with LOG_LEVEL set so every request it makes is also in the app log.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "Audit this Fastify API for security problems without changing any code. Read src/app.ts, src/server.ts, every file under src/plugins/external and src/plugins/app, src/routes/api/autohooks.ts, src/routes/api/auth/index.ts, src/routes/api/users/index.ts, src/routes/api/tasks/index.ts, the schemas under src/schemas, the migrations, .env.example, docker-compose.yml and .github/workflows/ci.yml first. Start MySQL with docker compose, copy .env.example to .env with a 32-character COOKIE_SECRET and CAN_SEED_DATABASE=1, run npm ci, npm run db:migrate, npm run db:seed, npm run build and start dist/server.js. Then check with real requests: CORS origin handling with the session cookie, the helmet headers actually sent, rate limiting on login versus update-password and the 404 handler, whether /uploads and /api/docs need a session, whether a logged-in user can PATCH, upload to or delete the image of a task they do not own, timing of login for a known versus unknown email, and path handling on upload and on GET /api/tasks/:filename/image. Write security-audit.md: how you ran it, then findings grouped by authentication, authorization, input handling, transport and headers, and configuration, each with the request you sent, the response you got, severity and a fix with the file to change. List anything you could not confirm as unconfirmed. Do not open a pull request.",
  "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 security-audit.md as an artifact on the session, which the session read returns with a download link that works for an hour and is reissued on each read. It opens with how the API was run: the MySQL 8.4 container from docker-compose.yml, the .env values it set, the migrate and seed commands, the Node version in the sandbox against the 22 and 24 matrix in ci.yml, and the two seeded accounts it used, so the reader knows the conditions behind each finding.

The findings follow, grouped by authentication, authorization, input handling, transport and headers, and configuration. Each one quotes the request and the response copied from the transcript, names the file that would change, and gives a severity. A check that came back clean is listed as clean, and one the agent could not confirm, for example a timing difference too small to measure in the sandbox, is marked unconfirmed rather than dropped or promoted.

Before acting on it, a reviewer opens the transcript at GET /o/{org}/sessions/{id}/messages and matches a few findings to the tool calls that produced them: the curl with the second user's cookie against the first user's task, the burst against /api/auth/login, the OPTIONS preflight. A finding with no matching request is a reading of the code, and the report says so.

## Run it on every tagged release

A team that wants the audit before each release starts one run per tag: a GitHub Actions job on tag push sends POST /o/{org}/sessions with the key from its secrets, this prompt, and the release branch the tag was cut from in resources as https://github.com/your-org/your-app/tree/release-1.4, since resources names a repository at a branch. Send a request_id that is a UUID v5 of the tag so a re-run of the job returns the run already started instead of a second audit.

Each run boots its own MySQL and its own copy of the API in its own sandbox, so two tags audited at once never share a database or a port. Give the key a webhook URL and Rush POSTs run.completed with data.session_url when the turn ends; reading the session returns the artifact link, which the job can post on the release. A key runs at most max_concurrent sessions at once, 4 by default, and a tag sent past that answers 202 with status: "queued" and is audited when a slot frees.

## Why run it on Rush?

An audit that only reads code produces guesses; one that sends requests to the running app produces evidence. Running this stack means a MySQL container, a migration and seed step that refuses to run without CAN_SEED_DATABASE=1, a built dist directory and many requests with two different session cookies, which is an afternoon on a laptop that also has to be careful about its own uploads directory. In a Rush sandbox the agent has its own Docker daemon and its own disk, and both are discarded when the turn ends.

The report carries its own proof. Every request in it is also in the transcript with the response the app gave and the app's own log line for it, so a reviewer can tell a confirmed finding from a suspicion without re-running anything. The code is untouched: the result is a file, not a branch, and the team decides what to fix.

## Frequently asked questions

### Does it change any code or open a pull request?

No. The prompt forbids it and the result is a Markdown file on the session. Each finding names the file to change and a fix, and a follow-up on the same session, once a reviewer has chosen which findings to act on, can be asked to open a pull request with those changes.

### Does it need my production database or credentials?

No. It runs the MySQL 8.4 image from docker-compose.yml in the sandbox and seeds it with the repo's own seed script, which creates test users with a password the test helper already shows. Nothing in the audit touches a system outside the sandbox.

### What if the app does not start in the sandbox?

The report says so and falls back to a code reading, marking every finding as unconfirmed. The usual causes are a missing COOKIE_SECRET, which @fastify/env refuses at startup, or Docker not being available for MySQL; both show in the transcript.

### Will it flag the Swagger UI and the uploads directory as problems?

It checks them rather than assuming. It requests /api/docs and a file under /uploads without a session cookie and records what came back; whether exposing them is acceptable is a judgement for your team, and the report presents it as a configuration item with the responses attached.

## More Fastify jobs

- [Upgrade npm Dependencies in a Fastify API](https://getrush.ai/run/upgrade-npm-dependencies/fastify/)
- [Write Load Tests for a Fastify API](https://getrush.ai/run/write-load-tests/fastify/)
- [Write node:test Tests for a Fastify API](https://getrush.ai/run/write-node-test-tests/fastify/)

## The same job on other stacks

- [Run a Security Audit on a Feathers App](https://getrush.ai/run/security-audit/feathers/)
- [Run a Security Audit on a Flask App](https://getrush.ai/run/security-audit/flask/)
- [Security Audit of a Gin API](https://getrush.ai/run/security-audit/gin/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
