feat(electron-shell): 本地 HTTP/WS 入站做 Host/Origin 回环校验
服务只绑 127.0.0.1,但恶意网页可把自有域名解析到 127.0.0.1 后从本机浏览器直连(DNS rebinding)。新增 checkSource():Host 主机名必须是回环(127.0.0.1/localhost/::1),打包态端口必须等于 8799、dev 态不限端口(插件 ui-src 的 vite dev server 代理过来仍带自身 5180~5232 端口);Origin 非空同样校验,null(sandbox iframe/file://)一律拒绝,顶级导航(钉钉回调 /auth/done)无 Origin 按 Host 放行。HTTP 与 WS 握手共用,不通过直接 403 且不进路由,日志留痕。
This commit is contained in:
@@ -44,6 +44,43 @@ const MIME = {
|
||||
".woff2": "font/woff2", ".ttf": "font/ttf", ".txt": "text/plain",
|
||||
};
|
||||
|
||||
// ==================== 本机来源校验(防 DNS rebinding / 跨站直连) ====================
|
||||
// 服务只绑 127.0.0.1,但恶意网页可把自有域名解析到 127.0.0.1 后从本机浏览器直连(DNS rebinding),
|
||||
// 因此必须校验 Host 头;Origin 非空时同样校验(浏览器跨站请求必带 Origin)。
|
||||
// 规则:主机名必须是回环(127.0.0.1 / localhost / ::1);打包态再收紧端口必须等于本地服务端口。
|
||||
// dev 态不限端口 —— 插件 ui-src 各自 vite dev server(5180~5232)会代理到 8799 且多数未开
|
||||
// changeOrigin,Host/Origin 保留 dev 端口;顶级导航(钉钉回调 /auth/done、外部浏览器跳回)不带 Origin,按 Host 放行。
|
||||
const LOOPBACK_NAMES = new Set(["127.0.0.1", "localhost", "::1"]);
|
||||
|
||||
function isLoopbackName(hostname) {
|
||||
return LOOPBACK_NAMES.has(String(hostname || "").toLowerCase().replace(/^\[|\]$/g, ""));
|
||||
}
|
||||
|
||||
// 返回 "" 表示通过,否则返回拒绝原因(用于日志留痕)
|
||||
function checkSource(req) {
|
||||
let host;
|
||||
try {
|
||||
host = new URL(`http://${req.headers.host || ""}`);
|
||||
} catch (e) {
|
||||
return "Host 头非法";
|
||||
}
|
||||
if (!isLoopbackName(host.hostname)) return `Host 非本机回环(${host.hostname})`;
|
||||
if (config.IS_FROZEN && host.port !== String(config.PORT)) return `Host 端口非 ${config.PORT}(${host.port || "缺省"})`;
|
||||
|
||||
const origin = req.headers.origin;
|
||||
if (!origin) return ""; // 顶级导航 / 非浏览器客户端
|
||||
let o;
|
||||
try {
|
||||
o = new URL(origin);
|
||||
} catch (e) {
|
||||
return `Origin 非法(${origin})`; // 含 sandbox iframe / file:// 的 "null"
|
||||
}
|
||||
if (!["http:", "https:", "ws:", "wss:"].includes(o.protocol)) return `Origin 协议非法(${o.protocol})`;
|
||||
if (!isLoopbackName(o.hostname)) return `Origin 非本机回环(${o.hostname})`;
|
||||
if (config.IS_FROZEN && o.port !== String(config.PORT)) return `Origin 端口非 ${config.PORT}(${o.port || "缺省"})`;
|
||||
return "";
|
||||
}
|
||||
|
||||
// ==================== 工具 ====================
|
||||
function parseCookies(req) {
|
||||
const out = {};
|
||||
@@ -1155,6 +1192,14 @@ function afterLogin(account) {
|
||||
// ==================== 服务器启动 ====================
|
||||
function createServer() {
|
||||
const server = http.createServer(async (req, res) => {
|
||||
// 来源校验前置:不进路由(连公开的 /api/meta、静态资源也不服务,避免被当作探测入口)
|
||||
const deny = checkSource(req);
|
||||
if (deny) {
|
||||
logger.warning(`拒绝来源异常请求:${req.method} ${req.url} → ${deny}`);
|
||||
res.writeHead(403, { "Content-Type": "text/plain; charset=utf-8", Connection: "close" });
|
||||
res.end("Forbidden");
|
||||
return;
|
||||
}
|
||||
const url = new URL(req.url, `http://${req.headers.host || `127.0.0.1:${config.PORT}`}`);
|
||||
logger.debug(`${req.method} ${url.pathname}`); // 请求级调试日志(DEBUG 模式)
|
||||
try {
|
||||
@@ -1167,6 +1212,13 @@ function createServer() {
|
||||
// WS /ws:握手需有效统一账户会话
|
||||
const wss = new WebSocketServer({ noServer: true });
|
||||
server.on("upgrade", (req, socket, head) => {
|
||||
const wsDeny = checkSource(req);
|
||||
if (wsDeny) {
|
||||
logger.warning(`拒绝来源异常 WS 握手:${req.url} → ${wsDeny}`);
|
||||
socket.write("HTTP/1.1 403 Forbidden\r\n\r\n");
|
||||
socket.destroy();
|
||||
return;
|
||||
}
|
||||
const url = new URL(req.url, `http://127.0.0.1:${config.PORT}`);
|
||||
if (url.pathname !== "/ws") {
|
||||
socket.destroy();
|
||||
|
||||
Reference in New Issue
Block a user