Public: anyone can read this; the crew can change it.
Plugin config: Edit opens the file's own text #17774
Merged 15h 51m ago, live in production
sold/leaf-editor-9 → main 0 commits
2 files
changed +47
−6
src/web/pages/games_config.ts +7 −5
| @@ -26,7 +26,8 @@ const OPS: readonly string[] = ["set", "remove", "revert"]; | ||
| 26 | 26 | */ |
| 27 | 27 | interface Editing { |
| 28 | 28 | readonly path: string; |
| 29 | readonly content: string; | |
| 29 | /** The text typed; none when the file was only opened, or is reverted: the box then holds the file's own. */ | |
| 30 | readonly content?: string; | |
| 30 | 31 | readonly op: Op; |
| 31 | 32 | readonly error?: string; |
| 32 | 33 | readonly plan?: ConfigPlan; |
| @@ -116,7 +117,7 @@ function previewBlock(base: string, editing: Editing, plan: ConfigPlan): Html { | ||
| 116 | 117 | <form method="post" action="${base}/config/commit" class="row-fields"> |
| 117 | 118 | <input type="hidden" name="op" value="${editing.op}" /> |
| 118 | 119 | <input type="hidden" name="path" value="${plan.file}" /> |
| 119 | <input type="hidden" name="content" value="${editing.content}" /> | |
| 120 | <input type="hidden" name="content" value="${editing.content ?? ""}" /> | |
| 120 | 121 | <input type="hidden" name="sha" value="${plan.sha}" /> |
| 121 | 122 | ${btn("Commit and restart", { variant: "primary" })} ${btn("Cancel", { variant: "ghost", href: `${base}/config` })} |
| 122 | 123 | </form> |
| @@ -129,7 +130,8 @@ async function planned(ctx: Ctx, box: GameBox, service: string, form: Form) { | ||
| 129 | 130 | const path = field(form, "path").trim(); |
| 130 | 131 | const content = field(form, "content"); |
| 131 | 132 | const plan = op === "revert" ? await planRevertConfig(ctx.app, box, service, path) : await planConfig(ctx.app, box, service, path, op === "remove" ? null : content); |
| 132 | return { op, path, content: op === "revert" && plan.ok ? (plan.value.content ?? "") : content, plan }; | |
| 133 | // A revert is planned again from the history when it is committed: no text is sent for it. | |
| 134 | return { op, path, ...(op === "revert" ? {} : { content }), plan }; | |
| 133 | 135 | } |
| 134 | 136 | |
| 135 | 137 | // GET /games/:box/:service/config: the files, and an editor for `?path=`. |
| @@ -139,7 +141,7 @@ export async function gameConfigPage(ctx: Ctx): Promise<Response> { | ||
| 139 | 141 | if (got instanceof Response) return got; |
| 140 | 142 | if (!SERVICE_NAME.test(service)) return notFoundPage(ctx); |
| 141 | 143 | const path = ctx.url.searchParams.get("path") ?? ""; |
| 142 | return render(ctx, got.box, service, got.role, path === "" ? undefined : { path, content: "", op: "set" }); | |
| 144 | return render(ctx, got.box, service, got.role, path === "" ? undefined : { path, op: "set" }); | |
| 143 | 145 | } |
| 144 | 146 | // POST /games/:box/:service/config/preview: plan a change; nothing is written. |
| 145 | 147 | export async function gameConfigPreview(ctx: Ctx): Promise<Response> { |
| @@ -147,7 +149,7 @@ export async function gameConfigPreview(ctx: Ctx): Promise<Response> { | ||
| 147 | 149 | if (got instanceof Response) return got; |
| 148 | 150 | const p = await planned(ctx, got.box, got.service, got.form); |
| 149 | 151 | const role = "operator" satisfies Role; |
| 150 | return render(ctx, got.box, got.service, role, { path: p.path, content: p.content, op: p.op, ...(p.plan.ok ? { plan: p.plan.value } : { error: p.plan.error }) }); | |
| 152 | return render(ctx, got.box, got.service, role, { path: p.path, ...(p.content === undefined ? {} : { content: p.content }), op: p.op, ...(p.plan.ok ? { plan: p.plan.value } : { error: p.plan.error }) }); | |
| 151 | 153 | } |
| 152 | 154 | // POST /games/:box/:service/config/commit: plan again, and commit only on the commit that was |
| 153 | 155 | // previewed. |
test/game-plugin-config-doors.test.ts +40 −1
| @@ -13,10 +13,12 @@ import { commitToBranch, createRepo } from "../src/actions/repos.ts"; | ||
| 13 | 13 | import { createApp, type App } from "../src/app.ts"; |
| 14 | 14 | import { createToken } from "../src/core/tokens.ts"; |
| 15 | 15 | import { createUser, getUserByName, type User } from "../src/core/users.ts"; |
| 16 | import { getRepo } from "../src/core/repos.ts"; | |
| 16 | 17 | import { readCompose } from "../src/games/compose.ts"; |
| 17 | import { configMounts } from "../src/games/plugin_config.ts"; | |
| 18 | import { configMounts, withConfig } from "../src/games/plugin_config.ts"; | |
| 18 | 19 | import { boxNamed, createGameBox, grant } from "../src/games/store.ts"; |
| 19 | 20 | import { forgeHttp, type ForgeHttp } from "../src/http/server.ts"; |
| 21 | import { escapeHtml } from "../src/web/html.ts"; | |
| 20 | 22 | import { createForgeApp } from "../src/tui/forge.ts"; |
| 21 | 23 | import { gameServiceScreen } from "../src/tui/screens/games.ts"; |
| 22 | 24 | import { headlessTerminal } from "../src/tui/term/terminal.ts"; |
| @@ -47,6 +49,13 @@ const get = (path: string, tok: string) => http.handle(new Request(ORIGIN + path | ||
| 47 | 49 | const post = (path: string, tok: string, form: Record<string, string>, origin = ORIGIN) => |
| 48 | 50 | http.handle(new Request(ORIGIN + path, { method: "POST", headers: { authorization: `Bearer ${tok}`, origin, "content-type": "application/x-www-form-urlencoded" }, body: new URLSearchParams(form) })); |
| 49 | 51 | const BASE = "/games/net/hub/config"; |
| 52 | /** The editor's text box, as the page sends it. */ | |
| 53 | const boxOf = (page: string): string | undefined => /<textarea[^>]*name="content"[^>]*>\n([^<]*)<\/textarea/.exec(page)?.[1]; | |
| 54 | /** The compose file, written as a commit by hand. */ | |
| 55 | const write = async (content: string): Promise<void> => { | |
| 56 | const done = await commitToBranch(app, alice, getRepo(app.db, "alice", "mc-network")!, { message: "by hand", files: [{ path: "docker-compose.yml", content }] }); | |
| 57 | if (!done.ok) throw new Error(JSON.stringify(done.error)); | |
| 58 | }; | |
| 50 | 59 | |
| 51 | 60 | beforeAll(async () => { |
| 52 | 61 | dir = await mkdtemp(join(tmpdir(), "forge-config-doors-")); |
| @@ -97,6 +106,7 @@ describe("the page", () => { | ||
| 97 | 106 | expect(listed).not.toContain("<script>alert(1)"); |
| 98 | 107 | const editor = await (await get(`${BASE}?path=${encodeURIComponent(FILE)}`, aliceTok)).text(); |
| 99 | 108 | expect(editor).toContain("Change a file"); |
| 109 | expect(boxOf(editor)).toBe(escapeHtml(NASTY)); | |
| 100 | 110 | const refused = await (await post(`${BASE}/preview`, aliceTok, { op: "set", path: FILE, content: "url: mysql://bob:hunter2@db/x\n" })).text(); |
| 101 | 111 | expect(refused).toContain("inside a URL"); |
| 102 | 112 | expect(refused).not.toContain("hunter2\n</textarea"); |
| @@ -111,6 +121,35 @@ describe("the page", () => { | ||
| 111 | 121 | expect(configMounts(await compose(), "hub")).toEqual([]); |
| 112 | 122 | }); |
| 113 | 123 | |
| 124 | test("a revert's preview sends no text of its own, and its commit is the earlier text", async () => { | |
| 125 | await write(withConfig(COMPOSE, "hub", FILE, "motd: first\n")); | |
| 126 | await write(withConfig(COMPOSE, "hub", FILE, "motd: second\n")); | |
| 127 | const preview = await (await post(`${BASE}/preview`, aliceTok, { op: "revert", path: FILE })).text(); | |
| 128 | expect(preview).toContain("What this does"); | |
| 129 | expect(preview).toContain('name="content" value=""'); | |
| 130 | expect(boxOf(preview)).toBe("motd: second\n"); | |
| 131 | const sha = /name="sha" value="([0-9a-f]+)"/.exec(preview)![1]!; | |
| 132 | expect((await post(`${BASE}/commit`, aliceTok, { op: "revert", path: FILE, content: "", sha })).status).toBe(303); | |
| 133 | expect(configMounts(await compose(), "hub")[0]).toMatchObject({ file: FILE, content: "motd: first\n" }); | |
| 134 | await write(COMPOSE); | |
| 135 | }); | |
| 136 | ||
| 137 | test("a file with a hidden value opens with the mark in its place, and the mark is never saved", async () => { | |
| 138 | const had = "motd: hi\npassword: hunter2\n"; | |
| 139 | await write(withConfig(COMPOSE, "hub", FILE, had)); | |
| 140 | const opened = boxOf(await (await get(`${BASE}?path=${encodeURIComponent(FILE)}`, aliceTok)).text()); | |
| 141 | expect(opened).toBe("motd: hi\npassword: ‹hidden›\n"); | |
| 142 | const content = opened!.replace("motd: hi", "motd: there"); | |
| 143 | const refused = await (await post(`${BASE}/preview`, aliceTok, { op: "set", path: FILE, content })).text(); | |
| 144 | expect(refused).toContain("still has ‹hidden›"); | |
| 145 | expect(refused).not.toContain("Commit and restart"); | |
| 146 | expect(boxOf(refused)).toBe(content); | |
| 147 | const { sha } = (await readCompose(app, boxNamed(app.db, "net")!))!; | |
| 148 | expect((await post(`${BASE}/commit`, aliceTok, { op: "set", path: FILE, content, sha })).status).toBe(303); | |
| 149 | expect(configMounts(await compose(), "hub")[0]).toMatchObject({ file: FILE, content: had }); | |
| 150 | await write(COMPOSE); | |
| 151 | }); | |
| 152 | ||
| 114 | 153 | test("gated as /games is: no grant is a 404, a viewer reads and may not write, a cross-origin write is refused", async () => { |
| 115 | 154 | expect((await get(BASE, bobTok)).status).toBe(404); |
| 116 | 155 | const box = boxNamed(app.db, "net")!; |
new above
offline