# Add Security Headers to a Quarkus REST API

Yes. An agent sets nosniff, X-Frame-Options, Referrer-Policy and a prod-only HSTS header in your Quarkus app's configuration, tests them and opens a PR.

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

## What the agent does

The agent reads how the service is wired before adding a header. In a Quarkus app like the `rest-json-quickstart` module of quarkusio/quarkus-quickstarts, `pom.xml` imports the `quarkus-bom` at `3.40.1.1`, compiles for Java 17, and depends on `quarkus-rest-jackson` plus `quarkus-junit` and `rest-assured` for tests. `FruitResource` serves `/fruits` with `GET`, `POST` and `DELETE` over an in-memory set, `LegumeResource` serves `/legumes`, and a `LoggingFilter` annotated `@Provider` implements `ContainerRequestFilter` to log method, path and remote address. There is no `src/main/resources/application.properties` at all, so every setting is a default. Two static pages, `META-INF/resources/fruits.html` and `legumes.html`, load AngularJS 1.4.8 from `ajax.googleapis.com` and a stylesheet from `cdnjs`, each with an inline `<script>` that calls the API.

Quarkus can set response headers from configuration, and that is the mechanism the agent picks over a JAX-RS response filter: `quarkus.http.header.<name>.value` is applied by the HTTP layer to every response, including the two static HTML pages, whereas a filter next to `LoggingFilter` would only see resource responses. The agent creates `application.properties` with `X-Content-Type-Options: nosniff`, `X-Frame-Options: DENY`, `Referrer-Policy: strict-origin-when-cross-origin`, a `Permissions-Policy` that turns off camera, microphone and geolocation, and a `Content-Security-Policy` that contains only `frame-ancestors 'none'`. Each is one quoted-key line in the file.

Two headers need judgment, and the pull request explains both. `Strict-Transport-Security` is written with the `%prod.` profile prefix, so it is sent only when the application runs in production, with a one-year `max-age` and `includeSubDomains`; sending it from a `dev` or `test` profile on plain HTTP would be meaningless, and sending it from a service that terminates TLS at a proxy is a deployment decision the description spells out. A `script-src` directive is deliberately left out of the `Content-Security-Policy`: `fruits.html` runs inline JavaScript and loads a framework from a Google CDN, so a strict policy would break the pages the quickstart ships. The agent says what would have to change, self-hosting the script and moving it out of the HTML, before `script-src` can be added.

Tests follow `FruitResourceTest`, which uses `@QuarkusTest` and RestAssured's `given().when().get("/fruits").then().statusCode(200)`. A new `SecurityHeadersTest` asserts each header's value on `/fruits`, asserts the same headers on `/fruits.html` to prove the static path is covered, and asserts that `Strict-Transport-Security` is absent, because tests run in the `test` profile. Because `FruitResourceIT` extends the unit test under `@QuarkusIntegrationTest`, the agent adds a matching `SecurityHeadersIT` so the native profile checks the same headers against the packaged application. It runs `./mvnw test` in the module with the wrapper's Maven 3.9.11 and pastes the Surefire summary.

## Start it with one API call

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

```json
{
  "agent": "claude",
  "input": "Add security headers to this Quarkus REST application and open a pull request. Read pom.xml, every class under src/main/java, src/main/resources/META-INF/resources/fruits.html and the tests under src/test/java first. Create src/main/resources/application.properties and set the headers with quarkus.http.header entries so they apply to resources and static files alike: X-Content-Type-Options nosniff, X-Frame-Options DENY, Referrer-Policy strict-origin-when-cross-origin, a Permissions-Policy that disables camera, microphone and geolocation, a Content-Security-Policy of frame-ancestors 'none' only, and Strict-Transport-Security with a one-year max-age and includeSubDomains under the %prod profile prefix only. Explain in the pull request why script-src is left out given the inline script and CDN in fruits.html and what would have to change first, and why HSTS is prod-only. Add a SecurityHeadersTest with @QuarkusTest and RestAssured asserting each header on /fruits and on /fruits.html and asserting HSTS is absent in the test profile, plus a SecurityHeadersIT extending it with @QuarkusIntegrationTest. Run ./mvnw test and paste the Surefire summary. Do not change the resources, the LoggingFilter or the HTML pages.",
  "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 new `src/main/resources/application.properties` of about seven lines, a `SecurityHeadersTest` and `SecurityHeadersIT` beside the existing tests, and a description that quotes the response headers RestAssured saw on `/fruits` and `/fruits.html` in the sandbox, along with the Surefire summary from `./mvnw test`. The two explanations, no `script-src` and prod-only HSTS, are in the description where a reviewer will read them.

In the quickstarts repository the pull request workflow builds every module on JDK 21 with a Maven cache and a star-import check; in a standalone Quarkus service the project's own `mvn verify` job runs the new tests, and the integration test runs wherever the `native` profile is built.

A reviewer checks the values against the frontends that call the API, since `X-Frame-Options: DENY` stops any embedding and the CSP's `frame-ancestors 'none'` says the same in the newer syntax, confirms that production really serves HTTPS before HSTS reaches a browser, and decides whether self-hosting the AngularJS script is worth doing so a `script-src` can follow.

## Run it on every new Quarkus service

A platform team sends one `POST /o/{org}/sessions` per service repository with this prompt and that repository in `resources`. Each run reads its own `application.properties` and static resources, so a service that already sets some headers gets only the missing ones, and a service with no inline scripts gets a `script-src` proposal instead of the explanation. 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 from, so the header set across all services can be compared before any of them merges.

## Why run it on Rush?

Headers are quick to add and quietly wrong: set in a filter that misses static files, or an HSTS that reaches a browser from a staging host on HTTP. In a Rush sandbox the agent boots the application under `@QuarkusTest`, requests both a resource and a static page and reads the actual response headers back, and every command and its output stays in the run's record, so the pull request shows the headers as sent.

The reasoning about what not to add is as useful as the properties file. The agent reads `fruits.html`, sees the inline script and the CDN, and writes down why `script-src` would break the page and what to change first. That is the paragraph a reviewer would otherwise have to work out themselves, and the same request produces it for the next service.

## Frequently asked questions

### Why configuration instead of a response filter?

`quarkus.http.header` entries are applied by the HTTP layer to every response, including `fruits.html` and `legumes.html` served from `META-INF/resources`. A JAX-RS filter beside `LoggingFilter` would cover `/fruits` and `/legumes` and miss the pages, and the test on `/fruits.html` is there to prove the difference.

### Why is there no Content-Security-Policy for scripts?

Because `fruits.html` runs an inline `<script>` and loads AngularJS 1.4.8 from `ajax.googleapis.com`. A `script-src 'self'` would break the page, and a policy with `'unsafe-inline'` protects nothing. The pull request sets `frame-ancestors 'none'` now and names the steps before `script-src` can follow.

### Why is HSTS prod-only?

`Strict-Transport-Security` tells browsers to refuse plain HTTP for the host for the whole `max-age`. In `dev` and `test` the app runs on HTTP, so the header is written with the `%prod.` prefix and the test asserts it is absent. Production behind a TLS-terminating proxy still needs the header on the response the browser sees, which the description points out.

### Does it work in native mode?

Yes. The header settings are runtime configuration, so a native executable reads them the same way, and `SecurityHeadersIT` runs the same assertions against the packaged application under `@QuarkusIntegrationTest`, as `FruitResourceIT` already does for the resource tests.

### Does this change CORS?

No. CORS is a separate `quarkus.http.cors` setting that the quickstart does not enable, and none of the added headers affects which origins may call the API. If your frontend runs on another origin, that is a different, equally small configuration change.

## More Quarkus jobs

- [Add Soft Delete to a Quarkus Panache Entity](https://getrush.ai/run/add-soft-delete/quarkus/)
- [Add Structured Logging to a Quarkus App](https://getrush.ai/run/add-structured-logging/quarkus/)
- [Review Pull Requests on a Quarkus Service](https://getrush.ai/run/review-pull-requests/quarkus/)

## The same job on other stacks

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

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