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

Remove middle wildcard support in rules.js #17715

Merged
merged 7 commits into from
Apr 16, 2019
Merged
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
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