# Add Security Headers With a Traefik Middleware

Yes. An agent declares a Traefik headers middleware in Docker Compose labels, attaches it to the HTTPS routers, checks the headers with curl and opens a PR.

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

## What the agent does

The agent reads how traffic reaches the app. In a Docker Compose deployment laid out like fastapi/full-stack-fastapi-template, `compose.yml` runs `traefik:v3.7` as `proxy` with `--providers.docker`, `--providers.docker.exposedbydefault=false` and an `http` entrypoint on port 80, and each service opts in with labels: `backend` has `traefik.enable=true`, a `backend-http` router on `Host(`${DOMAIN:-localhost}`)` and a service port of 8000, and `adminer` has the same shape on the `adminer.` subdomain. `compose.deploy.yml` adds the `https` entrypoint on 443, redirects `http` to it, resolves certificates through the `le` resolver with the TLS challenge, and declares `backend-https` and `adminer-https` routers with `tls=true`. Since `backend/app/main.py` mounts the built frontend with `app.frontend("/", directory=FRONTEND_DIR)`, one router serves both the API and the pages, and no response today carries a `Strict-Transport-Security`, `X-Content-Type-Options`, `X-Frame-Options` or `Referrer-Policy` header.

Traefik's `headers` middleware is declared the same way the routers are, as labels. The agent adds to the `backend` service in `compose.deploy.yml` a `traefik.http.middlewares.secure-headers.headers.*` block: `stsSeconds` with `stsIncludeSubdomains`, `contentTypeNosniff`, `frameDeny` and a `referrerPolicy` of `strict-origin-when-cross-origin`, then attaches it with `traefik.http.routers.backend-https.middlewares=secure-headers@docker` and the same line on `adminer-https`. The middleware lives on the backend's labels only because Traefik's Docker provider reads a middleware from any container; the agent writes a comment saying so, because the next person will look for it on the proxy. `stsPreload` is left off and named as a decision for the owner of the domain, since preload is hard to undo.

A Content-Security-Policy is the part the agent does not guess at. The built Vite app loads only its own hashed files under `/assets`, but `/docs` and `/redoc` are FastAPI's interactive pages, which load Swagger UI and ReDoc from a CDN, so a strict `default-src 'self'` would break them. The agent sets `contentSecurityPolicy` as a report-only value through `customResponseHeaders` with `Content-Security-Policy-Report-Only`, lists in the pull request the sources those pages need, and leaves the enforcing policy for a follow-up once the team decides whether the docs pages stay public.

To prove the headers are served, the agent installs Docker in its sandbox with `sudo apt-get install -y docker.io`, starts `dockerd`, adds the Compose v2 plugin so that `docker compose` understands the `develop` and `depends_on.condition` keys this file uses, and attaches the middleware to `backend-http` in `compose.yml` as well so the check works without a certificate. `docker compose -f compose.yml -f compose.deploy.yml config` renders the merged labels, `docker compose up -d --wait proxy backend db` brings up the local stack from `compose.override.yml`, and `curl -sI -H 'Host: localhost' http://localhost/api/v1/utils/health-check/` shows the headers on the response. The agent then adds one step to `.github/workflows/test-docker-compose.yml`, which already builds, runs `scripts/prestart.sh` and curls the backend on port 8000: a request through the proxy on port 80 whose output is piped to `grep` for each header, so the workflow fails if a label is ever renamed.

## Start it with one API call

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

```json
{
  "agent": "claude",
  "input": "Add security headers through Traefik. Read compose.yml, compose.override.yml, compose.deploy.yml, deployment-docker-compose.md, backend/app/main.py and .github/workflows/test-docker-compose.yml first. Declare a headers middleware in labels with HSTS including subdomains, nosniff, frame deny and a referrer policy, attach it to the backend-https and adminer-https routers in compose.deploy.yml and to backend-http in compose.yml, and add a report-only Content-Security-Policy that keeps /docs and /redoc working, with the enforcing policy left as a documented follow-up. Install Docker in the sandbox, bring up proxy, backend and db, curl the health check through the proxy and quote the headers. Add a step to test-docker-compose.yml that fails when a header is missing. 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 a pull request that changes `compose.yml`, `compose.deploy.yml` and `.github/workflows/test-docker-compose.yml`, and adds a paragraph to `deployment-docker-compose.md` naming the middleware and where its labels live. No application code changes: the headers are added at the proxy, so `backend/app/main.py` and the CORS middleware it configures are untouched.

The description holds the rendered labels from `docker compose config`, the `curl -sI` output through the proxy with every new header visible, the report-only policy with a sentence per source explaining which page needs it, and the list of what was deliberately left out: preload, an enforcing policy, and headers on the `adminer` HTTP router, which exists only for local development. The repository's `Test Docker Compose` workflow runs on the branch and now checks the headers itself.

Before merging, a reviewer confirms the domain is served only over HTTPS before HSTS goes live, because the value applies to every subdomain for as long as `stsSeconds` says, and decides whether `/docs` should keep loading its assets from a CDN or whether the team wants an enforcing policy sooner.

## Run it on every GitHub issue labelled security

In the Project that holds the repository, create an automation with a general form of this page's prompt and a `github` trigger on `issues.labeled` with `security` in its `labels`. The issue is added after the prompt, so the prompt says to read the issue that follows, make the proxy or header change it asks for in the Compose labels, verify it through the proxy with curl and open a pull request that links the issue. The automation runs as the member who created it, and the trigger fires only for a label added by a repository owner, member or collaborator.

A scan report that lists a missing `Strict-Transport-Security` becomes one issue with the label, and the run opens the pull request with the curl output in it. The next finding, say a `Permissions-Policy` value or the enforcing CSP, starts its own run and its own pull request, so each change is reviewed on its own.

## Why run it on Rush?

Checking a proxy header needs Traefik, the backend and Postgres running together, a request that goes through port 80 rather than the backend's own 8000, and the merged view of three Compose files, which few people set up on a laptop for a labels change. The sandbox installs Docker, runs the stack and keeps the curl output, so the pull request opens with the headers shown rather than described.

The run's record keeps the rendered configuration and every request the agent made, and the new workflow step keeps checking the headers after merge, so a renamed label shows up as a failed check instead of a quiet regression. A company with several apps behind Traefik starts one run per repository, and each pull request carries that stack's own routers and its own list of pages a strict policy would break.

## Frequently asked questions

### Why put the middleware labels on the backend rather than the proxy?

Traefik's Docker provider builds its dynamic configuration from the labels of every container it can see, and the `proxy` service in `compose.yml` has no labels at all. A middleware declared on `backend` is a normal `@docker` middleware any router can reference, and keeping it next to the routers that use it makes the file easier to read.

### Will HSTS break local development on http://localhost?

No. Browsers ignore `Strict-Transport-Security` on a plain HTTP response, so the local stack from `compose.override.yml` keeps working on port 80, while the same label makes the header visible to curl for the workflow check. The header takes effect only once the deployed site answers over HTTPS through `compose.deploy.yml`.

### Why is the Content-Security-Policy report-only?

FastAPI's `/docs` and `/redoc` pages load their scripts from a CDN, so an enforcing `default-src 'self'` would blank them. A report-only policy shows in the browser console what an enforcing one would block, and the pull request lists those sources so the team can pick the policy knowingly.

### Does Adminer get the headers too?

Yes, on its HTTPS router in `compose.deploy.yml`, since it is exposed on a public subdomain there. Its HTTP router from `compose.yml` is for local development only and is left alone.

## The same job on other stacks

- [Add Security Headers to a Vapor App](https://getrush.ai/run/add-security-headers/vapor/)
- [Add Security Headers to a Wagtail Site from a Linear Issue](https://getrush.ai/run/add-security-headers/wagtail/)
- [Add Security Headers to an Actix Web App](https://getrush.ai/run/add-security-headers/actix/)
- [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/)
