What changed: lc runs magpie in Docker on a public host and asked that
only some addresses reach it at all. With a list set, a request from an
address outside it is refused 403 ("magpie answers only the addresses on
its allow list ..., and this request came from <addr>: add it there to
let it in") before its gateway key is looked at, so a leaked key is no use from
elsewhere. No list is as before.
- internal/settings/allowips.go: SplitAllowIPs, ParseAllowIP (an IP or a
CIDR range, v4 or v6; zones dropped, v4-mapped unmapped), CleanAllowIPs.
Settings gets AllowIPs (allowIPs).
- internal/gateway/allow.go: MAGPIE_ALLOW_IPS (comma or space separated)
sets the list in place of Settings'. The address judged is the
connection's own (r.RemoteAddr); X-Forwarded-For and the rest are never
believed (magpie has no trusted-proxy list; MAGPIE_TRUST_PROXY never
reads forwarding headers, and this follows it). Loopback and this
computer's own interface addresses always pass. An entry that doesn't
read lets nothing in and is logged once, so a typo never opens magpie
to every address. lanGuard checks it after the OAuth callback and
before the rebound-Host and key checks; `magpie web` is wrapped with
AllowGuard too.
- GUI: Settings > Network shows an "Allowed addresses" row under Share on local
network when sharing is on, the page is open from another computer, or
the env var is set (then read-only, saying so). POST
/api/settings/allowips saves it, refuses it while the env var is set,
refuses an entry that doesn't read, and refuses a list that would shut
out the address the page itself is open from (KeepsOut). The generic
settings save keeps AllowIPs. Strings in zh, zh-TW, ja and de.
- Docs: the Docker page (en, zh, ja) lists MAGPIE_ALLOW_IPS with a note
that a request from the host itself arrives from the bridge's gateway
(172.17.0.1), that Docker Desktop and rootless Docker show clients as
one address of their own, and that behind a proxy every request comes
from the proxy; docs/reference.md and docs/subsystems/gateway-routing.md
describe it. The READMEs list no env vars, so they are unchanged.
Why: a gateway key is the only lock on a public host; an allow list keeps
everyone else from even trying it.
Run (machine load average 75-140 from other sessions, so the large
packages ran in slices of their top-level tests, each slice
-timeout 9m, private GOCACHE, sandbox HOME):
- New tests: internal/gateway/allow_test.go TestAllowList (allowed,
denied, CIDR v4/v6, IPv6 literal, link-local with zone, v4-mapped,
loopback and own address always in, on the list still needs a key,
a typo shuts everything out, env over settings), TestAllowListServed
(a real connection from loopback passes lanGuard under a list naming
nothing of this computer's; a request from elsewhere is 403 naming its
address and MAGPIE_ALLOW_IPS), TestKeepsOut; internal/settings/allowips_test.go
TestCleanAllowIPs; internal/gui/allowips_settings_test.go
TestAllowIPsSetting; internal/gui/tests/allow-ips.test.cjs (14/14 in
chromium and webkit, at a 390px panel, en and zh, keys in zh, zh-TW,
ja and de). Broken by
hand: with lanGuard's check removed TestAllowList and TestAllowListServed
FAIL; with KeepsOut skipped TestAllowIPsSetting FAILs; with renderAllow
not called the GUI test has 5 failures. Restored, all pass.
- internal/gui/settings_keep_test.go gains AllowIPs:
TestSettingsSaveKeepsWhatItDoesNotSend failed without it, as it should
for a new field.
- go test -tags nogui: every package ok. internal/gateway (4 slices),
internal/agent (4), internal/provider (3), internal/gui (2),
internal/plugin (2), the rest whole. GUI Network-settings test files
(11) 98/98 and i18n-keys ok, both engines.
- gofmt -l clean; go vet ./..., GOOS=linux go vet -tags nogui ./...,
GOOS=windows go vet ./... ok; linux and windows builds ok.
- Not tried: a real Docker container on a public host (the bridge note
is from Docker's docs, not checked here); behind a real reverse proxy.
257 lines
14 KiB
JavaScript
257 lines
14 KiB
JavaScript
// Run with Node's test runner and Playwright on the module path; see README.md.
|
||
// Allowed addresses (lc on Discord: magpie in Docker on a public host):
|
||
// Settings › Network, under Share on local network, lists the IPs and CIDR
|
||
// ranges other computers may reach magpie from at all. Shown while magpie
|
||
// is shared or listens on the network, not otherwise. One is added from a
|
||
// field and its Add button (or Enter), saved as the gateway cleaned it,
|
||
// and taken away with its Remove; one that isn't an address, and a list
|
||
// that would shut out the page saving it, are refused in the page's
|
||
// language and nothing is saved. Set by MAGPIE_ALLOW_IPS, the list is
|
||
// shown with no field and no Remove. At a 390px panel every row fits, and
|
||
// no click moves the page. English and Chinese, Chromium and WebKit, the
|
||
// strings in all four languages; no backend, the API is faked here.
|
||
const assert = require("node:assert/strict");
|
||
const fs = require("node:fs/promises");
|
||
const path = require("node:path");
|
||
const { test } = require("node:test");
|
||
const { chromium, webkit } = require("playwright");
|
||
const { watchSettingsReads, settingsDrawn } = require("./reader.cjs");
|
||
|
||
const assets = path.resolve(__dirname, "../assets");
|
||
|
||
function settingsPayload(over) {
|
||
return {
|
||
theme: "light", lang: "en", tray: "panel", quotaLeft: false, currency: "usd",
|
||
dock: false, dockWindow: false, proxy: "", redact: false, redactPersonal: false, redactWords: [],
|
||
codexWarmup: "", claudeWarmup: "", codexWarmAt: "", claudeWarmAt: "", workbuddyCheckin: false, noStats: false,
|
||
trayUsage: "", trayUsageEvery: 3, vision: "", imageGen: "",
|
||
version: "0.1.400", dir: "/config/magpie", gateway: "http://127.0.0.1:3425",
|
||
proxyNow: "none", proxySource: "none", login: false,
|
||
visionModels: [], imageGenModels: [], workbuddyCheckins: [],
|
||
lan: true, lanURLs: ["http://192.168.1.20:3425"],
|
||
fx: { rate: 7.2, at: new Date().toISOString(), stale: false },
|
||
...over,
|
||
};
|
||
}
|
||
|
||
// what the gateway does with a list (internal/settings/allowips.go and
|
||
// gui/api.go), for the few forms this test writes
|
||
function clean(list) {
|
||
const out = [];
|
||
for (const raw of list) {
|
||
let e = raw.trim();
|
||
if (e === "10.1.2.3/8") e = "10.0.0.0/8";
|
||
if (!/^[0-9a-f:.]+(\/\d+)?$/i.test(e)) return { error: `"${raw}" is not an IP address or range: write it as 203.0.113.7, 10.0.0.0/8 or 2001:db8::/32` };
|
||
if (!out.includes(e)) out.push(e);
|
||
}
|
||
if (out.length && out.every((e) => e.startsWith("172.16."))) return { error: "that allow list would shut out 203.0.113.50, the address this page is open from: add it first" };
|
||
return { ips: out };
|
||
}
|
||
|
||
function server(lang, posts, over) {
|
||
const s = settingsPayload({ lang, allowIPs: [], ...over });
|
||
return async (route) => {
|
||
const url = new URL(route.request().url());
|
||
const json = (data, status = 200) => route.fulfill({ json: data, status });
|
||
if (url.pathname === "/boot.js") return route.fulfill({ contentType: "text/javascript", body: `window.bootPrefs = {lang:"${lang}",theme:"light"};` });
|
||
if (url.pathname === "/wails/runtime.js") return route.fulfill({ contentType: "text/javascript", body: "export const Window = {};" });
|
||
if (url.pathname === "/api/state") return json({ agents: [], profiles: [], settings: { lang, theme: "light" }, fx: s.fx });
|
||
if (url.pathname === "/api/settings") return json(s);
|
||
if (url.pathname === "/api/settings/allowips") {
|
||
const body = route.request().postDataJSON();
|
||
posts.push(body.ips);
|
||
if (s.allowIPsEnv) return json({ error: "MAGPIE_ALLOW_IPS sets the allow list; change it there" }, 400);
|
||
const c = clean(body.ips);
|
||
if (c.error) return json({ error: c.error }, 400);
|
||
s.allowIPs = c.ips;
|
||
return json(s);
|
||
}
|
||
if (url.pathname === "/api/plugins") return json({ plugins: [] });
|
||
if (url.pathname === "/api/usage/quotas") return json([]);
|
||
if (url.pathname === "/api/groups") return json({ groups: [], models: [] });
|
||
if (url.pathname.startsWith("/api/")) return json({});
|
||
const file = path.join(assets, url.pathname === "/" ? "index.html" : url.pathname);
|
||
const contentType = { ".html": "text/html", ".js": "text/javascript", ".css": "text/css", ".svg": "image/svg+xml", ".png": "image/png" }[path.extname(file)];
|
||
await route.fulfill({ body: await fs.readFile(file), contentType });
|
||
};
|
||
}
|
||
|
||
const en = {
|
||
name: "Allowed addresses", add: "Add", remove: "Remove", field: "IP address or range",
|
||
sub: "Other computers reach magpie only from these addresses or ranges (CIDR), even with a gateway key; this computer always can. None listed lets every address in",
|
||
envSub: "Set by MAGPIE_ALLOW_IPS. Other computers reach magpie only from these addresses, even with a gateway key; this computer always can",
|
||
notIP: "Write an address as 203.0.113.7, or a range as 10.0.0.0/8 or 2001:db8::/32",
|
||
shutOut: "That list leaves out the address this page is open from: add it first, or this page can’t reach magpie",
|
||
envSet: "MAGPIE_ALLOW_IPS sets this list; change it there",
|
||
};
|
||
const zh = { name: "允许的地址", add: "添加", remove: "移除" };
|
||
|
||
async function open(browser, lang, posts, over) {
|
||
const page = await (await browser.newContext({ viewport: { width: 390, height: 560 }, reducedMotion: "reduce" })).newPage();
|
||
page.setDefaultTimeout(5000);
|
||
const errors = [];
|
||
page.on("pageerror", (e) => errors.push(e.message));
|
||
await page.route("**/*", server(lang, posts, over));
|
||
await watchSettingsReads(page);
|
||
await page.goto("http://127.0.0.1:4999/?view=settings&tab=network");
|
||
await settingsDrawn(page);
|
||
return { page, errors };
|
||
}
|
||
|
||
// every row fits the panel: nothing cut, nothing past the edge
|
||
async function fits(box) {
|
||
const bad = await box.evaluate((b) => {
|
||
const out = [];
|
||
const right = b.getBoundingClientRect().right + 0.5;
|
||
for (const e of b.querySelectorAll(".row, input, button, .name, .sub")) {
|
||
const r = e.getBoundingClientRect();
|
||
if (r.right > right || r.width === 0) out.push(e.className + " " + r.right + ">" + right);
|
||
if (e.matches("button, .name") && e.scrollWidth > e.clientWidth + 1) out.push("cut: " + e.textContent);
|
||
}
|
||
if (document.documentElement.scrollWidth > innerWidth) out.push("page scrolls sideways");
|
||
return out;
|
||
});
|
||
assert.deepEqual(bad, []);
|
||
}
|
||
|
||
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
|
||
test(engine + ": allowed addresses are listed under Share on local network (lc)", async (t) => {
|
||
assert(["chromium", "webkit"].includes(engine), "BROWSER must be chromium or webkit");
|
||
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
|
||
t.after(() => browser.close());
|
||
|
||
await t.test("every language has the strings", async () => {
|
||
const { page } = await open(browser, "en", []);
|
||
for (const lang of ["zh", "zh-TW", "ja", "de"]) {
|
||
const missing = await page.evaluate(([lang, keys]) => keys.filter((k) => {
|
||
const d = I18N[lang];
|
||
return !d || !d[k] || d[k] === k;
|
||
}), [lang, Object.values(en).filter((s) => !["Add", "Remove"].includes(s))]);
|
||
assert.deepEqual(missing, [], lang);
|
||
}
|
||
await page.context().close();
|
||
});
|
||
|
||
await t.test("hidden while magpie answers only this computer", async () => {
|
||
const { page, errors } = await open(browser, "en", [], { lan: false, lanURLs: [] });
|
||
await page.locator("#lanList .row").first().waitFor();
|
||
assert.equal(await page.locator("#allowList .row").count(), 0);
|
||
assert.deepEqual(errors, []);
|
||
await page.context().close();
|
||
});
|
||
|
||
for (const lang of ["en", "zh"]) {
|
||
await t.test(lang, async () => {
|
||
const posts = [];
|
||
const { page, errors } = await open(browser, lang, posts);
|
||
const box = page.locator("#allowList");
|
||
await box.locator(".rule-row").waitFor();
|
||
const tr = (s) => page.evaluate((s) => t(s), s);
|
||
const w = {};
|
||
for (const [k, s] of Object.entries(en)) {
|
||
w[k] = await tr(s);
|
||
if (lang !== "en") assert.notEqual(w[k], s, `"${s}" has no ${lang} translation`);
|
||
if (lang === "zh" && zh[k]) assert.equal(w[k], zh[k]);
|
||
}
|
||
const row = box.locator(".rule-row");
|
||
assert.equal((await row.locator(".name").textContent()).trim(), w.name);
|
||
assert.equal((await row.locator(".sub").textContent()).trim(), w.sub);
|
||
const field = row.locator("input");
|
||
assert.equal(await field.getAttribute("aria-label"), w.field);
|
||
assert.equal(await page.locator("#allowList select").count(), 0);
|
||
await fits(box);
|
||
|
||
const view = page.locator("#view-settings");
|
||
const scroll = () => view.evaluate((v) => new Promise((ok) => requestAnimationFrame(() => requestAnimationFrame(() => ok(v.scrollTop)))));
|
||
await view.hover();
|
||
for (let i = 0; i < 40; i++) {
|
||
const r = await field.boundingBox();
|
||
if (r && r.y > 60 && r.y + r.height < 500 && (await scroll()) > 0) break;
|
||
await page.mouse.wheel(0, 120);
|
||
await page.waitForTimeout(30);
|
||
}
|
||
|
||
// added from the field, two at once: saved as the gateway cleaned them
|
||
await field.fill("203.0.113.50, 10.1.2.3/8");
|
||
let before = await scroll();
|
||
await row.locator("button", { hasText: w.add }).click();
|
||
await box.locator(".allow-ip-row .name", { hasText: "10.0.0.0/8" }).waitFor();
|
||
assert.deepEqual(posts.at(-1), ["203.0.113.50", "10.1.2.3/8"]);
|
||
assert.equal(await row.locator("input").inputValue(), "", "the field is left empty for the next one");
|
||
assert.equal(await scroll(), before, "Add moved the page");
|
||
// and with Enter
|
||
await row.locator("input").fill("2001:db8::/32");
|
||
await row.locator("input").press("Enter");
|
||
await box.locator(".allow-ip-row .name", { hasText: "2001:db8::/32" }).waitFor();
|
||
assert.deepEqual(posts.at(-1), ["203.0.113.50", "10.0.0.0/8", "2001:db8::/32"]);
|
||
await fits(box);
|
||
|
||
// refused, in the page's language, and nothing saved
|
||
await row.locator("input").fill("my-laptop");
|
||
before = await scroll();
|
||
await row.locator("button", { hasText: w.add }).click();
|
||
await page.waitForFunction((s) => document.querySelector("#allowList .rule-row .sub.err")?.textContent.trim() === s, w.notIP);
|
||
assert.equal(await row.locator("input").inputValue(), "my-laptop", "what the user wrote stays, to be corrected");
|
||
assert.equal(await box.locator(".allow-ip-row").count(), 3);
|
||
assert.equal(await scroll(), before, "a refusal moved the page");
|
||
await fits(box);
|
||
|
||
// taken away with its Remove
|
||
before = await scroll();
|
||
await box.locator(".allow-ip-row").filter({ hasText: "10.0.0.0/8" }).locator("button", { hasText: w.remove }).click();
|
||
await page.waitForFunction(() => document.querySelectorAll("#allowList .allow-ip-row").length === 2);
|
||
assert.deepEqual(posts.at(-1), ["203.0.113.50", "2001:db8::/32"]);
|
||
assert.equal(await scroll(), before, "Remove moved the page");
|
||
assert.equal((await row.locator(".sub").textContent()).trim(), w.sub, "a save clears the refusal");
|
||
|
||
const border = await box.locator(".row").first().evaluate((r) => getComputedStyle(r).borderLeftWidth);
|
||
assert(border === "0px" || border === "", "no left border accent: " + border);
|
||
assert.deepEqual(errors, []);
|
||
await page.context().close();
|
||
});
|
||
}
|
||
|
||
await t.test("a list that shuts out this page is refused", async () => {
|
||
const posts = [];
|
||
const { page, errors } = await open(browser, "zh", posts, { allowIPs: ["203.0.113.50"] });
|
||
const box = page.locator("#allowList");
|
||
const row = box.locator(".rule-row");
|
||
await row.waitFor();
|
||
await row.locator("input").fill("172.16.0.0/12");
|
||
await row.locator("button", { hasText: zh.add }).click();
|
||
// only this one: the old address taken away leaves the page out
|
||
await box.locator(".allow-ip-row", { hasText: "172.16.0.0/12" }).waitFor();
|
||
// scrolled with the wheel, as a reader would, until both rows show
|
||
await page.locator("#view-settings").hover();
|
||
for (let i = 0; i < 40; i++) {
|
||
const r = await box.locator(".allow-ip-row").last().boundingBox();
|
||
if (r && r.y > 60 && r.y + r.height < 460) break;
|
||
await page.mouse.wheel(0, 120);
|
||
await page.waitForTimeout(30);
|
||
}
|
||
await box.locator(".allow-ip-row").filter({ hasText: "203.0.113.50" }).locator("button").click();
|
||
const says = await page.evaluate((s) => t(s), en.shutOut);
|
||
await page.waitForFunction((s) => document.querySelector("#allowList .rule-row .sub.err")?.textContent.trim() === s, says);
|
||
assert.equal(await box.locator(".allow-ip-row").count(), 2, "nothing saved");
|
||
assert.deepEqual(errors, []);
|
||
await page.context().close();
|
||
});
|
||
|
||
await t.test("set by MAGPIE_ALLOW_IPS: shown, not edited", async () => {
|
||
const { page, errors } = await open(browser, "en", [], {
|
||
lan: false, lanURLs: [], onNetwork: true,
|
||
allowIPsEnv: "1.2.3.4,10.0.0.0/8", allowIPsFromEnv: ["1.2.3.4", "10.0.0.0/8"], allowIPs: ["9.9.9.9"],
|
||
});
|
||
const box = page.locator("#allowList");
|
||
await box.locator(".rule-row").waitFor();
|
||
assert.equal((await box.locator(".rule-row .sub").textContent()).trim(), en.envSub);
|
||
assert.deepEqual(await box.locator(".allow-ip-row .name").allTextContents(), ["1.2.3.4", "10.0.0.0/8"]);
|
||
assert.equal(await box.locator("input").count(), 0);
|
||
assert.equal(await box.locator("button").count(), 0);
|
||
await fits(box);
|
||
assert.deepEqual(errors, []);
|
||
await page.context().close();
|
||
});
|
||
});
|
||
}
|