# Add Security Headers to a SvelteKit App

Yes. An agent adds a Content Security Policy in svelte.config.js and the other headers in hooks.server.js, checks them on the built app and opens a pull request.

[Run this on Rush](https://cloud.getrush.ai/run/add-security-headers/sveltekit/). 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 before it writes a policy that would block it. In an app like sveltejs/realworld, `package.json` depends on `@sveltejs/kit` 2.52, `svelte` 5.55, `vite` 7.3 and `@sveltejs/adapter-vercel`, with `prettier` as the only check and `pnpm@10.20.0` as the package manager; `.nvmrc` says Node 20. `svelte.config.js` enables runes and sets the adapter and nothing else, so no `kit.csp` exists. `src/app.html` pulls Ionicons from `code.ionicframework.com`, fonts from `fonts.googleapis.com`, and the theme from `/conduit-theme.css` in `static/`, and links `/manifest.json`. `src/hooks.server.js` has a single `handle` that decodes the `jwt` cookie into `event.locals.user` and calls `resolve`, which is where response headers can be added.

Then it lists the other origins a page touches. `src/lib/api.js` calls `https://api.realworld.show/api` from server load functions and form actions, so the browser never calls it directly, but `ArticlePreview.svelte` renders `article.author.image` from whatever URL the API returns, and `constants.js` names `static.productionready.io` as the placeholder avatar. `+page.svelte` for an article renders `{@html data.article.body}`, markdown turned to HTML on the server with `marked` and `sanitize-html`, and `PreloadingIndicator.svelte` sets an inline `style` width, so `style-src` needs `'unsafe-inline'` or the indicator breaks. `data-sveltekit-preload-data` on the body means the client fetches `__data.json` from the same origin.

The agent writes the policy in `kit.csp` in `svelte.config.js` with `mode: 'auto'`, so SvelteKit adds nonces to the scripts it injects for hydration on server-rendered pages and hashes for prerendered ones: `default-src 'self'`, `script-src 'self'`, `style-src 'self' 'unsafe-inline' code.ionicframework.com fonts.googleapis.com`, `font-src 'self' code.ionicframework.com fonts.gstatic.com`, `img-src 'self' https: data:` because author images come from the API, `connect-src 'self'`, `frame-ancestors 'none'`, `base-uri 'self'` and `form-action 'self'`. In `hooks.server.js` it adds `X-Content-Type-Options: nosniff`, `Referrer-Policy: strict-origin-when-cross-origin`, `Permissions-Policy` denying camera, microphone and geolocation, and `Strict-Transport-Security` on the response `resolve` returns, leaving the cookie code as it is.

To check it, the agent runs `pnpm install`, `pnpm build` and `pnpm preview`, fetches `/`, `/login` and an article page with `curl -i` and reads the `content-security-policy` header and the nonce on each `<script>` tag, then opens the same pages in the sandbox browser and reads the console for blocked resources, which is the only way to see whether the icon font and the Google fonts still load and whether the preload indicator still animates. It runs `pnpm lint` so Prettier accepts the new lines. The repository has no tests and no workflow, so those requests and screenshots are the evidence.

## Start it with one API call

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

```json
{
  "agent": "claude",
  "input": "Add security headers to this SvelteKit app and open a pull request. Read package.json, svelte.config.js, src/app.html, src/hooks.server.js, src/lib/api.js, src/lib/constants.js, src/lib/ArticleList/ArticlePreview.svelte, src/routes/article/[slug]/+page.svelte and src/routes/PreloadingIndicator.svelte first, and list every external origin a page loads from. Add a Content Security Policy under kit.csp in svelte.config.js with mode auto: default-src self; script-src self; style-src self plus unsafe-inline and the Ionicons and Google Fonts hosts app.html uses; font-src self plus those font hosts; img-src self, https: and data: because author images come from the API; connect-src self; frame-ancestors none; base-uri self; form-action self. In src/hooks.server.js keep the cookie handling and add X-Content-Type-Options nosniff, Referrer-Policy strict-origin-when-cross-origin, a Permissions-Policy denying camera, microphone and geolocation, and Strict-Transport-Security to every response. Run pnpm install, pnpm build and pnpm preview, fetch /, /login and one article page with curl -i, then open them in the browser and confirm nothing is blocked in the console and the preload indicator still animates. Run pnpm lint. Paste the headers from curl and the console result in the pull request. Do not change app.html, the API client or any route.",
  "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 with a `csp` block under `kit` in `svelte.config.js` and four header lines in `src/hooks.server.js`, and nothing else changed. The description lists the origins the agent found in `app.html`, `constants.js` and the components, maps each to the directive that allows it, and pastes the `curl -i` response headers for the home, login and article pages, including the nonce value SvelteKit put on the inline script and the matching `script-src` entry.

A reviewer checks that `img-src` is as wide as the API's user images need and no wider, that `style-src` keeps `'unsafe-inline'` only because of the indicator's inline width and the theme's needs, that `frame-ancestors 'none'` is what the product wants, and that `Strict-Transport-Security` is set on the host that serves TLS. Then they open the preview URL from the pull request, look at the console, and merge.

## Run it on every new frontend deployed

A team with several SvelteKit frontends starts one run per repository with `POST /o/{org}/sessions`, this prompt and the repository in `resources`. The agent reads each app's `app.html` and components for its own origins, so a site that embeds a video player gets a `frame-src` entry and one that calls its API from the browser gets that host in `connect-src`, and each run opens its own pull request with its own headers pasted in.

The key's `webhook_url` receives `run.completed` for each run it starts, with `data.session_url` for the pull request link.

## Why run it on Rush?

A Content Security Policy written from memory breaks the page in a way nobody sees until a user reports a blank screen: a font host left out, a nonce missing on the hydration script, an inline style in one component. In a Rush sandbox the agent builds the app, serves it, reads the headers with `curl` and loads the pages in a real browser to read the console, and the headers and screenshots stay in the transcript with the pull request.

The same request works across every SvelteKit app the team owns, each run in its own sandbox, and a reviewer compares one list of origins with one policy instead of guessing which directive blocked what.

## Frequently asked questions

### Why put the CSP in svelte.config.js and not in hooks.server.js?

SvelteKit injects an inline script to hydrate each page, and only `kit.csp` knows to add a nonce or hash to it. With `mode: 'auto'` server-rendered pages get a nonce and prerendered pages get hashes. A policy set by hand in `handle` would have to allow `'unsafe-inline'` for scripts, which defeats the point.

### Will the icon font and Google Fonts still load?

Yes, because `app.html` names them and the agent puts `code.ionicframework.com`, `fonts.googleapis.com` and `fonts.gstatic.com` in `style-src` and `font-src`. The agent loads the pages in the sandbox browser and reads the console to confirm nothing is reported as blocked.

### Why does img-src allow any https host?

`ArticlePreview.svelte` renders `article.author.image`, a URL each user sets in their profile through the RealWorld API, so there is no fixed list of hosts. `'self' https: data:` allows those images without allowing scripts from anywhere. A deployment that proxies avatars can narrow it later.

### Does this change the login cookie?

No. `hooks.server.js` keeps the lines that read the `jwt` cookie into `event.locals.user`, and `cookies.set` in the login action is untouched. The agent adds headers to the response `resolve` returns and changes nothing about sessions.

## More SvelteKit jobs

- [Add a Sitemap to a SvelteKit App](https://getrush.ai/run/add-sitemap/sveltekit/)
- [Add Structured Data (JSON-LD) to a SvelteKit App](https://getrush.ai/run/add-structured-data/sveltekit/)
- [Convert a SvelteKit App from JavaScript to TypeScript](https://getrush.ai/run/convert-javascript-to-typescript/sveltekit/)

## The same job on other stacks

- [Add Security Headers to a Symfony App](https://getrush.ai/run/add-security-headers/symfony/)
- [Add Security Headers With a Traefik Middleware](https://getrush.ai/run/add-security-headers/traefik/)
- [Add Security Headers to a Vapor App](https://getrush.ai/run/add-security-headers/vapor/)

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