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

First-admin recovery for a child forge trusts nothing in the child's own data directory #15052

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

bun src/main.ts --first-admin --child no longer trusts any file in the child's data directory (#15040, finding 9 of #14777). What changed, all in src/core/firstadmincli.ts: Address: the code is printed for the address the parent works out for the child, not the url in the child's own parent.json.

Notes

shipped production is serving it deploy 5b5da4d

opened this

bun src/main.ts --first-admin --child <name> no longer trusts any file in the child's data directory (#15040, finding 9 of #14777).

What changed, all in src/core/first_admin_cli.ts:

  • Address: the code is printed for the address the parent works out for the child, not the url in the child's own parent.json. That address is --url if given, otherwise childPublicUrl(name, …) over the parent's FORGE_APP_URL, then FORGE_URL, then localhost:$PORT. Without --child, a forge's own parent.json still sets its address as before, since that file belongs to the operator.
  • Symlinks: before opening anything, lstat checks forge.db, forge.db-wal and forge.db-shm. If any of them is a symlink or not a regular file, the command refuses ("… is not a plain file: refused, no code was made") and does not open the database. A child could otherwise point one of these files at the parent's database or anywhere else.

Tests (in test/first-admin.test.ts; all three fail without the fix):

  • A spawned child whose parent.json names https://evil.test still gets its link at https://maria--forge.test, and "evil.test" appears nowhere in the output.
  • A child whose forge.db is a symlink to another forge's database is refused, and that database's forge_setting rows don't change.
  • The same check, with only forge.db-wal as the symlink.

Not done: there is still a short gap between the lstat check and the open. Closing it needs an O_NOFOLLOW open, which bun:sqlite doesn't expose. The child would have to swap the file in between, and the operator is right there running the command.

Checks run: bun scripts/gate.ts --fix and bun x tsc --noEmit are clean, and the four first-admin suites pass (52 tests). The 119 files named by scripts/fast.ts gave 1749 passes and 1 failure: "two pulls' migrations land in either order" in test/schema-migrations.test.ts. That test passes on its own (26/26) and doesn't touch this code.

To try: bun test test/first-admin.test.ts test/first-admin-recovery.test.ts.

🤖 Generated with Claude Code

Closes #15040

commented

bun src/main.ts --first-admin --child <name> no longer trusts any file in the child's data directory (#15040, finding 9 of #14777).

What changed, all in src/core/first_admin_cli.ts:

  • Address: the code is printed for the address the parent works out for the child, not the url in the child's own parent.json. That address is --url if given, otherwise childPublicUrl(name, …) over the parent's FORGE_APP_URL, then FORGE_URL, then localhost:$PORT. Without --child, a forge's own parent.json still sets its address as before, since that file belongs to the operator.
  • Symlinks: before opening anything, lstat checks forge.db, forge.db-wal and forge.db-shm. If any of them is a symlink or not a regular file, the command refuses ("… is not a plain file: refused, no code was made") and does not open the database. A child could otherwise point one of these files at the parent's database or anywhere else.

Tests (in test/first-admin.test.ts; all three fail without the fix):

  • A spawned child whose parent.json names https://evil.test still gets its link at https://maria--forge.test, and "evil.test" appears nowhere in the output.
  • A child whose forge.db is a symlink to another forge's database is refused, and that database's forge_setting rows don't change.
  • The same check, with only forge.db-wal as the symlink.

Not done: there is still a short gap between the lstat check and the open. Closing it needs an O_NOFOLLOW open, which bun:sqlite doesn't expose. The child would have to swap the file in between, and the operator is right there running the command.

Checks run: bun scripts/gate.ts --fix and bun x tsc --noEmit are clean, and the four first-admin suites pass (52 tests). The 119 files named by scripts/fast.ts gave 1749 passes and 1 failure: "two pulls' migrations land in either order" in test/schema-migrations.test.ts. That test passes on its own (26/26) and doesn't touch this code.

To try: bun test test/first-admin.test.ts test/first-admin-recovery.test.ts.

🤖 Generated with Claude Code

Approved

Approved. The child's parent.json no longer picks the address (childPublicUrl over the parent's own env), and forge.db, -wal and -shm are lstat-checked and refused if linked or not regular. Both come with tests that fail on main, and the change is small and in one module. This is a CLI-only change, so there is no page or preview behaviour to judge. Next, I would: (1) open the database through a path that cannot be swapped between lstat and open, or at least re-lstat after opening, to close the TOCTOU gap you already named; (2) check that the child directory itself (children/<bodyId>) and any parent component of it is not a link, and add a line to the --first-admin how-to in docs/features saying that linked child databases are refused.

Sign in to join this conversation.

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs