Repository navigation
Expand file tree
/
Copy pathnetwork_access.js
More file actions
52 lines (45 loc) · 1.97 KB
/
Copy pathnetwork_access.js
File metadata and controls
52 lines (45 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
function normalizedIp(value) {
return String(value || "").replace(/^::ffff:/, "");
}
function isLoopbackIp(value) {
const ip = normalizedIp(value);
return ip === "127.0.0.1" || ip === "::1" || ip === "localhost";
}
function isPrivateIp(value) {
const ip = normalizedIp(value);
return isLoopbackIp(ip) || /^(?:10\.|172\.(?:1[6-9]|2\d|3[01])\.|192\.168\.)/.test(ip);
}
function decideRequestAccess({
path,
ip,
isRailway,
allowPublicApi,
configuredKey,
authorization,
headerKey
}) {
const requestPath = String(path || "").split("?")[0];
// /test-bark 只是穿过网络层,路由本身仍必须通过与管理页相同的 Basic Auth。
if (requestPath.startsWith("/admin") || requestPath === "/healthz" || requestPath === "/test-bark") return { allow: true };
// 批注 2026-08-10:云平台反代的 10/172/192.168 地址不代表最终访客可信;
// 内部写接口只接受同容器 localhost,不能被公网代理转发后伪造心跳或唤醒事件。
if (requestPath.startsWith("/internal/")) {
return isLoopbackIp(ip) ? { allow: true } : { allow: false, status: 403, error: "Forbidden" };
}
if (allowPublicApi && requestPath.startsWith("/v1/")) {
if (!configuredKey) return { allow: false, status: 401, error: "公网 /v1 已开启,但 GATEWAY_API_KEY 未配置", authRejected: true };
const bearer = String(authorization || "").match(/^Bearer\s+(.+)$/i)?.[1]?.trim() || "";
const alternate = String(headerKey || "").trim();
if (bearer === configuredKey || alternate === configuredKey) return { allow: true };
return {
allow: false,
status: 401,
error: "Gateway API Key 无效或缺失",
authRejected: true,
authSource: bearer ? "bearer" : alternate ? "x-api-key" : "missing"
};
}
if (isLoopbackIp(ip) || (!isRailway && isPrivateIp(ip))) return { allow: true };
return { allow: false, status: 403, error: "Forbidden" };
}
module.exports = { decideRequestAccess, isLoopbackIp, isPrivateIp };