Skip to content

/cso audits repositories that track symlinks - #4540

Closed
nathanshox wants to merge 3 commits into
garrytan:mainfrom
nathanshox:fix/cso-in-repo-symlinks
Closed

nathanshox wants to merge 3 commits into
garrytan:mainfrom
nathanshox:fix/cso-in-repo-symlinks

Conversation

@nathanshox

@nathanshox nathanshox commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

/cso can now audit a repository that tracks symlinks, such as a CLAUDE.md that links to AGENTS.md. Today one such link stops every run. A link that stays inside the repository is now listed as an exclusion in coverage and never followed, and its target is audited under its own path. Users need no new flag or setting.

What this fixes

gstack-cso start fails with UNSAFE_PATH "Symlink or special source file" whenever the checkout tracks a symlink, even a relative link to another file in the same repository. Many repositories link CLAUDE.md to AGENTS.md so that every agent reads one instruction file.

--paths doesn't help either. The special-file walk in rejectSpecialFiles covers the whole checkout whatever the scope, and the capture loop refuses a symlink outside --paths with Special source input.

Why the refusal looks unintended

No comment, doc or commit explains why a contained link is refused. The tests cover only links that escape the repository (/etc/passwd, an outside directory) or are broken. The rest of the codebase already treats contained links as safe:

What changed

In-repository links (fix(cso): record in-repository symlinks as explicit snapshot exclusions)

  • A link is accepted only when its target is relative and resolves, lexically and through every intermediate link (realpath), to a regular file or directory inside the repository.
  • It gets one manifest entry: originalHash: not-read, transformation IN_REPOSITORY_LINK (excluded: ...). So it appears under snapshot-inputs exclusions, not as a gap. Nothing is followed or copied; the target is captured under its own path.
  • When nothing at the target is audited, the link is withheld and coverage counts it as a gap. That covers a target the snapshot doesn't capture (an ignored file, .git/config) and, under --paths, a target outside the scope.
  • assertEntriesStable re-checks the link's inode, its target and the repository path it resolves to. A link that is retargeted, replaced or deleted during capture, or whose intermediate links come to resolve elsewhere, fails with SNAPSHOT_RACE. The target is read twice, so a link that reuses the inode between the two lstat calls is caught too.
  • The Git blob hash of a link is the hash of its target string, so base comparison stays exact. An unchanged link is not a changed path; a retargeted link, or a file replaced by a link, is.
  • A link inside a host dependency tree such as node_modules is excluded like the rest of that tree, and a link outside --paths is listed as out of scope. Both are bound by identity without resolving their target, so a dangling node_modules/.bin link does not block the run.
  • A --paths entry that is itself a symlink is refused with INVALID_ARGUMENT, naming the path it points to; otherwise the scope would audit only the link.
  • Still refused: absolute targets, ../ escapes, broken or looping links, links that resolve to a FIFO or device, and chains that escape through an ignored link.

A link's target is not part of the snapshot's original identity. That matches every other not-read entry, since the link is never followed.

Scope (fix(cso): a special file outside --paths no longer blocks the audit)

  • The special-file walk covers only the --paths scope and its ancestors. It compares names without case, so a directory whose on-disk case differs from Git's spelling is still walked.
  • So an escaping link or an untracked FIFO outside the scope no longer blocks the run. The link is recorded as out of scope without resolving its target, and a change during capture still fails.
  • Inside the scope, an escaping, broken or special link is still refused, and so is a tracked path outside the scope that has been replaced on disk by a FIFO.

This is a separate commit, so it can be dropped if you'd rather keep the walk repository-wide. The first commit stands on its own; the changelog's last clause about an untracked FIFO or a symlink outside --paths would go with it.

Tests

New cases in test/cso-ordinary-repos.test.ts, next to the nested-repository ones:

  • start on a repository with a tracked file link and an untracked directory link succeeds. Both links are listed as exclusions, there are no gaps, and the snapshot holds the targets but not the links.
  • A link that points up out of its directory (web/README.md -> ../README.md) is accepted and is not a changed path against a base that already had it.
  • A retargeted link, and a file replaced by a link with the same bytes, are changed paths against the base.
  • A link to an ignored file and a link to .git/config are gaps, and the ignored target is not in the snapshot.
  • A broken link in an unignored node_modules is excluded, not refused.
  • Negative controls: absolute, escaping, broken, self-looping, a link to an ignored FIFO, and an in-repo link whose chain escapes through an ignored link. All are refused with UNSAFE_PATH.
  • A link retargeted to another contained file, or deleted, just before the final stability check fails with SNAPSHOT_RACE.
  • Under --paths: a link entry is refused, an in-scope link to an out-of-scope target is a gap, an out-of-scope link is listed as out of scope, and an entry deleted from disk is recorded as a deletion.
  • Second commit: an escaping link and a FIFO outside the scope are withheld; widening the scope refuses each of them.

Existing symlink tests in test/cso-snapshot-state.test.ts and test/cso-ordinary-repos.test.ts pass unchanged.

Checks (macOS)

  • bun run typecheck: no errors. bun run typecheck:test: no new diagnostics.
  • bun run format:cso:check: clean.
  • test/cso-ordinary-repos.test.ts and test/cso-snapshot-state.test.ts: all pass at each commit.
  • All 58 test/cso-*.test.ts files: 836 pass, 35 skip, 3 fail. The same 3 fail on main on this machine: the archive-cache lease recovery, the forged TAP summary witness and the Python runner import order.
  • On the repository that hit this, gstack-cso start --scope auth --offline now starts, and its links are listed as excluded: symlink to a file or directory inside this repository. Before the fix, the same command failed with UNSAFE_PATH.

A repository that tracks any symlink cannot be audited: gstack-cso start
fails with UNSAFE_PATH "Symlink or special source file", even when the
link is a relative pointer to another file in the same repository, such
as a CLAUDE.md that points to AGENTS.md. The capture loop refused the
same links a second time with "Special source input". Nothing records
why ordinary links were refused; the existing tests only cover links
that escape the repository or are broken.

Accept a link whose relative target resolves, lexically and through
every intermediate link, to a regular file or directory inside the
repository, and record it like a nested repository boundary: one
manifest entry, originalHash not-read, an excluded: transformation that
discloses the omission in snapshot-inputs coverage, nothing followed or
copied, and an identity check during capture so a retargeted, replaced
or deleted link fails with SNAPSHOT_RACE. The identity covers the inode,
the target and the repository path the link resolves to, so an
intermediate link that moves the chain elsewhere also fails, and the
target is re-read to rule out inode reuse between the two lstat calls.
The target is captured under its own path. Git hashes a link as its
target string, so base comparison stays exact.

When nothing at the target is audited, the link is withheld instead, so
coverage counts it as a gap. That covers a target the snapshot does not
capture, such as an ignored file or Git metadata, and a target outside
--paths.

A link inside a host dependency tree such as node_modules, or outside
--paths, is bound by identity without resolving its target: excluded
with the rest of the dependency tree, or listed as out of scope. A
dangling node_modules/.bin link does not block the run. A --paths entry
that is itself a symlink would audit only the link, so it is refused
with the path it points to.

Absolute, escaping, broken and cyclic links, links that resolve to a
FIFO or device, and chains that escape through an ignored link are
still refused.
--paths scopes what an audit reads, but the special-file walk covers the
whole checkout. One escaping link or untracked FIFO anywhere in the
repository blocks a run that would never read it, and narrowing --paths
cannot get around it.

Walk only the audit scope and its ancestors, comparing names without
case: on a case-insensitive filesystem the on-disk name can differ from
Git's spelling, and walking too much is the safe error. A symlink
outside the scope, escaping or not, is then recorded as withheld, bound
to its identity without resolving its target, so it still appears in
the out-of-scope list and a change during capture still fails closed.

Inside the scope an escaping, broken or special link is still refused,
and so is a tracked path outside the scope that has been replaced on
disk by a FIFO.
@trunk-io

trunk-io Bot commented Nov 19, 2026

Copy link
Copy Markdown

Merging to main in this repository is managed by Trunk.

  • To merge this pull request, check the box to the left or comment /trunk merge below.

After your PR is submitted to the merge queue, this comment will be automatically updated with its status. If the PR fails, failure details will also be posted here

@capy-ai

capy-ai Bot commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

Thank you @nathanshox. I confirmed this by reading main (92c6c8a): rejectSpecialFiles in lib/cso/snapshot.ts walks the whole checkout and refuses any file that isn't a regular file, so one tracked symlink stops every /cso run, whatever --paths says. gstack's own repo tracks one (connect-chrome). We're picking this up. Because it's a larger change to /cso's snapshot code, the maintainers will rebuild it on main with a security review and credit you, rather than merge this branch. This PR will be closed with a link once that lands. Labelled bug, S2.

@capy-ai capy-ai Bot added bug Something isn't working S2 Severity 2: broken workflow with no workaround labels Nov 20, 2026
capy-ai Bot added a commit that referenced this pull request Nov 20, 2026
…4540) (#4567)

`/cso` no longer stops on a repository that tracks a symlink, such as
gstack's own `connect-chrome` or `CLAUDE.md -> AGENTS.md`: each link is
listed in the report by its target string, or as withheld when the
target matches a secret pattern, and it is never followed.

Why: a security audit that refuses ordinary repositories audits nothing,
and following a link would let it point the scanner at `/etc/passwd` or
out through `..`.

Nothing to do. FIFOs, sockets and devices are still refused, and
`--paths` now limits that check to the audited directories.

Co-authored-by: nathanshox <[email protected]>
Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
Review-Finding: p1/specialist-batched#1 reader=security
Review-Finding: p1/red-team#3 reader=red-team
Review-Finding: p2/red-team#1 reader=red-team
Review-Finding: p3/outside-voice#1 reader=outside-voice
@capy-ai

capy-ai Bot commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

Thanks @nathanshox for finding this and for the first fix. We rebuilt it on current main as #4567, which is now merged, and credited you as co-author. /cso now audits repositories that track symlinks. It records each link by its target string and never follows it. A target that looks like a secret is withheld from the report. FIFOs, sockets and devices inside the audited paths are still refused, and --paths now limits that check. We're closing this PR in favour of #4567.

@capy-ai capy-ai Bot closed this Nov 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working S2 Severity 2: broken workflow with no workaround

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant