# Add Security Headers and a CSP to an Eleventy Site

Yes. An agent reads what your Eleventy layouts inline, writes a CSP and the other headers for Netlify, Vercel and Cloudflare Pages, checks the build and opens a PR.

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

## What the agent does

The agent clones the repository into its own cloud sandbox and reads how the site is built and hosted before writing a header. In a site like 11ty/eleventy-base-blog, `eleventy.config.js` bundles CSS and JavaScript into `dist/` with `addBundle`, runs the image transform plugin to emit `avif` and `webp`, and adds an Atom feed with an XSL stylesheet; `netlify.toml` has only a `[build]` block publishing `_site`, `vercel.json` sets only `trailingSlash`, and `public/` is copied to the site root by passthrough. The layout, `_includes/layouts/base.njk`, is what decides the policy: it writes the CSS bundle inline with `<style>{% getBundle "css" %}</style>`, and a comment right below it says to switch to `getBundleFileUrl` if you cannot allow `style-src 'unsafe-inline'`; it inlines the `heading-anchors` web component from `node_modules` in a `<script type="module">` in the head, and loads the JavaScript bundle from a file with `getBundleFileUrl` at the end of the body. There are no third-party scripts, fonts or analytics, so a strict policy is within reach.

It changes the layout so the policy can be strict without hashes or nonces. The inline `heading-anchors` script moves into the JavaScript bundle with the `{% js %}` paired shortcode, so it ships in the `dist/` file the page already loads and `script-src 'self'` holds; the CSS bundle switches from `getBundle` to a `<link rel="stylesheet">` on `getBundleFileUrl`, exactly the swap the layout's own comment describes, so `style-src 'self'` holds too. The policy it writes is `default-src 'self'`, `script-src 'self'`, `style-src 'self'`, `img-src 'self' data:`, `font-src 'self'`, `object-src 'none'`, `base-uri 'self'`, `form-action 'self'` and `frame-ancestors 'none'`, and it checks the feed's XSL stylesheet and the `data:` URIs the image plugin can emit still fit before settling on it. Beside the CSP go `X-Content-Type-Options: nosniff`, `Referrer-Policy: strict-origin-when-cross-origin`, a `Permissions-Policy` that turns off camera, microphone and geolocation, and `Strict-Transport-Security` with a one-year `max-age`.

Then it writes the headers once per host the README deploys to. A `public/_headers` file, which passthrough copies to `_site/_headers`, is the format both Netlify and Cloudflare Pages read, with one `/*` block carrying every header; `vercel.json` gains a `headers` array with the same values under a `/(.*)` source. GitHub Pages cannot set response headers, so the agent adds a `<meta http-equiv="Content-Security-Policy">` in the layout only when the site is built with the `--pathprefix` the `build-ghpages` script uses, and documents that `frame-ancestors` and HSTS have no meta form. The two copies of the policy are generated from one source, a `_data/csp.js` file whose value the layout and a small build-time script both read, so the Netlify and Vercel files cannot drift from each other.

To check it, the agent runs `npm install` and `npx @11ty/eleventy` in the sandbox, confirms `_site/_headers` is present and that no `<style>` or inline `<script>` body remains in any generated HTML with a grep over `_site/`, then serves `_site` with a small static server that applies the `_headers` file and opens the home page, a post, the tags page and the feed in its browser with the console open, so a resource the policy blocks shows up as a console error before anything is deployed. It validates `vercel.json` against Vercel's schema and the `_headers` syntax with the Netlify CLI, and puts the browser console output and the header listing into the pull request.

## Start it with one API call

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

```json
{
  "agent": "claude",
  "input": "Do the work the Linear issue describes: add security headers and a Content-Security-Policy to this Eleventy site. Read eleventy.config.js, package.json, netlify.toml, vercel.json, the public directory, every layout under _includes and the README's deployment section first. Find everything the layouts inline, move inline scripts into the JavaScript bundle and inline CSS into a linked bundle file so the policy can be 'self' only, and write a CSP plus X-Content-Type-Options, Referrer-Policy, Permissions-Policy and Strict-Transport-Security. Write them for every host the README deploys to: a _headers file in the passthrough directory for Netlify and Cloudflare Pages, a headers array in vercel.json, and a meta CSP for GitHub Pages builds only, generated from one data file so the copies cannot drift. Build the site, grep the output for any remaining inline style or script, serve the output with the headers applied, open several pages in the browser and record console errors, and validate the host config files. Do not change content or styling. Open a pull request with the header listing and the console output.",
  "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 adding `public/_headers` and `_data/csp.js`, extending `vercel.json` with a `headers` array, and changing `_includes/layouts/base.njk` in two places: the inline web component script becomes a `{% js %}` block that joins the bundle, and the inline CSS bundle becomes a `<link>` to the bundle file. No content, CSS or image changes, so every page renders as before and the diff is a handful of lines.

The description lists each header with its value and the reason for every CSP directive, shows the `grep` over `_site/` proving no inline style or script survives, and pastes the browser console from the pages the agent opened under the policy, which is empty when the policy fits. It names the one thing it could not do, headers on GitHub Pages, and what the meta fallback covers instead.

Before merging, a reviewer checks the Lighthouse best-practices audit on a deploy preview, decides whether to add `report-uri` or `report-to` so violations in the wild are logged, and confirms the HSTS `max-age` is right for a site that is always served over HTTPS, since it cannot be undone quickly.

## Run it on every Linear issue labelled security

Add an automation to the Project with this prompt and a `linear` trigger on `linear.issue.labeled` with the label your team uses for agent work. The trigger adds the labelled issue after the prompt, so an issue that names a new third-party script to allow, a comments widget or an analytics origin, steers the run to widen the policy by exactly that origin without any edit to the prompt; a change made by an app integration never fires it, and `max_runs_per_hour` caps how many issues the automation picks up in an hour.

From your own server, send `POST /o/{org}/sessions` with the repository and the issue URL in `resources` so the issue text reaches the agent through your Linear connection, and a `request_id` that is a UUID v5 of the issue id so a retried call returns the run already started. Set a `webhook_url` on the key to receive `run.completed` with `data.session_url` when the pull request is open.

## Why run it on Rush?

A Content-Security-Policy is right only when every page loads with an empty console under it, and that means knowing everything the layouts inline and every host's way of setting a header. The agent reads the layouts and the config files, moves the inline pieces into bundles, builds the site and opens it in its own browser with the headers applied, and the console output and header listing are in the run's record beside the pull request, so a reviewer sees the policy working rather than trusting a list of directives.

The same request runs across every static site an organisation publishes, each in its own sandbox with its own layouts and hosting files, which is how a team gets a strict policy on all of them in one review cycle, each widened only by the origins that site actually uses.

## Frequently asked questions

### Will the policy break my pages?

Not as written, because the agent first moves the inline script and CSS into the bundle files the page already loads and then opens the built pages in its browser under the policy, recording any console error. A third-party widget added later needs its origin added to the policy, which is one line in the shared data file.

### Why move the inline CSS into a file? The inlining was for speed.

An inline style block needs `style-src 'unsafe-inline'`, which weakens the policy for every page. The layout's own comment offers the linked bundle as the alternative, and the file is small and cached after the first page. If you prefer to keep it inline, the agent can emit a hash per build instead, which the pull request describes.

### Does this work on GitHub Pages?

Partly. GitHub Pages sets no custom response headers, so the agent adds the CSP as a `meta` tag for that build only. `frame-ancestors`, HSTS and the other headers have no meta equivalent and are simply absent there, which the pull request says plainly.

### Why one _headers file for two hosts?

Netlify and Cloudflare Pages both read a `_headers` file at the root of the published directory with the same syntax, so a single file in `public/`, which passthrough copies to the output, covers both. Vercel reads `vercel.json` instead, and the values are generated from the same data file.

### What about the Atom feed and its stylesheet?

The feed loads `pretty-atom-feed.xsl` from the same origin, which `style-src 'self'` allows, and the agent opens the feed in the browser under the policy to confirm it renders. Nothing in the feed is inline.

## More Eleventy jobs

- [Add Social Cards to an Eleventy Blog](https://getrush.ai/run/add-social-cards/eleventy/)
- [Add JSON-LD Structured Data to an Eleventy Blog](https://getrush.ai/run/add-structured-data/eleventy/)
- [Add a Table of Contents to Eleventy Blog Posts](https://getrush.ai/run/add-table-of-contents/eleventy/)

## The same job on other stacks

- [Add Security Headers to a Fiber App](https://getrush.ai/run/add-security-headers/fiber/)
- [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/)

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