cors org code example
Example 1: testing cors
curl -I -X OPTIONS \
-H "Origin: ${MY_URL}" \
-H 'Access-Control-Request-Method: GET' \
"${MY_URL}/SOMETHING" 2>&1 | grep -i 'Access-Control-Allow-Origin'
Example 2: what is CORS
Use CORS to allow cross-origin access.
CORS is a part of HTTP that lets servers specify any other hosts
from which a browser should permit loading of content.
How to block cross-origin access
To prevent cross-origin writes,
check an unguessable token in the request — known as a Cross-Site Request Forgery (CSRF) token.
prevent cross-origin reads of pages that require this token.
To prevent cross-origin reads of a resource,
ensure that it is not embeddable.
prevent embedding because embedding a resource always leaks some information about it.
To prevent cross-origin embeds,
ensure that your resource cannot be interpreted
Browsers may not respect the Content-Type header.
For example, if you point a <script> tag at an HTML document, the browser will try to parse the HTML as JavaScript. When your resource is not an entry point to your site, you can also use a CSRF token to prevent embedding.
Example 3: cors everywhere
This API enables cross-origin requests to anywhere.
Usage:
/ Shows help
/iscorsneeded This is the only resource on this host which is served without CORS headers.
/<url> Create a request to <url>, and includes CORS headers in the response.
If the protocol is omitted, it defaults to http (https if port 443 is specified).
Cookies are disabled and stripped from requests.
Redirects are automatically followed. For debugging purposes, each followed redirect results
in the addition of a X-CORS-Redirect-n header, where n starts at 1. These headers are not
accessible by the XMLHttpRequest API.
After 5 redirects, redirects are not followed any more. The redirect response is sent back
to the browser, which can choose to follow the redirect (handled automatically by the browser).
The requested URL is available in the X-Request-URL response header.
The final URL, after following all redirects, is available in the X-Final-URL response header.
To prevent the use of the proxy for casual browsing, the API requires either the Origin
or the X-Requested-With header to be set. To avoid unnecessary preflight (OPTIONS) requests,
it's recommended to not manually set these headers in your code.
Demo : https://robwu.nl/cors-anywhere.html
Source code : https://github.com/Rob--W/cors-anywhere/
Documentation : https://github.com/Rob--W/cors-anywhere/