Skip to content

Commit

Permalink
The evaluation of Regex expressions in the blacklist is broken (#790)
Browse files Browse the repository at this point in the history
* Fixed the blacklist's regex evaluation
* Added logic to fix broken regular expressions
* Replaced automatic regex fixing with helpful error message
  • Loading branch information
xAdler authored Oct 9, 2021
1 parent f6fb427 commit caacb45
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 17 deletions.
13 changes: 12 additions & 1 deletion inject.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var regStrip = /^[\r\t\f\v ]+|[\r\t\f\v ]+$/gm;
var regEndsWithFlags = /\/(?!.*(.).*\1)[gimsuy]*$/;

var tc = {
settings: {
Expand Down Expand Up @@ -393,7 +394,17 @@ function isBlacklisted() {

if (match.startsWith("/")) {
try {
var regexp = new RegExp(match);
var parts = match.split("/");

if (regEndsWithFlags.test(match)) {
var flags = parts.pop();
var regex = parts.slice(1).join("/");
} else {
var flags = "";
var regex = match;
}

var regexp = new RegExp(regex, flags);
} catch (err) {
return;
}
Expand Down
2 changes: 1 addition & 1 deletion options.html
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ <h3>Other</h3>
<br />
<em>
<a href="https://www.regexpal.com/">Regex</a> is supported.<br />
Be sure it is in "//g" format.<br />
Be sure to use the literal notation.<br />
ie: /(.+)youtube\.com(\/*)$/gi
</em>
</label>
Expand Down
38 changes: 23 additions & 15 deletions options.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,22 +180,30 @@ function createKeyBindings(item) {
function validate() {
var valid = true;
var status = document.getElementById("status");
document
.getElementById("blacklist")
.value.split("\n")
.forEach((match) => {
match = match.replace(regStrip, "");
if (match.startsWith("/")) {
try {
var regexp = new RegExp(match);
} catch (err) {
status.textContent =
"Error: Invalid blacklist regex: " + match + ". Unable to save";
valid = false;
return;
}
var blacklist = document.getElementById("blacklist");

blacklist.value.split("\n").forEach((match) => {
match = match.replace(regStrip, "");

if (match.startsWith("/")) {
try {
var parts = match.split("/");

if (parts.length < 3)
throw "invalid regex";

var flags = parts.pop();
var regex = parts.slice(1).join("/");

var regexp = new RegExp(regex, flags);
} catch (err) {
status.textContent =
"Error: Invalid blacklist regex: \"" + match + "\". Unable to save. Try wrapping it in foward slashes.";
valid = false;
return;
}
});
}
});
return valid;
}

Expand Down

0 comments on commit caacb45

Please sign in to comment.