Add ability to match against script content of data: URI

Related commit:
- ebc42ae21e
This commit is contained in:
Raymond Hill 2021-07-18 08:50:57 -04:00
parent 9c8691709d
commit 4fe8126c66
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
1 changed files with 29 additions and 10 deletions

View File

@ -77,16 +77,35 @@
} }
const magic = String.fromCharCode(Date.now() % 26 + 97) + const magic = String.fromCharCode(Date.now() % 26 + 97) +
Math.floor(Math.random() * 982451653 + 982451653).toString(36); Math.floor(Math.random() * 982451653 + 982451653).toString(36);
const validate = function() { const scriptTexts = new WeakMap();
const e = document.currentScript; const getScriptText = elem => {
if ( let text = elem.textContent;
e instanceof HTMLScriptElement && if ( text.trim() !== '' ) { return text; }
reContext.test(e.src) && if ( scriptTexts.has(elem) ) { return scriptTexts.get(elem); }
e !== thisScript && const [ , mime, content ] =
reNeedle.test(e.textContent) /^data:([^,]*),(.+)$/.exec(elem.src.trim()) ||
) { [ '', '', '' ];
throw new ReferenceError(magic); try {
switch ( true ) {
case mime.endsWith(';base64'):
text = self.atob(content);
break;
default:
text = self.decodeURIComponent(content);
break;
}
} catch(ex) {
} }
scriptTexts.set(elem, text);
return text;
};
const validate = ( ) => {
const e = document.currentScript;
if ( e instanceof HTMLScriptElement === false ) { return; }
if ( reContext.test(e.src) === false ) { return; }
if ( e === thisScript ) { return; }
if ( reNeedle.test(getScriptText(e)) === false ) { return; }
throw new ReferenceError(magic);
}; };
Object.defineProperty(owner, prop, { Object.defineProperty(owner, prop, {
get: function() { get: function() {
@ -106,7 +125,7 @@
}); });
const oe = window.onerror; const oe = window.onerror;
window.onerror = function(msg) { window.onerror = function(msg) {
if ( typeof msg === 'string' && msg.indexOf(magic) !== -1 ) { if ( typeof msg === 'string' && msg.includes(magic) ) {
return true; return true;
} }
if ( oe instanceof Function ) { if ( oe instanceof Function ) {