What am I doing wrong? For some reason, CSP blocks the js file call anyway, even though I specified nonce ID in attr.
<head>
<meta http-equiv="Content-Security-Policy" content="script-src 'nonce-Xiojd98a8jd3s9kFiDi29Uijwdu';">
</head>
Call Function:
jQuery.loadScript = function (url, callback) {
jQuery.ajax({
url: url,
dataType: 'script',
success: callback,
async: true,
scriptAttrs: { nonce: "Xiojd98a8jd3s9kFiDi29Uijwdu" }
});
}
$.loadScript("/js/temp.js");
Error message:
Refused to execute inline script because it violates the following Content Security Policy directive: "script-src 'nonce-Xiojd98a8jd3s9kFiDi29Uijwdu'". Either the 'unsafe-inline' keyword, a hash ('sha256-9pSu4Q2RG6fzg6RdmNxg1z3W+Y9EdC0f90RGYsLO/o4='), or a nonce ('nonce-...') is required to enable inline execution.
Jquery ver:
/*! jQuery v3.4.1 | (c) JS Foundation and other contributors | jquery.org/license */
Edited:
if i dont use $.loadScript function, all js files with installed nonce attr works GOOD.
The problem is in the dynamic call of the js file, but unfortunately I can not get rid of it. And it needs to be loaded dynamically.
Edited(2):
I tried through document.createElement ("script"), also through jQuery.globalEval and added nonce everywhere. The output turns out so that some functions (one of them uses foreach) do not work with an error: Refused to execute inline event handler because it violates the following Content Security Policy directive: "script-src 'nonce-Xiojd98a8jd3s9kFiDi29Uijwdu'". Either the 'unsafe-inline' keyword, a hash ('sha256-...'), or a nonce ('nonce-...') is required to enable inline execution.
Through the console in the "Network" tab, I see the loaded script.
Dont work --->
jQuery.globalEval($.loadScript("/js/temp.js"), {
nonce: "Xiojd98a8jd3s9kFiDi29Uijwdu"
});
Dont work again --->
var scriptTag = document.createElement("script");
scriptTag.type = "text/javascript";
scriptTag.src = "/js/temp.js";
scriptTag.setAttribute("nonce", "Xiojd98a8jd3s9kFiDi29Uijwdu");
(document.getElementsByTagName("head")[0] || document.documentElement).appendChild(scriptTag);
If i delete meta Content-Security-Policy - All codes above work. All scripts work as needed)