@waline/vercel 1.39.0 → 1.39.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/controller/rest.js +1 -2
- package/src/logic/base.js +58 -60
package/package.json
CHANGED
package/src/controller/rest.js
CHANGED
|
@@ -21,7 +21,7 @@ module.exports = class extends think.Controller {
|
|
|
21
21
|
const filename = this.__filename || __filename;
|
|
22
22
|
const last = filename.lastIndexOf(path.sep);
|
|
23
23
|
|
|
24
|
-
return filename.slice(last + 1,
|
|
24
|
+
return filename.slice(last + 1, - 3);
|
|
25
25
|
}
|
|
26
26
|
|
|
27
27
|
getId() {
|
|
@@ -32,7 +32,6 @@ module.exports = class extends think.Controller {
|
|
|
32
32
|
}
|
|
33
33
|
|
|
34
34
|
const last = decodeURIComponent(this.ctx.path.split('/').pop());
|
|
35
|
-
|
|
36
35
|
if (last !== this.resource && /^([a-z0-9]+,?)*$/i.test(last)) {
|
|
37
36
|
return last;
|
|
38
37
|
}
|
package/src/logic/base.js
CHANGED
|
@@ -13,65 +13,9 @@ module.exports = class BaseLogic extends think.Logic {
|
|
|
13
13
|
|
|
14
14
|
// oxlint-disable-next-line max-statements
|
|
15
15
|
async __before() {
|
|
16
|
-
const
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
if (origin) {
|
|
20
|
-
try {
|
|
21
|
-
const parsedOrigin = new URL(origin);
|
|
22
|
-
|
|
23
|
-
origin = parsedOrigin.hostname;
|
|
24
|
-
} catch (err) {
|
|
25
|
-
console.error('Invalid origin format:', origin, err);
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
let { secureDomains } = this.config();
|
|
30
|
-
|
|
31
|
-
if (secureDomains) {
|
|
32
|
-
secureDomains = think.isArray(secureDomains) ? secureDomains : [secureDomains];
|
|
33
|
-
|
|
34
|
-
secureDomains.push(
|
|
35
|
-
'localhost',
|
|
36
|
-
'127.0.0.1',
|
|
37
|
-
// 'github.com',
|
|
38
|
-
// 'api.twitter.com',
|
|
39
|
-
// 'www.facebook.com',
|
|
40
|
-
// 'api.weibo.com',
|
|
41
|
-
// 'graph.qq.com',
|
|
42
|
-
);
|
|
43
|
-
secureDomains = [
|
|
44
|
-
...secureDomains,
|
|
45
|
-
...this.ctx.state.oauthServices.map(({ origin }) => origin),
|
|
46
|
-
];
|
|
47
|
-
|
|
48
|
-
// 转换可能的正则表达式字符串为正则表达式对象
|
|
49
|
-
secureDomains = secureDomains
|
|
50
|
-
.map((domain) => {
|
|
51
|
-
// 如果是正则表达式字符串,创建一个 RegExp 对象
|
|
52
|
-
if (typeof domain === 'string' && domain.startsWith('/') && domain.endsWith('/')) {
|
|
53
|
-
try {
|
|
54
|
-
return new RegExp(domain.slice(1, -1)); // 去掉斜杠并创建 RegExp 对象
|
|
55
|
-
} catch (err) {
|
|
56
|
-
console.error('Invalid regex pattern in secureDomains:', domain, err);
|
|
57
|
-
|
|
58
|
-
return null;
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
return domain;
|
|
63
|
-
})
|
|
64
|
-
.filter(Boolean); // 过滤掉无效的正则表达式
|
|
65
|
-
|
|
66
|
-
// 有 referrer 检查 referrer,没有则检查 origin
|
|
67
|
-
const checking = referrer || origin;
|
|
68
|
-
const isSafe = secureDomains.some((domain) =>
|
|
69
|
-
think.isFunction(domain.test) ? domain.test(checking) : domain === checking,
|
|
70
|
-
);
|
|
71
|
-
|
|
72
|
-
if (!isSafe) {
|
|
73
|
-
return this.ctx.throw(403);
|
|
74
|
-
}
|
|
16
|
+
const referrerCheckResult = this.referrerCheck();
|
|
17
|
+
if (!referrerCheckResult) {
|
|
18
|
+
return this.ctx.throw(403);
|
|
75
19
|
}
|
|
76
20
|
|
|
77
21
|
this.ctx.state.userInfo = {};
|
|
@@ -134,11 +78,65 @@ module.exports = class BaseLogic extends think.Logic {
|
|
|
134
78
|
this.ctx.state.token = token;
|
|
135
79
|
}
|
|
136
80
|
|
|
81
|
+
referrerCheck() {
|
|
82
|
+
let { secureDomains } = this.config();
|
|
83
|
+
if (!secureDomains) {
|
|
84
|
+
return true;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const whitelistPath = ['/api/comment/rss'];
|
|
88
|
+
if (this.ctx.path && whitelistPath.includes(this.ctx.path)) {
|
|
89
|
+
return true;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const referrer = this.ctx.referrer(true);
|
|
93
|
+
let { origin } = this.ctx;
|
|
94
|
+
if (origin) {
|
|
95
|
+
try {
|
|
96
|
+
const parsedOrigin = new URL(origin);
|
|
97
|
+
|
|
98
|
+
origin = parsedOrigin.hostname;
|
|
99
|
+
} catch (err) {
|
|
100
|
+
console.error('Invalid origin format:', origin, err);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
secureDomains = think.isArray(secureDomains) ? secureDomains : [secureDomains];
|
|
105
|
+
secureDomains.push('localhost', '127.0.0.1');
|
|
106
|
+
secureDomains = [...secureDomains, ...this.ctx.state.oauthServices.map(({ origin }) => origin)];
|
|
107
|
+
|
|
108
|
+
// 转换可能的正则表达式字符串为正则表达式对象
|
|
109
|
+
secureDomains = secureDomains
|
|
110
|
+
.map((domain) => {
|
|
111
|
+
// 如果是正则表达式字符串,创建一个 RegExp 对象
|
|
112
|
+
if (typeof domain === 'string' && domain.startsWith('/') && domain.endsWith('/')) {
|
|
113
|
+
try {
|
|
114
|
+
return new RegExp(domain.slice(1, -1)); // 去掉斜杠并创建 RegExp 对象
|
|
115
|
+
} catch (err) {
|
|
116
|
+
console.error('Invalid regex pattern in secureDomains:', domain, err);
|
|
117
|
+
|
|
118
|
+
return null;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
return domain;
|
|
123
|
+
})
|
|
124
|
+
.filter(Boolean); // 过滤掉无效的正则表达式
|
|
125
|
+
|
|
126
|
+
// 有 referrer 检查 referrer,没有则检查 origin
|
|
127
|
+
const checking = referrer || origin;
|
|
128
|
+
const isSafe = secureDomains.some((domain) =>
|
|
129
|
+
think.isFunction(domain.test) ? domain.test(checking) : domain === checking,
|
|
130
|
+
);
|
|
131
|
+
|
|
132
|
+
return isSafe;
|
|
133
|
+
}
|
|
134
|
+
|
|
137
135
|
getResource() {
|
|
138
136
|
const filename = this.__filename || __filename;
|
|
139
137
|
const last = filename.lastIndexOf(path.sep);
|
|
140
138
|
|
|
141
|
-
return filename.slice(last + 1,
|
|
139
|
+
return filename.slice(last + 1, -3);
|
|
142
140
|
}
|
|
143
141
|
|
|
144
142
|
getId() {
|