Improve logger accuracy re. reported injected scriptlet

Related issue:
- https://github.com/uBlockOrigin/uBlock-issues/issues/2072
This commit is contained in:
Raymond Hill 2022-03-31 10:50:49 -04:00
parent a81d90f97e
commit efe2e0c78a
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
1 changed files with 42 additions and 27 deletions

View File

@ -117,7 +117,8 @@ const contentscriptCode = (( ) => {
')(', ')(',
'"', 'hostname-slot', '", ', '"', 'hostname-slot', '", ',
'"', 'scriptlets-slot', '"', '"', 'scriptlets-slot', '"',
'); void 0;', ');',
'0;',
]; ];
return { return {
parts: parts, parts: parts,
@ -221,21 +222,6 @@ const patchScriptlet = function(content, args) {
return content; return content;
}; };
const logOne = function(isException, token, details) {
µb.filteringContext
.duplicate()
.fromTabId(details.tabId)
.setRealm('extended')
.setType('dom')
.setURL(details.url)
.setDocOriginFromURL(details.url)
.setFilter({
source: 'extended',
raw: (isException ? '#@#' : '##') + `+js(${token})`
})
.toLogger();
};
scriptletFilteringEngine.reset = function() { scriptletFilteringEngine.reset = function() {
scriptletDB.clear(); scriptletDB.clear();
duplicates.clear(); duplicates.clear();
@ -319,7 +305,7 @@ const $scriptlets = new Set();
const $exceptions = new Set(); const $exceptions = new Set();
const $scriptletToCodeMap = new Map(); const $scriptletToCodeMap = new Map();
scriptletFilteringEngine.retrieve = function(request) { scriptletFilteringEngine.retrieve = function(request, options = {}) {
if ( scriptletDB.size === 0 ) { return; } if ( scriptletDB.size === 0 ) { return; }
const hostname = request.hostname; const hostname = request.hostname;
@ -348,27 +334,37 @@ scriptletFilteringEngine.retrieve = function(request) {
// Wholly disable scriptlet injection? // Wholly disable scriptlet injection?
if ( $exceptions.has('') ) { if ( $exceptions.has('') ) {
if ( logger.enabled ) { if ( Array.isArray(options.logEntries) ) {
logOne(true, '', request); options.logEntries.push({
isException: true,
token: '',
tabId: request.tabId,
url: request.url,
});
} }
return; return;
} }
$scriptletToCodeMap.clear(); $scriptletToCodeMap.clear();
for ( const rawToken of $scriptlets ) { for ( const token of $scriptlets ) {
lookupScriptlet(rawToken, redirectEngine, $scriptletToCodeMap); lookupScriptlet(token, redirectEngine, $scriptletToCodeMap);
} }
if ( $scriptletToCodeMap.size === 0 ) { return; } if ( $scriptletToCodeMap.size === 0 ) { return; }
// Return an array of scriptlets, and log results if needed. // Return an array of scriptlets, and log results if needed.
const out = []; const out = [];
for ( const [ rawToken, code ] of $scriptletToCodeMap ) { for ( const [ token, code ] of $scriptletToCodeMap ) {
const isException = $exceptions.has(rawToken); const isException = $exceptions.has(token);
if ( isException === false ) { if ( isException === false ) {
out.push(code); out.push(code);
} }
if ( logger.enabled ) { if ( Array.isArray(options.logEntries) ) {
logOne(isException, rawToken, request); options.logEntries.push({
isException,
token,
tabId: request.tabId,
url: request.url,
});
} }
} }
@ -411,18 +407,37 @@ scriptletFilteringEngine.injectNow = function(details) {
}; };
request.domain = domainFromHostname(request.hostname); request.domain = domainFromHostname(request.hostname);
request.entity = entityFromDomain(request.domain); request.entity = entityFromDomain(request.domain);
const scriptlets = this.retrieve(request); const logEntries = logger.enabled ? [] : undefined;
const scriptlets = this.retrieve(request, { logEntries });
if ( scriptlets === undefined ) { return; } if ( scriptlets === undefined ) { return; }
let code = contentscriptCode.assemble(request.hostname, scriptlets); let code = contentscriptCode.assemble(request.hostname, scriptlets);
if ( µb.hiddenSettings.debugScriptletInjector ) { if ( µb.hiddenSettings.debugScriptletInjector ) {
code = 'debugger;\n' + code; code = 'debugger;\n' + code;
} }
vAPI.tabs.executeScript(details.tabId, { const promise = vAPI.tabs.executeScript(details.tabId, {
code, code,
frameId: details.frameId, frameId: details.frameId,
matchAboutBlank: true, matchAboutBlank: true,
runAt: 'document_start', runAt: 'document_start',
}); });
if ( logEntries === undefined ) { return; }
promise.then(results => {
if ( Array.isArray(results) === false || results[0] !== 0 ) { return; }
for ( const entry of logEntries ) {
µb.filteringContext
.duplicate()
.fromTabId(entry.tabId)
.setRealm('extended')
.setType('dom')
.setURL(entry.url)
.setDocOriginFromURL(entry.url)
.setFilter({
source: 'extended',
raw: (entry.isException ? '#@#' : '##') + `+js(${entry.token})`
})
.toLogger();
}
});
}; };
scriptletFilteringEngine.toSelfie = function() { scriptletFilteringEngine.toSelfie = function() {