# Add Security Headers to a Nuxt App

Yes. An agent adds a Content Security Policy and related headers through Nuxt route rules, built from what your pages really load, tests them, and opens a PR.

[Run this on Rush](https://cloud.getrush.ai/run/add-security-headers/nuxt/). 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 loads from where before it writes a policy, because a Content Security Policy copied from a blog post blocks the first third-party frame it meets. In an app shaped like nuxt/movies, `package.json` pins `nuxt` to a nightly 4.6 build with `pnpm` 11 and Node 24 in `devEngines`, and `nuxt.config.ts` already has a `routeRules` block: `'/**'` sets `cache-control: private, no-store` and nothing else. The modules are `@vueuse/nuxt`, `@unocss/nuxt`, `@nuxt/image`, `@nuxtjs/i18n` and `@nuxtjs/html-validator`. `app/composables/tmdb.ts` fetches from `https://movies-proxy.vercel.app/tmdb`, the `image` provider in `nuxt.config.ts` serves every `NuxtImg` through `ipx` at the same proxy's `/ipx` path, `app/components/IframeModal.vue` renders an `iframe` whose `src` comes from `getTrailer` in `app/composables/item.ts`, which builds `https://www.youtube.com/embed/<key>`, and `app/components/video/Card.vue` loads thumbnails from `/youtube/vi/<key>/maxresdefault.jpg` through the image provider. `app/app.vue` sets `og:image` to `https://movies.nuxt.space/social-card.png`.

From that inventory it writes the headers into the existing `routeRules['/**'].headers` object next to `cache-control`, so there is no new module and no server middleware to maintain. The policy names what it found: `default-src 'self'`; `connect-src 'self'` plus the proxy origin from `apiBaseUrl`; `img-src 'self' data:` plus the proxy origin; `frame-src https://www.youtube.com`; `script-src 'self'` with the inline allowance Nuxt's hydration needs; `style-src 'self' 'unsafe-inline'` for the UnoCSS runtime styles; `object-src 'none'`; `base-uri 'self'`; `frame-ancestors 'none'`. Beside it go `X-Content-Type-Options: nosniff`, `Referrer-Policy: strict-origin-when-cross-origin`, `Permissions-Policy` with camera, microphone and geolocation off, and `Strict-Transport-Security`. The proxy origin is read from the same `apiBaseUrl` constant the config and the composable use, so switching to the local proxy on port 3001 changes the policy with it.

The agent keeps the policy honest by testing it in a browser, not just asserting its text. `playwright.config.ts` runs `tests/e2e/*.spec.ts` through `@nuxt/test-utils/playwright` on desktop Chrome, Safari and a mobile Chrome, and `tests/e2e/homepage.spec.ts` already opens `/tv/549` and switches language. The new `tests/e2e/security-headers.spec.ts` requests `/` and asserts each header is present with the expected value, then opens a title page, clicks the trailer so `IframeModal` loads the YouTube frame, and asserts no `securitypolicyviolation` event and no console error about a blocked resource occurred while posters and the frame loaded. If a directive turns out too tight, the agent loosens that one and says so in the pull request rather than dropping the policy.

`vitest.config.ts` runs the unit suite under `tests/unit` and the `*.nuxt.test.ts` files beside components, so the agent also adds a small unit test that imports the route rules and checks the policy contains the proxy origin and the YouTube embed origin and no `*` source. Before pushing it runs `pnpm run lint` with the `@antfu/eslint-config` rules, `pnpm run typecheck`, `pnpm run test:unit` and `pnpm exec playwright test`, the four commands the `tests-unit.yml` and `tests-e2e.yml` workflows run, and pastes the results into 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 Nuxt app and open a pull request. Read nuxt.config.ts, app/app.vue, app/composables/tmdb.ts, app/composables/item.ts, app/components/IframeModal.vue, app/components/video/Card.vue, app/constants/images.ts, playwright.config.ts, vitest.config.ts, tests/e2e/homepage.spec.ts and the workflows in .github/workflows first. Add a Content-Security-Policy, X-Content-Type-Options, Referrer-Policy, Permissions-Policy and Strict-Transport-Security to the existing routeRules['/**'].headers in nuxt.config.ts; build the policy from the origins the app really uses: the proxy in apiBaseUrl for connect-src and img-src, www.youtube.com for frame-src, and whatever Nuxt hydration and UnoCSS need for script-src and style-src. Do not add a module. Write tests/e2e/security-headers.spec.ts that asserts the headers on / and then opens a title page, plays the trailer and asserts nothing was blocked, and a unit test under tests/unit that checks the policy's sources. Run pnpm run lint, pnpm run typecheck, pnpm run test:unit and pnpm exec playwright test and paste the results in the pull request. Do not change the cache-control rule.",
  "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 edits one block of `nuxt.config.ts` and adds `tests/e2e/security-headers.spec.ts` and a unit test under `tests/unit`. No component changes, no new dependency, and the `cache-control` rule stays as it was. The description lists every header with its value, explains each policy source by the file that needs it, names any directive that had to be loosened and why, and pastes the lint, typecheck, unit and Playwright output.

On a repository with the `Tests unit` and `Tests e2e` workflows, the pull request runs the unit tests, ESLint and `nuxt typecheck`, then installs Playwright browsers and runs the e2e suite on the three configured projects, so a policy that blocks the trailer frame on WebKit fails there before anyone merges it.

A reviewer checks the policy sources against the deployment they run, since a different image proxy or a self-hosted TMDB proxy changes `connect-src` and `img-src`, and decides whether `Strict-Transport-Security` should carry `includeSubDomains` for their domain. Then they can watch the browser console on the preview deployment for violations the tests did not cover.

## Run it on every Nuxt app without a Content Security Policy

A team with several Nuxt sites sends one `POST /o/{org}/sessions` per repository with this prompt and that repository in `resources`. Each run inventories its own fetch calls, image providers and embeds, so a site that loads fonts from a CDN or a map from another origin gets those sources and nothing else, and each run opens its own pull request.

Give the API key a `webhook_url` and Rush POSTs `run.completed` when each turn ends, with the `session_url` to read the pull request and the test output from.

## Why run it on Rush?

The hard part of a Content Security Policy is not writing it but finding out what it breaks. A policy is only checked by loading real pages in a real browser and watching for blocked requests. In a Rush sandbox the agent builds the app, runs the Playwright suite on Chromium, WebKit and a mobile viewport, opens the trailer modal to load the one cross-origin frame the app has, and keeps every command and its output in the run's record, so the pull request shows the policy holding under the pages that stress it rather than asserting that it should.

The headers live in the `routeRules` block the config already has, so there is nothing new to learn and no module whose defaults need overriding. The sources are derived from the same constants the fetch composable and the image provider use, which keeps the policy and the code from drifting apart. The same request works on any Nuxt app that renders on the server.

## Frequently asked questions

### Why route rules instead of the nuxt-security module?

The app already has a `routeRules['/**']` block, and Nitro applies its `headers` to every server response, including the ones Vercel serves. One edit covers the whole site with no new dependency. The module is a good choice when you want nonces or per-route policies; the pull request says so if the agent thinks the app has grown to need them.

### Will it break the trailer or the posters?

That is what the e2e test is for. It opens a title page, clicks play so `IframeModal` loads the YouTube embed, waits for posters from the image proxy, and fails on any `securitypolicyviolation` event or blocked-resource console error. `frame-src` names `www.youtube.com` and `img-src` names the proxy origin because those files load from there.

### What happens when the proxy URL changes?

The policy reads the origin from the same `apiBaseUrl` constant `nuxt.config.ts` and `app/composables/tmdb.ts` use, so pointing the app at the local proxy on port 3001 or at your own deployment updates `connect-src` and `img-src` with it. The unit test checks the policy contains that origin.

### Does it add Strict-Transport-Security on localhost too?

The header is sent on every response, and browsers ignore it over plain HTTP, so local development is unaffected. The agent sets a `max-age` and leaves `preload` out, because preloading a domain is a decision for the people who own it, and the pull request says how to add it.

## More Nuxt jobs

- [Add a Sitemap to a Nuxt App](https://getrush.ai/run/add-sitemap/nuxt/)
- [Add Structured Data (JSON-LD) to a Nuxt App](https://getrush.ai/run/add-structured-data/nuxt/)
- [Add a Watchlist Page to a Nuxt App from a Linear Issue](https://getrush.ai/run/add-watchlist/nuxt/)

## The same job on other stacks

- [Add Security Headers to a Quarkus REST API](https://getrush.ai/run/add-security-headers/quarkus/)
- [Add Security Headers and a CSP to a React Router App](https://getrush.ai/run/add-security-headers/react-router/)
- [Add Security Headers to a Slim 4 App](https://getrush.ai/run/add-security-headers/slim/)

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