All jobs

Add CORS to a Rocket JSON API

Yes. An agent adds a CORS fairing with origins read from Rocket.toml, a preflight route, and local Client tests for allowed and refused origins, in 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 the API before it writes a header. In a workspace like rwf2/Rocket's examples/, examples/Cargo.toml lists the member crates and examples/serialization/Cargo.toml depends on rocket by path from ../../core/lib with the json, msgpack and uuid features, so the example tracks the in-tree version rather than a published release. src/main.rs attaches json::stage(), msgpack::stage() and uuid::stage(); src/json.rs mounts new, update and get under /json with format = "json", keeps the messages in a Mutex<Vec<String>> behind State, and registers a 404 catcher. src/tests.rs drives it with rocket::local::blocking::Client, asserting statuses and bodies for bad and good GET, PUT and POST calls. Nothing sets Access-Control-Allow-Origin, there is no route answering OPTIONS, and the crate has no Rocket.toml of its own; the fairings and config examples show the two patterns the agent reuses, a Fairing with Kind::Response and AdHoc::config::<T>() reading a struct from configuration.

It writes the fairing by hand rather than pulling a crate. A cors.rs module defines a CorsConfig with an allowed_origins list and a max_age, extracted with AdHoc::config so the values live in a new Rocket.toml under [default] and can differ per profile. The Cors fairing implements on_response: when the request's Origin header matches an entry exactly, it sets Access-Control-Allow-Origin to that origin, adds Vary: Origin, and on a preflight also sets Access-Control-Allow-Methods, Access-Control-Allow-Headers echoing what the browser asked for, and Access-Control-Max-Age. An origin that is not listed gets no CORS headers at all, so the browser refuses, and a request without Origin passes through untouched. The agent avoids a wildcard, because the example exists to show the right shape.

Preflight needs a route. Rocket answers OPTIONS with 404 unless something is mounted, so the agent adds an #[options("/<_..>")] handler returning Status::NoContent and mounts it beside the JSON routes in json::stage(), which is enough for the fairing to decorate the response. The format = "json" guards on the existing routes stay as they are, since a preflight carries no body and takes the new route instead.

Then it tests against the same Client the example already uses. New cases in src/tests.rs send GET /json/0 with an Origin header that is in the list and assert the allow header and Vary; send the same with an origin that is not listed and assert no allow header; and send OPTIONS /json/0 with Origin, Access-Control-Request-Method and Access-Control-Request-Headers and assert the 204 and the three preflight headers. It runs cargo test inside examples/serialization and, because the repository's CI runs scripts/test.sh --examples across the whole examples workspace, builds that too before opening the pull request.

Start it with one API call

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

{
  "agent": "claude",
  "input": "Add CORS to the JSON API in examples/serialization of this Rocket workspace. Read examples/Cargo.toml, examples/serialization/Cargo.toml, examples/serialization/src/main.rs, src/json.rs, src/tests.rs, examples/fairings/src/main.rs, examples/config/src/main.rs, examples/config/Rocket.toml and scripts/test.sh first. Write a cors.rs module with a CorsConfig struct, allowed_origins and max_age, read through AdHoc::config from a new Rocket.toml, and a Fairing on Kind::Response that sets Access-Control-Allow-Origin and Vary: Origin only for an exact origin match, plus Allow-Methods, Allow-Headers and Max-Age on preflight. Add an options route for /<_..> returning 204 mounted with the JSON routes. Do not use a wildcard origin and do not add a CORS crate. Add tests in src/tests.rs with the blocking Client for an allowed origin, a refused origin and a preflight. Run cargo test in examples/serialization and scripts/test.sh --examples, then open a pull request that lists the headers set and the configuration keys.",
  "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 that adds examples/serialization/src/cors.rs and examples/serialization/Rocket.toml, edits src/main.rs to attach the fairing and the config, edits src/json.rs to mount the preflight route, and extends src/tests.rs with three cases. The description lists the response headers the fairing sets and when, the allowed_origins and max_age keys and their defaults, and the fact that a missing or unlisted origin receives no CORS headers.

The run's record keeps the cargo test output with each test named and the result of scripts/test.sh --examples, so a reviewer can see the new preflight test among the existing JSON and MessagePack tests and that the rest of the examples workspace still builds.

Before merging, a reviewer confirms the origins in Rocket.toml are the ones the frontend is served from, that Vary: Origin is present so a shared cache never serves one origin's allow header to another, and that Access-Control-Allow-Credentials is absent unless the API actually uses cookies.

Run it on every Linear issue labelled cors

In the console, add an automation to the Project with this prompt and a linear trigger on linear.issue.labeled, filtered to the cors label and the backend team. When someone labels an issue such as "browser blocks POST /json from the dashboard origin", the trigger puts the issue after the prompt and the run reads the origin it names into allowed_origins. Only label changes made by a person in your Rush org fire the trigger, and the automation starts at most max_runs_per_hour runs in an hour.

From your own server, the same body goes to POST /o/{org}/sessions with the repository in resources and the issue URL beside it, read 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 instead of opening a second pull request.

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

Why run it on Rush?

CORS is easy to get wrong in a way that looks fine from curl: a wildcard that quietly allows every site, a preflight that returns 404 because no route answers OPTIONS, a missing Vary that lets a cache leak one origin's header to another. The agent exercises the three browser paths with Rocket's own local Client in its sandbox and reads the headers that came back, instead of trusting the fairing from the diff.

The commands and their output stay with the run, so the pull request can point at the test that saw 204 with the preflight headers and at the one that saw a refused origin receive nothing, which is what a reviewer would otherwise need a browser and a second origin to reproduce.

Frequently asked questions

Why not use the rocket_cors crate?

The example depends on the in-tree rocket at ../../core/lib, a development version ahead of any published release, so a third-party crate pinned to a released Rocket may not compile against it. A fairing of a few dozen lines has no such coupling, and it keeps the example readable as a sample.

Will it allow every origin?

No. The fairing matches the Origin header exactly against allowed_origins in Rocket.toml and sets no CORS header otherwise. A wildcard is not written, and a test asserts an unlisted origin gets nothing.

What about preflight requests?

Rocket has no route for OPTIONS by default, so a browser's preflight would get 404. The agent mounts an options route for every path under /json that returns 204, and the fairing adds the allow-methods, allow-headers and max-age headers to that response.

Can I change the origins per environment?

Yes. The struct is read with AdHoc::config, so [debug] and [release] sections in Rocket.toml, or ROCKET_ALLOWED_ORIGINS in the environment, override the default list the way the config example shows for its own keys.

More Rocket jobs

The same job on other stacks

Keep reading