Files
IceCodeNew c38a364e6a nowledge-mem 0.1.1: return from the connection test page, by IceCodeNew (#537)
The connection test page no longer depends on window.name, which the app's frame navigation drops. The language travels as `/test?lang=xx` and the page always shows Back to settings, which calls history.back() in the frame. The page takes no return address. Host (loopback:<port>), the per-process token and Origin null/self are unchanged in handle(). 0.1.1: package.json and mem.mjs's User-Agent version were bumped together.

Reviewed at 390130262eaaff2bf4bfd16b3483626c0c730d7c (every changed line in packages/nowledge-mem).

Run at that head:
- mote: `HOME=$(mktemp -d) bun test packages/nowledge-mem`: 53 pass, 1 skip (live), 0 fail. `bun scripts/check.mjs`: exit 0.
- Broken by hand: dropping `history.back()` from the test page turned "the running service answers 403 ..." red. Dropping the `lang` query from settings.js turned "configuration page saves the Mem connection ..." red. Both passed again once restored.
- Browser harness: a window frames the real page/settings.html under magpie's plugin-page CSP in an `<iframe sandbox="allow-scripts">` and runs the package's real service. Chromium ran on mote and WebKit on the Linux VM, because WebKit launches time out on mote right now. In both engines: Test connection opened /test?lang=zh, Back read 返回设置, and Back returned the frame to settings.html while the top window stayed at its own URL (it had a pushState entry before). The settings page was usable again afterwards. `lang` set to `"><img onerror>`, `__proto__` and `constructor` injected nothing. `lang` is only used as a key into a fixed table, and the label is set through textContent.

Not tried: the real magpie app (its bridge.js was stubbed) and a real Nowledge Mem server. Nit, not blocking: `?lang=__proto__` or `?lang=constructor` gives empty button labels, because the lookup isn't Object.hasOwn. Only a hand-typed URL can do this.

npm: @magpie-community/gateway-nowledge-mem has never been on npm. Its first version needs an owner (trusted publishing can't create a package), so magpie installs it from dist.
2026-10-31 01:08:33 +08:00

66 lines
3.3 KiB
JavaScript

import { expect, test } from "bun:test"
import { _internal } from "./service.mjs"
const req = (headers) => ({ headers: { get: (name) => headers[name.toLowerCase()] ?? null } })
const port = 3479
test("the test service only answers on a loopback Host", () => {
expect(_internal.hostOK(req({ host: "127.0.0.1:3479" }), port)).toBe(true)
expect(_internal.hostOK(req({ host: "localhost:3479" }), port)).toBe(true)
expect(_internal.hostOK(req({ host: "evil.example:3479" }), port)).toBe(false)
expect(_internal.hostOK(req({ host: "127.0.0.1:1" }), port)).toBe(false)
expect(_internal.hostOK(req({}), port)).toBe(false)
})
test("the test service only takes the sandbox or its own Origin", () => {
expect(_internal.originOK(req({ origin: "null" }), port)).toBe(true)
expect(_internal.originOK(req({ origin: "http://127.0.0.1:3479" }), port)).toBe(true)
expect(_internal.originOK(req({}), port)).toBe(true)
expect(_internal.originOK(req({ origin: "http://evil.example" }), port)).toBe(false)
expect(_internal.originOK(req({ origin: "https://localhost:3479" }), port)).toBe(false)
})
test("the running service answers 403 to a foreign Host, a foreign Origin and a wrong token", async () => {
const { default: service } = await import("./service.mjs")
const { request } = await import("node:http")
// A port the system just handed out; the service needs a fixed one.
const probe = Bun.serve({ hostname: "127.0.0.1", port: 0, fetch: () => new Response() })
const free = probe.port
probe.stop(true)
const realFetch = globalThis.fetch
globalThis.fetch = async () => new Response("{}", { status: 503 })
await service.server(undefined, { port: free, serverUrl: "https://mem.example.com", apiKey: "nmem_x" })
const send = (method, path, headers) => new Promise((resolve, reject) => {
const r = request({ host: "127.0.0.1", port: free, method, path, headers }, (res) => {
let body = ""
res.on("data", (c) => { body += c })
res.on("end", () => resolve({ status: res.statusCode, body }))
})
r.on("error", reject)
r.end()
})
try {
const own = `127.0.0.1:${free}`
const page = await send("GET", "/test?lang=zh", { host: own })
expect(page.status).toBe(200)
expect(page.body).toContain('id="back"')
expect(page.body).toContain("history.back()")
expect(page.body).not.toContain("window.name")
const token = page.body.match(/name="nm-token" content="([0-9a-f]+)"/)[1]
expect((await send("GET", "/test", { host: `evil.example:${free}` })).status).toBe(403)
expect((await send("POST", "/api/test", { host: `evil.example:${free}`, origin: "null", "x-nmem-token": token })).status).toBe(403)
expect((await send("POST", "/api/test", { host: own, origin: "http://evil.example", "x-nmem-token": token })).status).toBe(403)
expect((await send("OPTIONS", "/api/test", { host: own, origin: "http://evil.example" })).status).toBe(403)
expect((await send("POST", "/api/test", { host: own, origin: "null", "x-nmem-token": "0".repeat(64) })).status).toBe(403)
expect((await send("POST", "/api/test", { host: own, origin: "null" })).status).toBe(403)
const ok = await send("POST", "/api/test", { host: own, origin: "null", "x-nmem-token": token })
expect(ok.status).toBe(200)
expect(JSON.parse(ok.body).ok).toBe(false)
} finally {
_internal.stop()
globalThis.fetch = realFetch
}
})