Explorar o código

fix: ignore invalid regexps in @include/@exclude

fixes #1196
tophf %!s(int64=5) %!d(string=hai) anos
pai
achega
6aa27b6df8
Modificáronse 2 ficheiros con 20 adicións e 5 borrados
  1. 3 2
      src/background/utils/tester.js
  2. 17 3
      test/background/tester.test.js

+ 3 - 2
src/background/utils/tester.js

@@ -95,8 +95,9 @@ function bindRE(re) {
 function autoReg(str) {
   // regexp mode: case-insensitive per GM documentation
   if (str.length > 1 && str[0] === '/' && str[str.length - 1] === '/') {
-    const re = new RegExp(str.slice(1, -1), 'i');
-    return { test: bindRE(re) };
+    let re;
+    try { re = new RegExp(str.slice(1, -1), 'i'); } catch (e) { /* ignore */ }
+    return { test: re ? bindRE(re) : () => false };
   }
   // glob mode: case-insensitive to match GM4 & Tampermonkey bugged behavior
   const reStr = str2RE(str.toLowerCase());

+ 17 - 3
test/background/tester.test.js

@@ -227,7 +227,7 @@ test('include', (t) => {
     q.end();
   });
 
-  t.test('should include by regexp', (q) => {
+  t.test('should include by glob', (q) => {
     const script = buildScript({
       meta: {
         include: [
@@ -242,6 +242,20 @@ test('include', (t) => {
     q.end();
   });
 
+  t.test('should include by regexp', (q) => {
+    const script = buildScript({
+      meta: {
+        include: [
+          '/invalid-regexp(/',
+          '/https://www\\.google\\.com/.*/',
+        ],
+      },
+    });
+    q.ok(testScript('https://www.google.com/', script), 'should ignore the invalid regexp and match target');
+    q.notOk(testScript('https://www.hello.com/', script), 'should not match nontarget');
+    q.end();
+  });
+
   t.test('should support magic TLD', (q) => {
     const script = buildScript({
       meta: {
@@ -288,7 +302,7 @@ test('exclude', (t) => {
     q.end();
   });
 
-  t.test('should include by regexp', (q) => {
+  t.test('should include by glob', (q) => {
     const script = buildScript({
       meta: {
         match: [
@@ -338,7 +352,7 @@ test('exclude-match', (t) => {
     q.end();
   });
 
-  t.test('should include by regexp', (q) => {
+  t.test('should include by glob', (q) => {
     const script = buildScript({
       meta: {
         match: [