# Add Security Headers to a Fiber App

Yes. An agent adds Fiber's helmet middleware with a Content Security Policy that fits the pages your app serves, proves it in a browser, and opens a PR.

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

## What the agent does

The agent reads what the app serves before choosing a single header, because a Content Security Policy copied from a blog post breaks the first page that loads a script from somewhere else. In a Fiber app like gofiber/boilerplate, `go.mod` declares module `boilerplate` on `go 1.26.0` with `github.com/gofiber/fiber/v3` v3.6.1 and `github.com/gofiber/utils/v2` as its direct dependencies. `app.go` builds everything inside `main`: `recover.New()`, `logger.New()`, a `/api/v1` group with `GET /users` and `POST /users` from `handlers/handlers.go`, `static.New("./static/public")` on `/*`, and `handlers.NotFound`, which sends `static/private/404.html` with a 404. `static/public/index.html` loads Bootstrap's script and stylesheet from `cdn.jsdelivr.net` and then `/js/app.js`, which calls `fetch` on `/api/v1/users` and posts a `FormData` body to the same path. The 404 page loads the same two Bootstrap files. There are no inline scripts and no inline styles, which is what makes a strict policy possible without nonces.

Fiber v3 ships the middleware, so nothing is added to `go.mod`. The agent imports `github.com/gofiber/fiber/v3/middleware/helmet` and registers `helmet.New` right after `recover.New()`, so every response, static files and JSON alike, carries the headers. The defaults already send `X-Content-Type-Options: nosniff`, `X-Frame-Options: SAMEORIGIN`, `Referrer-Policy: no-referrer`, `Cross-Origin-Opener-Policy: same-origin`, `Cross-Origin-Resource-Policy: same-origin`, `X-DNS-Prefetch-Control: off`, `X-Permitted-Cross-Domain-Policies: none` and `X-XSS-Protection: 0`. The agent sets `XFrameOptions` to `DENY`, because nothing in the app frames itself, and writes a `ContentSecurityPolicy` that names exactly what the two HTML files use: `default-src 'self'`, `script-src 'self' https://cdn.jsdelivr.net`, `style-src 'self' https://cdn.jsdelivr.net`, `img-src 'self' data:`, `connect-src 'self'`, `frame-ancestors 'none'`, `base-uri 'self'` and `form-action 'self'`. One default has to change: `Cross-Origin-Embedder-Policy` defaults to `require-corp`, which makes the browser refuse the Bootstrap files unless the CDN answers with a resource policy of its own or the tags carry `crossorigin`, so the agent sets it to `unsafe-none` and says why in the pull request rather than editing the CDN tags blind. The helmet config treats an empty string as "use the default", so a header with a non-empty default cannot be switched off by clearing it; the pull request notes that too.

`Strict-Transport-Security` needs care, because the app listens on plain `:3000` and the middleware only sends the header when `c.Secure()` is true: a TLS connection, or a request from a trusted proxy that reports `https` in `X-Forwarded-Proto`. The agent sets `HSTSMaxAge` to one year with subdomains included and adds a `TrustProxy` and `TrustProxyConfig` block to `fiber.Config` behind a `-trust-proxy` flag beside the existing `-port` and `-prod` flags, off by default, so a deployment behind a TLS-terminating proxy turns it on by listing the proxy's address range. Without that, `TrustProxy` alone trusts no peer and the header is never sent, which the pull request spells out so nobody wonders why `curl -I http://localhost:3000` shows no HSTS line.

To check the policy the way a user would, the agent builds and starts the app in its sandbox, opens `http://localhost:3000` in the sandbox's browser, watches the console for CSP violations while Bootstrap loads from the CDN and `/js/app.js` renders the user list, submits the add-user form, and visits a path that does not exist to see the 404 page styled. It then runs `curl -I` against `/`, `/api/v1/users` and `/nope` and pastes the headers into the pull request. Because `app.go` keeps its wiring inside `main`, the agent moves the app construction into a `newApp() *fiber.App` function that `main` calls, and adds `app_test.go` using Fiber's `app.Test` with `httptest.NewRequest` to assert the policy, `X-Frame-Options`, `nosniff` and the embedder policy on all three paths, and that a request with `X-Forwarded-Proto: https` from a trusted proxy gets the HSTS header while a plain one does not. It runs `gofmt -l .`, `go vet ./...`, `go test ./... -race` as `.github/workflows/test.yml` does on Go 1.26.x and 1.27.x, and `golangci-lint run` against the repository's `.golangci.yml`, then opens the pull request.

## Start it with one API call

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

```json
{
  "agent": "claude",
  "input": "Add security headers to this Fiber v3 app. Read go.mod, app.go, handlers/handlers.go, static/public/index.html, static/public/js/app.js, static/private/404.html, Dockerfile, .golangci.yml and .github/workflows/test.yml first. Register github.com/gofiber/fiber/v3/middleware/helmet in app.go right after the recover middleware. Keep the defaults except: set XFrameOptions to DENY, set CrossOriginEmbedderPolicy to unsafe-none because the HTML loads Bootstrap from cdn.jsdelivr.net without crossorigin attributes, set HSTSMaxAge to one year including subdomains, and write a ContentSecurityPolicy that allows scripts and styles from self and https://cdn.jsdelivr.net, images from self and data URLs, connect-src self, frame-ancestors none, base-uri self and form-action self; nothing wider. Add a -trust-proxy flag, off by default, that enables TrustProxy with a TrustProxyConfig so HSTS is sent behind a TLS-terminating proxy, and document it in README.md. Move the app construction into a newApp function and add app_test.go that uses app.Test to assert the headers on /, /api/v1/users and an unknown path, and that HSTS appears only for a trusted forwarded https request. Build and start the app, open it in the browser, confirm there are no CSP violations while the user list renders and the add-user form works, check the 404 page, and run curl -I on the three paths. Run gofmt, go vet, go test ./... -race and golangci-lint run, then open a pull request that pastes the curl output and explains the embedder policy choice and the HSTS condition.",
  "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 `app.go`, adds `app_test.go` and adds a short section to `README.md`. In `app.go` the construction moves into `newApp`, the helmet middleware sits after `recover` with its explicit config, and the new `-trust-proxy` flag feeds `TrustProxy` and `TrustProxyConfig` in `fiber.Config`. The description pastes the `curl -I` output for the index page, the users API and the 404 path, with the policy, `X-Frame-Options: DENY`, `X-Content-Type-Options: nosniff` and the embedder policy visible on each, and a screenshot of the browser console on `/` showing no violations with Bootstrap loaded from the CDN.

The run's record holds the build, the test run on the sandbox's Go toolchain, the lint run and the browser session, so the reviewer can see the page rendered under the policy rather than reason about it. `go.mod` and `go.sum` are untouched because Fiber v3 carries the middleware itself.

Before merging, a reviewer decides whether to pin Bootstrap's version in the CDN URL so the policy's allowed host stays meaningful, whether `img-src` should drop `data:`, whether to tighten the embedder policy later by adding `crossorigin` to the CDN tags, and which address range to pass to `TrustProxyConfig` in production. The existing `test.yml` runs the new test on both Go versions and both platforms once the branch is pushed.

## Run it on every new Fiber service

A team with several Fiber services starts one run per repository from its own server: one `POST /o/{org}/sessions` each, with that repository in `resources` and a `request_id` that is a UUID v5 of the repository's full name, so a retried call returns the run already started instead of opening a second pull request. Each run reads that service's own HTML and scripts, so a service with inline scripts gets a nonce-based policy and a pure JSON API gets a policy with no script sources at all.

Once the headers are in, an automation on the Project with a `github` trigger on `pull_request.opened` and `pull_request.synchronize` and the prompt "if this pull request adds a script, stylesheet, image or fetch to a host the Content-Security-Policy in app.go does not allow, comment with the tag and the directive it needs" keeps the policy and the pages from drifting apart. The trigger puts the pull request after the prompt; only work by an owner, member or collaborator fires it, never a fork's.

## Why run it on Rush?

A Content Security Policy is the one change that looks right in a terminal and fails in a browser: `curl -I` shows the header, and the page is blank because a CDN script was refused. The agent runs the app in its own sandbox and opens it in a real browser, so the policy is checked against the console, the rendered user list and the form submit before anyone reviews it, and the HSTS condition is checked with a forwarded request instead of assumed.

The sandbox keeps the build, the test and lint output, the curl headers and the browser session with the run. For a team with many Go services, each gets its own run and its own pull request with a policy derived from its own pages, not one policy pasted across all of them.

## Frequently asked questions

### Will the policy break Bootstrap loaded from the CDN?

Not with the policy the agent writes, which allows `https://cdn.jsdelivr.net` for scripts and styles because `index.html` and `404.html` load both from there. The part that would break it is the embedder policy default `require-corp`, which is why the agent sets `unsafe-none` and explains the alternative of adding `crossorigin` to the CDN tags. The browser check in the sandbox is what shows both files loading with no console violation.

### Why does curl on localhost show no Strict-Transport-Security line?

Because Fiber's helmet sends it only when the request is secure: TLS, or a trusted proxy reporting `https` in `X-Forwarded-Proto` or a similar header. The app listens on plain `:3000`, so a local curl never qualifies. The new `-trust-proxy` flag and `TrustProxyConfig` are how a deployment behind a TLS-terminating proxy makes the header appear, and the test asserts both cases.

### Does the agent change the API responses?

The handlers in `handlers/handlers.go` are untouched. JSON responses on `/api/v1/users` carry the same headers as the HTML, which is harmless and simpler than skipping them with `Next`. A reviewer who wants different headers for the API group can move the middleware onto the group or add a `Next` function; the pull request says where.

### Why split app.go into newApp?

Because everything was built inside `main`, there was no way to construct the app in a test. `newApp` returns the configured `*fiber.App`, `main` keeps the flag parsing, the database connect and `Listen`, and `app_test.go` uses `app.Test` to send requests without opening a port. The Dockerfile's `go build` still produces the same binary.

## More Fiber jobs

- [Add Structured JSON Logging to a Fiber App](https://getrush.ai/run/add-structured-logging/fiber/)
- [Add WebSockets to a Fiber App](https://getrush.ai/run/add-websockets/fiber/)
- [Review Pull Requests on a Fiber v3 App](https://getrush.ai/run/review-pull-requests/fiber/)

## The same job on other stacks

- [Add Security Headers to a Gin API](https://getrush.ai/run/add-security-headers/gin/)
- [Add Security Headers to a Hono App](https://getrush.ai/run/add-security-headers/hono/)
- [Add Security Headers to a Koa App](https://getrush.ai/run/add-security-headers/koa/)

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