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
Check · Review · Land · Live done — Landed and live in production.
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.
shipped production is serving it deploy 5b5da4d
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
urlin the child's ownparent.json. That address is--urlif given, otherwisechildPublicUrl(name, …)over the parent'sFORGE_APP_URL, thenFORGE_URL, thenlocalhost:$PORT. Without--child, a forge's ownparent.jsonstill sets its address as before, since that file belongs to the operator. - Symlinks: before opening anything,
lstatchecksforge.db,forge.db-walandforge.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.jsonnameshttps://evil.teststill gets its link athttps://maria--forge.test, and "evil.test" appears nowhere in the output. - A child whose
forge.dbis a symlink to another forge's database is refused, and that database'sforge_settingrows don't change. - The same check, with only
forge.db-walas 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. 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.
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:urlin the child's ownparent.json. That address is--urlif given, otherwisechildPublicUrl(name, …)over the parent'sFORGE_APP_URL, thenFORGE_URL, thenlocalhost:$PORT. Without--child, a forge's ownparent.jsonstill sets its address as before, since that file belongs to the operator.lstatchecksforge.db,forge.db-walandforge.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):parent.jsonnameshttps://evil.teststill gets its link athttps://maria--forge.test, and "evil.test" appears nowhere in the output.forge.dbis a symlink to another forge's database is refused, and that database'sforge_settingrows don't change.forge.db-walas the symlink.Not done: there is still a short gap between the
lstatcheck and the open. Closing it needs anO_NOFOLLOWopen, whichbun:sqlitedoesn'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 --fixandbun x tsc --noEmitare clean, and the four first-admin suites pass (52 tests). The 119 files named byscripts/fast.tsgave 1749 passes and 1 failure: "two pulls' migrations land in either order" intest/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