Report inline script tags in logger if present in document

Related issue:
- https://github.com/uBlockOrigin/uBlock-issues/issues/343
This commit is contained in:
Raymond Hill 2018-12-21 14:16:17 -05:00
parent 30093dc990
commit e9d76b3575
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
4 changed files with 172 additions and 174 deletions

View File

@ -497,8 +497,8 @@ var onMessage = function(request, sender, callback) {
} }
// Sync // Sync
var µb = µBlock, const µb = µBlock;
response, let response,
tabId, frameId, tabId, frameId,
pageStore = null; pageStore = null;
@ -527,7 +527,7 @@ var onMessage = function(request, sender, callback) {
case 'shouldRenderNoscriptTags': case 'shouldRenderNoscriptTags':
if ( pageStore === null ) { break; } if ( pageStore === null ) { break; }
const fctxt = µBlock.filteringContext.fromTabId(tabId); const fctxt = µb.filteringContext.fromTabId(tabId);
if ( pageStore.filterScripting(fctxt, undefined) ) { if ( pageStore.filterScripting(fctxt, undefined) ) {
vAPI.tabs.injectScript( vAPI.tabs.injectScript(
tabId, tabId,
@ -567,9 +567,12 @@ var onMessage = function(request, sender, callback) {
if ( µb.canInjectScriptletsNow === false ) { if ( µb.canInjectScriptletsNow === false ) {
response.scriptlets = µb.scriptletFilteringEngine.retrieve(request); response.scriptlets = µb.scriptletFilteringEngine.retrieve(request);
} }
if ( µb.logger.enabled ) {
if ( response.noCosmeticFiltering !== true ) { if ( response.noCosmeticFiltering !== true ) {
µb.logCosmeticFilters(tabId, frameId); µb.logCosmeticFilters(tabId, frameId);
} }
µb.logInlineScript(tabId, frameId);
}
break; break;
case 'retrieveGenericCosmeticSelectors': case 'retrieveGenericCosmeticSelectors':
@ -1317,15 +1320,15 @@ vAPI.messaging.listen('documentBlocked', onMessage);
/******************************************************************************/ /******************************************************************************/
let µb = µBlock; const µb = µBlock;
let broadcastTimers = new Map(); const broadcastTimers = new Map();
/******************************************************************************/ /******************************************************************************/
var domSurveyFinalReport = function(tabId) { const domSurveyFinalReport = function(tabId) {
broadcastTimers.delete(tabId + '-domSurveyReport'); broadcastTimers.delete(tabId + '-domSurveyReport');
let pageStore = µb.pageStoreFromTabId(tabId); const pageStore = µb.pageStoreFromTabId(tabId);
if ( pageStore === null ) { return; } if ( pageStore === null ) { return; }
vAPI.messaging.broadcast({ vAPI.messaging.broadcast({
@ -1358,8 +1361,8 @@ const logCosmeticFilters = function(tabId, details) {
/******************************************************************************/ /******************************************************************************/
var onMessage = function(request, sender, callback) { var onMessage = function(request, sender, callback) {
let tabId = sender && sender.tab ? sender.tab.id : 0; const tabId = sender && sender.tab ? sender.tab.id : 0;
let pageStore = µb.pageStoreFromTabId(tabId); const pageStore = µb.pageStoreFromTabId(tabId);
// Async // Async
switch ( request.what ) { switch ( request.what ) {
@ -1389,6 +1392,19 @@ var onMessage = function(request, sender, callback) {
} }
break; break;
case 'inlinescriptFound':
if ( µb.logger.enabled && pageStore !== null ) {
const fctxt = µb.filteringContext.duplicate();
fctxt.fromTabId(tabId)
.setType('inline-script')
.setURL(request.docURL)
.setDocOriginFromURL(request.docURL);
if ( pageStore.filterRequest(fctxt) === 0 ) {
fctxt.setRealm('net').toLogger();
}
}
break;
case 'logCosmeticFilteringData': case 'logCosmeticFilteringData':
logCosmeticFilters(tabId, request); logCosmeticFilters(tabId, request);
break; break;

View File

@ -49,48 +49,29 @@ const netFilteringCacheJunkyardMax = 10;
/******************************************************************************/ /******************************************************************************/
const NetFilteringResultCache = function() { const NetFilteringResultCache = function() {
this.boundPruneAsyncCallback = this.pruneAsyncCallback.bind(this);
this.init(); this.init();
}; };
/******************************************************************************/ NetFilteringResultCache.prototype = {
shelfLife: 15000,
NetFilteringResultCache.prototype.shelfLife = 15 * 1000; init: function() {
/******************************************************************************/
NetFilteringResultCache.factory = function() {
let entry = netFilteringCacheJunkyard.pop();
if ( entry === undefined ) {
entry = new NetFilteringResultCache();
} else {
entry.init();
}
return entry;
};
/******************************************************************************/
NetFilteringResultCache.prototype.init = function() {
this.blocked = new Map(); this.blocked = new Map();
this.results = new Map(); this.results = new Map();
this.hash = 0; this.hash = 0;
this.timer = null; this.timer = undefined;
}; return this;
},
/******************************************************************************/ dispose: function() {
NetFilteringResultCache.prototype.dispose = function() {
this.empty(); this.empty();
if ( netFilteringCacheJunkyard.length < netFilteringCacheJunkyardMax ) { if ( netFilteringCacheJunkyard.length < netFilteringCacheJunkyardMax ) {
netFilteringCacheJunkyard.push(this); netFilteringCacheJunkyard.push(this);
} }
return null; return null;
}; },
/******************************************************************************/ rememberResult: function(fctxt, result) {
NetFilteringResultCache.prototype.rememberResult = function(fctxt, result) {
if ( fctxt.tabId <= 0 ) { return; } if ( fctxt.tabId <= 0 ) { return; }
if ( this.results.size === 0 ) { if ( this.results.size === 0 ) {
this.pruneAsync(); this.pruneAsync();
@ -105,11 +86,9 @@ NetFilteringResultCache.prototype.rememberResult = function(fctxt, result) {
const now = Date.now(); const now = Date.now();
this.blocked.set(key, now); this.blocked.set(key, now);
this.hash = now; this.hash = now;
}; },
/******************************************************************************/ rememberBlock: function(fctxt) {
NetFilteringResultCache.prototype.rememberBlock = function(fctxt) {
if ( fctxt.tabId <= 0 ) { return; } if ( fctxt.tabId <= 0 ) { return; }
if ( this.blocked.size === 0 ) { if ( this.blocked.size === 0 ) {
this.pruneAsync(); this.pruneAsync();
@ -120,39 +99,27 @@ NetFilteringResultCache.prototype.rememberBlock = function(fctxt) {
now now
); );
this.hash = now; this.hash = now;
}; },
/******************************************************************************/ empty: function() {
NetFilteringResultCache.prototype.empty = function() {
this.blocked.clear(); this.blocked.clear();
this.results.clear(); this.results.clear();
this.hash = 0; this.hash = 0;
if ( this.timer !== null ) { if ( this.timer !== undefined ) {
clearTimeout(this.timer); clearTimeout(this.timer);
this.timer = null; this.timer = undefined;
} }
}; },
/******************************************************************************/ prune: function() {
const obsolete = Date.now() - this.shelfLife;
NetFilteringResultCache.prototype.pruneAsync = function() { for ( const entry of this.blocked ) {
if ( this.timer === null ) {
this.timer = vAPI.setTimeout(this.boundPruneAsyncCallback, this.shelfLife * 2);
}
};
NetFilteringResultCache.prototype.pruneAsyncCallback = function() {
this.timer = null;
var obsolete = Date.now() - this.shelfLife,
entry;
for ( entry of this.blocked ) {
if ( entry[1] <= obsolete ) { if ( entry[1] <= obsolete ) {
this.results.delete(entry[0]); this.results.delete(entry[0]);
this.blocked.delete(entry[0]); this.blocked.delete(entry[0]);
} }
} }
for ( entry of this.results ) { for ( const entry of this.results ) {
if ( entry[1].tstamp <= obsolete ) { if ( entry[1].tstamp <= obsolete ) {
this.results.delete(entry[0]); this.results.delete(entry[0]);
} }
@ -160,21 +127,28 @@ NetFilteringResultCache.prototype.pruneAsyncCallback = function() {
if ( this.blocked.size !== 0 || this.results.size !== 0 ) { if ( this.blocked.size !== 0 || this.results.size !== 0 ) {
this.pruneAsync(); this.pruneAsync();
} }
}; },
/******************************************************************************/ pruneAsync: function() {
if ( this.timer !== undefined ) { return; }
this.timer = vAPI.setTimeout(
( ) => {
this.timer = undefined;
this.prune();
},
this.shelfLife
);
},
NetFilteringResultCache.prototype.lookupResult = function(fctxt) { lookupResult: function(fctxt) {
return this.results.get( return this.results.get(
fctxt.getDocHostname() + ' ' + fctxt.getDocHostname() + ' ' +
fctxt.type + ' ' + fctxt.type + ' ' +
fctxt.url fctxt.url
); );
}; },
/******************************************************************************/ lookupAllBlocked: function(hostname) {
NetFilteringResultCache.prototype.lookupAllBlocked = function(hostname) {
const result = []; const result = [];
for ( const entry of this.blocked ) { for ( const entry of this.blocked ) {
const pos = entry[0].indexOf(' '); const pos = entry[0].indexOf(' ');
@ -183,6 +157,14 @@ NetFilteringResultCache.prototype.lookupAllBlocked = function(hostname) {
} }
} }
return result; return result;
},
};
NetFilteringResultCache.factory = function() {
const entry = netFilteringCacheJunkyard.pop();
return entry !== undefined
? entry.init()
: new NetFilteringResultCache();
}; };
/******************************************************************************/ /******************************************************************************/
@ -203,7 +185,23 @@ const FrameStore = function(frameURL) {
this.init(frameURL); this.init(frameURL);
}; };
/******************************************************************************/ FrameStore.prototype = {
init: function(frameURL) {
const µburi = µb.URI;
this.pageHostname = µburi.hostnameFromURI(frameURL);
this.pageDomain =
µburi.domainFromHostname(this.pageHostname) || this.pageHostname;
return this;
},
dispose: function() {
this.pageHostname = this.pageDomain = '';
if ( frameStoreJunkyard.length < frameStoreJunkyardMax ) {
frameStoreJunkyard.push(this);
}
return null;
},
};
FrameStore.factory = function(frameURL) { FrameStore.factory = function(frameURL) {
const entry = frameStoreJunkyard.pop(); const entry = frameStoreJunkyard.pop();
@ -213,25 +211,6 @@ FrameStore.factory = function(frameURL) {
return entry.init(frameURL); return entry.init(frameURL);
}; };
/******************************************************************************/
FrameStore.prototype.init = function(frameURL) {
const µburi = µb.URI;
this.pageHostname = µburi.hostnameFromURI(frameURL);
this.pageDomain = µburi.domainFromHostname(this.pageHostname) || this.pageHostname;
return this;
};
/******************************************************************************/
FrameStore.prototype.dispose = function() {
this.pageHostname = this.pageDomain = '';
if ( frameStoreJunkyard.length < frameStoreJunkyardMax ) {
frameStoreJunkyard.push(this);
}
return null;
};
/******************************************************************************/ /******************************************************************************/
/******************************************************************************/ /******************************************************************************/

View File

@ -425,10 +425,6 @@ const onHeadersReceived = function(details) {
const isRootDoc = requestType === 'main_frame'; const isRootDoc = requestType === 'main_frame';
const isDoc = isRootDoc || requestType === 'sub_frame'; const isDoc = isRootDoc || requestType === 'sub_frame';
if ( isRootDoc ) {
µb.tabContextManager.push(details.tabId, details.url);
}
let pageStore = µb.pageStoreFromTabId(fctxt.tabId); let pageStore = µb.pageStoreFromTabId(fctxt.tabId);
if ( pageStore === null ) { if ( pageStore === null ) {
if ( isRootDoc === false ) { return; } if ( isRootDoc === false ) { return; }
@ -783,11 +779,12 @@ const injectCSP = function(fctxt, pageStore, responseHeaders) {
} }
} else { } else {
fctxt.type = 'inline-script'; fctxt.type = 'inline-script';
if ( pageStore.filterRequest(fctxt) === 1 ) { const result = pageStore.filterRequest(fctxt);
if ( result === 1 ) {
builtinDirectives.push("script-src 'unsafe-eval' * blob: data:"); builtinDirectives.push("script-src 'unsafe-eval' * blob: data:");
if ( loggerEnabled ) {
fctxt.setRealm('net').toLogger();
} }
if ( result !== 0 && loggerEnabled ) {
fctxt.setRealm('net').toLogger();
} }
} }

View File

@ -548,13 +548,19 @@ var matchBucket = function(url, hostname, bucket, start) {
// cosmetic filters. // cosmetic filters.
µBlock.logCosmeticFilters = function(tabId, frameId) { µBlock.logCosmeticFilters = function(tabId, frameId) {
if ( this.logger.enabled ) {
vAPI.tabs.injectScript(tabId, { vAPI.tabs.injectScript(tabId, {
file: '/js/scriptlets/cosmetic-logger.js', file: '/js/scriptlets/cosmetic-logger.js',
frameId: frameId, frameId: frameId,
runAt: 'document_start' runAt: 'document_start'
}); });
} };
µBlock.logInlineScript = function(tabId, frameId) {
vAPI.tabs.injectScript(tabId, {
frameId: frameId,
file: '/js/scriptlets/inlinescript-logger.js',
runAt: 'document_start'
});
}; };
/******************************************************************************/ /******************************************************************************/