Imported from boise-state-development/agentcore-public-stack (
.claude/skills/cors-deployment/SKILL.md). Install upstream withnpx skills add boise-state-development/agentcore-public-stack --skill cors-deployment. Copyright stays with the author.
Allowed origins (CORS_ORIGINS)
What it is for now
The deployed SPA calls app-api same-origin: appApiUrl: '/api' in
environment.development.ts / environment.production.ts, routed by
CloudFront's /api/* behavior to the app-api ALB. Same-origin is what lets the
BFF Token Handler's __Host- session cookies work, and it means the browser
never makes a cross-origin call to app-api in a deployed environment. It never
calls the AgentCore Runtime at all: app-api proxies chat to it
(apis/app_api/chat/proxy_routes.py).
So the list is less about CORS than its name suggests. It has four live jobs:
| Job | Where | Breaks without it |
|---|---|---|
| S3 bucket CORS for presigned uploads straight from the browser | constructs/data/file-upload-construct.ts, constructs/rag/rag-data-construct.ts (+ spa/rag-cors-updater-construct.ts), constructs/fine-tuning/fine-tuning-data-construct.ts |
Uploads fail in the browser with a CORS error |
Local dev: ng serve on :4200 calls http://localhost:8000 (environment.ts), a real cross-origin request |
CORSMiddleware in apis/app_api/main.py |
Every local request fails preflight |
| Origin allowlists that reuse the variable | voice WebSocket origin check (apis/app_api/voice/routes.py), OAuth2CallbackUrl validation (apis/shared/middleware/agentcore_context.py), the SKIP_AUTH guard in apis/app_api/main.py |
Voice and MCP OAuth consent reject legitimate origins; the guard against SKIP_AUTH on a non-local deployment loses its input |
CORSMiddleware on app-api and inference-api in deployed environments |
apis/app_api/main.py, apis/inference_api/main.py |
Nothing today — no browser calls them cross-origin. Harmless; keep it for local dev and for any future cross-origin client |
Do not remove or empty CORS_ORIGINS as "CORS cleanup after the BFF move": the
allowlists in the third row depend on it.
How the list is built
CDK_DOMAIN_NAME→ always applied ashttps://{value}, firstCDK_CORS_ORIGINS→ additional global origins (optional, comma-separated)- Per-section
CDK_*_CORS_ORIGINS→ extras for one construct (optional)
localhost is never auto-included. Use CDK_CORS_ORIGINS=http://localhost:4200
for an environment local dev must reach.
CDK_DOMAIN_NAME → domainName → "https://{domainName}" (always first)
CDK_CORS_ORIGINS → extraCorsOrigins (appended)
Result: config.corsOrigins = "https://{domainName},{extras}"
Every consumer calls buildCorsOrigins(config, additionalOrigins?) from
infrastructure/lib/config.ts, which splits, deduplicates and appends the
section extras:
// Container env var (Fargate / AgentCore Runtime)
CORS_ORIGINS: buildCorsOrigins(config, config.appApi.additionalCorsOrigins).join(','),
// S3 bucket CORS rule
cors: [{ allowedOrigins: buildCorsOrigins(config, config.ragIngestion.additionalCorsOrigins) }]
The Python services read CORS_ORIGINS with no hardcoded fallback: an empty
value allows no origins.
Section extras exist on AppApiConfig, InferenceApiConfig,
RagIngestionConfig and FineTuningConfig. The file-upload bucket takes none
(buildCorsOrigins(config)), and neither does the SPA distribution, which
serves the app rather than calling anything cross-origin.
Workflow requirements
CDK_DOMAIN_NAME and CDK_CORS_ORIGINS go in the job-level env: block,
not workflow-level, because they read vars.*, which needs environment: on
the job (at workflow level they resolve to empty strings). Every workflow that
runs synth or deploy needs:
env:
CDK_DOMAIN_NAME: ${{ vars.CDK_DOMAIN_NAME }}
CDK_CORS_ORIGINS: ${{ vars.CDK_CORS_ORIGINS }}
Adding a consumer
First ask whether the browser really makes a cross-origin request to it. A new
app-api route doesn't: it is same-origin through /api. Something the browser
reaches directly — a new bucket with presigned uploads, a new WebSocket — does.
- Import
buildCorsOriginsfrominfrastructure/lib/config.ts(relative to the construct's folder underlib/constructs/<area>/) - Call
buildCorsOrigins(config, config.mySection.additionalCorsOrigins) - If it needs its own extras: add
additionalCorsOrigins?: stringto the section's config interface and load it inloadConfig()fromCDK_MY_SECTION_CORS_ORIGINS - Add
CDK_DOMAIN_NAMEandCDK_CORS_ORIGINSto the workflow job env - Add a test beside the existing
buildCorsOriginscases ininfrastructure/test/config.test.ts
A new backend origin check reads CORS_ORIGINS at request time, as
_allowed_callback_origins() does, so tests can monkeypatch it.
Common mistakes
- Putting
vars.*in workflow-levelenv: - Hardcoding
http://localhost:4200inbuildCorsOriginsor a Python fallback - Using
config.domainNamedirectly instead ofbuildCorsOrigins() - Setting
corsOriginsincdk.context.json(overrides domain derivation) - Debugging a deployed "CORS error" on an app-api call: in a deployed environment it is almost always something else (a CloudFront behavior, a redirect to another origin, or a 5xx without CORS headers), because the call is same-origin
