Skip to content

fix(result): keep Result file reads inside their Result through symlinks - #467

Open
wjsoj wants to merge 2 commits into
hypit-ai:mainfrom
wjsoj:fix/result-file-symlink-containment
Open

wjsoj wants to merge 2 commits into
hypit-ai:mainfrom
wjsoj:fix/result-file-symlink-containment

Conversation

@wjsoj

@wjsoj wjsoj commented Nov 5, 2026 •

Copy link
Copy Markdown
Member

Closes #477

Scope

This PR closes the symbolic link vector for build-file reads only. It does not address external-file references: a file: URI in an external-file value (packages/result/src/types.ts, packages/result/src/file-reference.ts, and openFile in packages/result/src/node/store.ts) is still an unrestricted local read under the same untrusted-archive threat model. That remains a separate open issue and needs a design decision (for example, whether external-file URIs from an imported Result should be trusted at all, or limited to an allowed set of roots).

Problem

FileBuildResultRepository checks each build-file path lexically: assertBuildResultPath rejects .., absolute paths and backslashes, and containedResultPath rejects a resolved path that leaves the Result directory. The existing test "filesystem decodes Result and writer data before exposing it" shows that ../outside.mp4 is meant to be refused.

A symbolic link gets past both checks, whether it is a file or directory inside the Result directory, or the Build directory or its date bucket itself (.hypit/results/2026-09-02/bld_X -> /home/user). openFile (and the read of a composite Output's value document) then follow the link to whatever file it targets. openFile uses file.build ?? build, so a file reference naming such a linked Build is enough.

Impact

Severity: low to medium. Result directories are not always produced locally. The complex-explainer example has users extract a downloaded media.tar.gz straight into the project, and that archive includes a portable Result under .hypit/results. tar restores symbolic links as they are stored. If an archive contains files/video.mp4 -> ~/.ssh/id_rsa, a linked directory, or a linked Build directory, that host file is then read as the Output's bytes:

  • hypit get <build> <output> --to ... copies it into the export destination.
  • A Run that reuses the historical Output attaches it to the new Build. From there it can be copied into new Results or uploaded to a remote provider as an input resource.

Reproduction

const directory = buildResultDirectory(root, build);
await mkdir(join(directory, "files"), { recursive: true });
await symlink("/etc/passwd", join(directory, "files", "video.mp4"));
await new FileBuildResultRepository(root).openFile(build,
  { kind: "build-file", path: "files/video.mp4", size: 1, mediaType: "video/mp4" });
// streams /etc/passwd

Linked Build directory variant:

await mkdir(join(buildResultDirectory(root, build), ".."), { recursive: true });
await symlink(homedir(), buildResultDirectory(root, build));
await new FileBuildResultRepository(root).openFile(build,
  { kind: "build-file", build, path: ".ssh/id_rsa", size: 1, mediaType: "application/octet-stream" });
// streams ~/.ssh/id_rsa

Fix

physicalResultPath keeps the lexical check, then measures containment from the Result root: the allowed directory is join(realpath(<results root>), <bucket>, <build>), and realpath(target) must stay strictly below it. Only the Result root is resolved physically, so a linked Build directory or date bucket no longer moves the allowed directory along with it. The resolved path is the one opened.

  • A missing file still resolves to undefined (unavailable). ENOTDIR and ELOOP from realpath are treated the same way as ENOENT.
  • A link that stays inside the same Result keeps working.
  • A Result root that is itself a symlink (for example .hypit/results moved to another disk) is unaffected.

The value-document read of composite Outputs uses the same check, and a missing value document now fails with a direct "is missing value document" error instead of re-reading the lexical path.

Tests

  • filesystem repository refuses Result files that resolve outside their Result covers a file symlink out, a directory symlink out, a ranged read, a missing file and an in-Result alias.
  • filesystem repository refuses Result files under a Build or date directory linked outside the root covers a linked Build directory, a linked date bucket, and a file reference naming a linked Build from another Build. It fails before this change ("Missing expected rejection").
  • filesystem repository reads Result files when the Result root is itself a symbolic link covers a linked Result root and a path below a regular file (ENOTDIR, unavailable rather than an error).
  • pnpm check: passes.
  • node --import tsx --test packages/result/test/*.test.ts: 16 tests, 16 pass. packages/cli/test/*.test.ts: 124 tests, 123 pass, 0 fail.
  • pnpm test: 1150 tests, 1127 pass, 23 skipped, 0 fail.

wjsoj added 2 commits November 5, 2026 08:04
A Build Result path is checked lexically against its Result directory,
but a symbolic link inside the directory (for example one restored from
an extracted project archive) still led openFile and value-document
reads to any file on the host. Resolve the physical path and require it
to stay below the Result directory before opening it.
Containment was checked against the physical Build directory, so a Build
directory or date bucket that was itself a symbolic link (for example one
pointing at the home directory) made every file below that link count as
inside the Result. Resolve only the Result root physically, append the
lexical bucket and Build, and require the physical file to stay below
that.

Treat ENOTDIR and ELOOP like a missing file, and report a missing value
document directly instead of re-reading the lexical path.
SanHsien added a commit to SanHsien/hypit that referenced this pull request Nov 7, 2026
…i#460-hypit-ai#474; advance watermarks

No PR was merged upstream, so nothing was cherry-picked: 11 PRs = 5 defer
(hypit-ai#464 hypit-ai#466 hypit-ai#467 hypit-ai#469 hypit-ai#471, pinned in monitored) + 6 not applicable; 5 issues =
1 defer (hypit-ai#473) + 4 not applicable. hypit-ai#464 npm pin is moot because the fork gates
publish jobs to upstream (b641cce). ROADMAP row updated with the deferred items.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
SanHsien added a commit to SanHsien/hypit that referenced this pull request Nov 7, 2026
… symlinks (port of upstream PR hypit-ai#467)

Ported before upstream merge: containedResultPath was lexical only, so a
symlink or a linked Build/date directory in an extracted Result archive led
openFile and value-document reads outside the Result. physicalResultPath
compares realpath against the Result root plus the Build location. Tests
from the PR (directory links as junctions on Windows) fail before (2) and
pass after (13/13). external-file is untouched, as upstream. Replace with
upstream's version once hypit-ai#467 is merged.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Symbolic links in a Build Result let hypit get read files outside the Result

1 participant