What the agent does
The agent reads how responses leave the service before it adds anything. In an API shaped like launchbadge/realworld-axum-sqlx, Cargo.toml pins axum 0.3.4 with the tower-log feature, tower 0.4.11 and tower-http 0.2.0 with only the trace feature turned on, beside sqlx 0.5 on Postgres, tokio 1.14 and env_logger. src/http/mod.rs builds the app as api_router().layer(ServiceBuilder::new().layer(AddExtensionLayer::new(ApiContext { config, db })).layer(TraceLayer::new_for_http())) and binds 0.0.0.0:8080, so there is already one place every response passes through. api_router() merges the users, profiles and articles routers, and src/http/error.rs turns every Error into a response, including the 401 that carries WWW-Authenticate.
It turns on the set-header feature of tower-http in Cargo.toml and writes src/http/headers.rs, a function that returns the stack of SetResponseHeaderLayer values the app needs: X-Content-Type-Options: nosniff, X-Frame-Options: DENY, Referrer-Policy: no-referrer, a Content-Security-Policy of default-src 'none'; frame-ancestors 'none', since this service returns JSON and never HTML, and Cache-Control: no-store, because every /api response is either per-user or carries a token. Each layer uses if_not_present, so a handler that sets its own value for one of these headers wins. Strict-Transport-Security is different: the binary listens on plain HTTP and TLS ends at whatever sits in front of it, so the agent adds an optional hsts_max_age to the clap Config in src/config.rs, read from HSTS_MAX_AGE like DATABASE_URL and HMAC_KEY are, documents it in .env.sample, and only adds the header when the value is set.
The layers go into the existing ServiceBuilder in serve(), outside the router, so the 401 and 422 responses built in error.rs carry the headers as well as the 200s. The repository has no Rust tests at all; its rust.yml workflow runs cargo test --verbose against an empty suite and then checks out the Realworld Postman collection and runs it with APIURL=http://localhost:8080/api. The agent adds the first unit tests in headers.rs: it builds a Router with one get handler, wraps it in the new layers and drives it with tower::ServiceExt::oneshot, asserting each header value, that Strict-Transport-Security is absent when hsts_max_age is None and present when it is set, and that a handler which sets its own Cache-Control is left alone. None of this needs Postgres, so the tests run anywhere cargo test does.
Before pushing, the agent runs cargo build --verbose and cargo test --verbose, the two commands the workflow runs, and cargo fmt --check. It starts the server against a local Postgres the way README.md describes, with sqlx db setup and the .env values, and pastes a curl -i http://localhost:8080/api/tags response into the pull request so a reviewer sees the header block as the proxy will see it.
Start it with one API call
POST https://api.phoenix.so/o/{org}/sessions with this body:
{
"agent": "claude",
"input": "Add security response headers to this Axum API and open a pull request. Read Cargo.toml, src/http/mod.rs, src/http/error.rs, src/config.rs, .env.sample and .github/workflows/rust.yml first. Enable the set-header feature of tower-http and add src/http/headers.rs with SetResponseHeaderLayer values for X-Content-Type-Options nosniff, X-Frame-Options DENY, Referrer-Policy no-referrer, a Content-Security-Policy of default-src 'none'; frame-ancestors 'none', and Cache-Control no-store, each with if_not_present. Add an optional hsts_max_age to Config read from HSTS_MAX_AGE, document it in .env.sample, and set Strict-Transport-Security only when it is given. Apply the layers in the ServiceBuilder in serve() so error responses carry them too. Add unit tests in headers.rs that drive a one-route Router with tower::ServiceExt::oneshot and assert every header, the HSTS on and off cases, and that a handler's own Cache-Control is kept. Run cargo fmt --check, cargo build --verbose and cargo test --verbose, start the server against Postgres as README.md describes, and paste a curl -i response for GET /api/tags and the test output in the pull request. Do not change any route handler or the migrations.",
"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 that touches Cargo.toml, Cargo.lock, src/config.rs, src/http/mod.rs, .env.sample and adds src/http/headers.rs with its tests. The description shows the curl -i output for one route with the five headers present and Strict-Transport-Security absent, and a second request with HSTS_MAX_AGE set, plus the cargo test summary.
On a repository with the Rust workflow, the pull request runs cargo build --verbose, cargo test --verbose and the Realworld Postman collection against the built server. The collection checks status codes and JSON bodies, not headers, so it passes unchanged, and the new tests run in the step that used to run none.
A reviewer checks that the Content-Security-Policy suits an API that serves no HTML, that Cache-Control: no-store does not break a client that caches GET /api/tags, that Strict-Transport-Security is off by default so a local http://localhost:8080 run is unaffected, and that the layers sit outside the router so a 401 from the AuthUser extractor carries them. Then they set HSTS_MAX_AGE in the deployment that has TLS in front of it.
Run it on every new Axum service
A team that runs several Axum services sends one POST /o/{org}/sessions per repository with this prompt and that repository in resources. Each run reads its own src/http/mod.rs and Cargo.toml, so a service already on tower-http 0.4 with a different layer stack gets the headers added to its own ServiceBuilder rather than a copy of someone else's, and each run opens its own pull request.
Give the API key a webhook_url and Rush POSTs run.completed when each turn finishes, with the session_url to read the pull request link from. A handler on your server can then post the link to the channel where that service's owners review changes.
The use case this job follows, with its automation and trigger: Hand off a long task.
Why run it on Rush?
Security headers are small and easy to get subtly wrong: a Content-Security-Policy copied from a web app breaks nothing on a JSON API but also protects nothing, and Strict-Transport-Security sent from a plain-HTTP binary locks a browser out of local development. In a Rush sandbox the agent runs the real server, fetches a route with curl -i and keeps that output in the run's record, so the pull request shows the exact header block rather than a description of one.
The repository has no tests today, and cargo test in CI runs nothing. Writing the first tests as part of this change gives the team a place to add the next ones, and the agent proves they pass by running them in the sandbox before it pushes. The same request body works on every Axum service a team owns.
Frequently asked questions
Does this change axum or tower-http versions?
No. It adds the set-header feature to the tower-http 0.2.0 line already in Cargo.toml and uses SetResponseHeaderLayer from that version. Upgrading axum from 0.3 is a separate job with its own API changes.
Why is Strict-Transport-Security optional?
The binary binds 0.0.0.0:8080 and serves plain HTTP; TLS is terminated by a proxy or load balancer in front of it. A browser that receives HSTS from http://localhost:8080 would refuse plain HTTP to that host afterwards, so the header is sent only when HSTS_MAX_AGE is set in the environment that actually has TLS.
Will the Realworld Postman collection still pass?
Yes. The collection asserts status codes and JSON bodies for each Realworld route. The new headers change nothing in the bodies, and Cache-Control: no-store has no effect on Newman. The agent runs cargo test and the server locally before pushing; the workflow runs the collection on the pull request.
What if a route needs a different header value?
Every layer uses if_not_present, so a handler that sets its own Cache-Control or Content-Security-Policy keeps its value. The tests include that case. If you want the layer to win instead, change if_not_present to overriding for that header.
More Axum jobs
The same job on other stacks
- Add Security Headers to a chi API (chi)
- Add Security Headers to a Cloudflare Worker (Cloudflare Workers)
- Add Security Headers to a CodeIgniter 4 App (CodeIgniter)