Same-Origin Policy restricts how a document or script loaded from one origin can interact with a resource from another origin.
The same-origin policy controls interactions between two different origin.
Origin = scheme + host + port
- Cross-origin writes are typically allowed. Some HTTP requests require preflight.
- Links
- Redirects
- Form submissions
- Cross-origin embedding is typically allowed.
- JavaScript with
<script src="…"></script>
. Error details for syntax errors are only available for same-origin scripts. - CSS applied with
<link rel="stylesheet" href="…">
. Due to the relaxed syntax rules of CSS, cross-origin CSS requires a correct Content-Type header. - Images displayed by
<img>
. - Media played by
<video>
and<audio>
. - External resources embedded with
<object>
and<embed>
. - Fonts applied with
@font-face
. Some browsers allow cross-origin fonts, others require same-origin. - Anything embedded by
<iframe>
. Sites can use the X-Frame-Options header to prevent cross-origin framing.
- JavaScript with
- Cross-origin reads are typically disallowed.
- But read access is often leaked by embedding.
- Read the dimensions of an embedded image.
- The actions of an embedded script.
- The availability of an embedded resource.
- But read access is often leaked by embedding.
Browsers still send request to the resource server for you.
However, browsers block the response and send error to Javascript script.
As the error you see on the developer console, you can set fetch mode to no-cors
.
It mitigates the CORS error, but it does NOT solve the root cause.
In fact, it tells the browser to block my frontend JavaScript code from looking at contents of the response body and headers under all circumstances.
no-cors
mode only allows a limited set of headers in the request:
1. Accept
2. Accept-Language
3. Content-Language
4. Content-Type
with a value of application/x-www-form-urlencoded
, multipart/form-data
, or text/plain
.
Other request headers will be stripped.
No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
Same-origin policy ONLY restricts browsers!
Thus, by setting up a proxy server, you can bypass same-origin policy.
a. Web -> Proxy Server
b. Proxy Server -> Resource Server
c. Proxy Server <- Resource Server
d. Web <- Proxy Server
You can add any response header you want on proxy server.
Use CORS to allow cross-origin access.