Skip to content
This repository has been archived by the owner on Nov 6, 2023. It is now read-only.

Commit

Permalink
Remove middle wildcard support in rules.js (#17715)
Browse files Browse the repository at this point in the history
* Remove middle wildcard support in rules.js

* Fix regression caused by previous commit and update comments

* Update unit tests to please Travis

* Add unit tests to assert we dropped middle wildcard support

* Update rules.js
  • Loading branch information
Chan Chak Shing authored and zoracon committed Apr 16, 2019
1 parent 29e55a2 commit 7bc1255
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
15 changes: 8 additions & 7 deletions chromium/background-scripts/rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -562,22 +562,23 @@ RuleSets.prototype = {
return nullIterable;
}

// Replace each portion of the domain with a * in turn
// Replace www.example.com with www.example.*
// eat away from the right for once and only once
let segmented = host.split(".");
for (let i = 0; i < segmented.length; i++) {
let tmp = segmented[i];
segmented[i] = "*";
if (segmented.length > 1) {
const tmp = segmented[segmented.length - 1];
segmented[segmented.length - 1] = "*";

results = (this.targets.has(segmented.join(".")) ?
new Set([...results, ...this.targets.get(segmented.join("."))]) :
results);

segmented[i] = tmp;
segmented[segmented.length - 1] = tmp;
}

// now eat away from the left, with *, so that for x.y.z.google.com we
// check *.z.google.com and *.google.com (we did *.y.z.google.com above)
for (let i = 2; i <= segmented.length - 2; i++) {
// check *.y.z.google.com, *.z.google.com and *.google.com
for (let i = 1; i <= segmented.length - 2; i++) {
let t = "*." + segmented.slice(i, segmented.length).join(".");

results = (this.targets.has(t) ?
Expand Down
18 changes: 13 additions & 5 deletions chromium/test/rules_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,15 +203,23 @@ describe('rules.js', function() {
assert.deepEqual(res3, new Set(value), 'wildcard matches sub domains');
});

it('matches middle wildcards', function() {
let target = 'sub.*.' + host;
it('matches right wildcards', function() {
const target = host + '.*';
this.rsets.targets.set(target, value);

let res1 = this.rsets.potentiallyApplicableRulesets('sub.star.' + host);
const res1 = this.rsets.potentiallyApplicableRulesets(host + '.tld');
assert.deepEqual(res1, new Set(value), 'default case');

let res2 = this.rsets.potentiallyApplicableRulesets('sub.foo.bar.' + host);
assert.isEmpty(res2, new Set(value), 'only matches one label');
const res2 = this.rsets.potentiallyApplicableRulesets(host + '.tld.com');
assert.isEmpty(res2, 'wildcard matches second level domains');
});

it('ignore middle wildcards', function() {
const target = 'www.*.' + host;
this.rsets.targets.set(target, value);

const res1 = this.rsets.potentiallyApplicableRulesets('www.cdn.' + host);
assert.isEmpty(res1, 'middle wildcards are matched');
});
});
});
Expand Down

0 comments on commit 7bc1255

Please sign in to comment.