Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#3297 - Use parent domain as source for 1p-script and 3p* type-based rules #3298

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 55 additions & 33 deletions src/js/dynamic-net-filtering.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,28 @@

/******************************************************************************/

(function() {

µBlock.dynamicFilterScopeKeys = {
'*': '*',
'domain': '+',
'host': '.'
};

µBlock.dynamicFilterTypeScopes = {
'*': 'host',
'image': 'host',
'3p': 'domain',
'inline-script': 'host',
'1p-script': 'domain',
'3p-script': 'domain',
'3p-frame': 'domain'
};

})();

/******************************************************************************/

µBlock.Firewall = (function() {

/******************************************************************************/
Expand Down Expand Up @@ -58,6 +80,12 @@ var typeBitOffsets = {
'image': 10,
'3p': 12
};
var domainMask = 0;
for (let [type, offset] of Object.entries(typeBitOffsets)) {
if (µBlock.dynamicFilterTypeScopes[type] === 'domain') {
domainMask |= (3 << offset);
}
}

var actionToNameMap = {
'1': 'block',
Expand Down Expand Up @@ -147,44 +175,34 @@ Matrix.prototype.assign = function(other) {
/******************************************************************************/

Matrix.prototype.copyRules = function(other, srcHostname, desHostnames) {
var thisRules = this.rules;
var otherRules = other.rules;
var ruleKey, ruleValue;
const thisRules = this.rules;
const otherRules = other.rules;

// Specific types
ruleValue = otherRules['* *'] || 0;
if ( ruleValue !== 0 ) {
thisRules['* *'] = ruleValue;
} else {
delete thisRules['* *'];
}
ruleKey = srcHostname + ' *';
ruleValue = otherRules[ruleKey] || 0;
if ( ruleValue !== 0 ) {
thisRules[ruleKey] = ruleValue;
} else {
delete thisRules[ruleKey];
}
const setRule = function(key, valFunc) {
const val = valFunc(key)
if ( val != 0 ) {
thisRules[key] = val;
} else {
delete thisRules[key];
}
};

// Global types
setRule('* *', k => otherRules[k])

// Domain scoped types - keep session host rules
setRule(domainFromHostname(srcHostname) + ' *', k => (thisRules[k] & ~domainMask) | (otherRules[k] & domainMask))

// Host scoped types - keep session domain rules
setRule(srcHostname + ' *', k => (thisRules[k] & domainMask) | (otherRules[k] & ~domainMask))

// Specific destinations
for ( var desHostname in desHostnames ) {
for ( let desHostname in desHostnames ) {
if ( desHostnames.hasOwnProperty(desHostname) === false ) {
continue;
}
ruleKey = '* ' + desHostname;
ruleValue = otherRules[ruleKey] || 0;
if ( ruleValue !== 0 ) {
thisRules[ruleKey] = ruleValue;
} else {
delete thisRules[ruleKey];
}
ruleKey = srcHostname + ' ' + desHostname ;
ruleValue = otherRules[ruleKey] || 0;
if ( ruleValue !== 0 ) {
thisRules[ruleKey] = ruleValue;
} else {
delete thisRules[ruleKey];
}
setRule('* ' + desHostname, k => otherRules[k])
setRule(srcHostname + ' ' + desHostname, k => otherRules[k])
}

return true;
Expand All @@ -207,8 +225,12 @@ Matrix.prototype.hasSameRules = function(other, srcHostname, desHostnames) {
if ( (thisRules[ruleKey] || 0) !== (otherRules[ruleKey] || 0) ) {
return false;
}
ruleKey = domainFromHostname(srcHostname) + ' *';
if ( (thisRules[ruleKey] & domainMask) !== (otherRules[ruleKey] & domainMask) ) {
return false;
}
ruleKey = srcHostname + ' *';
if ( (thisRules[ruleKey] || 0) !== (otherRules[ruleKey] || 0) ) {
if ( (thisRules[ruleKey] & ~domainMask) !== (otherRules[ruleKey] & ~domainMask) ) {
return false;
}

Expand Down
25 changes: 10 additions & 15 deletions src/js/messaging.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,29 +252,24 @@ var getHostnameDict = function(hostnameToCountMap) {
var getFirewallRules = function(srcHostname, desHostnames) {
var r = {};
var df = µb.sessionFirewall;
r['/ * *'] = df.lookupRuleData('*', '*', '*');
r['/ * image'] = df.lookupRuleData('*', '*', 'image');
r['/ * 3p'] = df.lookupRuleData('*', '*', '3p');
r['/ * inline-script'] = df.lookupRuleData('*', '*', 'inline-script');
r['/ * 1p-script'] = df.lookupRuleData('*', '*', '1p-script');
r['/ * 3p-script'] = df.lookupRuleData('*', '*', '3p-script');
r['/ * 3p-frame'] = df.lookupRuleData('*', '*', '3p-frame');

for (let type of Object.keys(µBlock.dynamicFilterTypeScopes)) {
r['* * ' + type] = df.lookupRuleData('*', '*', type);
}

if ( typeof srcHostname !== 'string' ) {
return r;
}

r['. * *'] = df.lookupRuleData(srcHostname, '*', '*');
r['. * image'] = df.lookupRuleData(srcHostname, '*', 'image');
r['. * 3p'] = df.lookupRuleData(srcHostname, '*', '3p');
r['. * inline-script'] = df.lookupRuleData(srcHostname, '*', 'inline-script');
r['. * 1p-script'] = df.lookupRuleData(srcHostname, '*', '1p-script');
r['. * 3p-script'] = df.lookupRuleData(srcHostname, '*', '3p-script');
r['. * 3p-frame'] = df.lookupRuleData(srcHostname, '*', '3p-frame');
for (let [type, scope] of Object.entries(µBlock.dynamicFilterTypeScopes)) {
r[µBlock.dynamicFilterScopeKeys[scope] + ' * ' + type] = df.lookupRuleData(srcHostname, '*', type);
}

for ( var desHostname in desHostnames ) {
r['/ ' + desHostname + ' *'] = df.lookupRuleData('*', desHostname, '*');
r['* ' + desHostname + ' *'] = df.lookupRuleData('*', desHostname, '*');
r['. ' + desHostname + ' *'] = df.lookupRuleData(srcHostname, desHostname, '*');
}

return r;
};

Expand Down
33 changes: 19 additions & 14 deletions src/js/popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,19 @@ if ( dfPaneVisibleStored ) {

/******************************************************************************/

const DOMAIN_SCOPE_KEY = '+';
const HOST_SCOPE_KEY = '.';

/******************************************************************************/

var messaging = vAPI.messaging;
var popupData = {};
var dfPaneBuilt = false;
var reIP = /^\d+(?:\.\d+){1,3}$/;
var scopeToSrcHostnameMap = {
'/': '*',
'.': ''
'*': '*',
[DOMAIN_SCOPE_KEY]: '',
[HOST_SCOPE_KEY]: ''
};
var dfHotspots = null;
var hostnameToSortableTokenMap = {};
Expand Down Expand Up @@ -117,14 +123,16 @@ var reCyrillicAmbiguous = /[\u042c\u0430\u0433\u0435\u043e\u043f\u0440\u0441\u04

var cachePopupData = function(data) {
popupData = {};
scopeToSrcHostnameMap['.'] = '';
scopeToSrcHostnameMap[DOMAIN_SCOPE_KEY] = '';
scopeToSrcHostnameMap[HOST_SCOPE_KEY] = '';
hostnameToSortableTokenMap = {};

if ( typeof data !== 'object' ) {
return popupData;
}
popupData = data;
scopeToSrcHostnameMap['.'] = popupData.pageHostname || '';
scopeToSrcHostnameMap[DOMAIN_SCOPE_KEY] = popupData.pageDomain || '';
scopeToSrcHostnameMap[HOST_SCOPE_KEY] = popupData.pageHostname || '';
var hostnameDict = popupData.hostnameDict;
if ( typeof hostnameDict !== 'object' ) {
return popupData;
Expand Down Expand Up @@ -259,7 +267,7 @@ var updateFirewallCell = function(scope, des, type, rule) {
}
cells.toggleClass('ownRule', ownRule);

if ( scope !== '.' || des === '*' ) {
if ( scope === '*' || des === '*' ) {
return;
}

Expand Down Expand Up @@ -309,12 +317,7 @@ var updateAllFirewallCells = function() {
if ( rules.hasOwnProperty(key) === false ) {
continue;
}
updateFirewallCell(
key.charAt(0),
key.slice(2, key.indexOf(' ', 2)),
key.slice(key.lastIndexOf(' ') + 1),
rules[key]
);
updateFirewallCell(...key.split(' '), rules[key]);
}

positionRulesetTools();
Expand Down Expand Up @@ -778,7 +781,9 @@ var setFirewallRule = function(src, des, type, action, persist) {
what: 'toggleFirewallRule',
tabId: popupData.tabId,
pageHostname: popupData.pageHostname,
srcHostname: src,
srcHostname: src === DOMAIN_SCOPE_KEY ? popupData.pageDomain :
src === HOST_SCOPE_KEY ? popupData.pageHostname :
src,
desHostname: des,
requestType: type,
action: action,
Expand All @@ -793,7 +798,7 @@ var setFirewallRule = function(src, des, type, action, persist) {
var unsetFirewallRuleHandler = function(ev) {
var cell = uDom(this);
setFirewallRule(
cell.attr('data-src') === '/' ? '*' : popupData.pageHostname,
cell.attr('data-src'),
cell.attr('data-des'),
cell.attr('data-type'),
0,
Expand All @@ -820,7 +825,7 @@ var setFirewallRuleHandler = function(ev) {
action = 1;
}
setFirewallRule(
cell.attr('data-src') === '/' ? '*' : popupData.pageHostname,
cell.attr('data-src'),
cell.attr('data-des'),
cell.attr('data-type'),
action,
Expand Down
16 changes: 8 additions & 8 deletions src/popup.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,19 @@ <h2 data-i18n="popupHitDomainCountPrompt">&nbsp;</h2>
</div>
</div><!-- DO NOT REMOVE --><div class="tooltipContainer">
<div id="firewallContainer" class="minimized">
<div><span data-i18n="popupAnyRulePrompt"></span><span data-src="/" data-des="*" data-type="*" data-i18n-tip="popupTipGlobalRules" data-tip-position="under"> </span><span data-src="." data-des="*" data-type="*" data-i18n-tip="popupTipLocalRules" data-tip-position="under"> </span></div>
<div><span data-i18n="popupImageRulePrompt"></span><span data-src="/" data-des="*" data-type="image"> </span><span data-src="." data-des="*" data-type="image"> </span></div>
<div><span data-i18n="popup3pAnyRulePrompt"></span><span data-src="/" data-des="*" data-type="3p"> </span><span data-src="." data-des="*" data-type="3p"> </span></div>
<div><span data-i18n="popupInlineScriptRulePrompt"></span><span data-src="/" data-des="*" data-type="inline-script"> </span><span data-src="." data-des="*" data-type="inline-script"> </span></div>
<div><span data-i18n="popup1pScriptRulePrompt"></span><span data-src="/" data-des="*" data-type="1p-script"> </span><span data-src="." data-des="*" data-type="1p-script"> </span></div>
<div><span data-i18n="popup3pScriptRulePrompt"></span><span data-src="/" data-des="*" data-type="3p-script"> </span><span data-src="." data-des="*" data-type="3p-script"> </span></div>
<div><span data-i18n="popup3pFrameRulePrompt"></span><span data-src="/" data-des="*" data-type="3p-frame"> </span><span data-src="." data-des="*" data-type="3p-frame"> </span></div>
<div><span data-i18n="popupAnyRulePrompt"></span><span data-src="*" data-des="*" data-type="*" data-i18n-tip="popupTipGlobalRules" data-tip-position="under"> </span><span data-src="." data-des="*" data-type="*" data-i18n-tip="popupTipLocalRules" data-tip-position="under"> </span></div>
<div><span data-i18n="popupImageRulePrompt"></span><span data-src="*" data-des="*" data-type="image"> </span><span data-src="." data-des="*" data-type="image"> </span></div>
<div><span data-i18n="popup3pAnyRulePrompt"></span><span data-src="*" data-des="*" data-type="3p"> </span><span data-src="+" data-des="*" data-type="3p"> </span></div>
<div><span data-i18n="popupInlineScriptRulePrompt"></span><span data-src="*" data-des="*" data-type="inline-script"> </span><span data-src="." data-des="*" data-type="inline-script"> </span></div>
<div><span data-i18n="popup1pScriptRulePrompt"></span><span data-src="*" data-des="*" data-type="1p-script"> </span><span data-src="+" data-des="*" data-type="1p-script"> </span></div>
<div><span data-i18n="popup3pScriptRulePrompt"></span><span data-src="*" data-des="*" data-type="3p-script"> </span><span data-src="+" data-des="*" data-type="3p-script"> </span></div>
<div><span data-i18n="popup3pFrameRulePrompt"></span><span data-src="*" data-des="*" data-type="3p-frame"> </span><span data-src="+" data-des="*" data-type="3p-frame"> </span></div>
</div><div id="rulesetTools"><span id="saveRules" class="fa" data-i18n-tip="popupTipSaveRules" data-tip-position="under">&#xf023;</span><span id="revertRules" class="fa" data-i18n-tip="popupTipRevertRules" data-tip-position="under">&#xf12d;</span></div>
</div>
</div>

<div id="templates" style="display: none">
<div><span><sup></sup><span></span></span><span data-src="/" data-des="" data-type="*"></span><span data-src="." data-des="" data-type="*"></span><span data-src="." data-des="" data-type="*"></span></div>
<div><span><sup></sup><span></span></span><span data-src="*" data-des="" data-type="*"></span><span data-src="." data-des="" data-type="*"></span><span data-src="." data-des="" data-type="*"></span></div>
<div id="actionSelector"><span id="dynaAllow"></span><span id="dynaNoop"></span><span id="dynaBlock"></span></div>
<div id="hotspotTip"></div>
<div id="tooltip"></div>
Expand Down