Charles Cao on X (2114238140835844575) asked for a format-preserving
swap: sk_w3axCByTQgK1PxqPhfKTrJOLBqz6sbZv sent as another sk_ key of the
same shape and put back on the way home. With Mask secrets on, a key went
as {{API_KEY_xxxxxxxx}}, which a tool's check of the key's form fails
and a model may remark on.
Settings › Privacy has "Keep a key's form" under Mask secrets (shown only
while secrets are masked; off by default, so nothing changes for anyone
who doesn't turn it on). settings.RedactShape → redact.Options.Shape;
api.go keeps it when a page from before doesn't send it.
How (internal/redact/shape.go):
- Only a secret's kind, 16+ characters, all of [A-Za-z0-9-_.]. Personal
data, the user's words and anything else still go as placeholders. A
made-up key needs no JSON escaping.
- Prefix kept (sk-proj-, sk-ant-api03-, github_pat_, AKIA/ASIA, or up to
4 letters plus - or _), same length, separators in place, and each
other character a random one of the same class. A hex key stays hex
of its case. Random from HMAC(placeholder key, kind+value), so it is
the same every turn and a vendor's prompt cache still matches.
- Kept in values like a placeholder (makeRoom shared with placeholder),
so masking records, Value(), signed-thinking re-masking (the put
closure) and known-value re-masking work unchanged; kindOf reads it.
- restore() puts made-up keys back first, then placeholders.
RestoreJSON no longer returns early when one is in the body;
hasShapes copies nothing while none was made.
- A stream holds an event's end that may start a made-up key
(partialTail → shapeTail), but never one inside a key that has come
whole: a key ending in "s" is also the start of every sk_ key, and
without that its end was held and the key went out unrestored (found
as 1 in ~40 fresh processes; 0 of 150 after).
- apply() leaves a made-up key that came back unrestored alone
(outsideShapes), as it does a placeholder.
Tests: shape_test.go — form kept (the reporter's key, hex, ghp_,
sk-proj-), same fake twice, Restore; off is placeholders; email/short/
odd values not shaped; MaskJSON then RestoreJSON with escaped tool
args; signed thinking re-masked to the same fake; no re-mask of a fake;
a stream split mid-key, after one character, and a key ending as one
starts. GUI: redact-shape.test.cjs (both engines, 5 languages, 1100 and
640px; row under Mask secrets, Off by default, one save with the rest
kept, no scroll, a later save keeps it; no row while secrets are off).
redact-words-any-case now finds Mask personal data by place after the
new row, not by index.
Broken by hand, each red: no restoreShapes (KeepsForm, JSON, Stream),
no shapeTail (Stream), no outsideShapes (JSON), no hasShapes (JSON),
put ignoring Shape (all three), shapeTail without the whole-key bound
(Stream).
Budgets: app.js 1,321,994 → 1,322,289 and the core 1,529,542 →
1,529,837, the row's 295 bytes, Settings' own row, no room added.
Doc: docs/subsystems/gateway-routing.md, a bullet beside the other
masking options.
Run at 4dfd88206 + this change (worktree key-shape):
- GUI (node --test, playwright 1.62.0, both engines): redact-shape
22/22, redact-words-any-case 20/20, i18n-keys 4/4, privacy-model
24/24, redact-kinds 22/22, mask-actions 20/20, masking-records
52/52, provider-proxy 8/8, click-scroll 22/22.
- go test -tags nogui ./internal/gui ./internal/redact
./internal/settings: ok (internal/masking has no tests).
- TestShapeStream in 150 fresh processes: 0 red.
- Rebased on 04439c859: budget tests, internal/redact,
internal/settings ok; gofmt clean; vet -tags nogui on darwin, linux
and windows ok; i18n-keys 4/4.
- Earlier, at load ~40: account-discovery 22 red, red the same on
origin/main defbb1a15, #3497 (ggbdpq has it); redact-rule-edit
webkit zh-TW 420px red once, 3/3 alone, #3407.
- The full suite was not run before this push: it is queued behind
other sessions' suites (one per machine).
Not tried: a real vendor turn with the setting on.
120 lines
6.6 KiB
JavaScript
120 lines
6.6 KiB
JavaScript
// Run with Node's test runner and Playwright on the module path; see README.md.
|
||
// Settings › Privacy's "Keep a key's form" (Charles Cao on X): a row right
|
||
// under Mask secrets while secrets are masked, none while they aren't, Off
|
||
// unless the user turned it on, so a key goes as a placeholder as before.
|
||
// A click on On saves redactShape with the rest of the settings as they
|
||
// were, and moves nothing; a later save from the page keeps it. In every
|
||
// language, Chromium and WebKit, in a wide window and a narrow one.
|
||
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 { reach, watchSettingsReads, settingsDrawn } = require("./reader.cjs");
|
||
|
||
const assets = path.resolve(__dirname, "../assets");
|
||
const words = {
|
||
en: { name: "Keep a key's form", on: "On", off: "Off" },
|
||
zh: { name: "保留密钥格式", on: "开启", off: "关闭" },
|
||
"zh-TW": { name: "保留金鑰格式", on: "開啟", off: "關閉" },
|
||
ja: { name: "キーの形式を保つ", on: "オン", off: "オフ" },
|
||
de: { name: "Schlüsselformat beibehalten", on: "An", off: "Aus" },
|
||
};
|
||
|
||
function serve(lang, saved, redact = true) {
|
||
let settings = { lang, theme: "light", redact, redactPersonal: false, redactWords: ["Nightjar"] };
|
||
const state = { agents: [], profiles: [], settings };
|
||
return async (r) => {
|
||
const url = new URL(r.request().url());
|
||
const json = (data) => r.fulfill({ json: data });
|
||
if (url.pathname === "/boot.js") return r.fulfill({ contentType: "text/javascript", body: `window.bootPrefs = {lang:"${lang}",theme:"light",web:true};` });
|
||
if (url.pathname === "/wails/runtime.js") return r.fulfill({ contentType: "text/javascript", body: "export const Window = {};" });
|
||
if (url.pathname === "/api/state") return json(state);
|
||
if (url.pathname === "/api/settings") {
|
||
if (r.request().method() === "POST") {
|
||
settings = JSON.parse(r.request().postData());
|
||
saved.push(settings);
|
||
}
|
||
return json(settings);
|
||
}
|
||
if (url.pathname === "/api/groups") return json({ models: [], groups: [], pools: [] });
|
||
if (url.pathname === "/api/providers") return json({ providers: [], presets: [], excluded: [], gateway: { running: true, window: true, url: "http://127.0.0.1:3999" } });
|
||
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 r.fulfill({ body: await fs.readFile(file), contentType });
|
||
};
|
||
}
|
||
|
||
async function open(engine, lang, width, saved, redact) {
|
||
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
|
||
const page = await (await browser.newContext({ viewport: { width, height: 1400 } })).newPage();
|
||
page.setDefaultTimeout(5000);
|
||
const errors = [];
|
||
page.on("pageerror", (e) => errors.push(e.message));
|
||
await page.route("**/*", serve(lang, saved, redact));
|
||
await watchSettingsReads(page);
|
||
await page.goto("http://magpie.test/?view=settings&tab=privacy");
|
||
await page.locator("#redactList .row").first().waitFor();
|
||
await settingsDrawn(page);
|
||
return { browser, page, errors };
|
||
}
|
||
|
||
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
|
||
for (const [lang, w] of Object.entries(words)) {
|
||
for (const width of [1100, 640]) {
|
||
test(`${engine} ${lang} ${width}px: a key keeps its form only when turned on`, async (t) => {
|
||
const saved = [];
|
||
const { browser, page, errors } = await open(engine, lang, width, saved, true);
|
||
t.after(() => browser.close());
|
||
const row = page.locator("#redactShapeRow");
|
||
await reach(page, row);
|
||
const m = await row.evaluate((r) => {
|
||
const sub = r.querySelector(".sub"), list = document.querySelector("#redactList").getBoundingClientRect(), b = r.getBoundingClientRect();
|
||
return {
|
||
name: r.querySelector(".name").textContent, sub: sub.textContent,
|
||
on: r.querySelector(".segs .opt.on")?.textContent,
|
||
first: r.previousElementSibling?.querySelector(".name")?.textContent === t("Mask secrets"),
|
||
cut: sub.scrollWidth > sub.clientWidth + 1 || sub.scrollHeight > sub.clientHeight + 1,
|
||
inside: b.left >= list.left - 1 && b.right <= list.right + 1,
|
||
wide: document.documentElement.scrollWidth > document.documentElement.clientWidth,
|
||
};
|
||
});
|
||
assert.equal(m.name, w.name);
|
||
assert.equal(m.on, w.off, "off unless the user turned it on");
|
||
assert(m.first, "right under Mask secrets");
|
||
assert(!m.cut && m.inside && !m.wide, "shown in full, inside the page");
|
||
if (lang !== "en") assert(!m.sub.includes("made-up"), "its words translated");
|
||
|
||
const before = await page.evaluate(() => [scrollX, scrollY, document.querySelector("#view-settings").scrollTop]);
|
||
const b = await row.locator(".segs .opt", { hasText: w.on }).boundingBox();
|
||
await page.mouse.click(b.x + b.width / 2, b.y + b.height / 2);
|
||
await page.waitForFunction(() => document.querySelector("#redactShapeRow .segs .opt.on")?.textContent === t("On"));
|
||
await new Promise((r) => setTimeout(r, 300));
|
||
assert.equal(saved.length, 1, "one save");
|
||
assert.equal(saved[0].redactShape, true);
|
||
assert.equal(saved[0].redact, true);
|
||
assert.deepEqual(saved[0].redactWords, ["Nightjar"]);
|
||
assert.deepEqual(await page.evaluate(() => [scrollX, scrollY, document.querySelector("#view-settings").scrollTop]), before, "the click moved nothing");
|
||
|
||
// another choice on the page keeps it: Mask personal data
|
||
const p = page.locator("#redactShapeRow + .row").locator(".segs .opt", { hasText: w.on });
|
||
await reach(page, p);
|
||
await p.click();
|
||
await page.waitForFunction(() => document.querySelector("#redactShapeRow + .row .segs .opt.on")?.textContent === t("On"));
|
||
await new Promise((r) => setTimeout(r, 300));
|
||
assert.equal(saved.length, 2);
|
||
assert.equal(saved[1].redactPersonal, true);
|
||
assert.equal(saved[1].redactShape, true, "a later save keeps it");
|
||
assert.deepEqual(errors, []);
|
||
});
|
||
}
|
||
}
|
||
test(`${engine}: no key's form while secrets aren't masked`, async (t) => {
|
||
const { browser, page, errors } = await open(engine, "en", 1100, [], false);
|
||
t.after(() => browser.close());
|
||
assert.equal(await page.locator("#redactShapeRow").count(), 0);
|
||
assert.deepEqual(errors, []);
|
||
});
|
||
}
|