Harden xml-prune scriptlet

This commit is contained in:
Raymond Hill 2022-09-25 06:49:41 -04:00
parent 3227d7f591
commit 596145ceb9
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
1 changed files with 14 additions and 6 deletions

View File

@ -1692,7 +1692,6 @@
reUrl = new RegExp(urlPattern.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')); reUrl = new RegExp(urlPattern.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'));
} }
const pruner = text => { const pruner = text => {
if ( selector === '' ) { return text; }
if ( (/^\s*</.test(text) && />\s*$/.test(text)) === false ) { if ( (/^\s*</.test(text) && />\s*$/.test(text)) === false ) {
return text; return text;
} }
@ -1714,16 +1713,25 @@
} }
return text; return text;
}; };
const urlFromArg = arg => {
if ( typeof arg === 'string' ) { return arg; }
if ( arg instanceof Request ) { return arg.url; }
return String(arg);
};
const realFetch = self.fetch; const realFetch = self.fetch;
self.fetch = new Proxy(self.fetch, { self.fetch = new Proxy(self.fetch, {
apply: function(target, thisArg, args) { apply: function(target, thisArg, args) {
if ( reUrl.test(String(args[0])) === false ) { if ( selector === '' || reUrl.test(urlFromArg(args[0])) === false ) {
return Reflect.apply(target, thisArg, args); return Reflect.apply(target, thisArg, args);
} }
return realFetch(...args).then(response => return realFetch(...args).then(realResponse =>
response.text() realResponse.text().then(text =>
).then(text => new Response(pruner(text), {
new Response(pruner(text)) status: realResponse.status,
statusText: realResponse.statusText,
headers: realResponse.headers,
})
)
); );
} }
}); });