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

Plugin config: Edit opens the file's own text #17774

opened this

Merged 15h 51m ago, live in production

sold/leaf-editor-9 → main 0 commits

  • Merged. sold/leaf-editor-9 was merged into main as eaeb462.

Review

Sign in to review this change.
2 files changed +47 −6
src/web/pages/games_config.ts +7 −5
@@ -26,7 +26,8 @@ const OPS: readonly string[] = ["set", "remove", "revert"];
2626 */
2727 interface Editing {
2828 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;
3031 readonly op: Op;
3132 readonly error?: string;
3233 readonly plan?: ConfigPlan;
@@ -116,7 +117,7 @@ function previewBlock(base: string, editing: Editing, plan: ConfigPlan): Html {
116117 <form method="post" action="${base}/config/commit" class="row-fields">
117118 <input type="hidden" name="op" value="${editing.op}" />
118119 <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 ?? ""}" />
120121 <input type="hidden" name="sha" value="${plan.sha}" />
121122 ${btn("Commit and restart", { variant: "primary" })} ${btn("Cancel", { variant: "ghost", href: `${base}/config` })}
122123 </form>
@@ -129,7 +130,8 @@ async function planned(ctx: Ctx, box: GameBox, service: string, form: Form) {
129130 const path = field(form, "path").trim();
130131 const content = field(form, "content");
131132 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 };
133135 }
134136
135137 // GET /games/:box/:service/config: the files, and an editor for `?path=`.
@@ -139,7 +141,7 @@ export async function gameConfigPage(ctx: Ctx): Promise<Response> {
139141 if (got instanceof Response) return got;
140142 if (!SERVICE_NAME.test(service)) return notFoundPage(ctx);
141143 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" });
143145 }
144146 // POST /games/:box/:service/config/preview: plan a change; nothing is written.
145147 export async function gameConfigPreview(ctx: Ctx): Promise<Response> {
@@ -147,7 +149,7 @@ export async function gameConfigPreview(ctx: Ctx): Promise<Response> {
147149 if (got instanceof Response) return got;
148150 const p = await planned(ctx, got.box, got.service, got.form);
149151 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 }) });
151153 }
152154 // POST /games/:box/:service/config/commit: plan again, and commit only on the commit that was
153155 // previewed.
test/game-plugin-config-doors.test.ts +40 −1
@@ -13,10 +13,12 @@ import { commitToBranch, createRepo } from "../src/actions/repos.ts";
1313 import { createApp, type App } from "../src/app.ts";
1414 import { createToken } from "../src/core/tokens.ts";
1515 import { createUser, getUserByName, type User } from "../src/core/users.ts";
16 import { getRepo } from "../src/core/repos.ts";
1617 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";
1819 import { boxNamed, createGameBox, grant } from "../src/games/store.ts";
1920 import { forgeHttp, type ForgeHttp } from "../src/http/server.ts";
21 import { escapeHtml } from "../src/web/html.ts";
2022 import { createForgeApp } from "../src/tui/forge.ts";
2123 import { gameServiceScreen } from "../src/tui/screens/games.ts";
2224 import { headlessTerminal } from "../src/tui/term/terminal.ts";
@@ -47,6 +49,13 @@ const get = (path: string, tok: string) => http.handle(new Request(ORIGIN + path
4749 const post = (path: string, tok: string, form: Record<string, string>, origin = ORIGIN) =>
4850 http.handle(new Request(ORIGIN + path, { method: "POST", headers: { authorization: `Bearer ${tok}`, origin, "content-type": "application/x-www-form-urlencoded" }, body: new URLSearchParams(form) }));
4951 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 };
5059
5160 beforeAll(async () => {
5261 dir = await mkdtemp(join(tmpdir(), "forge-config-doors-"));
@@ -97,6 +106,7 @@ describe("the page", () => {
97106 expect(listed).not.toContain("<script>alert(1)");
98107 const editor = await (await get(`${BASE}?path=${encodeURIComponent(FILE)}`, aliceTok)).text();
99108 expect(editor).toContain("Change a file");
109 expect(boxOf(editor)).toBe(escapeHtml(NASTY));
100110 const refused = await (await post(`${BASE}/preview`, aliceTok, { op: "set", path: FILE, content: "url: mysql://bob:hunter2@db/x\n" })).text();
101111 expect(refused).toContain("inside a URL");
102112 expect(refused).not.toContain("hunter2\n</textarea");
@@ -111,6 +121,35 @@ describe("the page", () => {
111121 expect(configMounts(await compose(), "hub")).toEqual([]);
112122 });
113123
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
114153 test("gated as /games is: no grant is a 404, a viewer reads and may not write, a cross-origin write is refused", async () => {
115154 expect((await get(BASE, bobTok)).status).toBe(404);
116155 const box = boxNamed(app.db, "net")!;

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

Keyboard shortcuts

Docs