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.
This commit is contained in:
IceCodeNew
2026-10-31 01:08:33 +08:00
committed by GitHub
parent 28881086c5
commit c38a364e6a
8 changed files with 32 additions and 38 deletions
+9 -9
View File
@@ -256,21 +256,21 @@ not show.
has `connect-src 'none'`, so the page cannot call Mem itself; the package's
small host part serves a loopback page on `127.0.0.1:<port>` and the frame
navigates to it. The **Test connection** button tests the saved options, so
save changes first. The page passes only the way back and the language in
`window.name`; the test page asks for a click, then the service reads the saved
plugin options itself and makes one public health request and one authenticated
Working Memory request. No URL, key or Space travels through the page. It
save changes first. The language travels in the test URL's query; the test
page asks for a click, then the service reads the saved plugin options itself
and makes one public health request and one authenticated Working Memory
request. No URL, key, Space or return address travels through the page. It
writes nothing and shows reachability, the key and the Space with a fixed code
and milliseconds. **Back to settings** returns to the page; the test saves
nothing and changes no form values.
and milliseconds. **Back to settings** uses the frame's history to return to
the plugin page; the test saves nothing and changes no form values.
The helper binds `127.0.0.1`, answers only when `Host` is `127.0.0.1:<port>` or
`localhost:<port>`, and needs the page's per-process token. Its JSON endpoint
accepts `Origin: null` from the sandboxed page and nothing else, and returns no
`access-control-allow-origin: *`; `POST` and the page's `fetch` are therefore
closed to other sites. The back link is accepted only for a loopback
`/_magpie/plugin/` page. Because the `Host` must be loopback, the test works in
the desktop app, not through a remote `magpie web` proxy.
closed to other sites. The test page takes no return address; its Back button
only walks the frame's history. Because the `Host` must be loopback, the test
works in the desktop app, not through a remote `magpie web` proxy.
The test needs the plugin host, which the gateway hook already starts; a
page-only install of an older Magpie version shows the test page failing to
+1 -1
View File
@@ -6,7 +6,7 @@
import { createHash } from "node:crypto";
// Bumped with package.json: Mem's own clients record it on imported Threads.
export const version = "0.1.0";
export const version = "0.1.1";
// The only report codes magpie's Plugins page shows; tools turn these into
// messages, never the service's own text.
+1 -3
View File
@@ -763,9 +763,7 @@ test("configuration page saves the Mem connection for the shipped hook", async (
// The main page's Test button tests the saved connection; the test page
// reads only the way back and the language.
elements["test-saved"].onclick()
expect(location.assigned).toBe("http://127.0.0.1:3479/test")
expect(JSON.parse(window.name)).toEqual({ returnTo: location.href, lang: "en" })
window.name = ""
expect(location.assigned).toBe("http://127.0.0.1:3479/test?lang=en")
expect(options.apiKey).toBeUndefined()
expect(options.spaceId).toBeUndefined()
expect(options.agentId).toBeUndefined()
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@magpie-community/gateway-nowledge-mem",
"version": "0.1.0",
"version": "0.1.1",
"description": "Nowledge Mem recall, Context Bundle and optional conversation capture at the Magpie gateway, for every connected harness",
"type": "module",
"files": [
+5 -4
View File
@@ -1,7 +1,7 @@
// Only the official configuration bridge is used. The connection test is a
// local loopback page the frame navigates to, because the page's sandbox has
// no network (connect-src 'none'); it receives this form's values in
// window.name and never saves them.
// no network (connect-src 'none'). It tests the saved options; the language
// travels in the query and the test page's Back button uses history.back().
const {languageFor, languageTags, translate} = nowledgeMemI18n;
const $ = (id) => document.getElementById(id);
let language = 'en', busy = false, savedOptions = null, pendingCapture = null;
@@ -118,8 +118,9 @@ $("test-saved").onclick = () => {
if (!savedOptions) return;
const url = validUrl(savedOptions.serverUrl || "http://127.0.0.1:14242");
if (!url) { message("configured", "invalidUrl", true); return }
window.name = JSON.stringify({ returnTo: location.href.split("?")[0], lang: language });
location.assign(testService() + "/test");
const target = new URL(testService() + "/test");
target.searchParams.set("lang", language);
location.assign(target.href);
};
$("save").onclick = save;
+5 -5
View File
@@ -5,11 +5,11 @@
import { MemFailure, request, resolveOptions } from "./mem.mjs";
const texts = {
en: { testing: "Testing the connection…", connected: "Connected", failed: "Connection failed", back: "Back to settings", noReturn: "No settings page to return to.", noService: "The local test service did not answer.", unauthorized: "key rejected", unbound: "Space not found", unavailable: "unreachable", invalid: "invalid address", notfound: "Working Memory not supported", empty: "no answer", run: "Test" },
zh: { testing: "正在测试连接…", connected: "连接正常", failed: "连接失败", back: "返回设置", noReturn: "没有可返回的设置页面。", noService: "本地测试服务没有响应。", unauthorized: "密钥被拒绝", unbound: "Space 不存在", unavailable: "无法连接", invalid: "地址无效", notfound: "不支持 Working Memory", empty: "没有返回内容", run: "测试" },
"zh-TW": { testing: "正在測試連線…", connected: "連線正常", failed: "連線失敗", back: "返回設定", noReturn: "沒有可返回的設定頁面。", noService: "本機測試服務沒有回應。", unauthorized: "金鑰被拒絕", unbound: "Space 不存在", unavailable: "無法連線", invalid: "位址無效", notfound: "不支援 Working Memory", empty: "沒有傳回內容", run: "測試" },
ja: { testing: "接続を確認しています…", connected: "接続できました", failed: "接続に失敗しました", back: "設定に戻る", noReturn: "戻る設定ページがありません。", noService: "ローカルのテストサービスが応答しません。", unauthorized: "キーが拒否されました", unbound: "Space が見つかりません", unavailable: "接続できません", invalid: "アドレスが無効です", notfound: "Working Memory 非対応", empty: "応答がありません", run: "テスト" },
de: { testing: "Verbindung wird geprüft…", connected: "Verbunden", failed: "Verbindung fehlgeschlagen", back: "Zurück zu den Einstellungen", noReturn: "Keine Einstellungsseite zum Zurückkehren.", noService: "Der lokale Testdienst antwortet nicht.", unauthorized: "Schlüssel abgelehnt", unbound: "Space nicht gefunden", unavailable: "nicht erreichbar", invalid: "ungültige Adresse", notfound: "Working Memory nicht unterstützt", empty: "keine Antwort", run: "Test" },
en: { testing: "Testing the connection…", connected: "Connected", failed: "Connection failed", back: "Back to settings", noService: "The local test service did not answer.", unauthorized: "key rejected", unbound: "Space not found", unavailable: "unreachable", invalid: "invalid address", notfound: "Working Memory not supported", empty: "no answer", run: "Test" },
zh: { testing: "正在测试连接…", connected: "连接正常", failed: "连接失败", back: "返回设置", noService: "本地测试服务没有响应。", unauthorized: "密钥被拒绝", unbound: "Space 不存在", unavailable: "无法连接", invalid: "地址无效", notfound: "不支持 Working Memory", empty: "没有返回内容", run: "测试" },
"zh-TW": { testing: "正在測試連線…", connected: "連線正常", failed: "連線失敗", back: "返回設定", noService: "本機測試服務沒有回應。", unauthorized: "金鑰被拒絕", unbound: "Space 不存在", unavailable: "無法連線", invalid: "位址無效", notfound: "不支援 Working Memory", empty: "沒有傳回內容", run: "測試" },
ja: { testing: "接続を確認しています…", connected: "接続できました", failed: "接続に失敗しました", back: "設定に戻る", noService: "ローカルのテストサービスが応答しません。", unauthorized: "キーが拒否されました", unbound: "Space が見つかりません", unavailable: "接続できません", invalid: "アドレスが無効です", notfound: "Working Memory 非対応", empty: "応答がありません", run: "テスト" },
de: { testing: "Verbindung wird geprüft…", connected: "Verbunden", failed: "Verbindung fehlgeschlagen", back: "Zurück zu den Einstellungen", noService: "Der lokale Testdienst antwortet nicht.", unauthorized: "Schlüssel abgelehnt", unbound: "Space nicht gefunden", unavailable: "nicht erreichbar", invalid: "ungültige Adresse", notfound: "Working Memory nicht unterstützt", empty: "keine Antwort", run: "Test" },
};
export function textsFor(lang) {
+6 -14
View File
@@ -32,30 +32,22 @@ function page() {
h1 { margin: 0 0 10px; font-size: 17px; }
p { margin: 0 0 14px; font-size: 14px; line-height: 1.5; }
button { font: inherit; font-size: 13px; padding: 5px 12px; margin: 0 0 18px; }
a { display: block; font-size: 13px; color: #0a6cff; }
@media (prefers-color-scheme: dark) { body { color: #f5f5f7; background: #1c1c1e; } a { color: #6ea8ff; } }
@media (prefers-color-scheme: dark) { body { color: #f5f5f7; background: #1c1c1e; } }
</style>
<h1>Nowledge Mem</h1>
<p id="state"></p>
<button id="run" type="button" hidden></button>
<a id="back" hidden></a>
<button id="back" type="button" hidden></button>
<script>
(() => {
const all = ${JSON.stringify(messages)};
let cfg = {};
try { cfg = JSON.parse(window.name || "{}"); } catch {}
window.name = "";
const t = all[cfg.lang] || all.en;
const t = all[new URL(location.href).searchParams.get("lang")] || all.en;
const state = document.getElementById("state");
const run = document.getElementById("run");
const back = document.getElementById("back");
if (typeof cfg.returnTo === "string" && /^https?:\\/\\/(127\\.0\\.0\\.1|localhost):\\d+\\/_magpie\\/plugin\\//u.test(cfg.returnTo)) {
back.href = cfg.returnTo;
back.textContent = t.back;
back.hidden = false;
} else {
state.textContent = t.noReturn;
}
back.textContent = t.back;
back.hidden = false;
back.addEventListener("click", () => history.back());
run.textContent = t.run;
run.hidden = false;
run.addEventListener("click", async () => {
+4 -1
View File
@@ -41,8 +41,11 @@ test("the running service answers 403 to a foreign Host, a foreign Origin and a
})
try {
const own = `127.0.0.1:${free}`
const page = await send("GET", "/test", { host: own })
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)