d3js blocked by Content-Security-Policy
See original GitHub issueWhen the server sends a restrictive Content-Security-Policy header,
Content-Security-Policy: default-src 'self'; script-src 'self'; img-src 'self'
the following error comes up in Firefox:
Content Security Policy: The page's settings blocked the loading of a resource: An attempt to call JavaScript from a string (by calling a function like eval) has been blocked
and a similar to that of Chrome:
Refused to evaluate a string as JavaScript because 'unsafe-eval' is not an allowed source of script in the following Content Security Policy directive: "script-src 'self'".
The culprit is the following code, where a Function object is being created:
dsv.parse = function(text, f) {
var o;
return dsv.parseRows(text, function(row, i) {
if (o) return o(row, i - 1);
var a = new Function("d", "return {" + row.map(function(name, i) {
return JSON.stringify(name) + ": d[" + i + "]";
}).join(",") + "}");
o = f ? function(row, i) {
return f(a(row), i);
} : a;
});
};
The following replacement looks sensible to me, am I missing something?
dsv.parse = function(text, f) {
var o;
return dsv.parseRows(text, function(row, i) {
if (o) return o(row, i - 1);
var a = function(d) {
var r = {};
row.forEach(function(name, i) {
r[name] = d[i];
});
return r;
};
o = f ? function(row, i) {
return f(a(row), i);
} : a;
});
};
Best regards, uu1101.
– edit: fixed code
Issue Analytics
- State:
- Created 9 years ago
- Comments:13 (8 by maintainers)
Top Results From Across the Web
How to fix 'because it violates the following content security ...
'because it violates the following content security policy directive' is a browser error message that occurs when Content Security Policy is blocking a ......
Read more >Content Security Policy (CSP) - AppSec Monkey
Content Security Policy is an outstanding browser security feature that can prevent XSS (Cross-Site Scripting) attacks. It also obsoletes the ...
Read more >Chrome Extension "Refused to load the script because it ...
Google Chrome has CSP (Content Security Policy), which means chrome extensions don't allow the external script. If you are using the vue cdn ......
Read more >blocked:csp Understanding why CSP blocks resources
You may be seeing blocked:csp in Chrome developer tools when the browser ... CSP stands for Content Security Policy, and it is a...
Read more >A novel policy-based XSS defense mechanism for browsers
Index Terms—Cross-site scripting attacks, content security policy, origin confinement, ... can block more XSS attacks, whereas it may introduce.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Related PR https://github.com/d3/d3-dsv/pull/87
Unfortunately this is still an issue for most companies with a CSP policy, I got this one today: