fix: 版本按提交号标当前,并拦住跨站调用与旧 pin 补丁

同版本号的旧标签不再显示当前。本机接口拒绝跨站请求,更新只接受已知 ref。#41 去掉 dsh-purge:committed 短路。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
叫我小杨同学andCursor committed 2026-09-22 00:15:14 +08:00
1 parent 566fc3683c
commit f3afd9a5cb
7 files changed
+172 -43

No files matched your search

+71 -21
View File
@@ -701,12 +701,62 @@ export function apply(ctx, config) {
installWebServer(ctx, cfg);
}
function headerOf(request, name) {
const headers = request?.headers;
if (!headers) return "";
const value = headers[name] ?? headers[String(name).toLowerCase()];
if (Array.isArray(value)) return String(value[0] || "");
return String(value || "");
}
function denyCrossOrigin(response) {
response.writeHead(403, {
"cache-control": "no-store",
"content-type": "application/json; charset=utf-8",
});
response.end(JSON.stringify({ ok: false, error: "cross-origin blocked" }));
}
function allowPluginRequest(request, response) {
const site = headerOf(request, "sec-fetch-site").toLowerCase();
if (site === "cross-site") {
denyCrossOrigin(response);
return false;
}
const origin = headerOf(request, "origin").trim();
if (origin === "null") {
denyCrossOrigin(response);
return false;
}
if (!origin) return true;
let originHost = "";
try {
originHost = new URL(origin).host;
} catch {
denyCrossOrigin(response);
return false;
}
const host = headerOf(request, "host").trim();
if (!host || originHost.toLowerCase() !== host.toLowerCase()) {
denyCrossOrigin(response);
return false;
}
return true;
}
function installWebServer(ctx, cfg) {
// 桌面端内嵌 @deepseek-ai/dsh-web-app,设置页同样走 webServer(常见 43120),不是只给官方 dsh web。
watchWebTreeReady(ctx);
ctx.inject(["webServer"], (host) => {
host.effect(() => {
host.webServer.register({
const registerPluginRoute = (spec, label) => host.webServer.register({
...spec,
handler: async (request, response) => {
if (!allowPluginRequest(request, response)) return;
return spec.handler(request, response);
},
}, label);
registerPluginRoute({
kind: "exact",
path: "/dsh-purge/status",
handler: async (request, response) => {
@@ -752,7 +802,7 @@ function installWebServer(ctx, cfg) {
}
},
}, "dsh-purge: status");
host.webServer.register({
registerPluginRoute({
kind: "exact",
path: "/dsh-purge/last-user",
handler: async (request, response) => {
@@ -770,7 +820,7 @@ function installWebServer(ctx, cfg) {
}
},
}, "dsh-purge: last user");
host.webServer.register({
registerPluginRoute({
kind: "exact",
path: "/dsh-purge/rewind/options",
handler: async (request, response) => {
@@ -788,7 +838,7 @@ function installWebServer(ctx, cfg) {
}
},
}, "dsh-purge: rewind options");
host.webServer.register({
registerPluginRoute({
kind: "exact",
path: "/dsh-purge/rewind",
handler: async (request, response) => {
@@ -828,7 +878,7 @@ function installWebServer(ctx, cfg) {
response.end();
},
}, "dsh-purge: rewind");
// host.webServer.register({
// registerPluginRoute({
// kind: "exact",
// path: "/dsh-purge/continue",
// handler: async (request, response) => {
@@ -866,7 +916,7 @@ function installWebServer(ctx, cfg) {
// response.end();
// },
// }, "dsh-purge: continue");
// host.webServer.register({
// registerPluginRoute({
// kind: "exact",
// path: "/dsh-purge/continue/settings",
// handler: async (request, response) => {
@@ -895,7 +945,7 @@ function installWebServer(ctx, cfg) {
// response.end();
// },
// }, "dsh-purge: continue settings");
host.webServer.register({
registerPluginRoute({
kind: "exact",
path: "/dsh-purge/update",
handler: async (request, response) => {
@@ -920,7 +970,7 @@ function installWebServer(ctx, cfg) {
response.end();
},
}, "dsh-purge: update");
host.webServer.register({
registerPluginRoute({
kind: "exact",
path: "/dsh-purge/apply",
handler: async (request, response) => {
@@ -984,7 +1034,7 @@ function installWebServer(ctx, cfg) {
}
},
}, "dsh-purge: apply");
host.webServer.register({
registerPluginRoute({
kind: "exact",
path: "/dsh-purge/revert",
handler: async (request, response) => {
@@ -1008,7 +1058,7 @@ function installWebServer(ctx, cfg) {
}
},
}, "dsh-purge: revert");
host.webServer.register({
registerPluginRoute({
kind: "exact",
path: "/dsh-purge/uninstall",
handler: async (request, response) => {
@@ -1037,7 +1087,7 @@ function installWebServer(ctx, cfg) {
}
},
}, "dsh-purge: uninstall");
host.webServer.register({
registerPluginRoute({
kind: "exact",
path: "/dsh-purge/override",
handler: async (request, response) => {
@@ -1081,7 +1131,7 @@ function installWebServer(ctx, cfg) {
},
}, "dsh-purge: override");
installRulesHttp(host.webServer);
host.webServer.register({
registerPluginRoute({
kind: "exact",
path: "/dsh-purge/restart",
handler: async (request, response) => {
@@ -1211,29 +1261,29 @@ function installRulesHttp(webServer) {
};
}
const CORS_HEAD = {
"access-control-allow-origin": "*",
"access-control-allow-methods": "GET, POST, OPTIONS",
"access-control-allow-headers": "content-type",
};
function sendJson(response, status, payload) {
response.writeHead(status, {
"cache-control": "no-store",
"content-type": "application/json; charset=utf-8",
...CORS_HEAD,
});
response.end(JSON.stringify(payload));
}
function sendOptions(response) {
response.writeHead(204, { ...CORS_HEAD, "access-control-max-age": "86400" });
response.writeHead(204, { "cache-control": "no-store" });
response.end();
}
function registerRoute(webServer, spec) {
try {
return webServer.register(spec);
const handler = spec.handler;
return webServer.register({
...spec,
handler: async (request, response) => {
if (!allowPluginRequest(request, response)) return;
return handler(request, response);
},
});
} catch (e) {
console.warn("[dsh-purge] route", spec?.path, String(e && e.message ? e.message : e));
return () => {};