Repository navigation
Description
Problem
Substrate's HTTP server (go-chi, emulator/server.go buildRouter) emits no CORS headers and has no OPTIONS preflight handler. That makes it unusable from any browser-based AWS SDK client: a page served from another origin (e.g. a Vite dev server on http://localhost:5173) issues a CORS preflight OPTIONS before every non-simple request, gets no Access-Control-Allow-* headers back, and the browser blocks the actual EC2/STS call. Every SDK call fails before it reaches the emulator.
This came up building spawn-ts — a browser-native port of spore.host spawn (launches + self-terminates EC2 from a web page). Substrate is the ideal deterministic, offline, cost-visible backend to test it against (RunInstances/DescribeInstances/CreateTags/Terminate/Stop/StartInstances are all already supported, and POST /v1/control/time lets a 4h TTL fire instantly). But without CORS a browser can't reach it at all.
Proposed fix
Add CORS middleware to buildRouter() (e.g. github.com/go-chi/cors, which substrate can already pull as a chi sibling), reflecting the request origin (or a configurable allowlist) and handling OPTIONS preflight:
Access-Control-Allow-Origin: reflected origin or configured allowlist (default*is reasonable for a local dev emulator)Access-Control-Allow-Methods:GET, POST, PUT, DELETE, HEAD, OPTIONSAccess-Control-Allow-Headers: reflectAccess-Control-Request-Headers— the AWS SDK sends manyx-amz-*/amz-sdk-*/authorization/content-typeheaders, so a permissive reflect is simplestAccess-Control-Expose-Headers: at leastx-amzn-RequestId,x-amz-request-id,x-amzn-ErrorTypeso the SDK can read request IDs / error types- Respond
204toOPTIONSbefore hitting resource handlers
Suggest making it opt-in/configurable via substrate.yaml (server.cors.enabled, server.cors.allowed_origins) so it's off-by-default for non-browser use but easy to turn on. Happy to send a PR.
Notes
- Browser SDK clients also need
Access-Control-Expose-Headersor they can't parse the AWS error type on failures — worth including so seeded failure paths remain testable from the browser. - Environment: substrate
main, go-chi v5,--address :4566.
Activity
feat(server): opt-in CORS for browser SDK clients (#346); bump Go to …
chore(release): v0.72.0 (#349)