Created
February 26, 2020 09:52
-
-
Save ScottHelme/261b1bb8547c4745748bece042c99ee2 to your computer and use it in GitHub Desktop.
Detect SRI failures.
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) { | |
console.log("Got an error!"); | |
console.log("Page: " + error.srcElement.baseURI); | |
console.log("Asset: " + error.srcElement.src); | |
console.log("Integrity: " + error.srcElement.integrity); | |
console.log("Element: " + error.srcElement.outerHTML); | |
console.log("Message: " /*+ ???*/); | |
console.log(error); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The intended purpose of the script is to reliably detect SRI failures on link and script tags.
I have a demo page setup here with an SRI failure for demonstration: https://scotthelme.co.uk/p/0576f1af-7540-49ba-9469-9e2a3fe1f634/