瀏覽代碼

feat: update isRemote (#1491)

Thành Thân Thiện 3 年之前
父節點
當前提交
477fb191bb
共有 2 個文件被更改,包括 12 次插入1 次删除
  1. 1 1
      src/common/util.js
  2. 11 0
      test/common/index.test.js

+ 1 - 1
src/common/util.js

@@ -253,7 +253,7 @@ const FORCED_ACCEPT = {
 };
 
 export const isRemote = url => url
-  && !(/^(file:|data:|https?:\/\/(localhost|127\.0\.0\.1[:/]))/.test(url));
+  && !(/^(file:\/\/|data:|https?:\/\/(localhost|127\.0\.0\.1|(192\.168|172\.16|10\.0)\.[0-9]+\.[0-9]+|\[(::1|(fe80|fc00)::[.:0-9a-f]+)\]|.+\.(test|example|invalid|localhost))(:[0-9]+|\/|$))/i.test(url));
 
 /** @typedef {{
   url: string,

+ 11 - 0
test/common/index.test.js

@@ -13,6 +13,17 @@ test('isRemote', (t) => {
   t.notOk(isRemote('http://localhost/a.user.js'));
   t.notOk(isRemote('https://localhost/a.user.js'));
   t.notOk(isRemote('http://127.0.0.1/a.user.js'));
+  t.notOk(isRemote('http://127.0.0.1:5555/a.user.js'));
+  t.notOk(isRemote('http://192.168.1.32/a.user.js'));
+  t.notOk(isRemote('http://172.16.0.1/a.user.js'));
+  t.notOk(isRemote('http://10.0.0.1/a.user.js'));
+  t.notOk(isRemote('http://[::1]/a.user.js'));
+  t.notOk(isRemote('http://[fe80::6996:2ba9:37e6:8762]/a.user.js'));
+  t.notOk(isRemote('http://[fc00::90:90]/a.user.js'));
+  t.notOk(isRemote('http://example.test/a.user.js'));
+  t.notOk(isRemote('https://example.example/a.user.js'));
+  t.notOk(isRemote('http://example.invalid/a.user.js'));
+  t.notOk(isRemote('https://example.localhost/a.user.js'));
   t.end();
 });