Lusito / forget-me-not

Make the browser forget website data, except for the data you want to keep.
zlib License
227 stars 25 forks source link

Invalid rule matching. (Matching end of string and escaping special characters especially the dot.) #79

Closed david-caroli closed 6 years ago

david-caroli commented 6 years ago

screenshot from 2018-08-19 17-17-45

The Problem:
In the screen-shot are 3 examples of rules that the domain www.reddit.com matches, even though it shouldn't:

The likely cause:
The cause of this seems to be the conversion of the rule strings to regular expressions.
The culprit function seems to be getRegExForRule(rule: string) in settings.ts.

getRegExForRule(rule: string) correctly matches the start of the string using a ^, but it should do the same for the end of the string. (line 107 and line 110)

Additionally the function doesn't escape the characters from the rule when adding them to the regular expression. This leads to the dots in the rule not being escaped and treated as a special character. (line 116 and line 117)

Possibly a solution:
I have never used TypeScript and I don't use JavaScript often so take the following with a grain of salt.

Change line 117 from reParts.push("."); to reParts.push("\\.");.

Before the return at line 121 add a line with reParts.push("$");.
(see also RegEx Special Chars)

---
I hope the problem is reproducible and can be fixed. Thanks in advance!

Lusito commented 6 years ago

Thanks for the report! Not sure what I was thinking writing it this way.

There were other problems with this, so I've written a fix that addresses all of the issues. I've also added unit-tests for this, so this won't happen in the future. Please have a look at the above commit if you want to double check if there is anything I missed.

david-caroli commented 6 years ago

@Lusito The code looks good to me. Thank you for the quick fix and response!