Articles

Manage CORS for multiple APIs from one allowlist

Reference one CORS preset from two APIs and change the allowed browser origin without redeploying either.

Updated

Download the shared starter and open its shared-cors folder.

One browser frontend, two APIs, and two copies of the same CORS allowlist. The problem shows up when the frontend moves: one API accepts the new origin and the other still blocks it. Gregale supports an account-wide CORS preset so both rules can read the same list. Change the preset once instead of editing or redeploying either API.

Put two disposable APIs behind the same policy

Reserve two new, randomly named disposable apps with POST /v1/apps. Require a fresh create response and save each returned slug and app ID before deploying. Stop if a name already exists. Deploy this fixture to those two apps. Each serves the same synthetic inventory on GET /api/inventory and has no CORS middleware. Use the named Gregale app host for each test. The path must be available without credentials for this fixture; never open an existing private API just to try this.

Run node browser.mjs locally to start three frontend origins. They are all http://localhost, but the ports make them different browser origins. At each origin, enter the two disposable HTTPS app URLs. The page adds a synthetic request header so the browser performs a preflight, then attempts to read the inventory JSON. It does not send cookies or keys.

Set one allowlist

On Hobby or higher, create a preset using the CORS presets API. Omit app_id so rules on both apps in the same account can reference it. An account-wide preset is shareable, not globally enforced: it affects only rules that reference it.

json
{
  "name": "inventory-demo-unique",
  "allow_origins": ["http://localhost:4173"],
  "allow_methods": ["GET"],
  "allow_headers": ["X-Fixture-Client"],
  "allow_credentials": false,
  "max_age_seconds": 0
}

Send the JSON to POST /v1/cors-presets using your Gregale management credential. Save its returned ID. On each disposable app, send a POST /v1/apps/{slug}/edge-rules with this body, substituting the exact app host and preset ID:

json
{
  "kind": "cors",
  "match_host": "YOUR-DISPOSABLE-APP.gregale.dev",
  "match_path": "/api/inventory",
  "priority": 100,
  "enabled": true,
  "action": { "cors_preset_id": "PRESET_ID" }
}

Do not mix inline CORS settings and the preset reference. Read the browser JSON from port 4173; from port 4174 the browser should not be able to read it. Port 4175 stays a never-allowed control.

Move the browser frontend without redeploying the APIs

Send one PATCH /v1/cors-presets/PRESET_ID with {"allow_origins":["http://localhost:4174"]}. This replaces the array; include both exact origins if you need a migration overlap. Allow the change to propagate, then try both frontends again. The new origin should read the same fixture data from both APIs, and the old origin should lose browser access. Check that both recorded 100% deployment IDs are unchanged.

Use a real browser for client behavior. A direct HTTP 200 does not prove a browser can read the response; a matching Access-Control-Allow-Origin and a permitted preflight are the useful HTTP checks. CORS is not API authentication. Keep authorization on protected data and use precise origins, particularly if you enable credentials.

Run the checks and clean up

node --test server.test.mjs checks the fixture origin's responses and confirms that the app itself supplies no CORS headers. With the fixture deployed on two fresh public disposable apps named dot-cors-…, the included python verify-hosted.py APP_A APP_A_ID APP_B APP_B_ID script verifies both creation IDs, refuses hosts with existing active CORS rules (including wildcard covering rules), and checks preflights and response headers, compares before/after deployment history, and deletes the preset and rules it created. It reads the management key from GREGALE_TOKEN and never sends that key to either app hostname. Delete the two disposable apps when done.

For manual testing, remove the two referencing rules first, then delete the preset. Do not delete a shared preset still used by real apps. See edge rules and the single-API CORS guide for other setups.

What we verified on Gregale

On Gregale, both APIs initially returned 204 preflights and the exact inventory JSON for localhost:4173. After one preset update, both allowed localhost:4174 and no longer allowed the old origin. The never-allowed origin stayed denied and both deployment histories stayed the same. These were hosted HTTP preflight and response checks; the browser harness is included for readers to verify browser behavior separately.

Questions

Does a shared CORS preset apply to every API?

No. It only applies to edge rules referencing the preset. It does not replace authorization.