[PR #5891] [MERGED] fix(mari): post-execution scan closes the sandbox's new-sensitive-file review gap #6982

Closed
opened 2026-10-23 04:15:27 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/5891
Author: @kolacheee
Created: 9/6/2026
Status: ✅ Merged
Merged: 9/6/2026
Merged by: @kolacheee

Base: staging ← Head: fix/5786-sandbox-postexec-scan


📝 Commits (2)

  • 9852fba fix(mari): post-execution scan closes the sandbox's new-sensitive-file gap (#5786)
  • 0fac65d fix(mari): count staged cards and bound the scan walk (CodeRabbit)

📊 Changes

6 files changed (+763 additions, -64 deletions)

View changed files

📝 CHANGELOG.md (+2 -0)
📝 packages/server/src/services/professor-mari/workspace-agent.service.ts (+226 -55)
📝 packages/server/src/services/professor-mari/workspace-change-review.service.ts (+1 -1)
📝 packages/server/src/services/professor-mari/workspace-shell-sandbox.ts (+236 -3)
📝 scripts/regressions/mari-guard-sibling-paths.regression.ts (+295 -4)
📝 scripts/regressions/mari-write-readback.regression.ts (+3 -1)

📄 Description

Linked issue

Closes #5786

Why this change

The shell sandbox's write-deny list is built only from sensitive paths that exist at spawn, so a command could create a brand-new supply-chain-sensitive file — a package.json in a fresh directory, a root Dockerfile that was absent — with no deny rule and no user review. The #5777 pre-execution heuristic catches common write shapes, but it is a command-string check by design; any shape it misses created a reviewed-class file with zero review. That is a false negative on the sensitive-file approval floor, found during the #5776–#5778 adversarial review.

What changed

Every sandboxed bash run is now bracketed by the safety net the issue proposed:

  • Fingerprint before, re-walk after. snapshotSensitiveWorkspaceFiles records every sensitive file (sha256; bytes retained up to the 512 KB review cap; >8 MB fingerprinted by size, never read); detectUnreviewedSensitiveChanges finds whatever changed unreviewed, whatever command shape produced it. New sensitive directories and newly created skip-named dirs (a fresh dist/) are walked in full — no rule covers them.
  • Revert, then stage through the normal pipeline. Created files are unlinked, modified files restored byte-exact from the snapshot, then stageSensitiveFileChange (re-reading the restored disk as "before") turns the change into an ordinary approval card. Capped at 5 cards per run — everything past the cap is still reverted.
  • Unforgeable reporting. Staged lines live in the engine region of the bash output (after the Sandbox: header, before the stdout:/stderr: markers), and isStagedSensitiveMutation recognizes them only there. Every string the engine interpolates into that region — the command, file paths — is flattened to one line first, so neither a newline-bearing command nor a newline-bearing filename can mint a forged engine line or truncate the region. Behavioral fixtures prove the classifications through the real resolver.

Hardening from two adversarial review rounds (15 findings, all applied)

  • Never opens non-regular files: symlinks fingerprinted by identity and never followed — this scan runs in the unsandboxed server process, and following a link named package.json would have read arbitrary host files into an approval card; FIFOs can no longer hang the run; device targets are never read.
  • Per-entry failure containment on both walks: a chmod-000 file still produces a diffable fingerprint and its siblings are still caught; uninspectable subtrees are reported loudly, never silently dropped.
  • The data-loss guard: a "created" hit under a subtree the pre-run walk could not inspect is never deleted — it may be a pre-existing file that was merely invisible — and is reported for the user instead.
  • Bytes, not text: snapshot retention and restore are byte-exact (a utf8 round-trip corrupts bun.lockb-style binaries); staging happens only when content provably round-trips as text; an 8 MB aggregate retention budget bounds a scan against thousands of planted manifests.
  • Bounded teardown: abort and timeout settle within a grace window with SIGKILL escalation, so a TERM-trapping child can neither suppress the scan nor race it with dying writes.

Honest residuals (follow-up issue filed)

  • Package-store trees (node_modules, .pnpm, .pnpm-store) stay outside the scan for cost; writing there was always sandbox-permitted for builds, and the review floor defends the manifests that control what gets installed. Named in the CHANGELOG too.
  • On macOS, seatbelt has no PID-namespace teardown, so a process surviving the escalated kill could write after the scan; Linux is covered by bubblewrap's --die-with-parent/--unshare-all.

Validation

  • pnpm check passes locally
  • Container (Docker / Podman) built and ran without issue
  • Ran the app, clicked through the changes manually
  • Checked edge cases (light + dark mode, mobile viewport, empty states, error paths)
  • Above manual verification completed (describe below)
  • Read and followed CONTRIBUTING.md

Manual verification notes

  • mari-guard-sibling-paths lane extended with functional temp-workspace coverage: the exact reported gap (fresh-dir package.json), tampering, covered-directory silence, new-sensitive-dir descent, a manifest planted in a pre-existing dist/, binary content, a smuggled sensitive-named symlink, oversize files, per-entry containment and the snapshot-blind attribution guard (POSIX-gated), and the node_modules negative — plus behavioral staged/echo-forgery/nested-marker fixtures through resolveWorkspaceMutationVerification, and wiring pins.
  • All Mari guard lanes (mari-guard-sibling-paths, mari-write-readback, mari-ask-latch, mari-understood-request, prompt) pass; server lint clean; zero-error build.
  • Not exercised against live sandbox backends end-to-end (bwrap/seatbelt need their platforms); the scan/revert/stage layer is platform-independent and functionally tested, and the sandbox spawn machinery is unchanged.

Docs and release impact

  • No docs changes needed (CHANGELOG entry included, honestly scoped)
  • Updated docs (README / CONTRIBUTING / android/README / CHANGELOG) as needed
  • Translated docs on the docs-i18n branch updated to match, or a [docs-i18n] follow-up issue opened (see CONTRIBUTING.md § Translated documentation)
  • Version/release files updated (only if this PR includes a version bump)

UI evidence (if applicable)

Not applicable — the user-visible surface is the existing sensitive-change approval card, reached through a new path.

Summary by CodeRabbit

  • Bug Fixes

    • Improved workspace sandbox protection by detecting newly created or modified sensitive files after shell commands run.
    • Unreviewed dependency, installer, launcher, and workflow files are now reverted and sent through the normal approval process.
    • Added safeguards for interrupted or timed-out commands to ensure workspace changes are handled safely.
    • Preserved permitted writes in package storage directories used during builds.
  • Tests

    • Added regression coverage for sensitive-file detection, rollback behavior, approval staging, binary files, oversized files, and interrupted commands.

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Pasta-Devs/Marinara-Engine/pull/5891 **Author:** [@kolacheee](https://github.com/kolacheee) **Created:** 9/6/2026 **Status:** ✅ Merged **Merged:** 9/6/2026 **Merged by:** [@kolacheee](https://github.com/kolacheee) **Base:** `staging` ← **Head:** `fix/5786-sandbox-postexec-scan` --- ### 📝 Commits (2) - [`9852fba`](https://github.com/Pasta-Devs/Marinara-Engine/commit/9852fbae7c2350d40cc68456d84ac9f5f3d770a4) fix(mari): post-execution scan closes the sandbox's new-sensitive-file gap (#5786) - [`0fac65d`](https://github.com/Pasta-Devs/Marinara-Engine/commit/0fac65dc7de74ce84d389f73d508b71c73b44524) fix(mari): count staged cards and bound the scan walk (CodeRabbit) ### 📊 Changes **6 files changed** (+763 additions, -64 deletions) <details> <summary>View changed files</summary> 📝 `CHANGELOG.md` (+2 -0) 📝 `packages/server/src/services/professor-mari/workspace-agent.service.ts` (+226 -55) 📝 `packages/server/src/services/professor-mari/workspace-change-review.service.ts` (+1 -1) 📝 `packages/server/src/services/professor-mari/workspace-shell-sandbox.ts` (+236 -3) 📝 `scripts/regressions/mari-guard-sibling-paths.regression.ts` (+295 -4) 📝 `scripts/regressions/mari-write-readback.regression.ts` (+3 -1) </details> ### 📄 Description ## Linked issue Closes #5786 ## Why this change The shell sandbox's write-deny list is built only from sensitive paths that **exist at spawn**, so a command could create a brand-new supply-chain-sensitive file — a `package.json` in a fresh directory, a root `Dockerfile` that was absent — with no deny rule and no user review. The #5777 pre-execution heuristic catches common write shapes, but it is a command-string check by design; any shape it misses created a reviewed-class file with zero review. That is a false negative on the sensitive-file approval floor, found during the #5776–#5778 adversarial review. ## What changed Every sandboxed bash run is now bracketed by the safety net the issue proposed: - **Fingerprint before, re-walk after.** `snapshotSensitiveWorkspaceFiles` records every sensitive file (sha256; bytes retained up to the 512 KB review cap; >8 MB fingerprinted by size, never read); `detectUnreviewedSensitiveChanges` finds whatever changed unreviewed, whatever command shape produced it. New sensitive *directories* and newly created skip-named dirs (a fresh `dist/`) are walked in full — no rule covers them. - **Revert, then stage through the normal pipeline.** Created files are unlinked, modified files restored byte-exact from the snapshot, then `stageSensitiveFileChange` (re-reading the restored disk as "before") turns the change into an ordinary approval card. Capped at 5 cards per run — everything past the cap is still reverted. - **Unforgeable reporting.** Staged lines live in the engine region of the bash output (after the `Sandbox:` header, before the `stdout:`/`stderr:` markers), and `isStagedSensitiveMutation` recognizes them only there. Every string the engine interpolates into that region — the command, file paths — is flattened to one line first, so neither a newline-bearing command nor a newline-bearing filename can mint a forged engine line or truncate the region. Behavioral fixtures prove the classifications through the real resolver. ## Hardening from two adversarial review rounds (15 findings, all applied) - **Never opens non-regular files**: symlinks fingerprinted by identity and never followed — this scan runs in the *unsandboxed* server process, and following a link named `package.json` would have read arbitrary host files into an approval card; FIFOs can no longer hang the run; device targets are never read. - **Per-entry failure containment on both walks**: a chmod-000 file still produces a diffable fingerprint and its siblings are still caught; uninspectable subtrees are reported loudly, never silently dropped. - **The data-loss guard**: a "created" hit under a subtree the *pre-run* walk could not inspect is never deleted — it may be a pre-existing file that was merely invisible — and is reported for the user instead. - **Bytes, not text**: snapshot retention and restore are byte-exact (a utf8 round-trip corrupts `bun.lockb`-style binaries); staging happens only when content provably round-trips as text; an 8 MB aggregate retention budget bounds a scan against thousands of planted manifests. - **Bounded teardown**: abort *and* timeout settle within a grace window with SIGKILL escalation, so a TERM-trapping child can neither suppress the scan nor race it with dying writes. ## Honest residuals (follow-up issue filed) - Package-store trees (`node_modules`, `.pnpm`, `.pnpm-store`) stay outside the scan for cost; writing there was always sandbox-permitted for builds, and the review floor defends the *manifests* that control what gets installed. Named in the CHANGELOG too. - On macOS, seatbelt has no PID-namespace teardown, so a process surviving the escalated kill could write after the scan; Linux is covered by bubblewrap's `--die-with-parent`/`--unshare-all`. ## Validation - [ ] `pnpm check` passes locally - [ ] Container (Docker / Podman) built and ran without issue - [ ] Ran the app, clicked through the changes manually - [ ] Checked edge cases (light + dark mode, mobile viewport, empty states, error paths) - [ ] Above manual verification completed (describe below) - [ ] Read and followed `CONTRIBUTING.md` ### Manual verification notes - `mari-guard-sibling-paths` lane extended with functional temp-workspace coverage: the exact reported gap (fresh-dir `package.json`), tampering, covered-directory silence, new-sensitive-dir descent, a manifest planted in a pre-existing `dist/`, binary content, a smuggled sensitive-named symlink, oversize files, per-entry containment and the snapshot-blind attribution guard (POSIX-gated), and the `node_modules` negative — plus behavioral staged/echo-forgery/nested-marker fixtures through `resolveWorkspaceMutationVerification`, and wiring pins. - All Mari guard lanes (`mari-guard-sibling-paths`, `mari-write-readback`, `mari-ask-latch`, `mari-understood-request`, `prompt`) pass; server lint clean; zero-error build. - **Not exercised against live sandbox backends end-to-end** (bwrap/seatbelt need their platforms); the scan/revert/stage layer is platform-independent and functionally tested, and the sandbox spawn machinery is unchanged. ## Docs and release impact - [x] No docs changes needed (CHANGELOG entry included, honestly scoped) - [ ] Updated docs (README / CONTRIBUTING / android/README / CHANGELOG) as needed - [ ] Translated docs on the `docs-i18n` branch updated to match, or a `[docs-i18n]` follow-up issue opened (see CONTRIBUTING.md § Translated documentation) - [ ] Version/release files updated (only if this PR includes a version bump) ## UI evidence (if applicable) Not applicable — the user-visible surface is the existing sensitive-change approval card, reached through a new path. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **Bug Fixes** - Improved workspace sandbox protection by detecting newly created or modified sensitive files after shell commands run. - Unreviewed dependency, installer, launcher, and workflow files are now reverted and sent through the normal approval process. - Added safeguards for interrupted or timed-out commands to ensure workspace changes are handled safely. - Preserved permitted writes in package storage directories used during builds. - **Tests** - Added regression coverage for sensitive-file detection, rollback behavior, approval staging, binary files, oversized files, and interrupted commands. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-10-23 04:15:27 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Pasta-Devs/Marinara-Engine#6982
No description provided.