# Add Security Headers to a Wagtail Site from a Linear Issue

Yes. Label the Linear issue and an agent fixes dead security settings in your Wagtail site, adds a nonce-based content security policy and a referrer policy, tested.

[Run this on Rush](https://cloud.getrush.ai/run/add-security-headers/wagtail/). 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 site already sends before it adds a header, and in a Wagtail site shaped like wagtail/bakerydemo the first finding is that some of it is dead. `bakerydemo/settings/base.py` puts `SecurityMiddleware` first in `MIDDLEWARE`, followed by WhiteNoise, sessions, CSRF, auth, messages, `XFrameOptionsMiddleware` and Wagtail's redirect middleware, and `requirements/base.txt` pins `Django>=6.0,<6.1`, `wagtail>=8.0,<8.1`, `whitenoise` and `django-csp>=3.8,<4`. `bakerydemo/settings/production.py` sets `SECURE_SSL_REDIRECT` from the environment with `True` as the default, `SECURE_PROXY_SSL_HEADER` for `X-Forwarded-Proto`, `SECURE_HSTS_SECONDS` to 30 days unless the environment says otherwise with `SECURE_HSTS_INCLUDE_SUBDOMAINS` off, and `SECURE_CONTENT_TYPE_NOSNIFF`. It also sets `SECURE_BROWSER_XSS_FILTER`, a setting Django removed several major versions ago, and `REFERRER_POLICY`, read from a `SECURE_REFERRER_POLICY` environment variable, which is the name used by a third-party package that appears in no requirements file; Django's own setting is `SECURE_REFERRER_POLICY`, so the environment knob changes nothing and the site sends Django's default. `bakerydemo/settings/test.py` turns `SECURE_SSL_REDIRECT` off and `SECURE_HSTS_SECONDS` to 0 so tests run over plain HTTP.

The content security policy in `base.py` is a block of `CSP_*` settings that only runs when `CSP_DEFAULT_SRC` is in the environment, always in report-only mode, with each directive split from its own variable, written for django-csp 3. Two facts decide the approach: django-csp 4 changed its configuration to a `CONTENT_SECURITY_POLICY` dictionary and breaks the old names, and the pinned Django 6.0 ships its own `ContentSecurityPolicyMiddleware` with `SECURE_CSP` and `SECURE_CSP_REPORT_ONLY` dictionaries and a `csp` template context processor that provides a nonce. The agent picks the built-in, which the agent confirms against the release notes for the pinned version, drops `django-csp` from `requirements/base.txt`, and rewrites the block: a report-only policy by default with `default-src` and `img-src` on `self`, `script-src` on `self` plus the nonce, `style-src` on `self`, and an environment switch that moves the same policy to `SECURE_CSP` for enforcement, with `/admin/` and `/django-admin/` excluded because the Wagtail admin ships its own inline scripts and styles. The inventory behind that policy is short: `bakerydemo/templates/base.html` has one inline `<script>` that sets `data-theme` from `localStorage` and `prefers-color-scheme`, three stylesheets served from `static`, and no inline `style` attributes across the templates, so the one script gets the nonce from the context processor and nothing else needs an exception.

The rest is settings hygiene in the same files. `SECURE_BROWSER_XSS_FILTER` is removed, `REFERRER_POLICY` becomes `SECURE_REFERRER_POLICY` set to `strict-origin-when-cross-origin` with the environment override kept, `SECURE_CROSS_ORIGIN_OPENER_POLICY` is left at Django's default and noted, and a small middleware in `bakerydemo/base/middleware.py` adds a `Permissions-Policy` header that denies camera, microphone and geolocation, since Django has no setting for it. `X-Frame-Options` stays at Django's `DENY` default; Wagtail's preview panel loads the page in a frame from the admin, and `base.html` already checks `request.in_preview_panel`, so the agent loads a page preview in the sandbox browser after the change to confirm the admin's own frame handling still renders it.

Tests follow `bakerydemo/base/tests/test_seo.py`: a `WagtailPageTestCase` whose `setUpTestData` creates a `Site` on `testserver`, a `HomePage` with `hero_text` and `hero_cta`, publishes a revision and requests `home.url` with the test client. `test_security_headers.py` asserts `Content-Security-Policy-Report-Only` is present with a `nonce-` value that matches the attribute on the inline theme script, `X-Content-Type-Options` is `nosniff`, `Referrer-Policy` is the chosen value, `X-Frame-Options` is `DENY`, `Permissions-Policy` is present, and no `Strict-Transport-Security` is sent under the test settings, with one `override_settings` case that sets `SECURE_HSTS_SECONDS` to see the header appear. The agent runs what `.github/workflows/ci.yml` runs: `uv pip install -r requirements/development.txt` on Python 3.12, `./manage.py check`, `./manage.py makemigrations --check --noinput`, `./manage.py test` with `DJANGO_SETTINGS_MODULE=bakerydemo.settings.test` as `AGENTS.md` recommends, and `make lint-server` for `ruff format --check`, `ruff check`, `curlylint` and `djhtml`; it also runs `./manage.py check --deploy` under the production settings to read Django's own security warnings before and after.

## Start it with one API call

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

```json
{
  "agent": "claude",
  "input": "A Linear issue labelled security says: \"Audit and fix the security headers: content security policy, referrer policy, permissions policy, and remove settings that no longer do anything.\" Do the work in this Wagtail repository. Read requirements/base.txt, bakerydemo/settings/base.py, production.py, dev.py and test.py, bakerydemo/urls.py, bakerydemo/templates/base.html and every template under bakerydemo/templates for inline scripts and styles, bakerydemo/base/tests/test_seo.py, AGENTS.md, Makefile and .github/workflows/ci.yml first. Check the pinned Django version's release notes for its built-in content security policy support and, if the pinned version has it, replace the django-csp block in base.py with SECURE_CSP_REPORT_ONLY by default and an environment switch to SECURE_CSP, add the ContentSecurityPolicyMiddleware after SecurityMiddleware and the csp context processor, put the nonce on the inline theme script in base.html, exclude the admin paths, and drop django-csp from the requirements; otherwise upgrade django-csp and say why. Remove SECURE_BROWSER_XSS_FILTER, replace REFERRER_POLICY with SECURE_REFERRER_POLICY set to strict-origin-when-cross-origin keeping the environment override, and add a middleware that sends a Permissions-Policy header denying camera, microphone and geolocation. Add bakerydemo/base/tests/test_security_headers.py in the style of test_seo.py asserting each header, the nonce match, and HSTS absent under test settings and present with override_settings. Run ./manage.py check, ./manage.py makemigrations --check --noinput and ./manage.py test with the test settings, make lint-server, and ./manage.py check --deploy under production settings before and after. Load a page and an admin preview in the browser to confirm nothing is blocked. Do not change the uwsgi configuration, the Docker files or any workflow. Open a pull request that links the issue, lists each header with its value, and quotes the test, lint and deploy-check 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 on its own branch that changes `requirements/base.txt`, `bakerydemo/settings/base.py`, `bakerydemo/settings/production.py` and `bakerydemo/templates/base.html`, adds `bakerydemo/base/middleware.py` and `bakerydemo/base/tests/test_security_headers.py`. The body links the Linear issue, lists each header with the value a page now sends, names the two settings that were dead and why, states that the policy is report-only until the environment switch is set, and quotes the test run, `make lint-server` and the `check --deploy` output before and after.

The transcript keeps the install, the first test run in which the header tests failed, the passing run, the lint, both deploy checks, and the browser session in which the agent loaded a page and an admin preview with the policy in place and read the console for reports. `GET /o/{org}/p/{project}/sessions/{id}/changes` returns the diff per file.

Before merging, a reviewer checks that the policy stays report-only by default so a production deploy cannot block the theme script before reports have been read, that the admin paths are excluded, that the nonce reaches the one inline script and that no template still carries an inline handler, and that dropping `django-csp` left no import behind. Turning enforcement on is a later environment change, after a week of reports.

## Run it on every Linear issue labelled security

In the Project that holds the site, add an automation with this prompt and a `linear` trigger on `linear.issue.labeled` filtered to the `security` label. Each labelled issue starts its own run in its own sandbox and ends in its own pull request; the issue text follows the prompt as data, so one issue can ask for a stricter `img-src` or a different referrer value, and the trigger fills in no placeholder. Only a change by someone in your Rush org fires it, and changes made by an app integration never do.

A handler on your own server does the same without the automation: it receives Linear's webhook, puts the issue text into `input`, and sends `POST /o/{org}/sessions` with the repository in `resources` and a `request_id` that is a UUID v5 of the issue id, so a redelivered webhook returns the run already started. A `webhook_url` on that key posts `run.completed` or `run.failed` when the turn ends, with `data.session_url` to read the pull request.

## Why run it on Rush?

Security settings rot quietly: a setting Django removed keeps sitting in `production.py`, an environment variable that a never-installed package would have read changes nothing, and a report-only policy that is only switched on by an environment variable has never run. Reading the settings against the pinned Django's own documentation, running `check --deploy` and asserting each header in a test is exactly the slow, careful work that gets postponed, and the agent does it in its own sandbox with the test settings, the production settings and a browser.

The result is a pull request that says which headers the site now sends and proves it with a test and a deploy check, and that keeps enforcement a deliberate later step. Several Django or Wagtail sites get the same audit at once, each in its own sandbox with its own pull request, with each site's templates read for inline scripts rather than assumed clean.

## Frequently asked questions

### Will the policy break the Wagtail admin?

No. The admin paths are excluded from the policy, because the Wagtail admin ships inline scripts and styles of its own, and the site templates are covered by a policy built from their actual inventory: one inline theme script, which gets a nonce, and stylesheets served from `static`. The policy is report-only until the environment switch turns enforcement on.

### Why drop django-csp?

The repository pins `django-csp` below version 4, whose configuration format the next major version breaks, and pins a Django version that ships its own content security policy middleware, settings and nonce context processor. One fewer dependency and the framework's own settings is the smaller change. If the pinned Django in your site is older, the agent upgrades `django-csp` instead and says so.

### Is HSTS sent in development or in tests?

No. `test.py` sets `SECURE_HSTS_SECONDS` to 0 and turns the HTTPS redirect off, and `dev.py` inherits `base.py` without the production block, so only `production.py` sends it, at 30 days without `includeSubDomains`. The test asserts its absence under the test settings and its presence with `override_settings`.

### What were the dead settings?

`SECURE_BROWSER_XSS_FILTER`, which Django no longer reads, and `REFERRER_POLICY`, a name belonging to a third-party package that is not installed; Django's setting is `SECURE_REFERRER_POLICY`. The pull request removes the first and renames the second with a value of `strict-origin-when-cross-origin`, keeping the environment override.

### Does the admin preview panel still work with X-Frame-Options DENY?

The agent checks it in the browser rather than reasoning about it: it opens a page preview from the admin after the change and confirms it renders. `base.html` already branches on `request.in_preview_panel`, and the pull request reports what the preview did.

## More Wagtail jobs

- [Add Schema.org Structured Data to a Wagtail Site](https://getrush.ai/run/add-structured-data/wagtail/)
- [Add Type Hints to a Wagtail Codebase](https://getrush.ai/run/add-type-hints/wagtail/)
- [Add a New Page Type to a Wagtail Site from a Linear Issue](https://getrush.ai/run/add-workshop-page/wagtail/)

## The same job on other stacks

- [Add Security Headers to an Actix Web App](https://getrush.ai/run/add-security-headers/actix/)
- [Add Security Headers to an aiohttp App](https://getrush.ai/run/add-security-headers/aiohttp/)
- [Add Security Headers to an Axum API](https://getrush.ai/run/add-security-headers/axum/)

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