Created
March 4, 2018 21:59
-
-
Save aldaris/736be0f4a8ca4a537c1bd957cb4c3c3e to your computer and use it in GitHub Desktop.
SRI report
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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