Skip to content

Instantly share code, notes, and snippets.

@aldaris
Created March 4, 2018 21:59
Show Gist options
  • Save aldaris/736be0f4a8ca4a537c1bd957cb4c3c3e to your computer and use it in GitHub Desktop.
Save aldaris/736be0f4a8ca4a537c1bd957cb4c3c3e to your computer and use it in GitHub Desktop.
SRI report
var observer = window.MutationObserver || window.WebKitMutationObserver;
if (observer) {
new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
mutation.addedNodes.forEach(processNode);
});
}).observe(document, { childList: true, subtree: true });
}
var processNode = function(node) {
var tagName = node.tagName ? node.tagName.toLowerCase() : '';
if (tagName === 'script' || tagName === 'link') {
if (!node.onerror) {
node.onerror = function(error) {
var json = {
"csp-report": {
"document-uri": window.top.location.href,
"referrer": "",
"blocked-uri": node.hasAttribute('src') ? node.getAttribute('src') : node.getAttribute('href'),
"violated-directive": "invalid-integrity",
"original-policy": "require-sri-for script"
}
};
var xhr = new XMLHttpRequest();
xhr.open('POST', 'https://mydomain.report-uri.com/r/d/csp/enforce', true);
xhr.setRequestHeader('content-type', 'application/csp-report');
xhr.send(JSON.stringify(json));
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment