Skip to content

fix: judge a save-script destination another local user can steer - #4584

Open
thymikee wants to merge 45 commits into
mainfrom
fix/save-script-ancestor-redirect
Open

thymikee wants to merge 45 commits into
mainfrom
fix/save-script-ancestor-redirect

Conversation

@thymikee

@thymikee thymikee commented Nov 16, 2026 •

Copy link
Copy Markdown
Member

Closes #4565.

open --save-script=<path> resolved the destination by name. A second local user who could write any directory on the way to it could replace that component, or leave a symbolic link there before the entry was created, and move the recording wherever they chose. Both the local writer and the remote staging path had it.

Node exposes no openat or renameat, so a publisher cannot resolve a destination relative to a verified directory handle, and no re-check closes a race with a local attacker. The window is closed by permissions: a destination is accepted only when no other non-root local user can modify any directory the publish traverses. Every directory the kernel steps into is judged, not only the final real path, since a plain directory under a shared one can hold a link into a private destination. .. is applied to the directory the walk reached, not to the text.

A destination is refused under script_ancestor_untrusted when a traversed directory is owned by neither you nor root, is writable by other or by its group, is where the entry would be created while others may add entries, or holds a link another user could have planted. Missing levels are created one at a time at 0700, and a refused destination creates nothing.

A group-writable directory is refused whoever owns it, with a chmod g-w hint. An earlier shape accepted one whose group looked private to this user; that is dropped. Membership cannot be proven — systemd-userdbd answers memberships /etc/passwd and /etc/group never show, so an unseen user can hold the gid. Under umask 002 a directory you create is group-writable and is refused until you chmod g-w it.

ACLs, network mounts and mounts over a checked directory sit outside the rule: it says no other local user can steer where the recording is written, not that nobody can read it.

Scope. The rule judges a destination a caller names. Destinations the daemon picks for itself keep main's behavior byte for byte; #4649 decides that chain separately.

Size. Production source is +687/−62 over 6 files (+625 net), most of it the host-kit walk. No new flag, command, state name or wire field.

🤖 Generated with Claude Code

@github-actions

github-actions Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Size Report

Metric Base Current Diff
Installed (including dependencies) 6.38 MB 6.39 MB +5.6 kB
Package (unpacked) 6.38 MB 6.39 MB +5.6 kB
Package (download) 1.94 MB 1.94 MB +1.8 kB

Startup median (7 runs, lower is better):

Scenario Base Current Diff
CLI --version 38.5 ms 31.4 ms -7.0 ms
CLI --help 75.6 ms 62.8 ms -12.8 ms

@thymikee

Copy link
Copy Markdown
Member Author

I found two confirmed gaps in the new ancestry rule at 02cfd69, and one docs claim that is wrong. CI is failing on the CodeQL alert described below, and there are no conflicts. The ancestry rule and the docs need to change before this can merge.

The rule trusts a directory from its mode bits and group only. untrustedWritableExposure and othersCreateEntriesIn never compare stats.uid with the current uid or root. Three real inputs pass. A sticky directory owned by another user (a teammate's chmod 1777 team) is exempted, yet its owner can rename or remove any entry. A 0755 directory owned by someone else that holds a component of your path lets that user rename the component and plant a symlink. An attacker-owned directory in sticky /tmp (for example /tmp/recordings, created before your mkdir -p) is silently accepted. The owner can also grant you write through a macOS ACL that mode bits do not show, or chmod it from 0777 to 0755 between the staging write and the second check. In each case the script is published into or under a directory another local user owns, which is the exposure #4565 closes. The rule should be: a component is trusted only when its owner is the current uid or root, and only then do the sticky and private-group rules apply. Please put that in untrustedWritableExposure (return an owner exposure before the sticky exemption) and make othersCreateEntriesIn treat such a directory as one others can create entries in. Host-kit tests that fake stats.uid should prove it, as the test file already does for the group database.

A symlink component can also bypass the rule. When the link's own parent is private, line 230 stats only the final target, and the walk continues up the lexical chain, so the ancestors of the real target are never checked. With ~/rec -> /srv/team/rec and /srv/team at 0775 group team without sticky, a teammate can swap /srv/team/rec for a link to a directory you can write, or for their own directory. Writing to /srv/team/rec/journey.ad directly is refused, but going through the link is not, so the docs sentence that a symbolic link inside a writable directory is refused is false here. The rule should be: every directory the kernel traverses to reach the destination's parent passes the ownership and mode test. Please keep the lexical lstat walk for planted links and also walk fs.realpathSync of the deepest existing ancestor. That also covers /tmp/shared/work/../x/journey.ad, where the lexical normalization hides a planted work link. A test with a private link whose target sits under a 0777 or 0775-shared directory should prove it. The existing test at atomic-file-ancestry.test.ts:192 shows only that a link is followed.

The docs misstate sticky semantics. replay-e2e.md:75 says a sticky team directory is safe, but the directory's owner can still rename and remove entries. Line 74 and remote-proxy.md:111 ("only you and root can write") describe a guarantee the code enforces from mode bits only, and the scriptAncestryFailure hint and the assertPrivateScriptDestination JSDoc repeat it. Once the owner check lands, please say that a sticky directory is accepted only when you or root own it, and that every directory must be owned by you or root. Fix the hint and the JSDoc in the same change.

Not blocking, take or leave: the local writer runs mkdirSync before any ancestry check (session-script-writer.ts:138) while the remote path asserts first, so a refused destination can leave a directory behind; the 'swapped ancestor' writer test (session-script-writer-ancestry.test.ts:109) repeats the world-writable branch and no test drives a swap after the identity is captured on the local route; privateGroupIds reads only /etc/group and /etc/passwd, so an LDAP or sssd private group is refused as shared; remote re-declares the two ancestry reason literals that host-kit owns and statDirectoryChain stats a single path; and atomic-publish-ownership.test.ts asserts source text.

The CodeQL alert on the quadratic /\/+$/ in session-script-destination.ts:420 still applies and fails the check, and this PR adds that file from #4511, so it counts against the PR even though it is outside the #4565 delta. Trimming slashes with new URL(baseUrl) or a loop clears it: #4584 (comment). The Coverage result has no captured failure. The new host-kit win32 and catch branches may trip a patch-coverage gate; the failing job log should show which.

Before merge, the ancestry rule should trust only directories owned by you or root, follow the resolved chain of any symlink, and the docs and hint should match. The CodeQL alert should be cleared too.

@thymikee
thymikee force-pushed the fix/save-script-ancestor-redirect branch from 3f17db5 to 450a915 Compare November 16, 2026 11:25
@thymikee

thymikee commented Nov 16, 2026 •

Copy link
Copy Markdown
Member Author

Thanks for the update. At 450a915 the CodeQL alert on the trailing-slash regex is gone, and all 21 checks pass. Two defects remain in the destination checks, and there are no conflicts. Both are traced from the source.

The new rule no longer judges every directory the path traverses, so atomic-file.ts:278 accepts destinations that the earlier revision refused. The spelled walk skips every component that is not a link, and the realpath walk covers only the final resolved chain. For example, take <root>/shared (0777, not sticky), <root>/shared/mine (0700, mine) and mine/rec as a link to ~/private. The destination <root>/shared/mine/rec/journey.ad passes. Another local user can rename mine and plant their own rec. The same gap exists for a directory reached through an intermediate link, and for a foreign-owned grand-ancestor of a link. This breaks the promise in replay-e2e.md:74 and remote-proxy.md:111, for both the local writer and the remote client. The rule should be that every directory the kernel traverses to reach the parent passes untrustedAncestryExposure. Enumerating them means taking directoryChain(fs.realpathSync(P)) for every existing prefix P of the spelled parent, deduplicating, and judging each entry. The current deepest-realpath walk becomes the last iteration, and assertNoPlantedLink keeps the link-stability rule. Please add a host-kit test for the 0777 shared / 0700 mine / rec link case that expects world_writable at <root>/shared, and a second test where the shared directory is an intermediate link's target. I assumed a same-parent rename needs write access on the parent only. That holds for Linux rename(2); macOS is unconfirmed.

The remote client refuses a path the local writer accepts. session-script-destination.ts:206 runs assertPrivateScriptDestination before the missing parent is created at :207. For open --save-script /tmp/rec/journey.ad with rec absent, the deepest existing directory is /tmp, so the new shared_parent rule fails the version with script_ancestor_untrusted, and every later version repeats it. The local writer creates the directory first (session-script-writer.ts:137), so it accepts the same path. This contradicts replay-e2e.md:73 and ADR 0016, and a remote recording to a fresh directory under /tmp is never delivered. Both routes should apply the rule at the same point relative to creating missing parents. The smallest fix is to drop the pre-mkdir assert at :206, because allocateStaging at :256 already judges after the mkdir and refuses a directory that another user created first. If you prefer to refuse before mkdir on both routes, please say so in the docs, since that reverses the promise that missing parents are created. Please add a remote test for a missing parent under a sticky directory that this user or root owns.

Not blocking, and you can take or leave these: the test at atomic-file-ancestry.test.ts:661 is named 'admits it with root named among its members' but asserts .toBeUndefined(), which means privateGroupId refuses it, so rename it or admit root; file.ts:4 exports othersCreateEntriesIn, privateGroupId, untrustedAncestryExposure, untrustedLinkExposure and four types that nothing outside host-kit uses, so keeping only assertPrivateScriptDestination, publishDirectoryIdentity and publishScriptFileSync would be enough; and the daemon test 'a recording refuses a directory another user made sticky and owns' (session-script-writer-ancestry.test.ts:155) puts the destination directly in the 1777 directory, which the old shared_parent rule already refused, so it could put the destination in a subdirectory and fake only the sticky directory's uid, or be dropped as a duplicate of the host-kit case.

The code-scanning thread on the trailing-slash regex no longer applies, because the regex is gone at this head, so please resolve it: #4584 (comment)

@thymikee

Copy link
Copy Markdown
Member Author

Head 546303c389 addresses both reviews.

Traversed directories. assertPrivateScriptDestination now judges every directory the kernel steps into, not the real path of the deepest existing ancestor alone. Each existing prefix of the spelled parent is expanded link by link, and the directory holding each link target is judged with it. Two cases that passed before now refuse, on the local writer and on the remote route: a plain directory under a 0777 one holding a link into a private destination, and ~/rec -> /srv/team/relay -> ~/chosen. Directories left through .. are judged as before.

Remote ordering. The remote client judged the chain before mkdir, so it refused the ordinary --save-script /tmp/rec/journey.ad shape while the local writer accepted it. The pre-mkdir call is gone; the check runs where the entry is really made, covered by a sticky-directory case.

Groups. privateGroupId now reads EVERY record carrying the numeric gid, not only the one named after the user, so ada:x:1000: beside team:x:1000:grace refuses. A duplicate-GID refusal, a same-gid alias admission, and a second-group primary refusal are covered. The residual risk is stated as reaching both ways: a name-service member can cause unsafe acceptance, not only refusal.

Scope of the claim. No ACL code. ADR 0016, the guide, and the remote page now name macOS and POSIX ACLs, network filesystems and mounts as outside the guarantee, and the unconditional confidentiality line is gone: the file is written 0600, that is not a promise about who can read it.

Hardening. The source-text check for the publisher entry point is deleted; the writer tests drive a real write and expect the typed refusals, so they cover dispatch by behaviour. The @agent-device/host-kit/file barrel keeps admission, identity capture and publication, and drops the six rule internals only its own tests used.

Checks: pnpm typecheck, pnpm lint, pnpm check:layering, pnpm check:production-exports, pnpm check:daemon-wire-compat, pnpm check:doc-links, pnpm check:doc-prose, pnpm check:adr-citations pass; the ancestry suites pass locally (106). Each new test fails on 450a915.

@thymikee

Copy link
Copy Markdown
Member Author

Thanks for the update. At 546303c the link to a last-component target such as rec -> shared/relay is now refused, and the trailing-slash regex is gone. Three ways to steer the destination still pass the rule, so the problem from the earlier review is not fully closed.

First, traversedDirectories judges only realpath(dirname(target)) for a link hop, so a link in the middle of the target path hides its directory. With home/rec -> <root>/shared/relay/sub, shared at 0777 and not sticky, and shared/relay -> home/chosen, the destination home/rec/journey.ad is accepted, and so is ../shared/relay/sub. Any local user who can write shared can swap relay between the check and the rename, on both the local writer and the remote client. This is the #4565 exposure and it contradicts the docs and ADR 0016. The rule should be that every directory where the kernel looks up a name, including the components of every link target at any depth, passes untrustedAncestryExposure. A walk that expands link targets component by component enumerates them; a realpath of the target's dirname does not. Please add a host-kit, daemon and remote test with rec -> shared/relay/sub that expects world_writable at shared.

Second, link stability is asked only by assertNoPlantedLink on the spelled path. Hop links are followed but their owner is never checked. Take ~/rec -> /tmp/relay, where another user created /tmp/relay in sticky /tmp pointing at ~/chosen. The spelled link is in a private home and passes, and /private/tmp is sticky and root-owned, so the foreign link is never asked. Spelling /tmp/relay/journey.ad directly is refused with planted_link, so one route gets two answers. The owner can replace the link after the check and redirect the publish. The rule should be that every link the kernel follows, spelled, hop, or inside a target, passes untrustedLinkExposure against the directory that holds it, judged in the same walk that enumerates directories. Please add a test that fakes the uid of a hop link in a sticky directory, as the existing tests fake ownership. I confirmed this from the code path only, because I could not create a link owned by another uid without root.

Third, resolvedDirectory uses JS fs.realpathSync, which folds .. lexically, while the kernel applies .. to the link's target. With home/work -> priv/x, priv/y at 0777 and home/y at 0700, the destination <root>/home/work/../y/journey.ad judges home/y and is accepted, yet the file is created in priv/y. Spelling priv/y/journey.ad directly is refused with world_writable. Absolute --save-script paths reach the rule unnormalized, so both routes are affected, and this breaks the .. claim in replay-e2e.md and ADR 0016. The rule should be that .. applies to the directory the walk is actually in, never to the spelled string. Please add a test with the home/work/../y shape that expects world_writable at priv/y.

Could one component walker replace assertNoPlantedLink, traversedDirectories, resolvedDirectory, linkTarget and the realpath calls? Start at / with the spelled components on a queue. For each component, judge the current directory with untrustedAncestryExposure, deduplicated. On .., move to parentDirectory(current). Otherwise lstat current/name: judge a link with untrustedLinkExposure against current, count one hop, and put its target components at the front of the queue, resetting to / for an absolute target. A directory becomes current, and an absent entry stops the walk with current as deepest. Then ask othersCreateEntriesIn(deepest). That one loop closes all three by construction and needs no realpath.

The Repo Guards & Compatibility check fails in Run Fallow audit on complexity of traversedDirectories (cognitive 35) and privateGroupId (cognitive 17), both added or rewritten in this delta. All checks passed at the earlier head. The one-walker design would also clear the first. There are no conflicts.

The github-advanced-security thread on the trailing-slash regex no longer applies: parentDirectory now uses endsWith/lastIndexOf, so it can be resolved.

Not blocking, take or leave: the four-line comment at atomic-publish-ownership.test.ts:31 that points to another test file can be deleted.

The shapes above were probed against the head atomic-file.ts on macOS.

Before merge, the two link walks need to become one kernel-lookup walk that judges every looked-up directory and every followed link, with the three regression shapes added and the Fallow audit passing.

@thymikee
thymikee force-pushed the fix/save-script-ancestor-redirect branch from 7041497 to 4964b44 Compare November 16, 2026 13:32
@thymikee

Copy link
Copy Markdown
Member Author

Yes — one walker replaces all five, and it is now the only thing that resolves a destination. Head: 4964b44.

The one loop, and what it cost. walkParent plus six named steps (startWalk, standIn, pathComponents, stepWalk, stepIntoEntry, stepThroughLink) is 92 code lines, 165 with the doc block that states the rules. What it deleted was also 92 code lines: traversedDirectories (36), assertNoPlantedLink (18), resolvedDirectory (10), directoryChain (10), a duplicate lstatIfPresentSync (8), parentDirectory (6) and linkTarget (4). So the walk is line-for-line parity with the four passes it replaced, and no realpath call remains in the file. path.dirname is used where a parent is needed, and .. is applied to state.deepest — the directory physically stood in — never to the spelling.

atomic-file.ts is 778 lines (was 667). The net production delta for this round is +126 lines: +111 here and +15 in the writer, where the two failure wrappers shared an 11-line clone. Almost all of it is the group exception, which went from 33 code lines to 98: /etc/nsswitch.conf is now a condition, the account name is read out of the same passwd database the rule judges rather than from a second host lookup, and the record parsing is split into named steps so neither decision is over the complexity floor. assertNoPlantedLink, traversedDirectories, resolvedDirectory, linkTarget, parentDirectory and directoryChain are gone, and the two oxlint-disable lines with them.

The three shapes, at all three levels. Each is refused at 4964b44 and each fails at 546303c:

  • rec -> shared/relay/sub, shared 0777 not sticky, relay itself a link: world_writable at shared. Host-kit refuses the ground under a link that leads into a shared directory, writer a recording refuses the ground a link target crosses on the way in, remote a remote recording refuses the ground a link target crosses on the way in. refuses the spelled spelling of a link the walk also follows holds the two routes to one answer.
  • ~/rec -> <tmp>/relay -> ~/chosen, relay another uid's file in sticky <tmp>: planted_link at relay. Writer and remote versions named for it, and host-kit refuses a link whose target stands in ground another local user owns. The link's uid is served by a fixture (serveHostOwnership in test-kit, which the sticky-directory test now uses too), so nothing here depends on the account the suite runs under — that also settles the root case you raised.
  • home/work/../y with home/work -> priv/x and priv/y 0777: world_writable at priv/y. Host-kit applies .. to the directory the walk is in, not to the spelling, writer a recording refuses the directory a .. steps back into, remote a remote recording refuses the directory a .. steps back into.

Two hardening items rode along: the hop count is refused as link_loop_exceeded at 30, kept below what the kernels apply themselves (32 on macOS, 40 on Linux) so this rule always reaches its limit first and a loop is never a pass; and a component that is neither directory nor link is refused as not_a_directory instead of reaching the rename. A link's own mode is never consulted — Linux reports one mode for every link — so what is judged is the directory the walk lands in, plus the ownership of the link and of the directory holding it, which is why an alias at 0777 into a 0700 directory now publishes.

The group exception fails closed. It is admitted only where /etc/nsswitch.conf exists, is readable, and lists files as the only source for both passwd and group. systemd is refused as well, not kept: systemd-userdbd answers membership from records these two files never show, which is exactly the member the check could not read. Bracketed actions and trailing comments are ignored; a missing line, an unreadable file, or any other source ends the claim and a group-writable directory is treated as shared. Tests cover ldap, sss, systemd, a source-less line, and a missing nsswitch.conf. The "can cause unsafe acceptance" paragraph is out of ADR 0016 and the guide, replaced by the condition.

The unused configurable mode on the script publisher is gone (SCRIPT_FILE_MODE is 0600), and the four-line comment at atomic-publish-ownership.test.ts:31 is deleted.

Fallow. FALLOW_BASE=origin/main pnpm check:fallow --base origin/main --gate all exits 0: dead code 0, complexity 0, duplication 0 over 19 changed files, with no baseline edits and no suppressions. traversedDirectories is gone with the second half of its cognitive load, and privateGroupId now delegates parsing (parseGroupRecord, parsePasswdRecord, nsswitchLine) to leave a pure decision in admittedPrivateGid. Two fixtures that had forced ENOTDIR with a file where a directory must be now use an owned but unwritable directory, so they still exercise the filesystem's own failure rather than being intercepted earlier by the walk.

Checks. CI is green at 4964b44, including Repo Guards & Compatibility, Coverage, Integration Tests, and the doc gates. Locally: pnpm lint, pnpm typecheck, pnpm check:layering, check:production-exports, check:daemon-wire-compat, check:agent-guidance, check:adr-citations, check:doc-prose and check:doc-links pass. pnpm check:affected --run --head HEAD is queued behind the host validation lock here, so I have not seen it terminate locally; CI ran the suites. One test file is split: atomic-file-ancestry.test.ts is 872 lines and the account-database decisions are in atomic-file-private-group.test.ts beside it, under the 1,000-line cap.

Reverting only atomic-file.ts to 546303c and re-running the new tests: 11 failed / 57 passed in host-kit, 7 failed / 93 passed across the writer, close-path and remote files. Nothing new passes without the walker.

@thymikee

Copy link
Copy Markdown
Member Author

Thanks for the update. At 4964b44 the Fallow failure from the earlier round is fixed and all 21 checks are green. The quadratic trailing-slash regex is also gone from session-script-destination.ts, so that scanning thread is fixed. There are no conflicts. One new defect blocks merge: relative link targets skip the walk.

stepThroughLink queues pathComponents(path.join(state.deepest, target)) for a relative target. That is an absolute path, but the walk then sets home = state.deepest (atomic-file.ts:390). The walk looks up <deepest>/<first absolute component>, finds nothing, and stops at the link's own directory. The target and the directories on the way to it are never judged. path.join also folds a .. in the target. I probed this on a scratch copy of the head file with stubbed imports, on macOS. home/rel -> ../shared/x with shared at 0777 is accepted, while 546303c refused it. home/rel2 -> open with home/open at 0777 is accepted, while spelling home/open is refused. The relative forms of the earlier shapes 1 and 3 are accepted too. This reopens #4565 for the common ln -s ../team/rec rec form, and it makes the promise in replay-e2e.md and ADR 0016 untrue. The rule: resolve a link target as the kernel does, from / for an absolute target and from the directory that holds the link for a relative one, applying each .. in the walk. Queueing the raw target components ([...pathComponents(target), ...rest]) should do it. Please add a relative-target case at host-kit level for rec -> ../shared/relay/sub and for a .. in the target. Please add one relative-target case on the writer or remote route.

lstatIfPresentSync (atomic-file.ts:739) has the same body as the exported lstatIfPresent in verified-file.ts:136. A third private copy sits at :771. Your comment says the duplicate was deleted, but one copy remains. Please import the shared helper, use it at :364 and :760, and delete both local copies. Is there a reason to keep any of them?

Not blocking, take or leave: the assertPrivateScriptDestination JSDoc at :195-198 and :215-216 still describes the deleted design and contradicts the new not_a_directory refusal, and the scriptAncestryFailure text says "owns or can write" for link_loop_exceeded and not_a_directory, which are neither.

Shape 2 (a foreign-owned hop link in a sticky directory) was traced from the code only, because an unprivileged process cannot create it. The probe confirms that 546303c accepts shapes 1 and 3. The change is 715 net production lines, just over the usual size line. There is no smaller owner beyond the two cleanups above.

Before merge, the relative-target walk must be fixed with regressions, and the duplicate lstat helpers must go.

@thymikee

Copy link
Copy Markdown
Member Author

All five are in. Head: 93156c8. Each one is a test that fails at 4964b44: reverting the production files to that commit turns 13 red across 46 files, and all 13 pass here. Two of them are shapes this change itself exposed, at the end.

1. The relative target. stepThroughLink now queues [...pathComponents(target), ...rest] and picks the starting directory by the target's own spelling: path.isAbsolute(target) ? '/' : path.dirname(link). Nothing about a target goes through path.join or path.resolve any more, so a .. inside one is applied one component at a time to the directory the walk has physically reached. ln -s ../team/rec rec is refused for the same reason the spelled-out chain is — writer, remote and host-kit each have a test for it — and priv/../x refuses at the shared ground the .. steps back into. Two notes from writing them: the fixture has to spell that target literally (fs.symlinkSync('priv/../x', …)), because path.join folds the .. away before the link exists; and the walk's answer for a relative target is now tested where it lands as well as where it refuses.

2. Staging escaped the judged directory. assertPrivateScriptDestination returns the directory it verified, and that value is what everything stages in: publishScriptFileSync builds its temp path with createAtomicPublishTempPath(judged, destination), re-walks, and refuses parent_replaced if the answer moved; allocateStaging mkdtemps inside it and keeps its identity for publishStaged. No parent is re-derived by path.dirname/path.join/path.resolve on either path — withAtomicPublishTempPathSync (the other publisher, which has no walk) keeps its path.dirname and that is the only remaining one. spelledParent is now plain path.dirname, used only by the walk.

Two consequences worth naming. The parent identity is compared against walk.deepest, not the spelled parent, so a .. destination no longer reports a replacement that never happened, and a refusal now names the directory the entry would really have been created in — elsewhere, not the link that points at it; two existing tests are updated to that. And the daemon writer stopped passing parentIdentity at all: the identity it had was taken from path.dirname(scriptPath), which is the wrong directory for exactly the destinations this round is about, so publishHealedScriptAtomically no longer takes the parameter. The remote path still passes its own, captured where staging was created, which is the pair worth comparing across a download. Both staging tests assert on where the bytes went: expect(staged).toEqual([privY]) with the shared home/y still empty.

3. initgroups:. nameServiceListsOnlyFiles checks passwd, group and initgroups, where a missing initgroups line falls back to the verified group line — what the kernel does — and an explicit one has to be files-only. Tests name sss, ldap files and systemd, and hold the inherited and initgroups: files cases at the same answer.

4. A bare destination. spelledParent('journey.ad') is . now, and startWalk resolves any non-absolute parent with path.resolve(''), so assertPrivateScriptDestination('journey.ad', publishDirectoryIdentity('.')) answers the cwd instead of starting at / and then refusing itself. path.resolve('') rather than process.cwd() because R86 keeps host-kit from reading the latter.

5. Judged once. WalkGround { stats: Map<string, fs.Stats>; judged: Set<string> } travels in WalkState: judged keyed by dev:ino so an alias or a .. does not re-judge ground already passed, stats keyed by path so the host is asked once per directory per walk. WalkOutcome and the stop step carry the walk's DirectoryAuthority, which is what othersCreateEntriesIn is asked with — the post-walk statDirectoryChain(walk.deepest) is gone, so the deepest directory is not read twice either. The test counts fs.statSync calls over home/a/../b/sub where a and b are two links to one directory and asserts every read is a distinct path; at 4964b44 that is 14 reads over 7 directories.

Also folded in: untrustedGroundWording gives each exposure its own wording so a link_loop_exceeded no longer claims someone else owns the path, and one 13-line clone fallow found between the two temp-path cleanups is closed by routing both through removeTempBestEffort.

One thing the first push broke, and a fourth shape it exposed. check:affected caught two suites this head now fails, both for reasons the rule itself owns — separate commit 93156c8:

  • isAtomicPublishTemporaryPath compared path.dirname(temp) to path.dirname(destination) as strings. The temp file lives in the directory the walk judged, which on macOS is /private/var/... while the destination is spelled /var/.... So the predicate stopped recognizing temp files produced for its own destination: the six filesystem-boundary-faults rows for the script writer and the session store went red (an injected EIO/ENOSPC/EMFILE at the write was no longer claimed), and any cleanup that names those files would have left them behind. It resolves both directories and compares them as directories. Test: recognizes a temp path spelled through the directory the entry is really created in, an alias in a scratch root so it is not macOS-specific.
  • Importing lstatIfPresent from verified-file.ts as planned made 64 top-level statements of eager work load on the daemon's startup path (host-kit/process.ts → owned-process-record.ts → atomic-file.ts → verified-file.ts), which eager-closure-budgets rightly refuses: a module the merge-base tree already had cannot be made eager by a split, and APPROVED_CLOSURE_ADDITIONS is not the honest fix for a nine-line helper. The helper now lives in atomic-file.ts and verified-file.ts imports it — the verified-file guards already sit behind this module via the façade, so the edge count is back to what it was. A module of its own was tried first; it grows nine capture-kit closures by one module and 49 statements, also red.

Delta. atomic-file.ts is 890 lines (was 778). Net production across the five files is +105: +112 in atomic-file.ts, −9 in verified-file.ts, +7 in the remote client, −5 in the writer. The growth is the return value and its contract (the doc block that says stage here is most of it), WalkGround, the wording table and the resolved-directory comparison; the lstat copies and the duplicated cleanup went the other way. atomic-file-ancestry.test.ts is over the cap with the new tests, so the three pure decisions moved to atomic-file-decisions.test.ts beside it (919 / 113 lines).

Checks. Reverting the five production files to 4964b44 turns 13 tests red across 46 files (the 12 above plus the temp-path one), and all 13 pass here. Focused: 46 files, 623 passed, 7 skipped, including filesystem-boundary-faults and eager-closure-budgets. pnpm format, lint, typecheck, check:layering, check:production-exports, check:daemon-wire-compat, check:agent-guidance, check:adr-citations, check:doc-prose and check:doc-links pass. pnpm check:affected --run --head HEAD terminates green at this head: lint, typecheck, layering, fallow, build, vitest-related (90 files, 415 tests) and unit (2126 files, 20135 passed, 10 skipped), then daemon-wire-compat, adr-citations and both doc gates. FALLOW_BASE=origin/main pnpm check:fallow --base origin/main --gate all exits 0 — dead code 0, complexity 0, duplication 0 over 22 changed files, no baseline edits and no suppressions. ADR 0016 and the guide carry the new claims: raw components and the link's own directory, the walk answering the staging directory, one judgment per directory, and initgroups in the name-service condition.

@apex-sentinel-staging

This comment has been minimized.

@thymikee
thymikee force-pushed the fix/save-script-ancestor-redirect branch from 93156c8 to 49ada50 Compare November 16, 2026 15:38
@thymikee

Copy link
Copy Markdown
Member Author

All six are in. Head: 49ada50, rebased onto origin/main so the branch now carries #4586 — the push is a force-push, and 54eefe8 below is the old 93156c8 with that rebase applied. Reverting the production files to 54eefe8 (this branch before the round) turns the two behaviour tests in items 1 and 4 red. Items 3 and 5 are tests that could not fail before, so each is proved by removing the rule it pins; items 2 and 6 are a split and a deletion, covered rather than pre-failed.

1. The relative destination (blocker). absoluteParent replaces spelledParent: a relative destination is queued as the resolved working directory's components with the destination's own components following them RAW, and startWalk always starts at /. So cd /shared/mine under a /shared at 0777 and not sticky is now refused at /shared instead of judging mine and nothing above it. The test publishes through the exported publishScriptFileSync exactly as you reproduced it — refuses a relative destination whose working directory sits in shared ground, with the cwd restored in a finally — and a companion holds the accepting case (answers the working directory for a relative destination over a safe chain), asserting the absolute cwd is what comes back. path.resolve(destination) is not used anywhere in this path: it folds, which is the whole reason the walk exists.

2. The test file is split. daemon-artifacts-script-ancestry.test.ts (369 lines) holds the eleven remote ancestry and refusal cases; daemon-artifacts-script-download.test.ts is 594. The client helper, the fake artifact daemon and the record helpers move to __tests__/support/script-publication-fixtures.ts (127) beside the two files, following the existing artifact-download-fixtures.ts convention — and the server is now closed by onTestFinished inside artifactServer, so no per-file registry is duplicated. Both names satisfy R84 as <module>-<aspect> for daemon-artifacts.ts; the file held 41 tests before the split and holds 41 after (30 + 11), with no case dropped or renamed. Lint is green with no suppression and no limit change, and #4586's retirement rows stayed in the download file untouched.

3. The alias test now walks two routes. a and b both name sub, and the spelling is home/a/../b/below/journey.ad, so sub is genuinely entered twice and the walk ends below it. It asserts assertPrivateScriptDestination(...) === below — that's the assertion that the second alias was really visited, since a walk that gave up answers home — and then that no path is read twice. Disabling the ground.stats cache fails it (22 reads over 8 distinct paths); the previous version stayed green with the cache gone.

4. The temp predicate compares directories, not spellings. Two things were wrong: it compared path text, and it used realpathSync, which applies a spelled .. lexically — for home/work/../y it answers home/y, not priv/y. So it rejected the real sibling in priv/y and accepted a look-alike in home/y. sameDirectory now stats both and compares dev:ino, and stat resolves .. the way the kernel does, so no realpath is involved. Test: recognizes a temp sibling reached through a .. that steps back from a link, asserting true for the real sibling and false for the planted one.

5. The three writer tests ask about one directory. All three now call serveHostOwnership on the directory at issue and assert the ancestor named (foreign, team, tmp/recordings). No process.getuid mock is left in that file, so nothing depends on who runs the suite. Removing the ownership half of untrustedAncestryExposure fails exactly those three.

6. parentIdentity is gone from publishScriptFileSync. The publisher takes the identity from the directory the walk judged, so an outside caller cannot steer what the comparison is against; the only remaining caller is the remote client, which legitimately captures one before its download. The externally captured identity is tested where it is actually honoured: assertPrivateScriptDestination(destination, publishDirectoryIdentity(staged)) refuses a replaced parent naming the linked-to directory, and accepts the identity of the directory the walk answers.

Delta. This round is production +17 (atomic-file.ts 890 → 907), nothing else in production. Across the PR, the five production files the rule touches go from 1,416 lines at merge-base to 2,223: atomic-file.ts 170 → 907, session-script-destination.ts 490 → 555, the writer 573 → 584, file.ts 31 → 34, verified-file.ts 152 → 143.

Checks. pnpm lint, format, typecheck, check:layering, check:production-exports, check:daemon-wire-compat, check:agent-guidance, check:adr-citations, check:doc-prose, check:doc-links pass; FALLOW_BASE=origin/main pnpm check:fallow --base origin/main --gate all is dead code 0 · complexity 0 · duplication 0 over 24 files, no baseline edits, no suppressions. Focused: 63 files, 1,641 passed, 7 skipped, including the remote suites, filesystem-boundary-faults and eager-closure-budgets. pnpm check:affected --run --head HEAD terminates green: lint, typecheck, layering, fallow, build, vitest-related (90 files, 415 tests) and unit (2,127 files, 20,194 passed, 10 skipped), then daemon-wire-compat, adr-citations and both doc gates. ADR 0016 and the guide carry the relative-destination rule and the fourth shape.

@apex-sentinel-staging apex-sentinel-staging Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apex Sentinel: 1 new finding on 49ada50. The verdict and the full list are in the summary comment.

@thymikee
thymikee force-pushed the fix/save-script-ancestor-redirect branch from 49ada50 to 7ed8e26 Compare November 16, 2026 16:27

@apex-sentinel-staging apex-sentinel-staging Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apex Sentinel: 1 new finding on 7ed8e26. The verdict and the full list are in the summary comment.

@thymikee

Copy link
Copy Markdown
Member Author

All four review items plus both review threads are in. Head: d86ee29 (on origin/main, carrying #4582). Each item below says how it is proved: a revert to 49ada50, or a perturbation of the rule the test pins where a revert cannot show anything.

1. The two chmod(0500) fixtures are gone (session-script-writer.test.ts, session-close-script.test.ts). Both now inject a deterministic EACCES at the staging-write seam, through one support module, daemon/src/__tests__/support/script-staging-failure.ts. It decides which write fails with host-kit's own isAtomicPublishTemporaryPath, so it does not rebuild the temp name, and the real writeFileSync serves every other call. Nothing about either test now depends on who runs the suite. Proved by the fixture rather than a revert — the tests passed at 49ada50 on a non-root host, which is the point: disabling the injection fails exactly those two.

2. The walk no longer trusts Node's remembered working directory, and both publishers create the entry in the judged directory.

  • absoluteParent confirms the spelling it queues is where the process stands — dev:ino of the resolved cwd against stat('.') — and refuses with parent_replaced on mismatch. Writing this fixture taught me something: Node answers the working directory from the first query after chdir, so the drift exists only once something has asked. The test settles that cache, renames the directory and plants a private one at the old path, which is the sequence a real process walks into.
  • The local publisher creates the entry at path.join(judged, basename(destination)). The remote publisher does the same in publishStaged, taking the directory from the judgment it already makes under the record lock, so nothing new is stored in the record.

Tests, all through the exported publisher: refuses a relative destination whose working directory has been moved out from under it, publishes a relative destination into the working directory it judged (asserts the path handed to renameSync), and refuses a relative destination whose working directory sits in shared ground, which now does go through publishScriptFileSync as my last reply claimed it did. The first two fail on 49ada50; separately, dropping the cwd confirmation fails the first and publishing through the spelled destination fails the second. The third is a strengthening — it passed at 49ada50 by the assertion helper, and now exercises the publisher.

3. ground.judged is deleted, with its doc comment, the walk's once-per-inode claim and the ADR sentence. All tests stay green without it, which is the finding: only the per-path stat cache is observable, and removing that fails the alias test (22 reads over 8 distinct paths).

4. The hop test states its numbers. A 30-link chain publishes, a 31-link chain is refused naming link30. MAX_SCRIPT_PATH_LINK_HOPS is imported only for a closing expect(…).toBe(30) that ties the literal to the constant. Lowering the constant to 29 fails this test; the derived version could not fail.

5. Missing parents are refused before they are created, on both routes, with the same walker. assertPrivateScriptDestinationPrefix is the pre-pass: the same walkParent, which ends at the first component that does not exist, so it judges exactly the prefix already there. Both routes run it before their mkdir, and the post-mkdir judgment is unchanged. The creation question is deliberately only asked after the mkdir — below the pre-pass those directories are the caller's own to create — which is what keeps mkdir -p <sticky shared>/recordings acceptable. Tests: a refused destination with a missing parent leaves no directories behind (local) and a remote recording refuses a destination with a missing parent and creates no directories. Both fail on 49ada50 with ['recordings'] where [] is expected. Thread resolved.

Remote entry path (discussion_r4588529567, fixed in the same commit): a remote recording creates the entry in the directory the walk judged, not the spelled one. work -> priv/x; between the last judgment and the link, work is repointed at a second private tree, so the spelled path answers elsewhere/y and nothing about it would raise an ownership refusal. The test asserts the recording is in priv/y and the swapped-to tree is empty. Restoring current.destination on that one line fails it with the wrong-directory landing, not an error.

Wire ledger. publishScriptVersion moved, so the digest is pasted as the coverage job asked, with a compatibleChanges rationale and no protocol bump: the declaration gained one destination check before its mkdir; no request field, response field, route, status or artifact framing changes, so a released peer sends and parses exactly the same bytes. npx vitest run test/wire-compat passes (51 tests) and check:daemon-wire-compat is clean against v0.21.37, protocol unchanged.

One split rode along. The new host-kit cases pushed atomic-file-ancestry.test.ts past the 1,000-line cap, so shared host fixtures moved to atomic-file-host.fixtures.ts (119 lines) beside the existing tmp-dir.fixtures.ts; the ancestry file is 951. The ownership-serving helper stayed inside the test file on purpose: as a shared module it clones test-kit's serveHostOwnership (fallow flagged the 6-line clone), and host-kit cannot depend on test-kit without inverting the package edge.

Delta. Production +68 this round: atomic-file.ts 907 → 959, the writer 584 → 591, the remote destination 555 → 563, the façade 34 → 35. Across the PR those five files go 1,416 at merge-base → 2,291.

Checks. pnpm lint, format, typecheck, check:layering, check:production-exports, check:daemon-wire-compat, check:agent-guidance, check:adr-citations, check:doc-prose, check:doc-links pass; FALLOW_BASE=origin/main pnpm check:fallow --base origin/main --gate all is dead code 0 · complexity 0 · duplication 0 over 26 files, no baseline edits, no suppressions. Focused: 65 files, 1,697 passed, 7 skipped, including both wire-compat lanes. ADR 0016 carries the two-question walk, the cwd confirmation, and that neither publisher hands the spelled destination to the kernel; the guide says a refused destination leaves no new directories behind.

@thymikee

Copy link
Copy Markdown
Member Author

I reviewed d86ee29 and found two problems to fix before merge. Compared with the earlier review, the shared-ancestor and spelled-destination issues are fixed. The 20 CI checks pass at this commit, and there are no conflicts.

The docs promise that a refused save leaves no directories behind (see replay-e2e.md and ADR 0016), but the code does not keep it. Both routes create the missing parent with no mode: session-script-writer.ts:144 and session-script-destination.ts:208. Under umask 002, the new directory is 0775. Without an admitted private group, the check that runs right after the mkdir refuses it as shared_group_writable. The earlier prefix check only judged ground that existed before. On the head file with process.umask(0o002), I got refused shared_group_writable .../home/recordings and recordings (775) stayed behind. On a umask-002 host, every save into a new directory fails and leaves a stray group-writable directory. The rule: every directory a script publish creates must itself pass the rule it is judged by. Please create missing parents with mode: 0o700 on both routes. A recursive mkdir applies the mode to every directory it creates. Please add one regression per route under process.umask(0o002) that asserts the save succeeds and the created parent is 0700.

nameServiceListsOnlyFiles admits only files for passwd, group and initgroups. Stock Ubuntu and Debian ship passwd: files systemd and group: files systemd, so privateGroupId returns undefined there. Those users run with umask 002, so every directory they create is 0775 with their private group, including git clones. untrustedAncestryExposure then refuses each one as shared_group_writable. I believe the docs' own example, --save-script ./workflows/e2e-settings.ad inside a cloned repo, is refused on stock Ubuntu. Before this PR the local route had no check, so this is a new false refusal. The exception that ADR 0016 justifies by the shape Ubuntu gives home directories admits nothing there, and the group, passwd and nsswitch parsing buys nothing on its target platform. Please resolve this at its owner. Either admit systemd as a source (userdb records come from root-owned drop-ins, the same trust level as /etc/group), or drop the exception, refuse group-writable directories with a chmod g-w hint, and rely on the 0700 creation above. Please run it once on a stock Ubuntu 24.04 install (not a Docker base image, which lists only files) against a 0775 project directory and show the save outcome.

Open threads: neither earlier inline thread still applies, so please resolve both. shared ancestor before mkdir is fixed by the prefix check before the mkdir (the leftover under umask 002 is the first problem above, a different mechanism). publish destination spelling is fixed because publishStaged now creates the file under the directory judged under the lock.

The walk itself is justified, since a per-component O_NOFOLLOW open cannot carry the create without openat. Production code grew from about 715 to about 875 net lines across five files, and the growth is patches around the walk. Would these cuts make it smaller and better owned? (1) Both callers already resolve an absolute destination, so could host-kit refuse a non-absolute one at its entry and delete the cwd branch of absoluteParent, standsIn and WalkStart? (2) Could host-kit own parent creation as one call (walk, mkdir the missing tail at 0700 inside the judged directory, judge again)? That removes the exported assertPrivateScriptDestinationPrefix seam and the two hand-sequenced copies, and makes "no directories left behind" true by construction. (3) Could the private-group exception be resolved one way, as above? (4) Could publishScriptFileSync keep one defense-in-depth comparison, either the parentIdentity check in the walk or the stillJudged !== judged check, since ADR 0016 says permissions, not re-checks, close the window? Please amend ADR 0016 first for the 0700 creation rule and the private-group decision.

Not blocking, take or leave: the relative-destination half of absoluteParent cannot fire from production because both callers pass absolute paths, and the publishScriptFileSync JSDoc at line 53 still documents the removed parentIdentity option.

The Ubuntu case comes from tracing the code and a macOS umask-002 reproduction, not a live Linux run.

Before merge, please create missing script parents at 0700 on both routes with the umask-002 regressions, and decide the private-group exception for stock Ubuntu.

@thymikee
thymikee force-pushed the fix/save-script-ancestor-redirect branch from d86ee29 to c852884 Compare November 16, 2026 19:36
@thymikee

Copy link
Copy Markdown
Member Author

Redone as one smaller rule, with ADR 0016 amended to match in c852884 (this round is squashed into that one commit). Both blockers are fixed and all four cuts are taken.

Blocker 1 — created directories now pass the rule they are judged by. mkdir with no mode is gone from both routes; host-kit creates at 0o700 and a recursive mkdir applies that mode to every level it makes, so a umask-002 host no longer creates 0775 and then refuses it. Your repro, run against d86ee29's packages/host-kit/src/file.ts on this macOS host with process.umask(0o002) and the writer's own sequence:

written: false
message: Refusing to save the script under .../home/recordings: another local user can write that part of the path.
details: {"reason":"script_ancestor_untrusted","exposure":"shared_group_writable",
          "ancestor":".../home/recordings", ...}
left behind recordings: 775

Same sequence at this head: the save goes through and both new levels are 700. Regressions: a recording creates a missing parent at 0700 on a shared umask (local route) and a remote recording creates a missing parent at 0700 on a shared umask (remote route), each asserting the save succeeds and both created levels are 0700. a refused destination with a missing parent leaves no directories behind and a recording that steps back out of a missing directory leaves no directories behind cover the leftovers, including the private/missing/../../shared/recordings shape.

Blocker 2 — the private-group exception is dropped, not widened to systemd. I took your second option. I did not go with systemd because I could not turn "userdb drop-ins are root-owned" into something the code can check: on the box that motivated the exception, membership is answered by a service, and /etc/group says nothing about whether it agrees, so an admitted systemd line would be a guess about the host's drop-ins. What replaced it: the mode alone decides, a group-writable directory is refused as shared_group_writable, and the hint names the directory and the command. Every line of /etc/group, /etc/passwd and /etc/nsswitch.conf parsing is deleted, along with the 181-line suite that tested it, so the answer no longer depends on which name services a host runs. The 0700 creation covers new directories, which is the half that mattered operationally.

outcome: refused
  message : Refusing to save the script under .../my-repo: the group can write that part of the path, and agent-device cannot tell who else belongs to that group.
  exposure: shared_group_writable | reason: script_ancestor_untrusted
  hint    : Close the directory to its group with `chmod g-w .../my-repo`, or save the script under a directory only you can write.
created under a private parent: workflows=700
project dir untouched: no directories left behind

That is a project directory at 0775 in this user's own primary group, umask 002: refusal with the hint and nothing created, then a save under a private parent at 0700. I could not run it on stock Ubuntu 24.04 — this environment has no Linux host, and no Docker, Lima, multipass or Vagrant to start one, so I did not fake it with a container either. The output above is the same code path on macOS, and a recording refuses a group-writable home and says how to close it pins the Ubuntu case as a unit test (a 0775 directory in this user's own group, refused with chmod g-w in the hint) — which is deterministic precisely because nothing reads an account database any more.

The four smaller-design questions

  1. Refuse a non-absolute destination at the entry, delete the cwd branch. Yes, done. spelledParent refuses a relative destination with a typed relative_destination, and absoluteParent, standsIn, WalkStart and the dev:ino-against-. guard are deleted — 52 lines out, 14 back. Both callers already expand the path (expandSessionPath locally, resolveUserPath on the remote client), so the branch you pointed at really was unreachable, and the stale-cwd hazard moves out of the rule rather than being emulated and then defended. refuses a destination that is not absolute, before judging or creating anything holds the refusal and that nothing was created.
  2. Host-kit owns parent creation as one call. Yes, done, as prepareScriptDestination: walk → refuse → create the missing tail at 0700 inside the directory the walk judged → judge the completed chain → answer with that directory. The assertPrivateScriptDestinationPrefix export and both hand-sequenced copies are gone; the local writer's pre-pass plus mkdirSync, and the remote client's mkdir, are each one call now. Two things fall out of it: "a refused destination leaves no directories behind" is true by construction rather than by each caller remembering the order, and the ..-behind-a-missing-component shape is refused as unresolvable_parent before the first mkdir — which is the only way that one can be refused, since a mkdir resolves that spelling as text and builds shared/recordings behind it. The walk also reports whether traversal completed, and publishing requires completion, so a walk that stopped short can never publish into the nearest ancestor. Where nothing was missing, the first walk's answer is the answer and there is no second walk at all.
  3. Resolve the private-group exception one way. Done as above: refused on the mode, with the hint, plus 0700 creation. refuses a group-writable 0775 directory with the hint to close the group and refuses a setgid group-writable directory on the same mode are the accepting-shape-now-refused cases; othersCreateEntriesIn and untrustedAncestryExposure take a trust of { uid } and nothing else.
  4. Keep one defense-in-depth comparison. Yes, and the local one is the one deleted: publishScriptFileSync walks once and the parentIdentity round-trip plus stillJudged !== judged are gone. The remote route keeps the single comparison you already accepted, because it is the only caller where real time sits between the two looks — it captures dev:ino when it allocates staging and compares once under the record lock before the rename. ADR 0016 now says the ownership rule is what closes the window and that a second re-check answers a race the permissions already exclude.

Also from your not-blocking list: the stale parentIdentity JSDoc is gone with the option it documented, the unreachable component === undefined branch in stepThrough is gone (its caller answers an empty queue), and the root-owned alias case now serves root ownership for the alias, its target and the sticky directory that holds it, so it cannot pass a rule that refused every root-owned link.

Lines. Measured your way — net added over origin/main, same five production files: 875 → 735, so 140 fewer. atomic-file.ts is 829 lines where d86ee29 was 959; the writer is 583 (was 591) and the remote client 561 (was 563), both because the sequencing moved into host-kit instead of being duplicated. atomic-file-host.fixtures.ts is 72 lines (was 119) and the private-group suite is deleted, so the test tree shrank too.

Checks. FALLOW_BASE=origin/main pnpm check:fallow --base origin/main --gate all clean (dead code 0, complexity 0, duplication 0), wire-compat green with the publishScriptVersion digest re-acked and its rationale extended — the only code change in that declaration is which host-kit call creates the parent, nothing on the wire moves. check:layering, check:production-exports, check:doc-prose, check:adr-citations and typecheck pass. Each new test fails against d86ee29's production code: the four host-kit parent cases, both umask-002 regressions and both routes' unresolvable_parent cases fail outright; the relative-destination and chmod g-w cases fail because d86ee29 walks the cwd instead and gives the old hint.

One thing to flag for your judgement, since it is a user-visible change I made under decision 3: a local 0775 checkout owned by its own group, which the exception admitted on a files-only host, is now refused with the chmod g-w hint. That is the false refusal you found on Ubuntu, moved to the hosts where the exception did work. I took it because the alternative was trusting a database I cannot read, and the fix is one command the message names.

Both inline threads were already resolved, so there is nothing to close there; I added a short note to each saying which change answers it.

@thymikee

Copy link
Copy Markdown
Member Author

Thanks for the update. At c852884 the two earlier threads on the shared ancestor and the publish destination are fixed, but one new problem remains: a umask-002 daemon host now refuses its own default recordings.

The daemon creates sessions/ itself, without a mode. open always runs ensureSessionDir, which calls fs.mkdirSync(<baseDir>/sessions/<name>, { recursive: true }) (session-store.ts:525; the tombstone writers at :373 and :442 do the same). Under umask 002, such as a stock Ubuntu login, sessions/ comes out 0775. baseDir is repaired to 0700, but sessions/ is not. Both the default path (session-script-writer.ts:182) and every remote-delivery version (session-script-writer.ts:169-172) walk through sessions/, and the new rule refuses a group-writable directory. A Node check of the two mkdir calls under umask 002 gave sessions=775 and remote-scripts=700. State directories that older versions created under umask 002 have the same 0775 sessions/. So on such a host, bare open --save-script and every remote-delivered recording would fail with script_ancestor_untrusted, and a remote user cannot apply the chmod g-w hint. Before this PR those saves worked. The tests miss it because each route test passes an explicit destination under a 0700 home and never runs ensureSessionDir first.

The rule to satisfy: every directory that agent-device creates on the chain of a script destination it chooses (state dir, sessions/, remote-scripts/) is created at 0700 and repaired to 0700 when it already exists. The daemon-paths owner should hold this, next to where daemon-registration-owner already creates and repairs baseDir, rather than each mkdir site. Please add one regression under process.umask(0o002) that runs ensureSessionDir (or open) first, then asserts that a default-path recording and a remote-delivery version are both written.

Not blocking, and fine to take or leave: atomic-file.ts:501 has a stray duplicate doc line, the two WalkOutcome doc blocks at :416-433 should merge, and the parentIdentity doc at :316-319 says "two walks" when the remote route now walks three or four times; publishScriptVersion discards the directory prepareScriptDestination returns and then re-walks at allocate and again at publish, so passing that answer through would drop the extra seam; in a sticky shared directory with two or more missing names, another user can plant the first name as a link between the walk and the recursive mkdir, which leaves a 0700 directory behind in the link target (from Node's recursive mkdir behavior), so a level-by-level non-recursive mkdir would close it, or the race can be stated as outside the no-leftover guarantee; and the PR body still describes the dropped private-group exception and the publisher-side comparison on both routes.

The finding above comes from a code trace plus a Node mode reproduction of the two daemon mkdir calls. No live umask-002 daemon run exists yet. The rule no longer reads account databases, so the Ubuntu outcome depends only on directory modes, which the test a recording refuses a group-writable home and says how to close it pins.

All 21 checks pass at c852884, but CI hosts use umask 022 and explicit 0700 test homes, so they do not reach this route. There are no conflicts. Before merge, sessions/ needs to be created and repaired at 0700 and the umask-002 regression added.

The open inline threads on the shared ancestor before mkdir (#4584 (comment)) and on the publish destination spelling (#4584 (comment)) are fixed at this head, so they can be resolved.

@thymikee

Copy link
Copy Markdown
Member Author

Head 235d0db439. The umask blocker and both cross-check races are in, each as a test that fails at c852884.

The daemon's own directories (your blocker). ensurePrivateDaemonStateDir now sits in daemon-registration-owner.ts beside repairStateMode, and every creator routes through it: launchDaemonProcess, SessionStore.ensureSessionDir and both tombstone writers, remoteScriptPath, and daemon startup for baseDir and sessionsDir. Creation carries mode: 0o700 and the repair walks every level the call created, not just the leaf — mkdirSync reports the first directory it made, so repairing only the leaf left sessions/ at 0775 while the session directory below it looked correct. The regression runs ensureSessionDir under process.umask(0o002), then writes a default-path recording and a remote-delivery version: both succeed, and sessions/, sessions/journey/ and remote-scripts/ are 0700. Reverted to c852884 it fails with your exact symptom — script_ancestor_untrusted / shared_group_writable at sessions.

lstat versus stat (blocker 1). Every step is judged from the lstat of the ENTRY; stat is gone from the rule. WalkGround is now judged: Map<path, {identity, uid, gid, mode}>: a name the walk already judged keeps that judgment and is re-read only to recognize the entry, so a directory swapped for a link between the judgment and entering it is refused (parent_replaced) instead of entered on its target's permissions. The answer is confirmed the same way before any caller stages, and identities are read { bigint: true } — Number(2n**53n + 1n) === Number(2n**53n), which the bigint test pins.

Recursive mkdir (your item 2). createMissingLevels issues one non-recursive mkdir per name, inside the directory the walk established, confirming that directory's identity before each creation and lstat-ing each level it made. EEXIST is a refusal (entry_appeared), never a retry, and the second walk starts from the first walk's whole memory plus the creations — so a level swapped out in between is refused rather than entered. Windows keeps the recursive call, which this race cannot reach, and the absolute check now runs before the platform branch, pinned by a test that is deliberately not skipped on Windows.

Pass-through. prepareScriptDestination returns { directory, identity }; allocateStaging stages in that directory after one identity check (destinationMoved), so a version is walked once rather than twice. One thing you may want to know: I first moved the three ground decisions into a sibling module to keep atomic-file.ts under the cap, and eager-closure-budgets refused it — a new module on the host-kit/file.ts edge is eager work for every entry that publishes a file. They stayed put, and the file now sits exactly at 1,000 lines.

Delta. Net production across the five files this rule owns is +926 (was 735 at c852884): +830 atomic-file.ts, +92 remote client, +8 writer, +5 façade, −9 verified-file.ts. Counting the daemon-side files the umask fix touches, all production is +976. Of the 816 added lines in atomic-file.ts, 291 are doc comments.

Checks. pnpm check:affected --run --head HEAD passes all runnable checks at this head (receipt run-U645gX, exactCommit): format, lint, typecheck, layering, fallow, build, the affected vitest projects, the full unit run, docs-links and docs-prose. Reverting only the production files to c852884 turns 8 tests red across the four suites, every one for the intended reason. Also locally: focused suites 505 passed; eager-closure-budgets 903; wire-compat 51, with the publishScriptVersion digest re-acked and the ack saying why nothing moves on the wire; check:fallow --gate all exits 0 — dead code 0, complexity 0, duplication 0, no baseline edits, no suppressions; check:production-exports and check:adr-citations green.

One caveat on that last test, so the count is not read as more than it is: eight of the nine new or rewritten tests go red at c852884 on this machine. The every-platform absolute check does not, because macOS already refused a relative destination through the walk — the check only becomes load-bearing where the walk is a no-op, so it is pinned by the Windows branch it now runs before, and by the test running unskipped there, not by this revert.

ADR 0016 and the guide now state one mkdir per level, the identity carried by the answer, bigint identities, and the daemon's 0700 state directories. The PR body describes the current design.

@thymikee

Copy link
Copy Markdown
Member Author

Thanks for the update. At 235d0db, the regex on the destination is gone, the walk now runs before any creation, and staging is allocated in the judged directory, so those parts of the earlier review (#4584 (comment)) are fixed. One finding remains.

The remote route copies host-kit's refusal instead of calling it. destinationMoved in session-script-destination.ts:415 repeats the parent_replaced branch of scriptAncestryFailure (atomic-file.ts:894-907): same message, reason, exposure, path, ancestor and hint. The identity check that throws it (:264) also re-implements host-kit's private stillTheJudgedEntry, without the isDirectory test. A later wording or detail change in host-kit would miss the remote route, and isAncestryReason (:477) already matches on the hand-kept reason string. The rule should be: host-kit confirms a ScriptDestination, and every parent_replaced refusal is built by scriptAncestryFailure. Please export one host-kit call, for example confirmScriptDestination(destination, prepared), call it from allocateStaging, and delete destinationMoved and the local identity comparison.

Not blocking, and you can take or leave these: ADR 0016 :227 still says the remote pre-rename comparison is the only identity comparison, while :222 and the code re-check identity on every walk, so one of them needs to change; createMissingLevels (atomic-file.ts:962) refuses on any EEXIST, so two processes saving into the same new directory can fail with entry_appeared, and judging the appeared name with the same lstat rule would accept this user's own directory and still refuse a plant; ensurePrivateDaemonStateDir (daemon-runtime.ts:216) repairs a pre-existing 0775 sessions/ only through the startup lines, and deleting them leaves every test green, so one startup test that a 0775 sessions/ ends at 0700 and a default recording is then written would pin it.

Could the change be smaller? The two fixes that carry the safety are judging every step from its single lstat, and creating each missing level with one non-recursive mkdir that judges an appeared entry by the same rule. The identity-compare and 'replaced' branch in judgeGround, the known-identity check in stepIntoEntry, stillTheJudgedEntry before each level, the ground seeded into the second walk, the bigint identities and remote destinationMoved look like they only catch swaps the ownership rule already excludes. A judged entry is owned by this user or root and sits in a directory others cannot rename in, or in a sticky one where they cannot replace it. The existing remote pre-rename comparison could stay as the one extra check. That would remove roughly 100-150 production lines and bring atomic-file.ts under the 1,000-line cap. Net production is now +1075 against the 700-line threshold, up from 735 at c852884; the description's +926/+976 counts do not match the diff. What am I missing that makes the recognition layer necessary?

The three earlier inline threads are fixed at this commit and can be resolved: the polynomial regex (#4584 (comment)), creation before the walk (#4584 (comment)), and staging outside the judged directory (#4584 (comment)).

CI shows 20 checks with 0 not passing at 235d0db. The CI hosts run umask 022, so the umask route is covered only by the process.umask regression, not by host configuration. The ownership argument above has not been checked with a second uid. No conflicts. Before merge, route the remote identity check and its parent_replaced refusal through one exported host-kit call and delete destinationMoved.

@thymikee
thymikee force-pushed the fix/save-script-ancestor-redirect branch from 235d0db to 45d4eb9 Compare November 16, 2026 23:40
@thymikee

Copy link
Copy Markdown
Member Author

Cut the recognition layer rather than defend it — head 45d4eb97.

Blocking finding: routed through one exported host-kit call. confirmScriptDestination(destination, prepared) is exported from @agent-device/host-kit/file and does the confirmation and the refusal: it walks the destination again, compares that walk's dev:ino with the identity prepareScriptDestination answered, and returns the directory to create the entry in. destinationMoved and the local identity comparison in allocateStaging are deleted, so the route holds no copy of the message, reason, exposure, ancestor or hint — every parent_replaced is built by scriptAncestryFailure. The hand-kept reason literals are gone too: host-kit exports scriptAncestryReason(error), which reads back the reason of the refusal it built. The surface for this rule is now exactly prepareScriptDestination + confirmScriptDestination + scriptAncestryReason; assertPrivateScriptDestination is deleted outright and publishDirectoryIdentity is module-internal, because check:production-exports refuses exported surface nobody consumes.

One placement difference from your suggestion: the confirm runs after the download and before the rename, not in allocateStaging. Calling it at allocation walked the destination a third time per version to re-derive a directory the call one line earlier had answered, and the wait that needs the check is the download. Per version at this head: two walks, one identity comparison.

What was cut. Your ownership argument is the reason the layer is gone rather than the reason it stays, so it is gone — the whole thing, not a part:

  • WalkGround, the per-walk Map of every directory the walk judged, and the map seeded from creation into the second walk.
  • The 'replaced' answer in judgeGround — re-reading a name to RECOGNIZE it and keeping the first judgment.
  • The known-identity check in stepIntoEntry, which compared the fresh lstat against the recorded identity of a name the walk had seen before.
  • stillTheJudgedEntry, run before answering and before creating each level.
  • The parentIdentity parameter that made the create-nothing walk compare a second identity.
  • The remote's destinationMoved, its identity comparison, and its hand-kept reason strings.

I could not name an attack that layer caught which the ownership rule lets through, and I could not reproduce one against the pre-cut code. Every actor able to swap a judged directory had already been refused at it: a level the walk stands in is owned by this user or root with its contents closed to others, or is sticky, where this user's entry cannot be replaced. Nothing here was kept for a reproduced attack, and I found nothing whose removal reopens one. −96 production lines in this commit; atomic-file.ts 1,000 → 939.

What was kept, and the scenario each one answers.

  1. One lstat per step, never stat. Destination ~/work/rec/journey.ad where rec is ln -s /tmp/attacker rec, with ~/work at 0700. A stat of rec answers the TARGET: a directory the attacker owns at 0700, which reads as safe, and the publish then creates through the link. lstat answers the ENTRY — a link, judged against the mode of ~/work that let it be planted — and the walk follows it one component at a time so a .. written inside the target is applied to the directory the kernel actually reaches. Pinned in atomic-file-ancestry.test.ts: the link-in-private-mode cases, the .. cases, and the two-routes-one-directory case.
  2. One non-recursive mkdir per missing level, at 0700. Destination <sticky /tmp>/recordings/sub/journey.ad, recordings absent. A recursive mkdir resolves the whole tail as text, so a link planted at recordings in that window carries the creation to its far side — and run as root it creates there with privileges. One mkdir per name can only land inside the directory the walk stopped in. Pinned in atomic-file-parent.test.ts: "refuses a destination that steps back out of a missing directory, and creates none", "refuses a missing level planted as a link, and creates nothing beyond it".
  3. EEXIST is judged, not refused and not trusted. Two sessions saving into the same new recordings/: one wins mkdir, the other finds the name taken. Your point is right — the old blind refusal failed that routine save with entry_appeared. Blind trust is the other failure: in that same window an attacker with a write into the parent can put a directory there that is theirs, or 0775, or a link. So the appeared entry gets the same single lstat and the same rule. Four tests: "adopts a missing level another process created, and creates the rest below it", "refuses a missing level that appeared as another user directory", "refuses a missing level that appeared group-writable", and "refuses a missing level that appeared sticky and world-writable" — that last one refuses even this user's own directory, because the sticky bit holds entries already there and says nothing about a name the destination still has to create.
  4. The one remote pre-rename comparison, as host-kit's call. Kept because you named it as the one extra check worth keeping, and because it is six lines and one call. What it does mechanically: an answer held across the download is used only where the name still resolves to the directory that answer named. The concrete shape is ~/rec at 0700 judged before the download; while the download runs the name is replaced — with a link to another 0700 directory of this user's, or with a different real directory. The second walk judges that replacement on its own merits and ACCEPTS it, because it is a private directory of this user; nothing about the walk itself notices the ground moved. Only the comparison refuses, and it also refuses the case where the chain is gone, instead of answering a nearer ancestor. I am not claiming more than that: under the cross-user threat model, whoever could make that swap had already been refused at the parent, and the only actor who can do it to its own private directory is code running as this user, which can read the recording anyway. So it is defense in depth against ground that moved under a held answer, not what excludes the attacker — which is what the ADR now says in those words. If you would rather have no comparison at all, that is a five-minute change and I would not argue: confirmScriptDestination would shrink to the second walk alone and the answer would stop carrying an identity.

Daemon state repair: yes to both, and named. repairPrivateStateDir lstats the entry first and refuses before touching anything: absent is a no-op; a link or a non-directory is state_dir_not_a_directory; a directory another uid owns is state_dir_foreign_owned with its mode left alone; already 0700 is a no-op. forcePrivateStateDir then opens the name with O_RDONLY | O_DIRECTORY | O_NOFOLLOW, fstats that descriptor and requires the same dev:ino as the entry it judged (state_dir_entry_replaced if not), re-checks ownership on the opened entry, and only then calls fchmodSync on that descriptor. ELOOP and ENOTDIR from the open become the not-a-directory refusal; EPERM/EROFS/ENOTSUP/EACCES emit a daemon_state_mode_repair_skipped warning rather than killing startup. The reason for the fd: chmod given a NAME resolves it again, so a repair that reached a legacy sessions under a directory another local user can write, run by a root daemon, would be changing the mode of a directory that user chose. The mode either lands on the entry that was judged or nothing happens.

Tests for it:

  • packages/daemon-contracts/src/daemon-registration-owner-state-dir.test.ts — "repairs the state directories it owns, whatever mode they were created with", "refuses a state name that is a link, and leaves where it leads alone" (asserts the link target's mode is untouched), "refuses a state directory another user owns, and leaves its mode", "warns and changes nothing when a state directory cannot be opened at all".
  • packages/daemon/src/core/__tests__/session-store-teardown-script.test.ts — "on a umask-002 host the daemon creates and repairs its own state directories at 0700", "repairs state directories a legacy version or a shared checkout created at 0775", "refuses to repair a sessions directory another user owns, and changes nothing there", "refuses a planted sessions link under state another user can write, and leaves where it leads alone".
  • packages/daemon/src/server/http/daemon-runtime-state-dir.test.ts — your startup pin: "startup repairs a state chain a legacy release or a shared checkout left group-writable" (0775 baseDir and sessionsDir end at 0700, then a default recording is really written through SessionStore), and "startup refuses state it may not force private" (state_dir_foreign_owned, mode intact). Deleting the two ensurePrivateDaemonStateDir lines from daemon-runtime.ts turns both red — I ran your experiment.
  • src/__tests__/daemon-launch-cwd.test.ts — "a launched daemon starts when forcing a mode on its state paths fails with EPERM/EROFS", which asserts fchmodSync(…, 0o700) exactly once for the state directory and chmodSync only on the log file, i.e. the directory's mode can only be forced through a descriptor.

ADR 0016. :222 and :227 no longer contradict, because the half that contradicted is cut. :227 now says the remote walks twice and compares identity once, which is what the code does, and :213 describes the EEXIST judgement in place of entry_appeared.

The delta, reconciled. Our counts differ only in which files count as test files. Under the rule your numbers use (everything except *.test.ts, __tests__/, test/, scripts/, docs/, website/, ledger.json), this head is +1,094 and 235d0db was +1,075 — your figure was right for that rule. My earlier +976 additionally excluded two src/*.fixtures.ts files (atomic-file-host.fixtures.ts +72, test-kit/src/host-ownership.fixtures.ts +27); +976 + 99 = +1,075.

File 235d0db 45d4eb97
packages/host-kit/src/internal/atomic-file.ts +830 +769
packages/daemon-contracts/src/daemon-registration-owner.ts +40 +155
packages/remote/src/session-script-destination.ts +92 +57
packages/host-kit/src/internal/atomic-file-host.fixtures.ts +72 +72
packages/test-kit/src/host-ownership.fixtures.ts +27 +27
packages/daemon/src/core/session-script-writer.ts +8 +8
packages/daemon/src/server/http/daemon-runtime.ts +7 +7
packages/host-kit/src/file.ts +5 +5
test/wire-compat/closure-policy.ts +2 +2
packages/test-kit/src/index.ts +1 +1
packages/host-kit/src/internal/verified-file.ts −9 −9
Total +1,075 +1,094

Why the total did not follow atomic-file.ts down: the state-directory repair grew +115 on this PR because your round-9 finding asked for judging before forcing and a descriptor-based mode change. So one file fell −61, the remote fell −35, and the repair rose +115. Excluding comment lines the head is +692 of code against +702 at 235d0db; 402 of the added lines are comments, and the reasoning a reviewer needs in order to not re-add the cut layer is precisely what those comments are, so I would rather keep the prose than thin it to move a count.

If the total still needs to come down, the honest split is by concern, not by deletion: the ancestry rule (atomic-file.ts: roughly 370 lines of kernel-shaped walk, 290 for the two destination calls plus per-level creation, 215 for exposure decisions and the refusal shape) is one change, and the daemon state-chain repair (+155 in daemon-registration-owner.ts, +7 in daemon-runtime.ts, plus the four suites above) is another that could be its own PR. What I cannot cut without reopening #4565 is a per-route copy of the ownership rule, a walk that trusts path.dirname of a destination containing a .., or stat where lstat is what judges the entry.

Red at 235d0db. With only the four production files reverted to 235d0db and these tests kept, 12 tests fail across 5 files: the four EEXIST cases, the foreign-owned and cannot-open repair cases, the two startup cases, and the two chmod-shape cases. The startup file is additionally red when only the two ensurePrivateDaemonStateDir lines are deleted. No suppressions, baselines or allowlists.

Checks at 45d4eb97. pnpm check:affected --run --head HEAD green on the final tree; unit sweep 17,372 passed / 9 skipped over 1,943 files; wire-compat 51 (the publishScriptVersion declaration hash moved with its doc text and is re-acked — no request, response, status or framing change); eager-closure-budgets 905; layering 483 pass / 0 fail; check:fallow --gate all dead 0 / complexity 0 / duplication 0; check:production-exports, check:doc-prose (32 clean), check:adr-citations, check:doc-links clean. atomic-file.ts is 939 lines; the largest file in the diff is 968 and none exceeds 1,000. The head carries one extra test-only commit: test/repo-guards/macos-helper-package-contents.test.ts asserts the packed tarball's file list through a real npm pack, measured 1.9s alone and over the 5s default once the suite runs every worker, so it went red on a full-suite run of this branch. It now names a 30s budget the way the other subprocess-backed tests do. Nothing in ed5c7b96 touches that file or the pack list.

Two things your comment names that this head does not claim to fix: CI still runs umask 022, so the shared-checkout route is covered by the process.umask regression plus tests that force 0775 with chmod rather than by host configuration — the new startup test deliberately sets modes on disk instead of moving the umask, so it is host-independent, but no CI host exercises the umask itself. And ownership is still served by a fixture uid (65534), not a real second account. A CI job that owns a scratch tree as another uid and skips when it cannot is the right fix; I would take it as a follow-up rather than inside this PR.

@apex-sentinel-staging apex-sentinel-staging Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apex Sentinel: 3 new findings on 45d4eb9. The verdict and the full list are in the summary comment.

@thymikee

Copy link
Copy Markdown
Member Author

Thanks for the update. At 45d4eb9 the host-kit changes close the earlier destination concerns, but one new problem remains: the daemon can now refuse to start on setups that work on main.

ensurePrivateDaemonStateDir runs on daemon launch (launchDaemonProcess, for baseDir), on startup (daemon-runtime.ts:215-216, for baseDir and sessions/) and per session in session-store.ts. When a level already exists, the repair throws state_dir_not_a_directory if it is a symlink (daemon-registration-owner.ts:341). It throws state_dir_foreign_owned if the level is not 0700 and this uid does not own it (:356). Two examples: a user whose ~/.agent-device or sessions/ links to another disk, and a root daemon in a container with a 0755 sessions/ owned by the mounted uid. On main both start, because the repair chmods or skips with daemon_state_mode_repair_skipped. At this head, launch or startup throws. Every command fails, not only --save-script. This also breaks the rule repairStateMode documents in the same file (:435-437). The test at daemon-runtime-state-dir.test.ts:66-92 pins the startup rejection.

The rule the code must satisfy: a state-mode repair never decides whether the daemon starts. It forces 0700 only on a directory entry this uid owns, reached without following a link. Otherwise it changes nothing and emits daemon_state_mode_repair_skipped. The destination walk stays the only thing that refuses a recording. The smallest form is an lstat fast path for a directory already at 0700. Otherwise open with O_RDONLY|O_DIRECTORY|O_NOFOLLOW, fstat, and fchmod only when the fstat uid is this process. Treat ELOOP/ENOTDIR, a foreign owner and EACCES/EPERM/EROFS alike as skip-with-diagnostic. Please flip the startup test to assert that startup succeeds, the foreign mode is unchanged, and a default recording is refused with script_ancestor_untrusted.

Could the repair be the smaller design above? The host-kit cut helps: atomic-file.ts is -61 net and the remote side is -35 net. Production net still rose from 1075 to 1094, because eaa9747 added about 115 lines of state-repair guards (three refusal reasons, an lstat-then-fstat identity compare, a double ownership check). One repair path would be about 40 lines, remove sameStateEntry, state_dir_entry_replaced, the duplicate owner check and privateStateRefusal, and bring production net to roughly 1020. ADR 0016:225 would first need to describe the repair as best-effort and never refusing, with the walk as the only refusal.

Not blocking, take or leave: the comment at atomic-file.ts:916 says the created level "would stand 0775" without the chmod, but mkdirSync(level, {mode: 0o700}) cannot exceed 0700 and the lstat judgment now runs before the chmod, so fix the comment or drop the chmod; and the timeout bump in test/repo-guards/macos-helper-package-contents.test.ts is unrelated, so ship it as its own one-line PR or name it in the PR body.

The three earlier inline threads no longer apply, so please resolve them: no regex runs on the destination at 45d4eb9 (#4584 (comment)); prepareScriptDestination walks before creating and creates one level at a time inside the judged directory (#4584 (comment)); allocateStaging runs mkdtemp in the judged directory (#4584 (comment)).

Checks: 20 pass and one review check is still running; no failing job overlaps the diff. CI hosts run umask 022 as a uid that owns its temp dirs, so the symlink and foreign-owner shapes only run through the simulated-ownership test, which asserts the refusal. The startup problem comes from the code and the pinning test, not a run against a symlinked ~/.agent-device or a real second uid. There are no conflicts. Before merge, the repair must skip with daemon_state_mode_repair_skipped instead of refusing on a symlinked or foreign-owned state level.

Separately, on main, launchDaemonProcess still opens daemon.log with a and repairs its mode by pathname. That is not a regression from this PR.

@thymikee
thymikee force-pushed the fix/save-script-ancestor-redirect branch from 45d4eb9 to 4b630c1 Compare November 17, 2026 00:32
@thymikee

Copy link
Copy Markdown
Member Author

Folded in as three commits on top of the host-kit round: b9f17ce472 (the repair + the ADR rule), 632cc84121 (the suites that pinned the old refusal), 4b630c1f5b (the ADR sentence that still described the deleted lstat-vs-fstat match).

The rule, in the ADR before the code (docs/adr/0016-active-session-script-publication.md:225): a state-mode repair never decides whether the daemon starts, and it is not a second destination rule. It forces 0700 on a directory entry this uid owns, reached without following a link, and on nothing else. Every other shape changes nothing and emits daemon_state_mode_repair_skipped. The walk stays the only refusal, and the ADR says so in the same paragraph that says the repair exists, so the next reader does not turn the skip back into a gate.

ensurePrivateDaemonStateDir: lstat fast path for a level that already stands 0700 → otherwise open(O_RDONLY | O_DIRECTORY | O_NOFOLLOW) → fstat → fchmod 0700 only if that descriptor's uid is this one. A link, a non-directory, a foreign owner, EACCES, EPERM, EROFS, ENOTDIR (through the one MODE_REPAIR_SKIPPED_CODES set the log repair already had, now shared): skip with the warning. Nothing in that path throws. sameStateEntry, state_dir_entry_replaced, the second ownership check and privateStateRefusal are deleted — the fstat of the descriptor about to be fchmoded is what decides, and there is no refusal left to carry a typed reason.

Both setups you named are pinned as startup tests, not unit tests: a sessions/ another uid owns at 0755, and ~/.agent-device as a symlink. Each asserts the runtime started, that daemon_state_mode_repair_skipped was emitted (counted in a diagnostics scope), that the owner's mode is untouched and that nothing was created on the link's far side — and, for the foreign-owned one, that a default-path recording is refused with script_ancestor_untrusted / exposure: foreign_owned naming that directory. The 0000-directory case is injected as EACCES at the open seam rather than staged as a mode, because a root process opens a 0000 directory it owns and the old staging would have proved nothing under CI's uid. serveHostOwnership now serves ids through fstat as well, since that is where a repair that changes an entry asks who owns it.

Creation of missing state levels keeps the plain recursive mkdir at 0700, which is the choice your item 5 left open; the reasoning and the cost of the alternative are on thread 4593326010, together with what I did change about the force.

The other cuts from the cross-check: WalkOutcome is a three-way ended: 'arrived' | 'short' | 'refused' union and the arrived/refused/stoppedShort builders are folded into the walk — they existed to fill fields no consumer read; the walked.identity === undefined guard in prepareScriptDestination is gone, since a short walk always carries the identity of the directory it judged; the two errno sets are one; the atomic-file.ts comment claiming a umask-002 level "would stand 0775" is corrected to what a umask can actually do to the mode it is handed; and the repeated walk/confirmation prose is trimmed to one place each. The unrelated repo-guards timeout bump is dropped from the branch — that check passes at origin/main here in 1.44s, so I cannot claim a budget I did not measure.

ADR walk counts are stated: remote walks twice per version where the parent stands, three times where a level has to be created, one identity comparison either way; local walks once, twice with a missing parent, compares nothing.

Net production is +1,022 (676 code, 380 comment; excluding tests, __tests__, scripts/, docs/, website/, ledger.json) — 72 lines below the head you reviewed. Red-at-235d0db accounting, so a pin is not mistaken for a rename: every flipped case is red there — the link, foreign-owner and EACCES cases in daemon-registration-owner-state-dir.test.ts, the same two repair shapes again in session-store-teardown-script.test.ts, and both startup cases (foreign-owned, symlinked) in daemon-runtime-state-dir.test.ts. Measured by checking those files out at 235d0db and running them: 5 failed in the first run, 2 in the second, all seven accounted for. Two tests in the rewritten state-dir suite are green at 235d0db on purpose: the repair-it-owns case carried over unchanged, and a new one that asserts a level standing 0700 is never opened at all, which is a cost pin for the fast path rather than a behavior change.

Disclosures carried over: the suites run under umask 022 and foreign ownership is a fixture uid (65534), so neither CI's umask nor a real second account is exercised on real ids; and test/repo-guards/macos-helper-package-contents.test.ts now has main's budget, so it can flake where one npm pack is slow — it runs in well under a second here, and the base comparison at origin/main under concurrent load was 1.44s.

thymikee and others added 2 commits November 17, 2026 19:56
… half

Splits PR #4584 down to the rule its reviewer asked for: the destination a
caller names with `--save-script` is judged before anything is created. The
daemon's own state-directory chain is removed from this change and moves to
#4649, which folds in #4620.

Kept: the walk of a caller's destination (one `lstat` per step, links walked,
`..` applied to the physical directory, a hop limit), per-level non-recursive
0700 creation that refuses before creating anything, absolute paths only, 0600
script files, and the remote client route answering the same rule.

Removed: the umask self-refusal, `ensurePrivateDaemonStateDir` and its chain
repair, the chain verdict and `state_chain_untrusted`, the one-spelling rule,
the own-state fallback, and daemon-state 0700 creation. Daemon-picked
destinations take `main`'s behavior and skip the walk, and the files that only
served them come back exactly as they are on `main`.

Restored, as a read-only walk check: the private-group exception. A
group-writable directory is accepted only where the caller owns it, its gid is
that user's primary group, no group record with that gid lists another user, no
other account claims that gid, and name services resolve accounts from local
files alone. A stock Ubuntu home is therefore usable; `ldap`, `sss`, `nis` or a
missing `/etc/nsswitch.conf` keeps the refusal.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The reduced route had carried two shortened strings: the no-clobber hint
dropped which flags replace the file, and the repair-commit hint dropped the
recovery a caller takes next. Both are user-facing text the reduced change has
no reason to touch, so they keep the words they have on `main`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@thymikee thymikee changed the title fix: refuse a save-script destination another local user can steer fix: judge a save-script destination another local user can steer Nov 17, 2026
thymikee and others added 2 commits November 17, 2026 20:02
The gate's typecheck project covers the suites that `tsc -b` alone left out, and
two of its findings were real: the gid a fixture directory inherits was kept
private while a sibling suite reads it, and a fake daemon could not answer a
request through the client's own materialization step, which is where a hostile
response is actually handled.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The eager-closure gate failed 65 façade entries: the account-file reader lived
in a module none of them evaluated, so an app-log reader paid for parsing
`/etc/passwd`. The gate's tolerance is for a statement-neutral split, and ADR
0027 gives growth no approval path, so the fix is the one it names — the new
code goes to a module every affected entry already evaluates.

`internal/owner-identity.ts` already answers who this process is on the host,
is in the merge-base closure of all 64 entries that reach the walk, and had
room under the 1,000-line cap. The reader keeps its name, its host seam, and
all 20 cases; the tests move with it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@thymikee
thymikee force-pushed the fix/save-script-ancestor-redirect branch from b9d6f76 to c4fa0ce Compare November 17, 2026 20:01
@thymikee

Copy link
Copy Markdown
Member Author

Reduced to the caller's destination rule

Split as agreed. Force-pushed with lease: head b9d6f76db2 → c4fa0ceb02, rebased onto origin/main (655ede2796). Draft, not for merge.

What this head carries. Only the rule a caller's --save-script destination is judged by: the walk (one lstat per step, links followed, .. applied to the physical directory, hop limit), per-level non-recursive 0700 creation that refuses before it creates anything, absolute paths only, 0600 script files, and the remote client route answering the same rule.

What left. The umask-002 self-refusal, ensurePrivateDaemonStateDir and the chain repair, the chain verdict and state_chain_untrusted, the one-spelling rule, the own-state fallback, and daemon-state 0700 creation. Those move to #4649, which folds in #4620. A destination this daemon picks for itself — the default recording, and sessions/remote-scripts/ — keeps main's behavior and is not walked, so the files that served only them are byte-identical to main.

The #4565 exception is back. A group-writable directory is accepted where the caller owns it, its gid is that user's primary group, no group record with that gid lists another user, no other account claims that gid, and /etc/nsswitch.conf resolves passwd, group and initgroups from files and systemd alone. A stock Ubuntu or Debian home is therefore a destination again. ldap, sss, nis, a database two lines name, or a missing /etc/nsswitch.conf keeps the refusal. 21 cases pin it, including the duplicate-line and same-gid-listing-another-user refusals.

One thing worth knowing before you read the diff. The account reads first lived in their own module, and check:affected then failed 64 façade entries, plus the shared-growth diagnostic with them: an app-log reader, a replay-port entry and the CLI and daemon hubs all started parsing /etc/passwd at import. The no-growth rule's tolerance is for a statement-neutral split, and ADR 0027 gives growth no approval path, so an allowlist row was not the fix. The reader now sits in internal/owner-identity.ts, which already answers who this process is on the host and is in the merge-base closure of all 64 entries that reach the walk. That file is 475 lines.

Size, stated plainly. The net production delta is +1,087 lines (1,151 added, 64 removed) across 8 files, against +1,356 across 13 files on b9d6f76db2. The reduction is real in shape — the daemon-state-directory half is gone, its three test files are deleted, and the five production files that only served it are back to main's bytes — but the insertions do not fall much further, because three things are nearly all of it: the walk at 725 lines in internal/atomic-file.ts, the restored reader at 276 in internal/owner-identity.ts, and the remote route at 63 in session-script-destination.ts. The writer is main plus one boolean, 30 changed lines: it decides whether the caller owns the destination, and that one answer chooses between the walked publish and the plain one.

Checks. pnpm check:affected --run --head HEAD passed every runnable lane, including unit at 20,603 tests. check:fallow, check:daemon-wire-compat (217 declarations, 4 changed, 0 removed, added or moved), check:adr-citations, check:doc-links and check:doc-prose are green. ADR 0016 now carries the caller rule and links #4649 for the state-directory question.

Outside this PR, found on the way. resolveMaterializedArtifactPath passes a daemon-supplied fileName to createWriteStream after taking its basename, so a name carrying a NUL byte raises ERR_INVALID_ARG_VALUE as an unhandled rejection on a non-script artifact. That code is main's and untouched here — this PR's remote route hashes its identifiers and stages under its own name, which its five hostile-daemon cases pin. I left it reported rather than fixed to keep this change narrow.

🤖 Generated with Claude Code

@apex-sentinel-staging

This comment has been minimized.

…rship

The private-group exception asked whether any other user holds a directory's
group id, and answered it from /etc/passwd and /etc/group. Those files are not
the whole answer on a systemd host: systemd-userdbd serves users and
memberships they never show, so `files systemd` in nsswitch.conf is not
evidence that the group files are complete, and an unseen user can hold the
gid. Reading the account files also made one destination answer differently per
host, which no other part of this rule does.

So the mode decides again, on every host: a group-writable ancestor that is not
sticky is refused as `shared_group_writable`, and the hint names the directory
and `chmod g-w <dir>`. Owner, sticky and root rules, per-level 0700 creation,
refusal before creation, and 0600 files are unchanged.

- owner-identity.ts returns to main; its private-group reader and its 21-case
  suite are removed, along with the account-file seeds only that probe needed.
- atomic-file.ts no longer carries a group-membership probe, and
  DirectoryAuthority no longer carries a gid nothing reads.
- A test now proves the judgment reads none of /etc/passwd, /etc/group or
  /etc/nsswitch.conf, so a host dependency cannot return unnoticed.
- The hostile-daemon artifactId case asserted a field shape production never
  answered, so it never ran. It now asserts the string destination, the bytes
  and the absence of scriptDeliveryFailure, per name and per world; four
  mutations (dropped delivery, steered destination, steered-and-quiet, write at
  a stray name) each fail it. The source also carried a raw NUL byte, which
  made git report a .test.ts file as binary.
- test-kit's host-ownership fixture loses the descriptor tracking, the three
  descriptor spies and HostOwnershipProbe, which no test used.

Refs #4565
@thymikee

Copy link
Copy Markdown
Member Author

Private-group exception dropped, per #4565 discussion

Head 80bc2129. Production is now +892/−67 over 7 files, of which +703/−40 is the host-kit walk (was +1,356/13). owner-identity.ts is back to main byte for byte, and with it the eager-closure growth the standalone reader caused.

Why it goes. The exception asked "does any other user hold this gid?" and answered from /etc/passwd and /etc/group. Those are not the whole answer on a systemd host: systemd-userdbd serves users and memberships those files never show, so nsswitch.conf naming only files systemd is not evidence that the group files are complete, and an unseen user can hold the gid. That also left one destination answering differently per host, which no other part of this rule does. A group-writable ancestor that is not sticky is refused again as shared_group_writable, with a hint naming the directory and chmod g-w <dir>. Owner, sticky and root rules, per-level 0700 creation, refusal before anything is created, and 0600 files are unchanged. DirectoryAuthority no longer carries a gid nothing reads.

This also fixes the Linux CI failure that started the investigation. atomic-file-parent.test.ts planted a 0775 directory and expected a refusal, which was correct on macOS and wrong on a Linux runner with a private primary group. With the exception gone the mode decides on every host, so the case is right everywhere again, and no test needs to simulate an account database. One new case pins that: the judgment must not read /etc/passwd, /etc/group or /etc/nsswitch.conf at all, so a host dependency cannot return unnoticed. The acceptance counterpart is now a mode pair — the same directory is refused at 0775 and accepted once chmod g-w leaves 0755.

Fixture orphans removed. serveHostOwnership keeps only the two lstat/stat spies its five callers use. throughDescriptorOnly, the descriptor tracking, the openSync/fstatSync/closeSync spies and HostOwnershipProbe are gone; so are the account-file seeds nothing uses any more.

The hostile-daemon assertion was dead, and is now live. Production answers savedScript as a string, and the test read .localPath off it, so the guard never ran. It now asserts the string destination, the downloaded bytes and the absence of scriptDeliveryFailure. It also gave every hostile name its own publication and scratch root: on a shared destination, iterations after the first fail with script_target_exists on a file the previous iteration legitimately wrote, which is exactly the noise a dropped delivery could hide in. .. is split into its own case — its download URL collapses to the server root, so the client reports a disclosed failure and writes nothing, which is correct behavior, not a steering refusal.

Proven by mutation, each failing the case and then reverted byte-identically:

Mutation Result
publish, but never report the path fails
resolve the destination from the response's artifactId fails
resolve from the response, and answer no path fails
publish at a name the response supplied, outside the destination fails

One file was unreviewable. daemon-artifacts-script-hostile-daemon.test.ts contained a raw NUL byte in a string literal, so git showed a .test.ts file as binary and GitHub rendered no diff. It now spells the same value 'evil\0.ad'.

A second defect, still out of scope and untouched by this PR: resolveMaterializedArtifactPath in daemon-artifacts.ts builds a write path from a daemon-supplied fileName, and a name carrying a NUL byte reaches createWriteStream, which raises ERR_INVALID_ARG_VALUE as an unhandled rejection and takes the client process down. That file has zero diff here, and the script route is not the crash path — it ignores fileName, which is what its hostile cases pin — so this is reported rather than fixed.

pnpm check:affected --run --head HEAD passed all 12 lanes at this head. PR stays draft.

Generated with Claude Code

@apex-sentinel-staging

This comment has been minimized.

@thymikee

Copy link
Copy Markdown
Member Author

Reviewed at 80bc212. The earlier findings from 5165fdb (#4584 (comment)) are mostly fixed: the state-repair and tombstone changes are gone, and the walk and 0600 publish now live in host-kit. One problem remains, and it needs a change before merge.

The new delivery-cause set in session-script-publication-capability.ts:34 (SCRIPT_DELIVERY_CAUSES and scriptDeliveryCause, :29-52) is wired into remoteScriptVersionDelivery (:343) and remoteCloseScript (:358-378). The only daemon errors that reach it come from the daemon-picked remote route, where session-script-writer.ts:135 sets a remoteScriptPath() and callerOwnsDestination is false, so publishFileSync runs. That route never runs the walk. So script_ancestor_untrusted and script_ancestry_replaced cannot happen there, and script_target_exists cannot happen on a random-UUID path. Every real daemon-side failure (EACCES, ENOSPC in sessions/remote-scripts) is now sent as cause: "script_download_failed". The client keeps it as-is (session-script-destination.ts:327-329), so it reaches --json, SDK and MCP output. The contract says cause is set "when the client found the failure" (contracts.ts:221), and nothing was downloaded. An agent that branches on cause will retry the download or blame the network instead of the daemon host, on a route the PR body says is unchanged from main. The hunk looks left over from the dropped state-chain half: its comment (:30-31) still names "the state-chain refusal of its own repair". It also re-spells host-kit's SCRIPT_ANCESTRY_REASONS. Two tests (session-close-script.test.ts:96-156 and session-script-writer-ancestry.test.ts around :503) mock writeSessionLog to return script_ancestor_untrusted for a remote session, a shape production cannot produce. The rule: the daemon names no delivery cause, because only the client finds one. Please restore session-script-publication-capability.ts and session-close-script.ts to main (remoteCloseScript(session, writeFailed: boolean)), delete those two tests, and keep the ADR sentence that the cause comes from the client's scriptDeliveryFailure.

Could the rest of the change shrink by about 300 lines with the same host-kit rule? I see: one walk loop that throws the AppError where it refuses (dropping WalkStep, the refused variant and the startWalk/stepThrough wrappers, about -80); one prepareScriptDestination(destination, {create}) with a single win32 branch and a literal 'windows' identity (about -25); publishScriptFileSync calling publishFileSync with an explicit directory so temp write, chmod and link-or-rename exist once (about -15); fewer wording variants for the untrusted reasons; and one-line ADR 0016 pointers instead of the long doc comments (about -130). If link-following use cases can be given up, refusing any symlink on the spelled path except root-owned links in root-owned directories would remove the queue emulation. That is a product call for the maintainer, not a demand.

Not blocking, take or leave: the {@link createStateLevels} at atomic-file.ts:763 points to a removed function; WalkOutcome.refused.ancestor/exposure are never read; publishDirectoryIdentity is only called in win32 branches, so its lstat branch is dead; parent_replaced has an empty wording reached through a special case (:733-739); replay-e2e.md:73 documents the destination rule only under open --save-script, though it now applies to every local route that names a file (open/close/session save-script, and replay --save-script including its default .healed.ad sibling); and ADR 0016:243 says a local write can fail with script_ancestry_replaced, but that is only the remote confirmation's refusal.

Two gaps in the hostile-daemon tests also need fixing before merge, because this is a remote-session change. They are not production bypasses. First, the hostile publication-ID, failure-text and mismatched-publication cases in daemon-artifacts-script-hostile-daemon.test.ts (:98, :214, :248) send scriptDeliveryReceipt on a wait request, but session-script-destination.ts:201 reads receipts only for batch. Those cases pass without reaching the receipt validator. Please send them as real batch requests, and add one case where a valid matching receipt publishes through the same seam. Second, namesFromTheResponse (:65-70) excludes the whole state and caller subtrees and observes nothing outside the scratch root, so a stray extra write there would go unnoticed. Please snapshot those trees too, and fail on any new entry other than the expected destination file.

Four open inline threads are fixed at this head and can be resolved, because those files now match main: the launchDaemonProcess ENOENT (#4584 (comment)), the ensureSessionDir stop (#4584 (comment)), the tombstone spelling split (#4584 (comment)) and the trailing-slash state-dir latch (#4584 (comment)). The arbitrary-reason gate thread (#4584 (comment)) is addressed by the cause set, but that set is the problem above, so it can be resolved once the set is removed.

The foreign-owner cases are simulated by spying on lstat/stat uids, so the sticky-bit and ownership guarantees rest on POSIX semantics rather than a run with a real second user. ACL, network-mount, bind-mount and systemd-userdb hosts are documented as out of scope and untested. The four Smoke Tests jobs (two plain, iOS and macOS) were still running or queued, so they have not failed. They barely touch this change, since they use no explicit script target. There are no conflicts. After the daemon cause set is removed, those Smoke Tests jobs need to finish green before merge.

The daemon named a delivery cause it could never have found. The cause set
read the daemon's own error reasons, and the only route it saw is the
daemon-picked remote path that runs no destination walk, so an EACCES or an
ENOSPC in its own state directory was disclosed as script_download_failed
and a client was sent to fix its ground for nothing. The cause now comes
only from the client that found the failure on its own filesystem:
session-script-publication-capability.ts and session-close-script.ts return
to main, and the two tests that mocked a refusal a remote session cannot
produce go with them.

Receipt cases sent a `wait` request, and the client reads a receipt only on
a batch, so the validator was never reached. They send a real batch now,
the mismatched-publication case names a second publication this same client
owns so a bypassed validator writes into it, and a matching receipt publishes
through the same seam to show the seam is honored at all. The tree is
snapshotted whole, `state` and `caller` included, so nothing a response
named can appear unasked for. Every case is proven to fail under a mutation.

The rule loses lines it carried twice: one walk loop that throws where it
refuses, long judgments replaced by one-line ADR 0016 pointers, one
prepareScriptDestination with one Windows branch, and the destination-ancestry
publisher reusing publishFileSync. The device-and-inode temp-sibling
comparison leaves production for the fault-injection support that needed it,
which is also where its two cases go. The empty parent_replaced wording, the
dangling createStateLevels link, and the dead lstat branch are fixed, and the
docs now say the check guards every local route that names a script file.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@thymikee
thymikee force-pushed the fix/save-script-ancestor-redirect branch from 4d994ee to e122458 Compare November 17, 2026 22:12
@thymikee

Copy link
Copy Markdown
Member Author

Third round pushed as e122458636.

The daemon cause set is gone. session-script-publication-capability.ts and session-close-script.ts are back to main, 0 lines of diff each. A cause now comes only from the client's scriptDeliveryFailure, which is the one place a destination refusal can actually be found; the daemon sees the daemon-picked remote path, which runs no walk. The three tests that mocked a refusal a remote session cannot produce went with the code, along with the imports that only served them.

Two hostile-daemon gaps are closed, with mutation proof. The receipt cases sent a wait, and session-script-destination.ts reads scriptDeliveryReceipt only on batch, so no case reached the validator. They send a real batch now. A matching receipt publishes through that same seam, so "nothing was written" can only come from a refusal and not from the receipt never being honored. The mismatched case names a second publication this same client owns, so a bypassed validator writes into that other recording instead of hashing to nothing. The whole tree is snapshotted, state and caller included, against an allow-predicate that names the client's own entries exactly, so a stray file in either subtree is reported rather than excused. Seven mutations, each killing the expected case and each restored byte-identically: dropped delivery, steered destination, receipt never honored, validator bypassed, write at the response's file name, predicate matched on text only, sibling staged beside the spelling.

The rule lost lines it carried twice. One walk loop that throws where it refuses, instead of a WalkStep channel threaded through three functions; prepareScriptDestination as one function with one Windows branch; the script publisher reusing publishFileSync; long judgments reduced to one-line ADR 0016 pointers. sameDirectory left production for test-kit, which is the only caller that needed a device-and-inode comparison, and its two cases moved with it. atomic-file.ts is 689 lines from 833, and production is +683/−63 over 6 files (+620 net). Fixed on the way: the empty parent_replaced wording, the dangling {@link createStateLevels}, and a lstat branch nothing could reach.

Docs now say the check guards every local route that names a script file, not just open --save-script. check:affected is green over all 12 lanes; check:fallow, wire-compat and production-exports are clean.

@thymikee

Copy link
Copy Markdown
Member Author

Reviewed at e122458. The earlier findings from #4584 (comment) are fixed: both daemon modules match main, the receipt cases are real batches with a positive control, and the walk refactor keeps every refusal rule. A separate security cross-check found no bypass by another local user or a hostile daemon. Three small items remain before the label, listed below. Also apply these notes: the mismatched-publication test in daemon-artifacts-script-hostile-daemon.test.ts loops over HOSTILE_NAMES but sends the same request each time, so one send is enough; the "dropped and disclosed" test never asserts data.scriptDeliveryFailure, so assert it or drop "and disclosed" from the title; isPublishTemporarySiblingOf in filesystem-boundary-faults.ts re-spells the temp-name rule that host-kit's isAtomicPublishTemporaryPath already owns, so delegate the name half to it or drop the unused re-export; and the "short" refusal in atomic-file.ts is thrown twice, once in walkSpelledParent and again in scriptDestination, so it can live in walkSpelledParent alone.

The five earlier inline threads no longer apply, because those files have no diff against main at this commit, so please resolve them: #4584 (comment), #4584 (comment), #4584 (comment), #4584 (comment) and #4584 (comment).

The foreign-owner and sticky-bit guarantees are still exercised by spying on lstat uids, not by a run with a real second local user, and ACL, network-mount and bind-mount hosts are documented as out of scope and untested.

The Coverage job failed on one test, the coordinate errorTaxonomy case in test/integration/interaction-contract/coordinate.contract.test.ts, which timed out after 5000ms (1 failed, 20943 passed). That route is coordinate interaction dispatch, which this diff does not touch, so the failure looks unrelated. Coverage is re-running and has not finished, so that attribution rests on the first run's log. Numeric Ranges now passes. There are no conflicts.

Before the label, please fix these three items:

  1. Daemon-picked mode behavior. atomic-file.ts:115 now chmods every explicit mode in the shared publisher, but main chmods only a preserved existing mode. With mode=0600 and umask 0200, main writes 0400 and this head writes 0600. That changes daemon-picked remote scripts and other generic callers, which this PR is meant to leave as on main. It is not an exposure. Please restore main's generic behavior, and apply the exact-mode correction only on the caller-script path.
  2. The external-write check in the hostile-daemon test. snapshotTree records names and kinds only. clientStateEntries (:63-71) accepts any extra hash-shaped record. Writes outside the scratch root are checked only against a few fixed targets. Please put the scratch root inside a controlled outer tree and snapshot that tree. Assert the exact expected record name, and include content or identity in the snapshot, so that any extra or modified file fails the test.
  3. Docs wording in replay-e2e.md:73-80.
    • Say "prevent other users from modifying entries" instead of "keep its contents closed to everyone else", because 0755 ancestors are accepted.
    • Keep the sticky-traversal exception in the group-writable refusal sentence.
    • Scope "leaves no directories" to an ancestry refusal.
    • Say "caller-named destinations" in the routes sentence, because daemon-picked defaults stay outside the walk.

@apex-sentinel-staging

This comment has been minimized.

The shared publisher chmod'ed every asked-for mode, so a caller that passes
`mode` now got the exact number where main gave it the mode ANDed with the
umask: 0600 under umask 0200 became 0600 instead of 0400. Daemon-picked
remote scripts and every other generic caller are meant to be unchanged by
this PR, so the generic route pays the umask again and only the caller-script
publisher asks for the number, through `exactMode`. Both sides are pinned: a
generic caller at umask 0200 answers 0400, a caller script answers 0600, and
dropping either half of the split fails the case that names it.

The hostile-daemon suite now watches a tree the client is planted DEEP inside,
so a `../../../../` name lands where the snapshot can see it instead of in
the system's scratch directory, and it snapshots WHAT each entry IS — bytes,
link target, kind — so a steered write through an allowed name is reported
rather than allowed by its name. Records are matched by exact name and by the
fields they carry, not by the shape of their digest. Four mutations fail the
new oracle: an extra record of the client's own shape, one appended byte in a
published file, a write outside the scratch root inside the watched tree, and
one above it.

The rule loses the rest of its duplicated wording: the `short` refusal lives
in the walk alone, which now creates what it stopped below and judges the
completed chain, and test-kit's temp-sibling predicate asks host-kit's
`isAtomicPublishTemporaryPath` for the name half. The mismatched-publication
case sends once, the dropped-receipt case says what it now asserts, and the
docs name the ancestry refusal, the sticky exception in the group-writable
sentence, and caller-named destinations.

Refs #4565

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@thymikee

Copy link
Copy Markdown
Member Author

Reviewed at e122458. Fixed at fe11597. The two daemon modules still match main.

1. Daemon-picked mode behavior. publishFileSync chmods only a preserved existing mode again, so main's behavior is back for every generic caller. The script publisher asks for the number through a new exactMode option, and publishScriptFileSync is the only caller that passes it. Two new cases pin both halves: a generic caller passing mode: 0o600 under umask 0200 answers 0400, and a caller script under umask 0200 and 0277 answers 0600. Removing the correction fails the exactMode case and both script cases; applying it to every asked-for mode fails the generic one.

2. The external-write check. The scratch root is now planted at <outer>/one/two/scratch, so a ../../../../ name resolves INSIDE the watched tree instead of in the system's scratch directory, where nothing was watching. The snapshot records what each entry IS — a full sha256 of the bytes, the raw link target, or the kind — and entriesThatMoved diffs both directions: a name that appeared, one that vanished, and one whose bytes or target changed. clientStateEntries is gone; records are matched by exact name and by every field they carry (destination, force, revision, and staging: [] after a publish), so a second hash-shaped record, a moved destination, or a revision that did not move all fail. Names resolving above the watched tree are checked for existence directly. Four mutations now fail this suite where they passed before: an extra record of the client's own shape, one appended byte in a published file, a write outside the scratch root inside the outer tree, and a write above the outer tree at a hostile name. The seven proofs from the last round still fail, re-run against this head.

3. Docs wording. All four: the group-permission sentence says a directory must prevent other users from modifying entries in it; the sticky-traversal exception is restated inside the group-writable refusal; the no-directories claim is scoped to a destination the ancestry check refuses; the routes sentence names caller-named destinations and says where no path is named at all, the daemon writes its own default location without this check.

The four notes are applied. The mismatched-publication case sends ONE batch, because nothing in that answer depends on a hostile string. The dropped-receipt case is retitled to what it asserts, and it now asserts scriptDeliveryFailure: a name that matches nothing of this client's discloses no failure, and the one hostile name past the receipt's own string budget discloses the receipt's malformed shape with an empty publicationId — never this client's delivery failure for a publication it never opened. isPublishTemporarySiblingOf asks host-kit's isAtomicPublishTemporaryPath for the name half and keeps only the dev:ino directory question, asked with the value's own directory so only the shape is judged there. The re-export stays: script-staging-failure.ts in the daemon's support imports it. The short refusal is thrown in walkSpelledParent alone, which now also creates the levels it stopped below and re-judges the completed chain, so scriptDestination asks only its own question.

pnpm check:affected --run --head HEAD is green across all twelve lanes, including the build and unit lanes. check:fallow reports no issues in the six changed files, wire-compat reports four changed and no removals, and the layering scan passes. Typecheck covers examples/sdk too.

The five inline threads you listed are resolved. The foreign-owner and sticky-bit guarantees are still exercised by spying on lstat, and ACL, network-mount and bind-mount hosts stay documented as out of scope.

🤖 Generated with Claude Code

The hostile-daemon cases read the expected record name from the record
directory AFTER the delivery they judge, in both places that name matters:
`ownRecordFiles` built the allowed list from a fresh listing, and the expected
`name` came from `recordNames(world)[0]`. A client that renamed its own
legitimate record to another hash-shaped name after publishing therefore handed
the assertion its own new name, and the rename passed. The pin now comes from
before the answer — `recordNames` at that moment, or the fixture's
`recordsAtSend` — and the same pin feeds both assertions, so an added or renamed
record is reported instead of accepted. `recordsAsWritten` compares the current
listing to the pin first, so a moved record fails as the name it moved to rather
than as a missing file.

Proven both ways with one mutation: after all publish work finishes, rename the
record to an unrelated hash-shaped name. Against the previous head that fails 1
of 7; against this one it fails 5.

Refs #4565

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@thymikee

Copy link
Copy Markdown
Member Author

Fixed at ecb174e. Test-only; production is still +687/−62.

ownRecordFiles took its names from a caller's PIN, and the expected name is that same pinned value — never a listing taken after the delivery. Cases that let a daemon answer now pin from the fixture's recordsAtSend, captured at send; the receipt cases pin with recordNames before the batch is answered. recordsAsWritten compares the current listing to the pin first, so an added record, or one moved to another hash-shaped name, fails as the name it moved to instead of surfacing as a missing file.

Proven with the rename mutation. After a publish has finished all of its work — bytes in place, staging released, record written at its final fields — the client's own record file is renamed to an unrelated hash-shaped name, and nothing else about it changes. Against fe11597, where the name was read after delivery, that fails 1 of 7 tests. Against ecb174e, it fails 5: both cases whose names were pinned report the entry that moved, and the three whose recordsAsWritten listing no longer matches the pin report the name it moved to.

pnpm check:affected --run --head HEAD is green across all twelve lanes. The source is restored byte-identically after the mutation runs, and this head contains the test change only.

🤖 Generated with Claude Code

@apex-sentinel-staging

This comment has been minimized.

@thymikee

Copy link
Copy Markdown
Member Author

The code is ready at ecb174e: the findings from the earlier review (#4584 (comment)) are fixed, and a separate security cross-check found no redirection by another local user or a hostile daemon. One docs sentence needs a fix before the label, described at the end. Not blocking, and you can take or leave these: the new publishScriptFileSync doc at https://github.com/callstack/agent-device/blob/ecb174e/packages/host-kit/src/internal/atomic-file.ts#L132 says the umask would "widen" the mode away from 0600, but a umask only clears bits, so it narrows it (as the comment at lines 115-118 already says), and the scratchWorld comment at https://github.com/callstack/agent-device/blob/ecb174e/packages/remote/src/__tests__/daemon-artifacts-script-hostile-daemon.test.ts#L61 mis-states where ../../../../tmp/evil lands (it resolves outside outer, and only escapeTargets catches it), so you could fix the comment and add path.resolve(process.cwd(), name) to escapeTargets to cover a client that resolves a hostile name against its working directory. The foreign-owner and sticky-bit guarantees are covered by lstat uid spies, not by a run with a real second local user. All 22 checks pass at ecb174e. The two failures at the previous head (Android Smoke Tests and Coverage) passed on re-run, and they exercise Android form typing and interaction dispatch, which this diff does not touch, so they look unrelated. There are no conflicts.

Before the label: replay-e2e.md:74 says, without qualification, that the script file is created at mode 0600. That is exact on the local caller-script route, because publishScriptFileSync sets the mode exactly. The remote route does not go through it. session-script-destination.ts:419-424 downloads into a private file that artifact-download.ts:74-82 opens with mode 0600 and no chmod. Under umask 0200 that gives 0400, and a restrictive owner-bit umask can stop remote staging altogether. The downloader is unchanged from main, so this is not a new exposure, only a doc promise the remote route does not keep. Please say that local recordings get exactly 0600, while remote recordings request 0600 and the umask can narrow it. Keep the downloader and the generic publisher as on main.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@thymikee

Copy link
Copy Markdown
Member Author

Ready at 723941b. Against ecb174e, this head changes only docs and one test helper (3 files, +13/−6). The one remaining item from the earlier review (#4584 (comment)) is fixed. replay-e2e.md now states exact 0600 for local recordings only. It says a remote download requests 0600, which the umask can narrow or block. The downloader and the generic publisher are unchanged. The two optional notes landed too: "narrow" in the publishScriptFileSync doc, and escapeTargets now also checks names resolved from the working directory, while skipping directories that already existed. The production code is identical to ecb174e, which passed the full review and the security cross-check, and ecb174e had all 22 checks green. CI is re-running on this head, and nothing is failing. There are no conflicts, and nothing else blocks merge.

@thymikee thymikee added the ready-for-human Valid work that needs human implementation, judgment, or maintainer merge label Nov 17, 2026
@apex-sentinel-staging

Copy link
Copy Markdown

Apex Sentinel · ✅ Approve · review of 723941b

Since the review of ecb174e: 0 new · 0 still open · 0 no longer raised

No open findings.

Reviewed with this repository's own Sentinel configuration (655ede2796e5).

💬 Talking to Sentinel

In a finding's thread, reply with:

Command What it does
@apex-sentinel-staging note <context> Adds context; Sentinel re-checks the cited code
@apex-sentinel-staging pivot <why> Says the finding is wrong; Sentinel re-checks the cited code
@apex-sentinel-staging like Marks the finding as useful
@apex-sentinel-staging dislike <why> Marks it as not useful, so it is not raised again

On this conversation, comment @apex-sentinel-staging review to run a full review once again.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready-for-human Valid work that needs human implementation, judgment, or maintainer merge

Projects

None yet

Development

Successfully merging this pull request may close these issues.

--save-script destination can be redirected by other local users who can write an ancestor directory

2 participants