Index: lib/filterClasses.js |
=================================================================== |
--- a/lib/filterClasses.js |
+++ b/lib/filterClasses.js |
@@ -766,16 +766,17 @@ |
let contentType = null; |
let matchCase = null; |
let domains = null; |
let sitekeys = null; |
let thirdParty = null; |
let collapse = null; |
let csp = null; |
+ let rewrite = null; |
let options; |
let match = (text.indexOf("$") >= 0 ? Filter.optionsRegExp.exec(text) : null); |
if (match) |
{ |
options = match[1].split(","); |
text = match.input.substr(0, match.index); |
for (let option of options) |
{ |
@@ -813,30 +814,32 @@ |
else if (option == "~THIRD_PARTY") |
thirdParty = false; |
else if (option == "COLLAPSE") |
collapse = true; |
else if (option == "~COLLAPSE") |
collapse = false; |
else if (option == "SITEKEY" && value) |
sitekeys = value.toUpperCase(); |
+ else if (option == "REWRITE" && value) |
+ rewrite = value; |
else |
return new InvalidFilter(origText, "filter_unknown_option"); |
} |
} |
try |
{ |
if (blocking) |
{ |
if (csp && Filter.invalidCSPRegExp.test(csp)) |
return new InvalidFilter(origText, "filter_invalid_csp"); |
return new BlockingFilter(origText, text, contentType, matchCase, domains, |
- thirdParty, sitekeys, collapse, csp); |
+ thirdParty, sitekeys, collapse, csp, rewrite); |
} |
return new WhitelistFilter(origText, text, contentType, matchCase, domains, |
thirdParty, sitekeys); |
} |
catch (e) |
{ |
return new InvalidFilter(origText, "filter_invalid_regexp"); |
} |
@@ -889,27 +892,30 @@ |
* @param {boolean} matchCase see RegExpFilter() |
* @param {string} domains see RegExpFilter() |
* @param {boolean} thirdParty see RegExpFilter() |
* @param {string} sitekeys see RegExpFilter() |
* @param {boolean} collapse |
* defines whether the filter should collapse blocked content, can be null |
* @param {string} [csp] |
* Content Security Policy to inject when the filter matches |
+ * @param {string} [rewrite] |
+ * The rewrite expression |
* @constructor |
* @augments RegExpFilter |
*/ |
function BlockingFilter(text, regexpSource, contentType, matchCase, domains, |
- thirdParty, sitekeys, collapse, csp) |
+ thirdParty, sitekeys, collapse, csp, rewrite) |
{ |
RegExpFilter.call(this, text, regexpSource, contentType, matchCase, domains, |
thirdParty, sitekeys); |
this.collapse = collapse; |
this.csp = csp; |
+ this.rewrite = rewrite; |
} |
exports.BlockingFilter = BlockingFilter; |
BlockingFilter.prototype = extend(RegExpFilter, { |
type: "blocking", |
/** |
* Defines whether the filter should collapse blocked content. |
@@ -917,17 +923,43 @@ |
* @type {boolean} |
*/ |
collapse: null, |
/** |
* Content Security Policy to inject for matching requests. |
* @type {string} |
*/ |
- csp: null |
+ csp: null, |
+ |
+ /** |
+ * The rewrite expression |
+ * @type {string} |
+ */ |
+ rewrite: null, |
+ |
+ /** |
+ * Perform the URL rewrite and check the origin. |
Manish Jethani
2018/05/04 22:50:48
Does the detail about checking the origin really n
hub
2018/05/07 20:11:56
Done.
|
+ * @param {string} urlString the string URL to rewrite |
Manish Jethani
2018/05/04 22:50:48
We can just say "the URL to rewrite"
Also shall w
hub
2018/05/07 20:11:55
"url" is fine by me. Done.
|
+ * @returns {string?} the rewritten URL, or the orginal in case of failure. |
Manish Jethani
2018/05/04 22:50:49
Nit: original URL
Also: (1) the type should be {s
hub
2018/05/07 20:11:55
Done.
|
+ */ |
+ rewriteUrl(urlString) |
+ { |
+ let rewritten = urlString.replace(this.regexp, this.rewrite); |
Manish Jethani
2018/05/04 22:50:48
Does this work with Unicode domains? For example h
Manish Jethani
2018/05/05 00:51:38
Never mind, it won't. I've filed Trac #6647 for th
Sebastian Noack
2018/05/05 17:07:07
How about this?
let rewritten = new URL(urlStri
Manish Jethani
2018/05/05 23:58:39
Yeah, I like the idea.
So it would be something l
Manish Jethani
2018/05/06 00:45:41
Oh well, redirectUrl can't be relative. It'll have
Sebastian Noack
2018/05/06 13:04:25
As far as I can tell, if we return an IDN-encoded
Manish Jethani
2018/05/06 13:59:31
Sorry, here if we accept a URL object as a paramet
Manish Jethani
2018/05/06 13:59:31
Yeah, after checking EasyList and EasyList China [
hub
2018/05/07 20:11:55
I'll leave it like that for now.
Sebastian Noack
2018/05/08 06:57:30
Since we are on the verge of landing the change mo
hub
2018/05/08 15:54:30
it is done now.
|
+ try |
+ { |
+ if (new URL(rewritten).origin == new URL(urlString).origin) |
Manish Jethani
2018/05/04 22:50:48
We should probably have a function in lib/common.j
Manish Jethani
2018/05/04 23:41:46
I ran a test to see if separating it out into its
Sebastian Noack
2018/05/05 13:34:43
As long as we only need this functionality in one
hub
2018/05/07 20:11:55
agreed.
|
+ return rewritten; |
+ } |
+ catch (e) |
+ { |
+ } |
+ |
+ return urlString; |
+ } |
}); |
/** |
* Class for whitelist filters |
* @param {string} text see Filter() |
* @param {string} regexpSource see RegExpFilter() |
* @param {number} contentType see RegExpFilter() |
* @param {boolean} matchCase see RegExpFilter() |