Skip to content

app: pin nested repositories and refuse unprotected worktrees in the Linux sandbox - #3364

Merged
ashhart merged 8 commits into
ashhart:mainfrom
eXsoR65:sandbox-git-gaps
Nov 21, 2026
Merged

ashhart merged 8 commits into
ashhart:mainfrom
eXsoR65:sandbox-git-gaps

Conversation

@eXsoR65

@eXsoR65 eXsoR65 commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

What this changes

Closes the two git gaps that kept the Linux command sandbox out of #1122. Reopened after #1699 was
closed for inactivity, refreshed onto upstream/main d6c4cce60. The bubblewrap backend is
back on top of current main, with nested repositories pinned and worktrees that reach outside the
workspace refused. It stays opt-in (TENSORFOLD_LINUX_SANDBOX=1); Linux commands keep failing
closed without it.

Fixes #1693.

What was broken

  • Nested repositories. Only the workspace root's .git was pinned. A repository nested under the
    workspace, or a submodule, let a sandboxed command rewrite its config and plant hooks.
  • Worktrees. When .git is a file (gitdir: …), the real metadata can live outside the
    workspace. The profile never mounted it, and the command ran anyway — unprotected, not refused.

What it does now

  • Every .git under the workspace is pinned like the root: writable bind, read-only config,
    config.worktree, hooks, info. The walk covers dependency folders and symlinked directories
    inside the workspace, and it refuses at its bounds (32 levels, 50,000 directories) instead of
    pinning a subset. A path it cannot inspect (permission denied, I/O error) also refuses, because
    skipping it could hide a repository; only a missing entry means there is nothing to pin.
  • Control entries a repository does not have yet are mounted read-only from empty placeholders, so a
    writable run cannot create config.worktree, hooks or info.
  • A worktree's .git pointer file is mounted read-only, so a run cannot repoint the workspace at
    metadata it built itself.
  • A worktree whose resolved gitdir or shared commondir lies outside the workspace is refused; so is
    a workspace with no repository, because bwrap cannot deny creating .git by name. Refusal returns
    code 126 with the reason, and nothing runs. Both suites cover the outside-commondir case: a
    worktree inside the workspace whose shared directory points outside it.
  • New tests fail without each fix: removing the nested walk fails "nested .git pinned"; restoring the
    pre-change worktree handling fails "Missing expected exception"; dropping the pointer freeze fails
    the conformance pointer-rewrite case, and dropping the placeholders fails the control-file case.

Receipt

  • Tests on head 6f76c40a: npm test 857/857 passed, 0 failed; typecheck clean;
    linux-port-docs guard clean; git diff --check clean.
  • Conformance (real namespace, opt-in): filesystem containment, git controls, nested repository
    config/hooks read-only
    , worktree refusal, network denial/allowing, cancellation.
  • Not run: a host without bwrap; the conformance test prints a skip there instead of claiming a
    pass. No distro other than Debian 13.
  • CI: running on this head; the branch's previous head passed every check (desktop,
    desktop-linux, appimage-linux, mobile, pytest, zig*, benchmark-clients).
  • Coordination: atc overlap reports no conflicting pull requests for the seven changed files,
    though it marks its own coverage incomplete (otherPullRequestsLimit, pullRequestsLimit,
    pullFilesUnavailable, timeLimit). atc who found no open pull request or worktree touching any
    of the seven files.

Limits

  • Opt-in until a maintainer decides it defaults on; commands stay refused otherwise.
  • The remaining deviation is the workspace-without-repository case: it is refused, not protected by a
    mount rule.
  • Still no clean-machine install test here; this branch changes no packaging.

@ashhart

ashhart commented Nov 16, 2026

Copy link
Copy Markdown
Owner

This change is careful about the default: it stays opt-in, keeps commands refused while the opt-in is off, and sets up namespace isolation with the network denied except for the network profiles and read-only mounts ordered so the workspace wins. Worktrees that point at metadata outside the workspace at startup are refused, and nested repositories found by the bounded walk get their configuration and hook files pinned read-only. Those parts look sound.

Two things keep me from calling it safe in every case. First, the walk that looks for nested repositories quietly stops at its depth limit and skips dependency folders; a repository beyond that depth or inside a skipped folder is left with writable configuration and hooks even though the change promises to pin every repository and to refuse rather than protect only some. A limit that refuses is fine; a limit that silently gives up is not. Second, a worktree's pointer file is left writable. During a writable run, a command can build its own metadata in the writable scratch area and repoint the workspace at it; the ordinary Git commands the host runs afterwards would then follow that pointer and can execute a planted hook. The protection here holds only at the moment the sandbox starts, not for the whole session.

Please make the depth bound refuse instead of skip and stop excluding those folders, pin the worktree pointer read-only and prevent new control files from appearing, and add coverage for deep and nested worktrees, shared directories that live outside the workspace, and pointer rewrites. Once those gaps are closed and the documentation lists the remaining limitations honestly, this is in good shape.

  • Synthena

@eXsoR65

eXsoR65 commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor Author

@ashhart

Both gaps are closed on head 6429e9a0 (13 checks pass, 2 nightly skips).

Walk bounds refuse instead of skipping. No folder is excluded any more: dependency folders and symlinked directories that resolve inside the workspace are walked, symlinks resolving outside are not followed (they are not mounted, so nothing is reachable through them), and real paths are de-duplicated. Hitting 32 directory levels or 50,000 directories throws and the command is refused with the reason, so a repository can never end up silently unpinned.

Worktree pointers are frozen. A gitdir: pointer is mounted read-only over itself, so a run cannot repoint the workspace at metadata it built in scratch. Control entries a repository does not have yet (config.worktree, hooks, info) are mounted read-only from empty placeholders, so they cannot be created either. One consequence worth naming: bwrap creates those mountpoints in .git as empty files/directories on first use, so a repository can gain an empty, read-only config.worktree; I documented that rather than hide it.

Coverage added. Unit: a repository inside node_modules, a repository eight levels deep, depth-bound refusal, and an inner worktree whose pointer is read-only. Conformance on a real namespace: creating .git/config.worktree fails and the placeholder stays empty; a repository inside node_modules still refuses a hooks write; rewriting an inner worktree's pointer fails and the pointer keeps its original content. Each case fails when its fix is reverted.

Remaining limits, now in docs/linux-port/STATUS.md: non-repository workspaces are refused; the two walk bounds above; the placeholder side effect; a worktree whose gitdir or commondir is outside the workspace is refused; opt-in stays until you decide otherwise. The review document marks the probe and mount-order blockers closed, runtime visibility (item 4) still open, and validation as partly closed — the Linux CI lane exists but does not install bubblewrap, so device-level conformance needs a host that has it.

Tests on this head: 857/857, typecheck clean, conformance passing.

@ashhart

ashhart commented Nov 16, 2026

Copy link
Copy Markdown
Owner

Thanks @eXsoR65, both gaps are closed as described. The walk bounds now refuse, nothing is skipped, the worktree pointer and missing control entries are frozen read-only, and each new test fails with its fix reverted. Documenting the placeholder side effect is the right call.

One piece of the coverage we asked for is still missing: a worktree whose gitdir is inside the workspace but whose commondir resolves outside it. pinWorktree refuses that case, but neither linux-sandbox.test.ts nor the conformance suite exercises the refusal. Please add that test. The rest is ours: we'll fold the multi-line comments into single lines and run the conformance suite on a Linux host with bubblewrap before landing.

  • Synthena

@eXsoR65

eXsoR65 commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor Author

@ashhart

Added on head 3092c000: a worktree that sits inside the workspace but whose commondir resolves outside it.

  • Unit: the walk finds shared-worktree/.git, resolves its commondir to <root>/elsewhere-shared/.git, and linuxSandboxArgv throws "The sandbox refuses a worktree whose shared git directory is outside the workspace".
  • Conformance: the same fixture, run through runSandboxedCommand from the main workspace, returns 126 with that reason and nothing executes.

One nuance worth reporting: reverting the explicit commondir check still refuses that workspace, but later and with the generic "will not mount git metadata outside the workspace" message — so the new test is what keeps the specific guard and its reason in place.

Tests on this head: 857/857, typecheck clean, conformance passing (shared-directory case included).

On the multi-line comments: agreed, and note the linux-port-docs guard only constrains documented claims, so folding comments cannot trip it.

@ashhart

ashhart commented Nov 17, 2026

Copy link
Copy Markdown
Owner

Thanks @eXsoR65. The shared-directory tests answer the latest request, including the distinction between the specific guard and the later generic refusal. We will handle the remaining source-file split, comments and CI. We also need to close the paths the repository walk still skips on inspection errors and finish the Linux confinement check before landing.

  • Synthena

@eXsoR65

eXsoR65 commented Nov 17, 2026

Copy link
Copy Markdown
Contributor Author

Closed the remaining walk skips on head 6f76c40a.

Inspection failures now refuse instead of being treated as "nothing here": a readdir that returns permission denied or an I/O error, a symlink whose realpath/stat fails for any reason other than absence, and a .git entry that cannot be lstated. Only ENOENT/ENOTDIR still means "no repository to pin" — that cannot hide writable config or hooks because there is nothing there.

Coverage, both failing if the fix is reverted:

  • Unit: a chmod 000 directory inside the workspace makes linuxSandboxArgv throw "The sandbox cannot inspect …/unreadable-dir"; the test restores the mode in a finally.
  • Conformance: the same directory in a real namespace returns 126 with that reason, nothing runs, and the cleanup restores the mode.

Documented in docs/linux-port/STATUS.md under limits. Tests on this head: 857/857, typecheck clean, conformance passing (uninspectable-path case included).

On the source-file split, comments and CI: understood, those are yours — say if you would rather I send any of them.

@ashhart

ashhart commented Nov 18, 2026

Copy link
Copy Markdown
Owner

Hi @eXsoR65, thanks for closing the walk's inspection-error gaps on the current head. Together with the earlier work, that covers what we asked: the depth and directory bounds now refuse instead of skipping, no folders are excluded, the worktree pointer and missing control entries are frozen read-only, and the deep, nested, shared-directory and pointer-rewrite cases each have a test that fails when its fix is reverted. The source split, comments and CI stay with us, and we'll finish the Linux confinement check on a host with the sandbox runtime before landing. Nothing further is needed from you.

  • Synthena

ashhart added a commit to eXsoR65/TensorFold that referenced this pull request Nov 19, 2026
ashhart added a commit to eXsoR65/TensorFold that referenced this pull request Nov 19, 2026
ashhart and others added 5 commits November 21, 2026 04:38
…Linux sandbox

A writable workspace pinned only its own .git, so a nested repository or
submodule could have its config and hooks changed from inside the sandbox,
and a worktree pointer could name a gitdir the profile never mounted. Pin
every .git under the workspace with the same read-only control entries,
and refuse a worktree whose gitdir or shared directory is outside the
workspace. A workspace with no repository keeps commands refused, because
bwrap cannot deny creating .git by name.
…trol files

Review follow-up: the nested-repository walk now refuses at its depth and
directory bounds instead of skipping deeper entries and dependency folders,
a worktree pointer is mounted read-only so a writable run cannot repoint the
workspace at metadata it built, and control entries a repository lacks are
mounted read-only from empty placeholders so they cannot be created.
Review follow-up: the unit and conformance suites now build a worktree
inside the workspace whose commondir resolves outside it, and assert the
refusal and its reason.
Review follow-up: a permission or I/O error while walking used to skip
the directory, which could hide a repository with writable config and
hooks. Missing entries still mean nothing to pin; every other inspection
failure refuses the command.
@ashhart
ashhart merged commit 5954c6c into ashhart:main Nov 21, 2026
14 of 16 checks passed
@ashhart

ashhart commented Nov 21, 2026

Copy link
Copy Markdown
Owner

Thank you @eXsoR65. This is merged into main with your commits under your own name, so you're listed as a TensorFold contributor. It ships in the next release, credited to you in the changelog and the release notes.

ashhart added a commit that referenced this pull request Nov 21, 2026
ashhart added a commit that referenced this pull request Nov 21, 2026
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.

Issue: Linux command sandbox — close the git and worktree gaps

2 participants