Public: anyone can read this; the crew can change it.
Plugin config: a line too long to read stays as the file has it #17782
Sentinel asked for changes (review 01m8wxf0dkr8fc7s1rsj9tvf0s)
sold/leaf-editor-7 → main 1 commit 1 thread · 1 unresolved
6 files
changed +196
−24
docs/features/game-plugin-config.md +1
| @@ -15,5 +15,6 @@ so a change is a commit, previewed first and undone by a revert (#17699). | ||
| 15 | 15 | - **Why a commit should restart the server, and what is not yet proven:** compose does not recreate a container whose `content:` changed (measured for the sleepers' AutoStopper config, `src/games/sleep.ts`) and does recreate one whose environment changed (the same measurement). A changed label is expected to recreate it too, since compose hashes the whole service, so each commit also sets the service's `forge.config` label to a hash of its files. **That the label does it, that the image reads an inline `content:` mount at start and that `SYNC_SKIP_NEWER_IN_DESTINATION=false` makes it replace the server's copy have not been tried on a real container.** The plan and the page therefore say "should restart", and what a person sees after the first commit on a box is the test: if the file is not in `/data/plugins` after the next check-in, `power` `recreate` on the service does it by hand, and the how-to is to be corrected. |
| 16 | 16 | - **Compose reads `${…}` in `content:`:** the forge writes a literal `$` as `$$`, and reads it back as `$`. |
| 17 | 17 | - **Secrets:** a key that names one (`api-key`, `dbPassword`), a file that is one (`forwarding.secret`, a private key block) and a value written out are refused, and so is a password inside a URL under any key (`mysql://user:pass@host/db`, `jdbc:…;password=…`, `?token=…`); the page and the plan name the line, never the value. Write `${CFG_NAME}` in the file instead: the images fill it from the container's environment while copying (yml, json, toml, properties), and the value belongs in the box's sealed `.env`. |
| 18 | - **A line too long to read** (over 4,000 characters, and what is indented under it) is not looked at for a secret. A file that has one is still edited: every door shows `‹kept: line 12 of <commit>, too long to read here›` in its place, and a save puts the file's own line back, byte for byte. Adding or changing such a line is refused, in words of its own. A revert may bring one back. | |
| 18 | 19 | |
| 19 | 20 | What the box holds is not read back: the agent sends no config files, so the page cannot say a server's copy differs from the repository's. That needs the agent to send `files: [{ service, path, sha256, text? }]` in each sync, and is a pull of its own. |
src/core/secret_text.ts +22 −2
| @@ -109,6 +109,22 @@ function inlineEnd(line: string, from: number): number { | ||
| 109 | 109 | /** Told each secret in the clear, a text at a time: its key and the value as written. */ |
| 110 | 110 | type Found = (key: string, value: string) => void; |
| 111 | 111 | |
| 112 | /** The lines (from 0) the scan does not read: each over `MAX_LINE`, and what stands under it, indented more. */ | |
| 113 | export function unreadLines(lines: readonly string[]): Set<number> { | |
| 114 | const out = new Set<number>(); | |
| 115 | let under: number | undefined; | |
| 116 | lines.forEach((line, at) => { | |
| 117 | if (line.length > MAX_LINE) under = indentOf(line); | |
| 118 | else if (under === undefined || line.trim() === "") return; | |
| 119 | else if (indentOf(line) <= under) { | |
| 120 | under = undefined; | |
| 121 | return; | |
| 122 | } | |
| 123 | out.add(at); | |
| 124 | }); | |
| 125 | return out; | |
| 126 | } | |
| 127 | ||
| 112 | 128 | /** Where a `value:` key stands in its line, and the secret's name beside it (`name: DB_PASSWORD`). */ |
| 113 | 129 | type Named = ReadonlyMap<number, string>; |
| 114 | 130 | const NONE: Named = new Map(); |
| @@ -233,11 +249,11 @@ function scan(text: string, found: Found, placeholders: boolean): string[] { | ||
| 233 | 249 | let pem = false; |
| 234 | 250 | const all = text.split("\n"); |
| 235 | 251 | const pairs = namedValues(all); |
| 252 | const unread = unreadLines(all); | |
| 236 | 253 | return all.map((line, at) => { |
| 237 | 254 | if (line.length > MAX_LINE) { |
| 238 | 255 | found(TOO_LONG, line); |
| 239 | // What it may have opened goes on below it, indented more. | |
| 240 | open = { key: TOO_LONG, indent: indentOf(line), until: "indent", whole: true }; | |
| 256 | open = undefined; | |
| 241 | 257 | return MASK; |
| 242 | 258 | } |
| 243 | 259 | const named = pairs.get(at) ?? NONE; |
| @@ -246,6 +262,10 @@ function scan(text: string, found: Found, placeholders: boolean): string[] { | ||
| 246 | 262 | pem = !/-----END [A-Z ]*PRIVATE KEY-----/.test(line); |
| 247 | 263 | return `${line.slice(0, indentOf(line))}${MASK}`; |
| 248 | 264 | } |
| 265 | if (unread.has(at)) { | |
| 266 | found(TOO_LONG, line.trim()); | |
| 267 | return `${line.slice(0, indentOf(line))}${/^\s*-\s/.test(line) ? "- " : ""}${MASK}`; | |
| 268 | } | |
| 249 | 269 | if (open !== undefined) { |
| 250 | 270 | const o = open; |
| 251 | 271 | const hide = (value = line.trim()): string => { |
src/games/plugin_config.ts +85 −17
| @@ -32,7 +32,7 @@ | ||
| 32 | 32 | */ |
| 33 | 33 | |
| 34 | 34 | import type { App } from "../app.ts"; |
| 35 | import { hideSecrets, MASK, MAX_LINE, maskUrl, PRIVATE_BLOCK, secretKey, secretLiterals } from "../core/secret_text.ts"; | |
| 35 | import { hideSecrets, MASK, MAX_LINE, maskUrl, PRIVATE_BLOCK, secretKey, secretLiterals, TOO_LONG, unreadLines } from "../core/secret_text.ts"; | |
| 36 | 36 | import type { User } from "../core/users.ts"; |
| 37 | 37 | import { lineDiff, type DiffLine } from "../web/render/json-doc.ts"; |
| 38 | 38 | import { commitCompose, composeHistory, readCompose, readRepoFile, setServiceEnv, setServiceLabels, setServiceListItem, setTopEntry } from "./compose.ts"; |
| @@ -115,21 +115,74 @@ export const secretFile = (file: string, text: string): boolean => secretKey(fil | ||
| 115 | 115 | const WHERE = "the image fills a `${CFG_NAME}` placeholder from the server's environment at start; the value belongs in the box's sealed .env, named in the file and never written in it"; |
| 116 | 116 | |
| 117 | 117 | /** The lines (from 1) that carry a password inside a URL, under any key. */ |
| 118 | const urlSecretLines = (text: string): number[] => text.split("\n").flatMap((l, n) => (maskUrl(l) === l ? [] : [n + 1])); | |
| 118 | const urlSecretLines = (text: string): number[] => text.split("\n").flatMap((l, n) => (l.length > MAX_LINE || maskUrl(l) === l ? [] : [n + 1])); | |
| 119 | 119 | |
| 120 | /** Why `text` may not be committed as `file`; undefined when it may. */ | |
| 121 | function textProblem(file: string, text: string): string | undefined { | |
| 122 | // The image fills only `${CFG_…}`: another placeholder would reach the plugin as written. | |
| 123 | const strict = text.replace(/\$\{(?!CFG_[A-Za-z0-9_]+\})/g, "$.{"); | |
| 120 | /** The image fills only `${CFG_…}`: another placeholder would reach the plugin as written. */ | |
| 121 | const strictly = (text: string): string => text.replace(/\$\{(?!CFG_[A-Za-z0-9_]+\})/g, "$.{"); | |
| 122 | ||
| 123 | /** The first line (from 1) of `text` that is not read for a secret and that `had` does not hold as it is. */ | |
| 124 | function newUnread(text: string, had: string): number | undefined { | |
| 125 | const left = new Map<string, number>(); | |
| 126 | const old = had.split("\n"); | |
| 127 | for (const n of unreadLines(old)) left.set(old[n]!, (left.get(old[n]!) ?? 0) + 1); | |
| 128 | const lines = text.split("\n"); | |
| 129 | for (const n of unreadLines(lines)) { | |
| 130 | const more = left.get(lines[n]!) ?? 0; | |
| 131 | if (more === 0) return n + 1; | |
| 132 | left.set(lines[n]!, more - 1); | |
| 133 | } | |
| 134 | return undefined; | |
| 135 | } | |
| 136 | ||
| 137 | /** | |
| 138 | * What stands in a shown text for a line too long to read: its number in the file at a commit. No | |
| 139 | * hash of it, which would let a guess at a short value be tried. | |
| 140 | */ | |
| 141 | const KEPT_LINE = /^\s*‹kept: line (\d{1,6}) of ([0-9a-f]{12}), too long to read here›\s*$/; | |
| 142 | const KEPT_LOST = "stands for a line too long to read here, and no such line is in this file. Open the file again."; | |
| 143 | ||
| 144 | /** A file's text as every door shows and edits it: secrets hidden, and each unread line named, so a save can put it back. */ | |
| 145 | function shownText(content: string, sha: string): string { | |
| 146 | const lines = content.split("\n"); | |
| 147 | const shown = hideSecrets(content).split("\n"); | |
| 148 | // A text hidden whole has no lines to name. | |
| 149 | if (shown.length !== lines.length) return shown.join("\n"); | |
| 150 | for (const n of unreadLines(lines)) shown[n] = `${/^\s*/.exec(shown[n]!)![0]}‹kept: line ${String(n + 1)} of ${sha.slice(0, 12)}, too long to read here›`; | |
| 151 | return shown.join("\n"); | |
| 152 | } | |
| 153 | ||
| 154 | /** `text` with the file's own lines back where it names them, byte for byte. */ | |
| 155 | async function withKept(text: string, at: (sha: string) => Promise<string | undefined>): Promise<Result<string>> { | |
| 156 | const files = new Map<string, readonly string[]>(); | |
| 157 | const lines = text.split("\n"); | |
| 158 | for (const [i, line] of lines.entries()) { | |
| 159 | const m = KEPT_LINE.exec(line); | |
| 160 | if (m === null) { | |
| 161 | if (line.includes("‹kept: line ")) return { ok: false, error: `Line ${String(i + 1)} ${KEPT_LOST}` }; | |
| 162 | continue; | |
| 163 | } | |
| 164 | if (!files.has(m[2]!)) files.set(m[2]!, (await at(m[2]!))?.split("\n") ?? []); | |
| 165 | const old = files.get(m[2]!)!; | |
| 166 | const n = Number(m[1]) - 1; | |
| 167 | if (!unreadLines(old).has(n)) return { ok: false, error: `Line ${String(i + 1)} ${KEPT_LOST}` }; | |
| 168 | lines[i] = old[n]!; | |
| 169 | } | |
| 170 | return { ok: true, value: lines.join("\n") }; | |
| 171 | } | |
| 172 | ||
| 173 | /** Why `text` may not be committed as `file` over `had`; undefined when it may. */ | |
| 174 | function textProblem(file: string, text: string, had: string): string | undefined { | |
| 175 | const strict = strictly(text); | |
| 124 | 176 | if (text.includes("\0")) return "A config file is text; this one has a NUL byte."; |
| 125 | 177 | if (new TextEncoder().encode(text).length > MAX_BYTES) |
| 126 | 178 | return `A config file here is at most ${String(MAX_BYTES / 1024)} KiB, because it lives in the compose file a person reads.`; |
| 127 | const long = text.split("\n").findIndex((l) => l.length > MAX_LINE); | |
| 128 | if (long >= 0) return `Line ${String(long + 1)} is longer than ${MAX_LINE.toLocaleString("en")} characters, too long to look for a secret in it: break it up.`; | |
| 179 | const long = newUnread(strict, strictly(had)); | |
| 180 | if (long !== undefined) | |
| 181 | return `Line ${String(long)} is new or changed, and it is too long to read here (over ${MAX_LINE.toLocaleString("en")} characters, or under such a line): break it up. One the file has already stays as it is.`; | |
| 129 | 182 | if (text.includes(MASK)) |
| 130 | 183 | return `This text still has ${MASK} where a secret was hidden; committing it would write that over the real value. Put a \${CFG_NAME} placeholder there.`; |
| 131 | 184 | if (secretFile(file, text)) return `${file} is a secret as a whole, and the repository is no place for one: keep it on the box (a secret file is named in git, never held there).`; |
| 132 | const keys = secretLiterals(strict); | |
| 185 | const keys = secretLiterals(strict).filter((k) => k !== TOO_LONG); | |
| 133 | 186 | if (keys.length > 0) |
| 134 | 187 | return `${keys.join(", ")} looks like a secret, and the repository is no place for one. Write \${CFG_${keys[0]!.toUpperCase().replace(/[^A-Z0-9]+/g, "_")}} in the file: ${WHERE}.`; |
| 135 | 188 | const urls = urlSecretLines(strict); |
| @@ -232,7 +285,7 @@ export async function configsOf(app: App, box: GameBox, service: string): Promis | ||
| 232 | 285 | file: m.file, |
| 233 | 286 | plugin: pluginOf(m.file), |
| 234 | 287 | ref: { repo: box.repo, branch: box.branch, path: box.path, at: `configs.${m.name}.content` }, |
| 235 | text: hideSecrets(m.content), | |
| 288 | text: shownText(m.content, compose.sha), | |
| 236 | 289 | })); |
| 237 | 290 | const plugins = [...new Set(files.map((f) => f.plugin))].sort().map((plugin) => ({ plugin, files: files.filter((f) => f.plugin === plugin) })); |
| 238 | 291 | return { ok: true, value: { sha: compose.sha, plugins } }; |
| @@ -254,15 +307,30 @@ export function restartWords(app: App, box: GameBox, service: string): string { | ||
| 254 | 307 | |
| 255 | 308 | /** |
| 256 | 309 | * Preview `content` (null removes the file) as `file` on a service: the diff, the restart, who is |
| 257 | * online, and the refusal when a secret is in it. | |
| 310 | * online, and the refusal when a secret is in it. `reverts`: the text is the file's own, from its | |
| 311 | * history, long lines too. | |
| 258 | 312 | */ |
| 259 | export async function planConfig(app: App, box: GameBox, service: string, file: string, content: string | null): Promise<Result<ConfigPlan>> { | |
| 260 | const text = content === null ? null : content.replaceAll("\r\n", "\n").replace(/\n*$/, "\n"); | |
| 261 | const bad = fileProblem(file) ?? (text === null ? undefined : textProblem(file, text)); | |
| 262 | if (bad !== undefined) return { ok: false, error: bad }; | |
| 313 | export async function planConfig(app: App, box: GameBox, service: string, file: string, content: string | null, reverts = false): Promise<Result<ConfigPlan>> { | |
| 314 | const misnamed = fileProblem(file); | |
| 315 | if (misnamed !== undefined) return { ok: false, error: misnamed }; | |
| 263 | 316 | const compose = await readCompose(app, box); |
| 264 | 317 | if (compose === undefined) return { ok: false, error: `No ${box.path} on ${box.repo}@${box.branch}.` }; |
| 265 | const before = configMounts(compose.text, service).find((m) => m.file === file)?.content; | |
| 318 | const inText = (all: string): string | undefined => configMounts(all, service).find((m) => m.file === file)?.content; | |
| 319 | const before = inText(compose.text); | |
| 320 | let text = content === null ? null : content.replaceAll("\r\n", "\n").replace(/\n*$/, "\n"); | |
| 321 | if (text !== null) { | |
| 322 | if (!reverts) { | |
| 323 | // The commit the text was opened at; a line that has left the file since is refused below. | |
| 324 | const whole = await withKept(text, async (sha) => { | |
| 325 | const all = compose.sha.startsWith(sha) ? compose.text : await readRepoFile(app, box, sha, box.path); | |
| 326 | return all === undefined ? undefined : inText(all); | |
| 327 | }); | |
| 328 | if (!whole.ok) return whole; | |
| 329 | text = whole.value; | |
| 330 | } | |
| 331 | const bad = textProblem(file, text, reverts ? text : (before ?? "")); | |
| 332 | if (bad !== undefined) return { ok: false, error: bad }; | |
| 333 | } | |
| 266 | 334 | if (before === undefined && text === null) return { ok: false, error: `${file} is not in the repository; there is nothing to remove.` }; |
| 267 | 335 | if (before === text) return { ok: false, error: `${file} already says that.` }; |
| 268 | 336 | try { |
| @@ -304,7 +372,7 @@ export async function planRevertConfig(app: App, box: GameBox, service: string, | ||
| 304 | 372 | const old = await readRepoFile(app, box, c, box.path); |
| 305 | 373 | const was = old === undefined ? undefined : configMounts(old, service).find((m) => m.file === file)?.content; |
| 306 | 374 | if (was === now) continue; |
| 307 | const planned = await planConfig(app, box, service, file, was ?? null); | |
| 375 | const planned = await planConfig(app, box, service, file, was ?? null, true); | |
| 308 | 376 | return planned.ok ? { ok: true, value: { ...planned.value, message: `games: ${service} config: revert ${file}` } } : planned; |
| 309 | 377 | } |
| 310 | 378 | return { ok: false, error: `${file} has no earlier version to go back to.` }; |
src/web/render/config-secrets.ts +4 −2
| @@ -5,7 +5,7 @@ | ||
| 5 | 5 | * the clear is refused, and so is a kept one moved under a key that would show it. |
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | import { hiddenValue, hideSecrets, literalSecret, MASK, maskUrl, PRIVATE_BLOCK, secretKey, severalSecrets } from "../../core/secret_text.ts"; | |
| 8 | import { hiddenValue, hideSecrets, literalSecret, MASK, MAX_LINE, maskUrl, PRIVATE_BLOCK, secretKey, severalSecrets } from "../../core/secret_text.ts"; | |
| 9 | 9 | import { pointer, type Json } from "./json-doc.ts"; |
| 10 | 10 | import { isJsonNumber } from "./json-schema.ts"; |
| 11 | 11 | |
| @@ -15,6 +15,7 @@ export const isKept = (v: Json): boolean => v.t === "string" && KEPT.test(v.v); | ||
| 15 | 15 | |
| 16 | 16 | export const KEPT_HINT = "Kept as it is in the file, and not shown. It is in this repository's history: seal it and change it."; |
| 17 | 17 | export const NEW_SECRET = "This looks like a secret, and the repository is no place for one. Write a placeholder such as ${NAME} and seal the value. If it is not a secret, edit the file as text."; |
| 18 | export const NEW_LONG = "This has a line too long to read here, so nothing can look for a secret in it. Break it up, or edit the file as text."; | |
| 18 | 19 | export const KEPT_LOST = "This stands for a value the file no longer has here. Open the file again."; |
| 19 | 20 | export const KEPT_MOVED = "This stands for a secret, and its new key would show it. Put it back under its own key."; |
| 20 | 21 | export const KEPT_CHANGED = "changed since you opened it, and it keeps a secret the form does not hold. Open it again to change it."; |
| @@ -85,7 +86,8 @@ export function keepDoc(doc: Json, original: Json): { doc: Json; errors: Map<str | ||
| 85 | 86 | }); |
| 86 | 87 | const now = secretLeaves(real); |
| 87 | 88 | for (const p of back) if (!now.has(p)) errors.set(p, KEPT_MOVED); |
| 88 | for (const p of secretLeaves(real, true).keys()) if (!back.has(p) && !errors.has(p)) errors.set(p, NEW_SECRET); | |
| 89 | for (const [p, v] of secretLeaves(real, true)) | |
| 90 | if (!back.has(p) && !errors.has(p)) errors.set(p, v.t === "string" && v.v.split("\n").some((l) => l.length > MAX_LINE) ? NEW_LONG : NEW_SECRET); | |
| 89 | 91 | return { doc: real, errors }; |
| 90 | 92 | } |
| 91 | 93 |
test/config-secrets.test.ts +2 −1
| @@ -2,7 +2,7 @@ | ||
| 2 | 2 | |
| 3 | 3 | import { describe, expect, test } from "bun:test"; |
| 4 | 4 | import { addedSecrets, hideSecrets, MAX_LINE, secretKey, secretLiterals } from "../src/core/secret_text.ts"; |
| 5 | import { hideDoc, hideText, isKept, keepDoc, KEPT_MOVED, KEPT_LOST, NEW_SECRET, secretLeaves } from "../src/web/render/config-secrets.ts"; | |
| 5 | import { hideDoc, hideText, isKept, keepDoc, KEPT_MOVED, KEPT_LOST, NEW_LONG, NEW_SECRET, secretLeaves } from "../src/web/render/config-secrets.ts"; | |
| 6 | 6 | import { formRows } from "../src/tui/screens/config_rows.ts"; |
| 7 | 7 | import { configFields } from "../src/web/render/config-form.ts"; |
| 8 | 8 | import { parseConfig } from "../src/web/render/config-format.ts"; |
| @@ -167,6 +167,7 @@ describe("the form", () => { | ||
| 167 | 167 | const at = (p: string, v: string) => [...keepDoc(updateAt(hideDoc(doc), p, () => str(v)), doc).errors]; |
| 168 | 168 | expect(at(PASS, "${RCON_PASSWORD}")).toEqual([]); |
| 169 | 169 | expect(at(PASS, "another-one")).toEqual([[PASS, NEW_SECRET]]); |
| 170 | expect(at(PASS, "x".repeat(MAX_LINE + 1))).toEqual([[PASS, NEW_LONG]]); | |
| 170 | 171 | expect(at("/services/mc/environment/HOST", `‹kept: ${PASS}›`)).toEqual([["/services/mc/environment/HOST", KEPT_MOVED]]); |
| 171 | 172 | expect(at(PASS, "‹kept: /no/where›")).toEqual([[PASS, KEPT_LOST]]); |
| 172 | 173 | expect(at(PASS, "‹kept: /port›")).toEqual([[PASS, KEPT_LOST]]); |
test/game-plugin-config.test.ts +82 −2
| @@ -11,10 +11,11 @@ import { commitToBranch, createRepo } from "../src/actions/repos.ts"; | ||
| 11 | 11 | import { createApp, type App } from "../src/app.ts"; |
| 12 | 12 | import { createToken } from "../src/core/tokens.ts"; |
| 13 | 13 | import { forgeHttp } from "../src/http/server.ts"; |
| 14 | import { getRepo } from "../src/core/repos.ts"; | |
| 14 | 15 | import { createUser, getUserByName, type User } from "../src/core/users.ts"; |
| 15 | 16 | import { readCompose, serviceEnv, serviceLabels } from "../src/games/compose.ts"; |
| 16 | 17 | import { hideSecrets, secretKey, secretLiterals } from "../src/core/secret_text.ts"; |
| 17 | import { commitConfig, configMounts, configsOf, fileProblem, planConfig, planRevertConfig } from "../src/games/plugin_config.ts"; | |
| 18 | import { commitConfig, configMounts, configsOf, fileProblem, planConfig, planRevertConfig, withConfig } from "../src/games/plugin_config.ts"; | |
| 18 | 19 | import { boxNamed, createGameBox, type GameBox } from "../src/games/store.ts"; |
| 19 | 20 | import { quietLogs } from "./helpers/globals.ts"; |
| 20 | 21 | |
| @@ -101,7 +102,8 @@ describe("which files and which secrets", () => { | ||
| 101 | 102 | const pair = await planConfig(app, box, "hub", HUB, "env:\n - value: hunter2\n name: DB_PASSWORD\n"); |
| 102 | 103 | expect(!pair.ok && pair.error).toContain("${CFG_DB_PASSWORD}"); |
| 103 | 104 | const long = await planConfig(app, box, "hub", HUB, `a: 1\nb: ${"x".repeat(4_000)}\n`); |
| 104 | expect(!long.ok && long.error).toContain("Line 2 is longer than 4,000 characters"); | |
| 105 | expect(!long.ok && long.error).toContain("Line 2 is new or changed, and it is too long to read here"); | |
| 106 | expect(!long.ok && long.error).not.toContain("looks like a secret"); | |
| 105 | 107 | const aliased = await planConfig(app, box, "hub", HUB, "x: &x hunter2\ntoken: *x\n"); |
| 106 | 108 | expect(aliased.ok).toBe(false); |
| 107 | 109 | expect(hideSecrets("x: &x hunter2\ntoken: *x\n")).not.toContain("hunter2"); |
| @@ -124,6 +126,84 @@ describe("which files and which secrets", () => { | ||
| 124 | 126 | }); |
| 125 | 127 | }); |
| 126 | 128 | |
| 129 | test("a hidden line deleted by hand is a removal like any other: shown masked, and saved without it", async () => { | |
| 130 | const FILE = "plugins/Old/config.yml"; | |
| 131 | const repo = getRepo(app.db, "alice", "mc-network")!; | |
| 132 | const text = withConfig(await compose(), "hub", FILE, "motd: hi\npassword: hunter2\nend: 1\n"); | |
| 133 | const done = await commitToBranch(app, alice, repo, { message: "old", files: [{ path: "net/docker-compose.yml", content: text }] }); | |
| 134 | expect(done.ok).toBe(true); | |
| 135 | const got = await configsOf(app, box, "hub"); | |
| 136 | const shown = got.ok ? got.value.plugins.flatMap((p) => p.files).find((f) => f.file === FILE)!.text : ""; | |
| 137 | expect(shown).toBe("motd: hi\npassword: ‹hidden›\nend: 1\n"); | |
| 138 | const plan = await save("hub", FILE, shown.replace("password: ‹hidden›\n", "")); | |
| 139 | expect(plan.diff.filter((l) => l.op !== " ").map((l) => `${l.op}${l.text}`)).toEqual(["-password: ‹hidden›"]); | |
| 140 | expect(configMounts(await compose(), "hub").find((m) => m.file === FILE)?.content).toBe("motd: hi\nend: 1\n"); | |
| 141 | await save("hub", FILE, null); | |
| 142 | }); | |
| 143 | ||
| 144 | describe("a line too long to read", () => { | |
| 145 | const FILE = "plugins/Heads/config.yml"; | |
| 146 | const long = `head: "${"QUJD".repeat(2_000)}"`; | |
| 147 | const had = `motd: hi\nheads:\n ${long}\n under: it\nend: 1\n`; | |
| 148 | const shown = async () => { | |
| 149 | const got = await configsOf(app, box, "hub"); | |
| 150 | if (!got.ok) throw new Error(got.error); | |
| 151 | return got.value.plugins.flatMap((p) => p.files).find((f) => f.file === FILE)!.text; | |
| 152 | }; | |
| 153 | const held = async () => configMounts(await compose(), "hub").find((m) => m.file === FILE)?.content; | |
| 154 | beforeAll(async () => { | |
| 155 | // As a file written by hand in the repository: the editor takes no new one. | |
| 156 | const text = withConfig(await compose(), "hub", FILE, had); | |
| 157 | const repo = getRepo(app.db, "alice", "mc-network")!; | |
| 158 | const done = await commitToBranch(app, alice, repo, { message: "heads", files: [{ path: "net/docker-compose.yml", content: text }] }); | |
| 159 | if (!done.ok) throw new Error(JSON.stringify(done.error)); | |
| 160 | }); | |
| 161 | ||
| 162 | test("another line changes, and it stays byte for byte, from the shown text or from its own", async () => { | |
| 163 | const text = await shown(); | |
| 164 | expect(text).not.toContain("QUJD"); | |
| 165 | expect(text).not.toContain("under: it"); | |
| 166 | expect(text).toMatch(/^motd: hi\nheads:\n‹kept: line 3 of [0-9a-f]{12}, too long to read here›\n {4}‹kept: line 4 of [0-9a-f]{12}, too long to read here›\nend: 1\n$/); | |
| 167 | const plan = await save("hub", FILE, text.replace("motd: hi", "motd: there")); | |
| 168 | expect(plan.diff.filter((l) => l.op !== " ").map((l) => `${l.op}${l.text}`)).toEqual(["+motd: there", "-motd: hi"]); | |
| 169 | expect(JSON.stringify(plan.diff)).not.toContain("QUJD"); | |
| 170 | expect(await held()).toBe(had.replace("motd: hi", "motd: there")); | |
| 171 | // The text opened before that commit names the same line still. | |
| 172 | await save("hub", FILE, text.replace("end: 1", "end: 2")); | |
| 173 | expect(await held()).toBe(had.replace("end: 1", "end: 2")); | |
| 174 | await save("hub", FILE, had); | |
| 175 | expect(await held()).toBe(had); | |
| 176 | }); | |
| 177 | ||
| 178 | test("changed by one character, doubled, or with a new line under it, it is refused in words of its own", async () => { | |
| 179 | for (const text of [had.replace("QUJD", "QUJE"), had.replace("end: 1", ` ${long}`), had.replace("under: it", "under: is"), had.replace("end: 1", "end: 1\n" + long)]) { | |
| 180 | const got = await planConfig(app, box, "hub", FILE, text); | |
| 181 | expect(!got.ok && got.error).toContain("too long to read here"); | |
| 182 | expect(!got.ok && got.error).not.toContain("looks like a secret"); | |
| 183 | expect(!got.ok && got.error).not.toContain("QUJ"); | |
| 184 | } | |
| 185 | // The rest of the file is read as any other. | |
| 186 | const secret = await planConfig(app, box, "hub", FILE, had.replace("end: 1", "password: hunter2")); | |
| 187 | expect(!secret.ok && secret.error).toContain("${CFG_PASSWORD}"); | |
| 188 | }); | |
| 189 | ||
| 190 | test("a name that stands for no such line is refused, never written", async () => { | |
| 191 | const text = await shown(); | |
| 192 | for (const bad of [text.replace("line 3", "line 1"), text.replace("line 3", "line 99"), text.replace(/of [0-9a-f]{12}/, `of ${"0".repeat(12)}`), text.replace("‹kept", "x: ‹kept")]) { | |
| 193 | const got = await planConfig(app, box, "hub", FILE, bad); | |
| 194 | expect(!got.ok && got.error).toContain("no such line is in this file"); | |
| 195 | } | |
| 196 | const masked = await planConfig(app, box, "hub", FILE, had.replace(long, "‹hidden›")); | |
| 197 | expect(masked.ok).toBe(false); | |
| 198 | }); | |
| 199 | ||
| 200 | test("a file with such a line is removed, and the revert puts it back", async () => { | |
| 201 | await save("hub", FILE, null); | |
| 202 | const back = await planRevertConfig(app, box, "hub", FILE); | |
| 203 | expect(back.ok && back.value.content).toBe(had); | |
| 204 | }); | |
| 205 | }); | |
| 206 | ||
| 127 | 207 | describe("a change, previewed and committed", () => { |
| 128 | 208 | test("the preview names the diff, the restart and who is online, and changes nothing", async () => { |
| 129 | 209 | const before = await compose(); |
new above
offline
High (DoS): this loop expands every marker line to the file's own line, and fetches a commit per distinct sha, before the size cap in
textProblemruns. Cap the marker count and distinct shas, and stop expanding once the running size passesMAX_BYTES. Do both before the first git read.