CLOUD CONCEPT

Same-Origin Policy

The Same-Origin Policy is a browser security rule that restricts how scripts from one origin can access documents and data belonging to another origin.

Cloud Concepts Hub →

What is Same-Origin Policy

An origin is defined by its URL scheme, hostname, and effective port, so two applications can share a domain name yet still have different origins. The policy limits cross-origin data access in browsers, while mechanisms such as Cross-Origin Resource Sharing (CORS) let servers explicitly permit selected access without replacing authentication or authorization.

The Same-Origin Policy establishes an isolation boundary inside web browsers. For typical HTTP and HTTPS URLs, an origin consists of the scheme, hostname, and effective port; paths do not participate. For example, https://app.example.com/dashboard and https://app.example.com/settings share an origin, but https://api.example.com, http://app.example.com, and https://app.example.com:8443 are different origins. Explicitly specifying the default HTTPS port, 443, does not create a different origin.

The policy primarily restricts cross-origin reading and interaction, not every cross-origin network request. A page can often submit a form to another origin or embed cross-origin images and scripts, subject to other browser controls. However, its JavaScript generally cannot read another origin's documents or fetch responses unless an applicable mechanism permits access. Consequently, a browser can send a request that changes server state even when it refuses to expose the response to the calling script.

CORS is the standard HTTP mechanism for allowing selected cross-origin response access. Suppose a frontend at https://app.example.com fetches a JSON object from an Amazon S3 endpoint. The browser sends an Origin header, and the server must return an appropriate Access-Control-Allow-Origin value for JavaScript to read the response. An S3 CORS rule can allow the frontend's exact origin, required methods, and request headers. Access-Control-Expose-Headers makes selected response headers readable by JavaScript; it does not grant access to objects.

Some cross-origin requests require an OPTIONS preflight before the browser sends the actual request. For example, a request with an Authorization header normally triggers preflight. The server must approve the requested method and headers. S3 evaluates preflight against its configured CORS rules; OPTIONS itself is not an AllowedMethods entry in an S3 CORS rule. Requests that do not require preflight may reach the server even when the browser ultimately blocks response access.

Credentialed browser requests need particular care. If a cross-origin fetch uses credentials mode include, the response must identify the allowed origin explicitly and include Access-Control-Allow-Credentials: true; Access-Control-Allow-Origin: * is not valid for that credentialed response. Browser cookie restrictions, including SameSite and third-party-cookie controls, still apply. Never blindly reflect arbitrary Origin values while enabling credentials, because doing so can expose authenticated responses to hostile websites.

Cloud deployments often introduce separate frontend, API, and object-storage origins. Serving the frontend and routing API paths through one public origin can reduce CORS complexity, although it adds routing and caching responsibilities. When using Amazon CloudFront, configure response headers consistently and forward the Origin and preflight request headers when the backend needs them. If responses vary by requesting origin or preflight parameters, configure the cache key accordingly or avoid caching those responses. Do not assume Vary alone configures CloudFront's cache key.

For troubleshooting, inspect the browser's network panel for the Origin header, any OPTIONS exchange, and the actual response headers and status. A backend authorization failure can appear to JavaScript as a CORS error if the error response lacks suitable CORS headers. Successful curl requests do not prove that browser access will work, because curl does not enforce the Same-Origin Policy.

The policy and CORS are not substitutes for IAM permissions, bucket policies, API authorization, or CSRF defenses. A private S3 object remains private when CORS permits an origin. AWS WAF can filter application attacks, Shield Advanced addresses DDoS protection, and AWS IoT policies authorize supported device actions; none implements the browser's same-origin boundary. Likewise, the Same-Origin Policy does not stop same-origin malicious scripts introduced through cross-site scripting. Use output encoding and a carefully designed Content Security Policy alongside these controls.

Key aspects of Same-Origin Policy

The Same-Origin Policy isolates browser origins, while carefully scoped CORS permissions enable necessary cross-origin access without replacing server-side security.

Origin-based isolation

Origins are distinguished by scheme, hostname, and effective port, not URL path. Separate subdomains are separate origins even when they belong to the same organization.

Browser-enforced access restrictions

The browser restricts cross-origin document interaction and response reading. It does not universally prevent requests from being sent, and non-browser clients do not inherit this enforcement.

Explicit exceptions through CORS

Servers use CORS response headers to permit selected browser origins to read responses. Allow only the origins, methods, and headers required by the application.

Preflight negotiation

Requests using certain methods or headers require an OPTIONS preflight that approves the intended request. Ensure routing, authentication handling, and edge rules do not unintentionally reject valid preflight traffic.

Credential-sensitive configuration

Credentialed cross-origin response access requires an explicit allowed origin and Access-Control-Allow-Credentials: true. Browser cookie policies remain independently applicable, and arbitrary origin reflection is unsafe.

Independent authorization and edge controls

CORS does not grant object permissions, authorize API operations, prevent CSRF, or mitigate DDoS attacks. Combine browser isolation with authorization, application security controls, and correctly configured edge caching.

Cloud services for Same-Origin Policy

A frontend delivered through CloudFront can retrieve objects from S3 and call APIs exposed through API Gateway. If these endpoints have different browser origins, configure CORS on the relevant response paths and maintain independent authorization for objects and API operations. CloudFront can manage response headers centrally, but its forwarding and caching configuration must match any origin-dependent backend behavior. API preflight requests must reach an appropriate responder without being inadvertently blocked by authentication or edge rules. Alternatively, routing frontend assets and API paths through one public origin can avoid many CORS requirements while preserving separate backend services.

Amazon Simple Storage Service image Amazon S3

S3 bucket CORS configurations define permitted browser origins, methods, request headers, and exposed response headers for object requests. CORS configuration is separate from IAM policies, bucket policies, and other object-access controls.

Open Amazon S3 →
Amazon CloudFront image Amazon CloudFront

CloudFront response headers policies can add CORS headers to responses delivered to browsers. When the backend evaluates CORS, configure request-header forwarding and cache keys so responses and preflight results are not incorrectly reused across origins.

Open Amazon CloudFront →
Amazon API Gateway image Amazon API Gateway

API Gateway supports CORS configuration for browser-facing APIs, with different configuration responsibilities for HTTP APIs and REST APIs. Ensure preflight and actual responses receive the necessary headers; REST API proxy integrations generally require the backend to return CORS headers on actual responses.

Open Amazon API Gateway →