Code review re. scriptlets lookup

Possibly fixes a race condition at browser launch causing empty
scriptlets to be injected (and cached).
This commit is contained in:
Raymond Hill 2025-04-11 18:20:51 -04:00
parent 90e3c352ec
commit f25a437fd1
No known key found for this signature in database
GPG key ID: 25E1490B761470C2
4 changed files with 19 additions and 15 deletions

View file

@ -460,6 +460,7 @@ class RedirectEngine {
for ( const [ token, entry ] of this.resources ) {
this.resources.set(token, RedirectEntry.fromDetails(entry));
}
this.modifyTime = Date.now();
return true;
}

View file

@ -25,11 +25,6 @@ import { redirectEngine as reng } from './redirect-engine.js';
/******************************************************************************/
const $mainWorldMap = new Map();
const $isolatedWorldMap = new Map();
/******************************************************************************/
// For debugging convenience: all the top function calls will appear
// at the bottom of a generated content script
const codeSorter = (a, b) => {
@ -243,18 +238,27 @@ export class ScriptletFilteringEngine {
}
}
const mainWorldMap = new Map();
const isolatedWorldMap = new Map();
for ( const token of scriptlets ) {
lookupScriptlet(token, $mainWorldMap, $isolatedWorldMap, options.debug);
lookupScriptlet(token, mainWorldMap, isolatedWorldMap, options.debug);
}
if ( scriptlets.size !== 0 ) {
if ( mainWorldMap.size === 0 ) {
if ( isolatedWorldMap.size === 0 ) { return; }
}
}
const mainWorldCode = [];
for ( const js of $mainWorldMap.values() ) {
for ( const js of mainWorldMap.values() ) {
mainWorldCode.push(js);
}
mainWorldCode.sort(codeSorter);
const isolatedWorldCode = [];
for ( const js of $isolatedWorldMap.values() ) {
for ( const js of isolatedWorldMap.values() ) {
isolatedWorldCode.push(js);
}
isolatedWorldCode.sort(codeSorter);
@ -267,8 +271,6 @@ export class ScriptletFilteringEngine {
...Array.from(exceptions).map(a => decompile(a, true)),
],
};
$mainWorldMap.clear();
$isolatedWorldMap.clear();
const scriptletGlobals = options.scriptletGlobals || {};

View file

@ -328,6 +328,7 @@ export class ScriptletFilteringEngineEx extends ScriptletFilteringEngine {
if ( typeof details.frameId !== 'number' ) { return; }
const hostname = hostnameFromURI(details.url);
if ( hostname === '' ) { return; }
const domain = domainFromHostname(hostname);
const scriptletDetails = this.retrieve({

View file

@ -92,9 +92,9 @@ export class StaticExtFilteringHostnameDB {
if ( target.includes('/') ) {
return this.#storeMatcher(target, iStr);
}
const iList = this.#hostnameToStringListMap.get(target);
const iList = this.#hostnameToStringListMap.get(target) ?? 0;
this.#hostnameToStringListMap.set(target, this.#linkedLists.length);
this.#linkedLists.push(iStr, iList !== undefined ? iList : 0);
this.#linkedLists.push(iStr, iList);
}
#storeMatcher(target, iStr) {
@ -105,11 +105,11 @@ export class StaticExtFilteringHostnameDB {
this.#matcherSlots.push({ isRegex, hn, pn, iList: 0 });
this.#matcherMap.set(target, iMatcher);
if ( isRegex === false ) {
const iMatcherList = this.#hostnameToMatcherListMap.get(hn) || 0;
const iMatcherList = this.#hostnameToMatcherListMap.get(hn) ?? 0;
this.#hostnameToMatcherListMap.set(hn, this.#linkedLists.length);
this.#linkedLists.push(iMatcher, iMatcherList);
} else {
const iMatcherList = this.#hostnameToMatcherListMap.get('') || 0;
const iMatcherList = this.#hostnameToMatcherListMap.get('') ?? 0;
this.#hostnameToMatcherListMap.set('', this.#linkedLists.length);
this.#linkedLists.push(iMatcher, iMatcherList);
}
@ -176,7 +176,7 @@ export class StaticExtFilteringHostnameDB {
}
#retrieveSpecificsByRegex(hn, out, hostname, pathname) {
let iMatchList = this.#hostnameToMatcherListMap.get(hn) || 0;
let iMatchList = this.#hostnameToMatcherListMap.get(hn) ?? 0;
while ( iMatchList !== 0 ) {
const iMatchSlot = this.#linkedLists[iMatchList+0];
const matcher = this.#matcherSlots[iMatchSlot];