# Add Security Headers to an Actix Web App

Yes. An agent adds a DefaultHeaders middleware with HSTS, nosniff, frame, referrer and content security policies to your Actix Web app, tests each, and opens a PR.

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

## What the agent does

The agent reads how responses are built before it adds a header to all of them. In an app shaped like the `basics/todo` crate of actix/examples, `Cargo.toml` takes `actix-web`, `actix-files` and `actix-session` with the `cookie-session` feature from the workspace, where `actix-web` is `4.11`, plus `sqlx 0.9` with the `sqlite` feature and `tera 2`. `src/main.rs` builds the `App` with `Tera` templates and a `SqlitePool` as app data, wraps `Logger::default()`, a `SessionMiddleware` on a cookie store with `cookie_secure(false)`, and an `ErrorHandlers` that serves `static/errors/400.html`, `404.html` and `500.html`, and registers `GET /`, `POST /todo`, `POST /todo/{id}` and `Files::new("/static", "./static")`. It binds `127.0.0.1:8080` with two workers.

The page itself decides the content security policy. `templates/index.html.tera` loads a Raleway stylesheet from `fonts.googleapis.com`, three stylesheets from `/static/css`, and posts its form to `/todo` with no inline script. So `script-src 'self'` costs nothing, `style-src` needs `'self'` and `https://fonts.googleapis.com`, `font-src` needs `https://fonts.gstatic.com`, and `form-action 'self'` matches the only form. The repository already shows the mechanism the agent uses: `guards/src/main.rs` wraps `DefaultHeaders::new().add(("Vary", "Accept-Version"))` on its app.

The agent adds one `DefaultHeaders` middleware in `main.rs` with `X-Content-Type-Options: nosniff`, `X-Frame-Options: DENY`, `Referrer-Policy: strict-origin-when-cross-origin`, `Permissions-Policy` that turns off camera, microphone and geolocation, the content security policy above, and `Strict-Transport-Security` only when an environment variable says the app is served over TLS, since it binds plain HTTP and HSTS on a local address is a trap. `wrap` calls run in reverse order of registration, so the agent registers it so that error pages from `ErrorHandlers` and files from `Files` carry the headers too, and moves the `App` construction into a function the tests can call.

The crate has no tests, so the agent adds a `#[cfg(test)]` module using `actix_web::test::init_service` and `TestRequest`: `GET /` carries every header, `GET /static/css/style.css` does too, a `GET` to an unknown path answers 404 from the error handler with the headers, and `Strict-Transport-Security` is absent unless the variable is set. `sqlx::query_as!` in `src/model.rs` checks its SQL at compile time, so the build needs `DATABASE_URL` and a migrated `todo.db` the way `.github/workflows/ci.yml` creates one with `sqlx database create` and `sqlx migrate run`; the agent does the same in the sandbox, then runs `cargo test -p todo` and `cargo clippy` as the workspace's `justfile` does.

## Start it with one API call

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

```json
{
  "agent": "claude",
  "input": "Add security headers to this Actix Web app and open a pull request. Read Cargo.toml, src/main.rs, src/api.rs, templates/index.html.tera, static/errors and .env first. Add one DefaultHeaders middleware with X-Content-Type-Options nosniff, X-Frame-Options DENY, Referrer-Policy strict-origin-when-cross-origin, a Permissions-Policy that disables camera, microphone and geolocation, and a Content-Security-Policy derived from what the templates actually load: script-src self, style-src self plus the font stylesheet host, font-src the font file host, form-action self. Add Strict-Transport-Security only when an environment variable such as SERVE_TLS is set, and say so in the README. Register the middleware so error pages from ErrorHandlers and static files carry the headers too. Move App construction into a function and add tests with actix_web::test that check the headers on GET /, on a static file, on a 404, and that the HSTS header is absent by default. Create and migrate the SQLite database the way the CI workflow does so the sqlx macros compile, run cargo test and cargo clippy for this crate, and paste the output in the pull request. Do not change the routes, the session key or the templates' markup.",
  "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 that touches `src/main.rs` for the middleware and the extracted app builder, adds a test module, and adds a few lines to `README.md` about the TLS variable. The description shows the response headers of `GET /` from a test run, the content security policy on one line with the hosts it allows, and the output of `cargo test` and `cargo clippy` for the crate.

The repository's `ci.yml` builds and tests every workspace member on the pull request, after creating `todo.db` with `sqlx`, so the new tests run there as well as in the sandbox.

A reviewer opens the app once with the pull request checked out and the browser console open: the page should render with its fonts and no policy violations. They check that `form-action 'self'` still lets the todo form post, that the frame and nosniff headers are on the 404 page, and that HSTS is off by default. Then they merge.

## Run it on every service without a security policy

A team with several Actix Web services sends one `POST /o/{org}/sessions` per repository with this prompt and that repository in `resources`. Each run derives the content security policy from the templates it finds, so a JSON API gets a short policy and a server-rendered app gets the hosts its pages load, and each opens its own pull request.

Give the key a `webhook_url` and Rush POSTs `run.completed` when each turn ends, with `session_url` to read the pull request from, so the policies across services can be compared before any one is merged.

## Why run it on Rush?

A content security policy written from memory breaks the page; one written from the template's actual stylesheet and font hosts does not. In a Rush sandbox the agent reads the template, writes the policy, builds the crate with the SQLite database the macros need, runs the tests and opens the page in a browser, and each of those steps stays in the run's record beside the pull request.

The change is a dozen lines, but the setup around it, migrating the database so `sqlx` compiles and checking the headers on error pages and static files, is what takes a morning. The agent does it in one pass, and the same request works unchanged on the next service.

## Frequently asked questions

### Why not add HSTS unconditionally?

Because the app binds `127.0.0.1:8080` over plain HTTP and a browser remembers HSTS per host. The agent sends it only when a variable says the app is behind TLS, and the README says which one, so a deployment behind a terminating proxy can turn it on.

### Will the policy block the Google font?

No. The template loads the stylesheet from `fonts.googleapis.com` and the font files come from `fonts.gstatic.com`, so `style-src` and `font-src` name those two hosts. Nothing else is allowed, and the test for `GET /` asserts the policy string.

### Does it need a database to run the tests?

Yes, for the build: `sqlx::query!` and `query_as!` check their SQL against `DATABASE_URL` at compile time unless `.sqlx` metadata exists. The agent creates and migrates `todo.db` the way `ci.yml` does, and the pull request repeats the two commands.

### What about the error pages and static files?

They get the headers too. `DefaultHeaders` is registered so it runs outside `ErrorHandlers` and `Files`, and the tests request a 404 path and `/static/css/style.css` to prove it.

## More Actix Web jobs

- [Add Soft Delete to an Actix Web API](https://getrush.ai/run/add-soft-delete/actix/)
- [Add Structured Logging to an Actix Web App](https://getrush.ai/run/add-structured-logging/actix/)
- [Dockerize an Actix Web App](https://getrush.ai/run/dockerize-app/actix/)

## The same job on other stacks

- [Add Security Headers to an aiohttp App](https://getrush.ai/run/add-security-headers/aiohttp/)
- [Add Security Headers to an Axum API](https://getrush.ai/run/add-security-headers/axum/)
- [Add Security Headers to a chi API](https://getrush.ai/run/add-security-headers/chi/)

## 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/)
