Public: anyone can read this; the crew can change it.

A plugin's config text with a secret written as an alias is hidden whole #17745

opened this

Merged 16h 14m ago, live in production

sold/leaf-editor-3 → main 0 commits 3 threads · 3 unresolved

  • Merged. sold/leaf-editor-3 was merged into main as e2a2838.

Review

Sign in to review this change.
4 files changed +45 −3
docs/features/config-form.md +9
@@ -100,6 +100,15 @@ forge's config all ask it.
100100 - **An alias under a secret's key** (`token: *t`) keeps the file from the form,
outdated

hideSecrets is shared, so this is not only the plugin config page. Say where the whole-text hiding applies.

101101 since its anchor stands under a key that would show it. Edit it as text. As
102102 with every file the form cannot hold, the terminal checks no schema on it.
103 - **A text with such an alias is hidden whole** wherever `hideSecrets` draws one:
104 - a plugin's config file, listed on a game server's config page, in the
105 terminal and by the MCP `games` tool (`plugin_config`);
106 - the diff of a change to it, its removal or its revert, in the same three
107 (over MCP `plugin_config_set` and `plugin_config_revert`);
108 - the web form's **Review** diff. Such a file has no form, so this is met only
109 when a secret's text has `*name` in it and the file has `&name`.
110
111 An alias to a `${NAME}` hides its own line only.
103112
104113 **Edit as text** masks nothing, on the web or in the terminal. A save that
105114 adds what looks like a secret to a config file (`addedSecrets`: the text's
src/core/secret_text.ts +24 −2
@@ -204,8 +204,30 @@ function scan(text: string, found: Found, placeholders: boolean): string[] {
204204 });
205205 }
206206
207 /** The text with each secret value replaced by MASK; `placeholders` hides `${NAME}` ones too (for a diff). */
208 export const hideSecrets = (text: string, placeholders = false): string => scan(text, () => {}, placeholders).join("\n");
207 /** The YAML anchors of a text whose value may be one a `*name` elsewhere stands for; a `${NAME}` is none. */
208 function anchors(text: string): Set<string> {
209 const out = new Set<string>();
210 for (const line of text.split("\n"))
211 for (const part of line.split("&").slice(1)) {
212 const m = /^([^\s,[\]{}"'*]+)(.*)$/.exec(part);
213 if (m !== null && !PLACEHOLDER.test(bare(m[2]!))) out.add(m[1]!);
214 }
215 return out;
216 }
217
218 /**
219 * The text with each secret value replaced by MASK; `placeholders` hides `${NAME}` ones too (for a
220 * diff). A secret written as an alias (`token: *x`) has its value under another key, which no line's
221 * own key gives away: such a text is hidden whole.
222 */
223 export function hideSecrets(text: string, placeholders = false): string {
224 const named = anchors(text);
225 let aliased = false;
226 const lines = scan(text, (_key, value) => {
227 if (!aliased && named.size > 0) for (const m of value.matchAll(/\*([^\s,[\]{}"']+)/g)) aliased ||= named.has(m[1]!);
228 }, placeholders);
229 return (aliased ? lines.map((l) => (l.trim() === "" ? l : MASK)) : lines).join("\n");
230 }
209231
210232 /** The keys whose values are secrets written out in the text (a placeholder is not one). */
211233 export function secretLiterals(text: string): string[] {
test/config-secrets.test.ts +9 −1
@@ -57,6 +57,14 @@ describe("a text", () => {
5757 expect(hideSecrets(fine)).toBe(fine);
5858 expect(secretLiterals(fine)).toEqual([]);
5959 });
60 test("a secret written as an alias hides the whole text, its anchor's value too; a placeholder behind one does not", () => {
61 for (const t of ["x: &x hunter2\ntoken: *x\n", "x: &x\n - hunter2\ntokens: *x\n", "x: &x |\n hunter2\ntokens: [*x]\n", "a: { b: &a 1, c: &x hunter2 }\npasswords:\n - *x\n", "x: &x hunter2\ndb: { password: *x }\n"]) {
62 expect(hideSecrets(t)).not.toContain("hunter2");
63 expect(hideSecrets(t).split("\n").length).toBe(t.split("\n").length);
64 }
65 expect(hideSecrets("x: &x ${T}\nhost: h\ntoken: *x\n")).toBe("x: &x ${T}\nhost: h\ntoken: ‹hidden›\n");
outdated

Compare whole outputs, not the first 11 characters, so a regression that masks later lines fails here.

66 expect(hideSecrets("x: &x shown\nhost: *x\n")).toBe("x: &x shown\nhost: *x\n");
67 });
6068 test("a commented-out value is hidden and is no reason to refuse", () => {
6169 expect(hideSecrets("# password: hunter2\n")).not.toContain("hunter2");
6270 expect(secretLiterals("# password: hunter2\n")).toEqual([]);
@@ -64,7 +72,7 @@ describe("a text", () => {
6472 // Each line is so long that a scan that goes back over it could not end before the test's own limit.
6573 test("a hostile line is read once", () => {
6674 const n = 300_000;
67 for (const l of [`{${"a:".repeat(n)}`, "a".repeat(n), `{${"token:[".repeat(n)}`, "&x ".repeat(n), `a://${"b".repeat(n)}`, "a=".repeat(n), `{${"password:{".repeat(n)}`, "[".repeat(n), `[${" ".repeat(n)}x`, "]".repeat(n), `password_${"_".repeat(n)}x`, `x${" ".repeat(n)}y`, "a://b:".repeat(n), "?a".repeat(n), "-----BEGIN ".repeat(n)]) expect(hideSecrets(`${l}\nhost: h`)).toEndWith("\nhost: h");
75 for (const l of [`{${"a:".repeat(n)}`, "a".repeat(n), `{${"token:[".repeat(n)}`, "&x ".repeat(n), `a://${"b".repeat(n)}`, "a=".repeat(n), `{${"password:{".repeat(n)}`, "[".repeat(n), `[${" ".repeat(n)}x`, "]".repeat(n), `password_${"_".repeat(n)}x`, `x${" ".repeat(n)}y`, "a://b:".repeat(n), "?a".repeat(n), "-----BEGIN ".repeat(n), "&".repeat(n), "&a *a ".repeat(n)]) expect(hideSecrets(`${l}\nhost: h`)).toEndWith("\nhost: h");
6876 for (const l of [`{password:"${'\\"'.repeat(n)}`, `password: ${"!a ".repeat(n)}x`]) expect(hideSecrets(l).length).toBeLessThan(40);
6977 });
7078 });
test/game-plugin-config.test.ts +3
@@ -98,6 +98,9 @@ describe("which files and which secrets", () => {
9898 const got = await planConfig(app, box, "hub", HUB, text);
9999 expect([text, got.ok]).toEqual([text, false]);
100100 }
101 const aliased = await planConfig(app, box, "hub", HUB, "x: &x hunter2\ntoken: *x\n");
102 expect(aliased.ok).toBe(false);
103 expect(hideSecrets("x: &x hunter2\ntoken: *x\n")).not.toContain("hunter2");
101104 expect((await planConfig(app, box, "hub", HUB, "# password: example\nsecret:\n provider: vault\n")).ok).toBe(true);
102105 const masked = await planConfig(app, box, "hub", HUB, "token: ‹hidden›\n");
103106 expect(!masked.ok && masked.error).toContain("hidden");

Search
↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs