I added a fetch call in my entry.js
file:
fetch('https://ipinfo.io/json')
.then(response => response.json())
.then(data => console.log(data));
My webpack build was fine until I added that, then i started getting errors regarding Content Security Policy.
Previously, I didn't have any content security policy defined.
I've tried several approaches to resolve errors (see end of post), the latest was adding this to head section of index.html file:
<meta http-equiv="Content-Security-Policy" content="script-src 'self'; connect-src https://ipinfo.io/json">
Now I am getting these errors in Chrome developer tools console, which seem to contradict themselves:
Error #1 - relating to jquery ajax get request to my own api
bundle.js:25 Refused to connect to 'http://localhost:3000/api/v1/resource?parameter=false&_=1598715905517' because it violates the following Content Security Policy directive: "connect-src https://ipinfo.io/json".
Error #2 - relating to fetch call to external website
bundle.js:1 Refused to connect to 'https://ipinfo.io/json' because it violates the following Content Security Policy directive: "default-src 'self'". Note that 'connect-src' was not explicitly set, so 'default-src' is used as a fallback.
Note: This second error says that connect-src
is not explicitly set, however the first error occurs because it is explicitly set.
Error #3 - relating to fetch call to external website
bundle.js:1 Refused to connect to 'https://ipinfo.io/json' because it violates the document's Content Security Policy.
Error #4 - relating to fetch call to external website
bundle.js:1
Uncaught (in promise) TypeError: Failed to fetch
at HTMLDocument.<anonymous> (bundle.js:1)
at u (bundle.js:25)
at l (bundle.js:25)
Other approaches
For reference, here is one of the other approaches I tried using a nonce
:
I added this to entry.js
:
__webpack_nonce__ = 'something-tricky';
And this to the <head>
section of the index file:
<meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'nonce-something-tricky';
The result was that it wouldn't load bundle.js
:
Refused to load the script 'http://localhost:3000/js/bundle.js' because it violates the following Content Security Policy directive: "script-src 'nonce-something-tricky'". Note that 'script-src-elem' was not explicitly set, so 'script-src' is used as a fallback.
This approach was based on:
https://webpack.js.org/guides/csp
https://stackoverflow.com/a/42924000
Further context
As i started trying all sorts of CSP combinations in the <head>
section, I noticed it started breaking lots of things like:
- script tag links in
index.html
to google fonts etc - css styles generated and included in bundle.js
So i'm aware of the need to get CSP completely right, or it won't work at all.
Edit
My last attempt was this:
<meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self' https://ipinfo.io; style-src 'self' fonts.googleapis.com 'unsafe-inline'; font-src 'self' fonts.gstatic.com">
And I am getting this error:
bundle.js:25 Refused to load the script 'https://ipinfo.io/json?callback=jQuery35106635073412967416_1598793439782&_=1598793439783' because it violates the following Content Security Policy directive: "script-src 'self'". Note that 'script-src-elem' was not explicitly set, so 'script-src' is used as a fallback.
When this code is in the entry.js
file:
$.get("https://ipinfo.io/json", function(response) {
console.log(response);
}, "jsonp");
That last error is weird, because it seems to be ignoring the domain that I specifically 'whitelisted' in the srcipt-src
directive.