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
Merged 16h 14m ago, live in production
sold/leaf-editor-3 → main 0 commits 3 threads · 3 unresolved
4 files
changed +45
−3
docs/features/config-form.md +9
| @@ -100,6 +100,15 @@ forge's config all ask it. | ||
| 100 | 100 | - **An alias under a secret's key** (`token: *t`) keeps the file from the form, |
| 101 | 101 | since its anchor stands under a key that would show it. Edit it as text. As |
| 102 | 102 | 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. | |
| 103 | 112 | |
| 104 | 113 | **Edit as text** masks nothing, on the web or in the terminal. A save that |
| 105 | 114 | 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[] { | ||
| 204 | 204 | }); |
| 205 | 205 | } |
| 206 | 206 | |
| 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 | } | |
| 209 | 231 | |
| 210 | 232 | /** The keys whose values are secrets written out in the text (a placeholder is not one). */ |
| 211 | 233 | export function secretLiterals(text: string): string[] { |
test/config-secrets.test.ts +9 −1
| @@ -57,6 +57,14 @@ describe("a text", () => { | ||
| 57 | 57 | expect(hideSecrets(fine)).toBe(fine); |
| 58 | 58 | expect(secretLiterals(fine)).toEqual([]); |
| 59 | 59 | }); |
| 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"); | |
| 66 | expect(hideSecrets("x: &x shown\nhost: *x\n")).toBe("x: &x shown\nhost: *x\n"); | |
| 67 | }); | |
| 60 | 68 | test("a commented-out value is hidden and is no reason to refuse", () => { |
| 61 | 69 | expect(hideSecrets("# password: hunter2\n")).not.toContain("hunter2"); |
| 62 | 70 | expect(secretLiterals("# password: hunter2\n")).toEqual([]); |
| @@ -64,7 +72,7 @@ describe("a text", () => { | ||
| 64 | 72 | // Each line is so long that a scan that goes back over it could not end before the test's own limit. |
| 65 | 73 | test("a hostile line is read once", () => { |
| 66 | 74 | 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"); | |
| 68 | 76 | for (const l of [`{password:"${'\\"'.repeat(n)}`, `password: ${"!a ".repeat(n)}x`]) expect(hideSecrets(l).length).toBeLessThan(40); |
| 69 | 77 | }); |
| 70 | 78 | }); |
test/game-plugin-config.test.ts +3
| @@ -98,6 +98,9 @@ describe("which files and which secrets", () => { | ||
| 98 | 98 | const got = await planConfig(app, box, "hub", HUB, text); |
| 99 | 99 | expect([text, got.ok]).toEqual([text, false]); |
| 100 | 100 | } |
| 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"); | |
| 101 | 104 | expect((await planConfig(app, box, "hub", HUB, "# password: example\nsecret:\n provider: vault\n")).ok).toBe(true); |
| 102 | 105 | const masked = await planConfig(app, box, "hub", HUB, "token: ‹hidden›\n"); |
| 103 | 106 | expect(!masked.ok && masked.error).toContain("hidden"); |
new above
offline
hideSecretsis shared, so this is not only the plugin config page. Say where the whole-text hiding applies.