All jobs

Add Security Headers to a Symfony App

Yes. An agent adds HSTS, frame, content-type and referrer headers and a content security policy to your Symfony app, proves them with PHPUnit and opens a PR.

Run this on RushOpens 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 already sends before adding a header. In a Symfony 8 app on PHP 8.4 shaped like symfony/demo, a grep across config, src and templates finds no Content-Security-Policy, no Strict-Transport-Security and no nelmio package in composer.json, so every header is new. config/packages/framework.yaml turns on sessions, ESI and fragments; config/packages/security.yaml protects the admin with a form login, CSRF on login and logout, and a remember-me cookie with a lifetime of 604800 seconds. Those tell the agent which headers are safe to add outright, X-Content-Type-Options, X-Frame-Options and Referrer-Policy, and which one needs care, the policy, because the templates are not static.

The policy is shaped by templates/base.html.twig and importmap.php. The base layout calls importmap('app'), which Symfony AssetMapper renders as an inline importmap script and module preloads, and importmap.php lists the packages it serves from the app's own origin: bootstrap 5.3.8, jquery 3.7.1, highlight.js 11.11.1 with a CSS theme, flatpickr with its stylesheet, typeahead.js and the Lato font CSS, all vendored by asset-mapper rather than loaded from a CDN. Only when FRANKENPHP_HOT_RELOAD is set does the layout pull two scripts from cdn.jsdelivr.net, and the dev profiler toolbar from config/packages/web_profiler.yaml injects its own inline script. So script-src cannot be a bare self: the inline importmap needs a nonce or a hash, and the agent checks whether the installed asset-mapper's importmap Twig function accepts attributes so a nonce can be passed, falling back to Content-Security-Policy-Report-Only if it cannot and saying so.

For the mechanism the agent follows the house pattern. src/EventSubscriber already holds four subscribers that implement getSubscribedEvents, such as RedirectToPreferredLocaleSubscriber on the request event, and config/services.yaml autoconfigures everything under src, so a new ResponseHeadersSubscriber on KernelEvents::RESPONSE needs no registration. It sets the static headers on every main response, generates a per-request nonce for the policy, and skips HSTS unless the request is secure and the environment is prod, because the test client in DefaultControllerTest requests http://localhost and the lint workflow runs lint:twig with the prod environment. Where the app needs the nelmio/security-bundle instead, for example for report URIs, the agent says why in the pull request rather than adding a dependency by default.

It proves it the way the repository proves everything else. tests/Controller/DefaultControllerTest.php is a WebTestCase smoke test over the public URLs, the root, /en/blog/ and /en/login, and the admin URLs that must redirect, with the DAMA transaction extension from phpunit.dist.xml wrapping each test. The agent adds tests/Controller/SecurityHeadersTest.php in the same style, asserting each header on a public page and that a response under the policy still renders the blog with the importmap, and runs vendor/bin/phpunit, which fails on any deprecation or notice because phpunit.dist.xml sets failOnDeprecation, failOnNotice and failOnWarning. Then it runs what .github/workflows/lint.yaml runs: php-cs-fixer check, lint:yaml on config, lint:twig with env prod, lint:container and PHPStan with the project's phpstan.dist.neon and baseline.

Start it with one API call

POST https://api.phoenix.so/o/{org}/sessions with this body:

{
  "agent": "claude",
  "input": "Add security headers to this Symfony app. Read composer.json, config/packages/framework.yaml, security.yaml, web_profiler.yaml, twig.yaml, asset_mapper.yaml, config/services.yaml, importmap.php, templates/base.html.twig, every class in src/EventSubscriber, tests/Controller/DefaultControllerTest.php, phpunit.dist.xml and .github/workflows/lint.yaml and tests.yaml first. Add a response subscriber under src/EventSubscriber that sets X-Content-Type-Options nosniff, X-Frame-Options DENY, a Referrer-Policy, Strict-Transport-Security only on secure requests in the prod environment, and a Content-Security-Policy that allows the app's own origin, a per-request nonce for the inline importmap script, and the two jsdelivr scripts only when FRANKENPHP_HOT_RELOAD is set. Check whether the installed asset-mapper lets importmap() take a nonce attribute; if not, ship the policy as Content-Security-Policy-Report-Only and explain. Make sure the dev profiler toolbar still works in dev. Add tests/Controller/SecurityHeadersTest.php as a WebTestCase asserting each header on / and /en/blog/. Run vendor/bin/phpunit, php-cs-fixer check, bin/console lint:yaml config, lint:twig templates --env=prod, lint:container and vendor/bin/phpstan analyze, fix what they report, and open a pull request that lists each header with its value and the reason, and quotes the test summary.",
  "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 adds one class under src/EventSubscriber, one test class under tests/Controller, and, if the nonce has to reach the template, a small change to templates/base.html.twig where importmap('app') is called. composer.json and composer.lock are unchanged unless the agent explains why the nelmio bundle was needed. The description lists each header with its value and the reason, and states whether the policy is enforcing or report-only and what it would take to enforce it.

The session read names the repository and the pull request URL. The transcript at GET /o/{org}/sessions/{id}/messages keeps the grep that found no existing headers, the files read, the phpunit run with the new test failing before the subscriber existed and passing after, and each linter from lint.yaml with its output. A reviewer sees the exact header values the test client received without running anything.

Before merging, a reviewer loads the blog and the admin in a browser on the branch, with the console open, to see whether the policy blocks anything the test client could not exercise, especially the flatpickr and typeahead assets on the admin post form and the profiler toolbar in dev. They also confirm HSTS is absent on plain http, since a wrong max-age there is hard to undo.

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 security review uses. Labelling the issue starts one run with the issue added after the prompt, so a finding from a scanner pasted into the issue reaches the agent as context. Changes made by an app integration never fire the trigger, and the automation's max_runs_per_hour cap bounds how many runs a bulk relabel can start.

From your own server, the same run starts with this request when your Linear webhook handler sees the label, with the issue URL in resources beside the repository and a request_id that is a UUID v5 of the delivery id so a retried webhook returns the run already started. A webhook URL on the key reports run.completed with data.session_url for the runs that key starts, and a follow-up on the same session can tighten the policy once the report-only period has shown what it would block.

The use case this job follows, with its automation and trigger: Turn a Linear label into a PR.

Why run it on Rush?

A content security policy written without running the app is a guess that breaks the admin form or the profiler toolbar on the first click. Checking it means composer install, a database with fixtures, phpunit, four linters and PHPStan, and then a browser on both the blog and the admin, which on a laptop is an afternoon of context switching. The agent does it in its own sandbox with PHP 8.4, SQLite and a browser, and throws the sandbox away when the turn ends.

The evidence stays attached to the change. The transcript keeps the grep that proved no headers existed, each header value the test client received, and every linter's output, so a reviewer can tell an enforced header from a report-only one without trusting the description. Nothing reaches main except through the pull request a person merges.

Frequently asked questions

Will the content security policy break the inline importmap?

Not if the nonce reaches it. Symfony AssetMapper renders importmap('app') as an inline script, so the subscriber generates a nonce per request and the agent checks whether the installed importmap Twig function accepts attributes to carry it. If it does not, the policy ships as Content-Security-Policy-Report-Only with a note on how to enforce it.

Does it use the nelmio/security-bundle?

Not by default. The repository already has an EventSubscriber directory with four subscribers and autoconfigures everything under src, so one more subscriber on the response event fits the house style with no new dependency. The agent adds the bundle only if a feature like report URIs calls for it, and says so in the pull request.

Will HSTS be sent in development or in tests?

No. The test client in DefaultControllerTest requests http://localhost, and the lint workflow runs with the prod environment, so the subscriber sends Strict-Transport-Security only when the request is secure and the kernel environment is prod. The new test asserts it is absent over plain http.

What about the dev profiler toolbar and FrankenPHP hot reload?

Both inject scripts the policy would otherwise block. The agent allows the two cdn.jsdelivr.net scripts only when FRANKENPHP_HOT_RELOAD is set, as base.html.twig already does, and either relaxes the policy in the dev environment or lets the profiler add its own nonce, and the pull request says which.

More Symfony jobs

The same job on other stacks

Keep reading