@sleep2agi/commhub-server 0.9.0-preview.193 → 0.9.0-preview.194
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/not-found-api-http.test.ts +67 -0
- package/src/server.ts +12 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sleep2agi/commhub-server",
|
|
3
|
-
"version": "0.9.0-preview.
|
|
3
|
+
"version": "0.9.0-preview.194",
|
|
4
4
|
"description": "CommHub Server — AI Agent communication hub with MCP protocol, multi-network isolation, user auth, and MCP tools (17 collaboration-core + node/provider ops tools; authoritative list: docs-site/docs/api/mcp-tools.md).",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "src/index.ts",
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
// #2946 —— 未匹配的 /api/* 必须 404,而不是 200 + 帮助页。
|
|
2
|
+
//
|
|
3
|
+
// 现场(.193):带 token 打 /api/lang/status /api/hub/profile /api/nodes/department
|
|
4
|
+
// /api/schedules/failures(四个都不存在)→ 全部 200 + 652B 的帮助页文本,
|
|
5
|
+
// 字节数完全相同。探针会把「端点改名/拼错」读成「正常」—— 假绿。
|
|
6
|
+
// 这与 #2559(401 被伪装成空数据)同类:错误被伪装成成功。
|
|
7
|
+
//
|
|
8
|
+
// 根路径 `/` 仍然返回帮助页(那是给人/curl 看的入口说明),这条也要钉住,
|
|
9
|
+
// 否则修 #2946 时很容易把根路径一起改成 404。
|
|
10
|
+
import { afterAll, beforeAll, expect, test } from "bun:test";
|
|
11
|
+
import { mkdtempSync } from "node:fs";
|
|
12
|
+
import { join } from "node:path";
|
|
13
|
+
import { tmpdir } from "node:os";
|
|
14
|
+
|
|
15
|
+
const dir = mkdtempSync(join(tmpdir(), "notfound-2946-"));
|
|
16
|
+
process.env.COMMHUB_DB ||= join(dir, "hub.db");
|
|
17
|
+
process.env.COMMHUB_AUTH_TOKEN ||= "notfound-2946-master-fixture";
|
|
18
|
+
|
|
19
|
+
let hub: { stop: (closeActiveConnections?: boolean) => void; port: number } | null = null;
|
|
20
|
+
let base = "";
|
|
21
|
+
let token = "";
|
|
22
|
+
|
|
23
|
+
beforeAll(async () => {
|
|
24
|
+
const { register } = await import("./auth.js");
|
|
25
|
+
const { bootServer } = await import("./server.js");
|
|
26
|
+
const a = register(`nf_owner_${Date.now()}`, "NotFou-2946-Strong1!");
|
|
27
|
+
expect(a.ok).toBe(true);
|
|
28
|
+
token = a.token!;
|
|
29
|
+
hub = bootServer({ port: 0, hostname: "127.0.0.1" }) as any;
|
|
30
|
+
base = `http://127.0.0.1:${hub!.port}`;
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
afterAll(() => { try { hub?.stop?.(true); } catch {} });
|
|
34
|
+
|
|
35
|
+
const call = async (path: string, withToken = true) => {
|
|
36
|
+
const res = await fetch(base + path, {
|
|
37
|
+
headers: withToken ? { Authorization: `Bearer ${token}` } : {},
|
|
38
|
+
});
|
|
39
|
+
const text = await res.text();
|
|
40
|
+
return { status: res.status, text };
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
test("不存在的 /api/* 路径返回 404 not_found(不再 200 + 帮助页)", async () => {
|
|
44
|
+
for (const path of ["/api/definitely-not-a-route", "/api/lang/status", "/api/hub/profile"]) {
|
|
45
|
+
const r = await call(path);
|
|
46
|
+
expect(r.status).toBe(404);
|
|
47
|
+
const body = JSON.parse(r.text);
|
|
48
|
+
expect(body.ok).toBe(false);
|
|
49
|
+
expect(body.error).toBe("not_found");
|
|
50
|
+
expect(body.path).toBe(path);
|
|
51
|
+
// 关键:不再把帮助页伪装成这个端点的返回
|
|
52
|
+
expect(r.text).not.toContain("Streamable HTTP");
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
test("已存在的端点不受影响", async () => {
|
|
57
|
+
const r = await call("/health", false);
|
|
58
|
+
expect(r.status).toBe(200);
|
|
59
|
+
expect(JSON.parse(r.text).ok).toBe(true);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
test("根路径仍然返回帮助页(不能一起改成 404)", async () => {
|
|
63
|
+
// 认证门在路由之前:/ 也要带 token 才看得到帮助页(不带是 401,本就不变)
|
|
64
|
+
const r = await call("/", true);
|
|
65
|
+
expect(r.status).toBe(200);
|
|
66
|
+
expect(r.text).toContain("CommHub MCP Server");
|
|
67
|
+
});
|
package/src/server.ts
CHANGED
|
@@ -5569,6 +5569,18 @@ return Bun.serve({
|
|
|
5569
5569
|
return withCors(req, Response.json({ ok: true, completions: rows }));
|
|
5570
5570
|
}
|
|
5571
5571
|
|
|
5572
|
+
// #2946 —— 未匹配的 /api/* 必须 404,而不是 200 + 帮助页。
|
|
5573
|
+
// 现场:带 token 打 /api/lang/status(不存在)→ 200 + 652B 帮助文本,
|
|
5574
|
+
// 四个不同的假路径返回完全相同的字节 ⇒ 探针把「端点改名/拼错」读成「正常」。
|
|
5575
|
+
// 这与 #2559(401 被伪装成空数据)同类:错误被伪装成成功。
|
|
5576
|
+
// 根路径 / 仍然返回帮助页(那是给人/curl 看的入口说明)。
|
|
5577
|
+
if (url.pathname.startsWith("/api/")) {
|
|
5578
|
+
return withCors(req, Response.json(
|
|
5579
|
+
{ ok: false, error: "not_found", path: url.pathname },
|
|
5580
|
+
{ status: 404 },
|
|
5581
|
+
));
|
|
5582
|
+
}
|
|
5583
|
+
|
|
5572
5584
|
return withCors(req, new Response(
|
|
5573
5585
|
`CommHub MCP Server v${SERVER_VERSION} (Streamable HTTP + SSE Push)
|
|
5574
5586
|
|