Skip to content

Add CORS support so browser-based AWS SDK clients can use the emulator #346

Closed

Description

@scttfrdmn

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, OPTIONS
  • Access-Control-Allow-Headers: reflect Access-Control-Request-Headers — the AWS SDK sends many x-amz-* / amz-sdk-* / authorization / content-type headers, so a permissive reflect is simplest
  • Access-Control-Expose-Headers: at least x-amzn-RequestId, x-amz-request-id, x-amzn-ErrorType so the SDK can read request IDs / error types
  • Respond 204 to OPTIONS before 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-Headers or 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

  1. added a commit that references this issue on Jul 20, 2026
  2. added a commit that references this issue on Jul 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions