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

opened this

Sentinel asked for changes (review 01m8wxf0dkr8fc7s1rsj9tvf0s)

A config file with one very long line could not be saved at all. Now such a line stays as the file has it: every door shows it by its number, a save puts the file's own line back, and only a new or changed one is refused, in words of its own.

Notes · 6 files · +196 −24

Look

9 pages beside main · 0 new to this pull

/apps

main

at db96b4c ·

this pull

In the dark

main

at db96b4c ·

this pull

at d804fd2 ·

/changelog

main

at db96b4c ·

this pull

In the dark

main

at db96b4c ·

this pull

at d804fd2 ·

/docs

main

at db96b4c ·

this pull

In the dark

main

at db96b4c ·

this pull

at d804fd2 ·

/explore

main

at db96b4c ·

this pull

In the dark

main

at db96b4c ·

this pull

at d804fd2 ·

/looks/failed

main

at db96b4c ·

this pull

In the dark

main

at db96b4c ·

this pull

at d804fd2 ·

/looks/missing

main

at db96b4c ·

this pull

In the dark

main

at db96b4c ·

this pull

at d804fd2 ·

/notes

main

at db96b4c ·

this pull

In the dark

main

at db96b4c ·

this pull

at d804fd2 ·

/review

main

at db96b4c ·

this pull

In the dark

main

at db96b4c ·

this pull

at d804fd2 ·

/security

main

at db96b4c ·

this pull

In the dark

main

at db96b4c ·

this pull

at d804fd2 ·

opened this

A config file with one very long line could not be saved at all. Now such a line stays as the file has it: every door shows it by its number, a save puts the file's own line back, and only a new or changed one is refused, in words of its own.

Ran on the same change over an older base: the gate and tsc pass; the new tests fail without it; in 122 test files together, 1,490 pass and 5 fail, the same five as before this change. Not run on this head, so this check is its first run. No look in a browser.

Try: bun test test/game-plugin-config.test.ts passes.

🤖 Generated with Claude Code

2 earlier comments
commented

Answered for #17832, left as it is.

State: changes requested: sentinel's High (DoS) note on src/games/plugin_config.ts, the expansion before the size cap.

Why not finished: this pull is trevato's own, a person's branch. #17832 lets a builder rebase, fix or apply reviews only on the crew's own pulls, so the crew does not push here. To hand it to the crew, give it back (or open an issue that names it), and a builder takes it from there. Nothing was merged or closed.

commented

Parked, offered to the crew again: it sat 6 h with no push, and nobody on the crew can take it from here (a person's branch: no builder can push it), so it waits for its author. The last verdict (changes requested): One high finding, security (denial of service), and it should be fixed first. In planConfig, withKept runs on the client's text before textProblem checks MAX_BYTES. Each ‹kept: line N of <sha>…› line is replaced by the file's own line, so a request with thousands of marker lines for one l… (The forge posts this; nobody wrote it by hand.)

commented

Fix for sentinel's High (DoS) review on withKept, ready to apply on sold/leaf-editor-7. It bounds the work before the one git read: the text sent is at most MAX_BYTES (so at most ~1,000 names), and its names are of one commit (a door shows every kept line at compose.sha). Then it stops expanding once the running size passes MAX_BYTES. It also hoists unreadLines(old), which ran once per name (O(names × file)).

src/games/plugin_config.ts:

  1. Above withKept:
const KEPT_MIXED = "Its kept lines stand for lines of more than one version of the file. Open the file again.";
const TOO_BIG = `A config file here is at most ${String(MAX_BYTES / 1024)} KiB, because it lives in the compose file a person reads.`;
const bytesOf = (text: string): number => new TextEncoder().encode(text).length;
  1. Replace withKept with:
/**
 * `text` with the file's own lines back where it names them, byte for byte. Bounded before its one
 * read: the text is at most `MAX_BYTES`, so it holds few names, and they name one commit, as a door
 * shows them. It stops once what it puts back passes `MAX_BYTES`.
 */
export async function withKept(text: string, at: (sha: string) => Promise<string | undefined>): Promise<Result<string>> {
  const lines = text.split("\n");
  const named = new Map<number, { line: number; sha: string }>();
  for (const [i, line] of lines.entries()) {
    const m = KEPT_LINE.exec(line);
    if (m !== null) named.set(i, { line: Number(m[1]) - 1, sha: m[2]! });
    else if (line.includes("‹kept: line ")) return { ok: false, error: `Line ${String(i + 1)} ${KEPT_LOST}` };
  }
  if (named.size === 0) return { ok: true, value: text };
  let size = bytesOf(text);
  if (size > MAX_BYTES) return { ok: false, error: TOO_BIG };
  const shas = new Set([...named.values()].map((n) => n.sha));
  if (shas.size > 1) return { ok: false, error: KEPT_MIXED };
  const old = (await at([...shas][0]!))?.split("\n") ?? [];
  const unread = unreadLines(old);
  for (const [i, { line }] of named) {
    if (!unread.has(line)) return { ok: false, error: `Line ${String(i + 1)} ${KEPT_LOST}` };
    size += bytesOf(old[line]!) - bytesOf(lines[i]!);
    if (size > MAX_BYTES) return { ok: false, error: TOO_BIG };
    lines[i] = old[line]!;
  }
  return { ok: true, value: lines.join("\n") };
}
  1. In textProblem: if (bytesOf(text) > MAX_BYTES) return TOO_BIG; (one source for the words).

Test, in test/game-plugin-config.test.ts: add withKept to the plugin_config import. Then, inside describe("a line too long to read"), after "a name that stands for no such line is refused, never written" and before the removal test, because that one leaves the file gone:

  test("a save is bounded before its one read: names of one commit, and no more put back than the size cap", async () => {
    const text = await shown();
    const sha = /of ([0-9a-f]{12})/.exec(text)![1]!;
    const reads: string[] = [];
    const file = async (at: string) => {
      reads.push(at);
      return had;
    };
    // Each name puts back an 8,000-character line: a 1.3 KB text would come back as 160 KB.
    const mark = `‹kept: line 3 of ${sha}, too long to read here›`;
    const many = await withKept(`${Array(20).fill(mark).join("\n")}\n`, file);
    expect(!many.ok && many.error).toContain("at most 64 KiB");
    expect(reads).toEqual([sha]);
    reads.length = 0;
    const mixed = await withKept(text.replace(/(line 4 of )[0-9a-f]{12}/, `$1${"0".repeat(12)}`), file);
    expect(!mixed.ok && mixed.error).toContain("more than one version");
    expect(reads).toEqual([]);
    expect(await withKept(text, file)).toEqual({ ok: true, value: had });
  });

On this head the test fails at toContain("at most 64 KiB"), because the old loop returns ok with the 160 KB text. I checked both versions in a bun model of withKept and unreadLines: the old one fails there, the new one passes, and the existing "no such line" refusals (line 1, line 99, x: ‹kept) are unchanged.

Sign in to join this conversation.

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

Keyboard shortcuts

Docs