In the response HTML of a website, with a domain like http://www.example.com
, there are many javascript files referenced. One of them references a javascript file on a different domain, and this script tag has the crossorigin="anonymous"
attribute set:
<script crossorigin="anonymous" src="//cdn.example.net/script.js"></script>
I've attempted to redirect the request to another url using a Google Chrome extension:
chrome.webRequest.onBeforeRequest.addListener(function(info) {
return {
redirectUrl: "http://example.org/custom.js"
};
}, {
urls: [
"*://cdn.example.net/script.js"
],
types: ["script"]
}, ["blocking"]);
However, when I try and load the site I get an error in the javascript console:
Redirect at origin
http://cdn.example.net
has been blocked from loading by Cross-Origin Resource Sharing policy: Received an invalid response.
Originhttp://www.example.com
is therefore not allowed access.
If I intercept the response HTML manually (outside of Chrome) and remove the attribute crossorigin="anonymous"
it works as expected.
I have the file at http://example.org/custom.js
set to send:
Access-Control-Allow-Origin: *
I have also tried removing / modifying the response headers from www.example.com
but this does not seem to make a difference. The response headers ( for reference ) from the main frame are:
HTTP/1.1 200 OK
Content-Type: text/html; charset=utf-8
Transfer-Encoding: chunked
Vary: Accept-Encoding
Status: 200 OK
X-XSS-Protection: 1; mode=block
X-Content-Type-Options: nosniff
X-UA-Compatible: chrome=1
Cache-Control: no-cache, no-store
X-Frame-Options: SAMEORIGIN
P3P: CP="NOI DSP COR NID ADMa OPTa OUR NOR"
Content-Encoding: gzip
There are other javascript files that load after this one, and depend on it, and I'd like to leave them as they are.
Again, the main issue really seems to be the crossorigin="anonymous"
tag ( which from what I can tell supposedly has the primary purpose of choosing whether error information will be exposed )