# Add Security Headers to a Hono App

Yes. An agent adds Hono's secureHeaders middleware with a Content Security Policy, drops the X-Powered-By header, asserts each header in Vitest and opens a PR.

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

## What the agent does

The agent reads the app and what it already sends. In a Worker shaped like the `basic` example in honojs/examples, `package.json` pins `hono` 4 with `wrangler` 3 and `vitest` 1, the scripts are `wrangler dev src/index.ts`, `wrangler deploy` and `vitest --run`, and `src/index.ts` registers `poweredBy()` on every route, `basicAuth` on `/auth/*`, `etag` on `/etag/*`, a custom `X-message` header on `/hello/*` and an `X-Response-Time` middleware, then serves text routes such as `/`, `/entry/:id`, `/book`, `/user-agent` and JSON at `/api/posts`. The one test in `src/index.test.ts` asserts `GET /` returns 200 and an `x-powered-by` of `Hono`. There is no `secureHeaders` call, so responses carry no `X-Content-Type-Options`, no `X-Frame-Options` and no Content Security Policy.

It imports `secureHeaders` from `hono/secure-headers`, which ships inside the `hono` package so no dependency is added, and registers it first with `app.use('*', secureHeaders({...}))` so every route, including the 404 and error handlers, is covered. The defaults set `X-Content-Type-Options`, `X-Frame-Options`, `Referrer-Policy`, `Strict-Transport-Security`, `Cross-Origin-Opener-Policy` and `Cross-Origin-Resource-Policy`; the agent adds a `contentSecurityPolicy` with `defaultSrc: ["'self'"]` and `frameAncestors: ["'none'"]`, which is safe here because the app returns plain text and JSON with no scripts or styles. Since the middleware removes `X-Powered-By`, the agent deletes the `poweredBy()` line rather than fighting it and leaves the `X-message` and `X-Response-Time` headers alone.

The test file grows from one case to a handful, all through `app.request()` as the existing test does, with Vitest's `globals: true` from `vitest.config.ts`. One asserts `x-powered-by` is now absent, one asserts each security header on `GET /`, one asserts the same headers on the custom 404 from `app.notFound` and on the 500 from `app.onError` via `/error`, and one asserts the headers on `/api/posts` where `prettyJSON()` runs. The agent runs `npm -w basic test` from the workspace root, the way the README says to run an example.

It also starts `wrangler dev` in the sandbox and reads the headers with `curl -I http://localhost:8787/` so the pull request shows what the Workers runtime actually sends, not only what the in-process `app.request()` returns. `wrangler.toml` keeps its `compatibility_date`; nothing else in the config changes.

## Start it with one API call

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

```json
{
  "agent": "claude",
  "input": "Add security headers to this Hono app and open a pull request. Read package.json, wrangler.toml, vitest.config.ts, src/index.ts and src/index.test.ts first. Register secureHeaders from hono/secure-headers before every other middleware with a Content Security Policy of default-src 'self' and frame-ancestors 'none', and remove the poweredBy middleware. Keep the X-message and X-Response-Time middleware. Extend src/index.test.ts with app.request() cases asserting that x-powered-by is absent and that the security headers are present on GET /, on the custom 404, on the /error route and on /api/posts. Run the tests with npm -w basic test, then start wrangler dev and paste the output of curl -I against the root route in the pull request along with the test summary. Do not change any route handler.",
  "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 `src/index.ts` and `src/index.test.ts` and nothing else; no dependency is added because the middleware is part of `hono`. The description lists each header and its value as `curl -I` printed them from `wrangler dev`, and the Vitest summary with the new cases.

The repository has no GitHub Actions workflow, so the Vitest run in the pull request description and the transcript are the proof. A reviewer who wants a check adds a workflow that runs `npm -w basic test` as a follow-up, or asks the agent to.

A reviewer checks that the policy does not block anything the app serves, which is simple here since every route returns text or JSON, that `Strict-Transport-Security` is acceptable for the domain, and that removing `X-Powered-By` broke no client. Then they tighten `frame-ancestors` or add `connect-src` when a browser frontend lands.

## Run it on every new Worker deployed

A platform team that owns many small Hono Workers starts one `POST /o/{org}/sessions` per repository with this prompt and the repository in `resources`. Each run reads what that Worker serves, so one that renders HTML with inline scripts gets a `scriptSrc` with a nonce rather than the bare `'self'` this text-only app gets, and each run opens its own pull request.

With a `webhook_url` on the key, each `run.completed` arrives with the `session_url`, and a handler can post the pull request link to the team channel.

## Why run it on Rush?

A Content Security Policy written from memory is the classic way to break a working page: a header that blocks the app's own fetches, or a frame policy that kills an embed nobody remembered. In a Rush sandbox the agent runs the real Worker under `wrangler dev`, reads the headers with `curl` and runs the test suite, so the pull request shows the exact header values a browser will receive.

The commands and their output are kept in the run, so when someone asks six months later why `X-Powered-By` disappeared, the answer is in the transcript next to the change. And the same request applies to every Hono service the team owns without anyone setting up a local Workers toolchain.

## Frequently asked questions

### Why does the existing test change?

It asserts `x-powered-by` is `Hono`, and the secure headers middleware removes that header. Advertising the framework has no benefit, so the pull request drops `poweredBy()` and flips the assertion to expect the header to be absent.

### Will the CSP break the JSON routes?

No. A Content Security Policy only governs what a browser may load when it renders a document. Text and JSON responses are unaffected, and the policy is there for the day an HTML route is added.

### Does this work outside Cloudflare Workers?

Yes. `hono/secure-headers` sets headers on the `Response`, which is the same on Workers, Deno, Bun and Node. The tests use `app.request()` and run anywhere; only the `curl -I` check uses `wrangler dev`.

### Can I keep X-Powered-By?

The middleware takes a `removePoweredBy: false` option. The pull request does not use it, and the description says where to flip it if a client depends on the header.

## More Hono jobs

- [Add Streaming Responses to a Hono API](https://getrush.ai/run/add-streaming-responses/hono/)
- [Add Structured Logging to a Hono Worker](https://getrush.ai/run/add-structured-logging/hono/)
- [Dockerize a Hono App Running on Bun](https://getrush.ai/run/dockerize-app/hono/)

## The same job on other stacks

- [Add Security Headers to a Koa App](https://getrush.ai/run/add-security-headers/koa/)
- [Add Security Headers to a Leptos Axum App](https://getrush.ai/run/add-security-headers/leptos/)
- [Add Security Headers to a Litestar App](https://getrush.ai/run/add-security-headers/litestar/)

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