[PR #8770] [MERGED] fix(server): harden Professor Mari's shell sandbox and code-check routing #8792

Closed
opened 2026-11-07 17:39:44 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/8770
Author: @SpicyMarinara
Created: 11/3/2026
Status: ✅ Merged
Merged: 11/3/2026
Merged by: @SpicyMarinara

Base: staging ← Head: fix/hardening-mari-shell-sandbox


📝 Commits (5)

  • c20411c fix(server): harden Professor Mari's macOS shell sandbox and code-check routing
  • ee75e99 fix(server): pin the shell sandbox's workspace and move aside new root .git/.env
  • 1401784 fix(server): report a moved-aside root .git or .env even when the post-run scan fails
  • 539512b test(server): a positive control for the code check's build-output rule
  • 560f01a test(server): the sibling-paths source pin allows the new log line before the rethrow

📊 Changes

6 files changed (+251 additions, -48 deletions)

View changed files

📝 packages/server/src/services/mari-db/mari-db.service.ts (+11 -3)
📝 packages/server/src/services/professor-mari/workspace-agent.service.ts (+4 -3)
📝 packages/server/src/services/professor-mari/workspace-commands.ts (+14 -4)
📝 packages/server/src/services/professor-mari/workspace-shell-sandbox.ts (+91 -21)
📝 scripts/regressions/mari-guard-sibling-paths.regression.ts (+7 -2)
📝 scripts/regressions/professor-mari/sandbox-secrets.regression.ts (+124 -15)

📄 Description

Important

Contributions target staging. Only SpicyMarinara may promote this repository's staging branch or a same-repository hotfix/* branch to main.

Linked issue

Closes #8769

Why this change

Professor Mari's shell commands are model-driven, so they run inside an OS sandbox. SECURITY.md lists sandbox escape, unintended host command execution and secret exposure as trust boundaries, and docs/home/professor-mari.md says .env, the key file and Git's internal files are not available to her.

On macOS, the sandbox only blocked reads of those paths. A sandboxed command could still change, rename, delete or create them, or move the workspace folder out from under the rules. Git config, .env and the encryption key are all used later outside the sandbox.

On Linux, the sandbox already blocks writes to these paths when they exist as a command starts. A root .git or .env that a command created had no rule, and host git and the server's .env loader read exactly those two.

Separately, the shell decided whether mari code check goes to the sandbox by reading its arguments differently from the CLI that runs it. Some spellings therefore ran pnpm check outside the sandbox, with the server's environment.

This PR closes these gaps.

What changed

  • What stops working: on macOS, a Professor Mari shell command now fails with 'Operation not permitted' if it changes, renames, deletes or creates .git, .env or .env.* files, or .encryption-key, in the workspace. The same applies if it renames the workspace folder or a folder that holds one of those files. .env.example, .env.sample and .env.template stay writable. On Linux, a root .git or .env that a command creates is now renamed to <name>.unreviewed-<random> once the command ends, and the command output says so.
  • On macOS, the shell sandbox now blocks writes as well as reads for .git, .env and .env.* files and .encryption-key. It refuses these names where they do not exist yet, and it pins the workspace folder and the folders holding these files so they cannot be renamed. Ordinary files and folders in the workspace stay writable.
  • On Linux, writes to these paths were already blocked when they existed at the start of a command. Now a root .git or .env created during a command is moved aside after the run, the same way a replaced package-store link already is.
  • mari code check now runs in the shell sandbox exactly when the CLI would run pnpm check. mari code check -- --help runs sandboxed, or is refused where no sandbox exists. A real help flag still shows the help text without running anything.
  • The sandbox-secrets regression now covers writing, renaming, deleting and creating these paths, renaming the workspace on macOS, the post-run move on every platform, and the code-check routing.

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

  • On macOS, with Professor Mari in Auto mode, ask her to do ordinary shell work in the repo: list files, edit a scratch file, make and remove a folder, and run node. Manually verify that all of it still works.
  • On macOS, manually verify that mari code check from Mari's shell still completes a real pnpm check in a checkout that has packages/server/data/.encryption-key. The workspace folder and the folders holding the key are now pinned.
  • Manually verify that mari code check --help from Mari's shell still shows the code help text.
  • On a Linux host with bubblewrap, run node ./scripts/run-regressions.mjs --filter scripts/regressions/professor-mari/sandbox-secrets.regression.ts and manually verify that it passes. It was only run with macos-seatbelt.
  • On a Linux host with bubblewrap, in a workspace with no root .git, have Mari run a shell command that creates one. Manually verify that afterwards it has been renamed to .git.unreviewed-<random> and that the command output reports this.

Agent-run checks (for reference; each regression was shown to fail without its fix):

  • node ./scripts/pnpm-runner.mjs build:shared: exit 0
  • node ./scripts/run-regressions.mjs --filter scripts/regressions/professor-mari/sandbox-secrets.regression.ts (round 2 fix): PASSED with macos-seatbelt
  • same regression with only the round-2 source files stashed (git stash push -- workspace-shell-sandbox.ts workspace-commands.ts): FAILED: 'so is the workspace itself'. Source restored with git stash pop.
  • scratch copy of the regression without the static root-pin assertion, on unfixed source: FAILED: 'the moved workspace never shows the key' (the key was printed). Copy deleted.
  • scratch copy with the live sandbox section skipped, on unfixed source: FAILED: 'TypeError: quarantineNewRootSecrets is not a function'. Copy deleted.
  • node ./scripts/run-regressions.mjs --filter scripts/regressions/professor-mari: 27/27 passed
  • node ./scripts/run-regressions.mjs --filter scripts/regressions/mari-guard-sibling-paths.regression.ts: PASSED (includes the aftermath-order source assertion)
  • node ./scripts/run-regressions.mjs --filter scripts/regressions/mari/: 39/39 passed
  • node ./scripts/run-regressions.mjs --filter shell-sandbox: 1/1 passed
  • node ./scripts/run-regressions.mjs --filter security: 32/32 passed
  • node ./scripts/pnpm-runner.mjs --filter @marinara-engine/server lint (tsc -b shared, tsc --noEmit, character-activity tsconfig): exit 0
  • npx tsc --noEmit -p tsconfig.json (root check:e2e-types): exit 0

Independent adversarial review: changes, 3 issue(s), all addressed in a fix-up pass.

Known limits and risks:

  • The Linux bubblewrap branch was not run here, because no Linux host was available. The post-run quarantine is platform-independent code and was tested by calling it directly, not through a live bubblewrap run.
  • On Linux, a root .git or .env that a command creates exists on the host until the command ends. Host tools running at that same moment could still read it. Forbidden names created below the root are not caught on Linux. The ponytail: comment in linuxBubblewrapArgs records this limit and the upgrade path: mount placeholders over a missing root .git and .env, then remove the empty mount points afterwards.
  • On macOS the rules match paths. A folder moved into the workspace from the sandbox's temp folder can therefore still bring these names in below the root. The root names themselves are refused, and host git and the .env loader read only the root's.
  • The quarantine cannot tell a command's .git or .env from one another process creates at the root during the run, for example a user running git init at that moment. Such a file is renamed, not deleted, and the output tells Mari to ask the user.
  • Pinning the workspace root also refuses changes to the workspace folder's own metadata, such as chmod or touch on the folder itself. Files and folders inside it are unaffected.
  • On Linux, a sandboxed command can still rename packages/server/data inside the workspace, because the masking mounts move with the folder. The key stays masked. This behaviour is older than this PR and unchanged.
  • The suggested -c core.fsmonitor=false and core.hooksPath flags on unsandboxed git calls were not added. The root cause is fixed in the sandbox, and those flags cannot stop all config-driven execution, such as filter drivers.
  • The name rule matches ASCII case only. Existing paths are matched canonically, and ignorable code points on HFS+ volumes are not covered.
  • The full pnpm check was not run inside the new sandbox.

Docs and release impact

  • No docs changes needed
  • Updated docs (README / CONTRIBUTING / android/README) 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)

🤖 Generated with Claude Code


🔄 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/8770 **Author:** [@SpicyMarinara](https://github.com/SpicyMarinara) **Created:** 11/3/2026 **Status:** ✅ Merged **Merged:** 11/3/2026 **Merged by:** [@SpicyMarinara](https://github.com/SpicyMarinara) **Base:** `staging` ← **Head:** `fix/hardening-mari-shell-sandbox` --- ### 📝 Commits (5) - [`c20411c`](https://github.com/Pasta-Devs/Marinara-Engine/commit/c20411ce455338d665f8a6adcc5190c5eaea4724) fix(server): harden Professor Mari's macOS shell sandbox and code-check routing - [`ee75e99`](https://github.com/Pasta-Devs/Marinara-Engine/commit/ee75e9941a8ac4cfdea4d897d13734c157776151) fix(server): pin the shell sandbox's workspace and move aside new root .git/.env - [`1401784`](https://github.com/Pasta-Devs/Marinara-Engine/commit/1401784e4dc2879f98cb42720a3be3854cb667b8) fix(server): report a moved-aside root .git or .env even when the post-run scan fails - [`539512b`](https://github.com/Pasta-Devs/Marinara-Engine/commit/539512bf7cc6420bb4b32522d7af53b4fbd5ad66) test(server): a positive control for the code check's build-output rule - [`560f01a`](https://github.com/Pasta-Devs/Marinara-Engine/commit/560f01afd39ed4b3d075056e09ac3dcf94cabc4e) test(server): the sibling-paths source pin allows the new log line before the rethrow ### 📊 Changes **6 files changed** (+251 additions, -48 deletions) <details> <summary>View changed files</summary> 📝 `packages/server/src/services/mari-db/mari-db.service.ts` (+11 -3) 📝 `packages/server/src/services/professor-mari/workspace-agent.service.ts` (+4 -3) 📝 `packages/server/src/services/professor-mari/workspace-commands.ts` (+14 -4) 📝 `packages/server/src/services/professor-mari/workspace-shell-sandbox.ts` (+91 -21) 📝 `scripts/regressions/mari-guard-sibling-paths.regression.ts` (+7 -2) 📝 `scripts/regressions/professor-mari/sandbox-secrets.regression.ts` (+124 -15) </details> ### 📄 Description > [!IMPORTANT] > Contributions target `staging`. Only `SpicyMarinara` may promote this repository's `staging` branch or a same-repository `hotfix/*` branch to `main`. ## Linked issue Closes #8769 ## Why this change Professor Mari's shell commands are model-driven, so they run inside an OS sandbox. SECURITY.md lists sandbox escape, unintended host command execution and secret exposure as trust boundaries, and docs/home/professor-mari.md says `.env`, the key file and Git's internal files are not available to her. On macOS, the sandbox only blocked reads of those paths. A sandboxed command could still change, rename, delete or create them, or move the workspace folder out from under the rules. Git config, `.env` and the encryption key are all used later outside the sandbox. On Linux, the sandbox already blocks writes to these paths when they exist as a command starts. A root `.git` or `.env` that a command created had no rule, and host git and the server's `.env` loader read exactly those two. Separately, the shell decided whether `mari code check` goes to the sandbox by reading its arguments differently from the CLI that runs it. Some spellings therefore ran pnpm check outside the sandbox, with the server's environment. This PR closes these gaps. ## What changed - What stops working: on macOS, a Professor Mari shell command now fails with 'Operation not permitted' if it changes, renames, deletes or creates `.git`, `.env` or `.env.*` files, or `.encryption-key`, in the workspace. The same applies if it renames the workspace folder or a folder that holds one of those files. `.env.example`, `.env.sample` and `.env.template` stay writable. On Linux, a root `.git` or `.env` that a command creates is now renamed to `<name>.unreviewed-<random>` once the command ends, and the command output says so. - On macOS, the shell sandbox now blocks writes as well as reads for `.git`, `.env` and `.env.*` files and `.encryption-key`. It refuses these names where they do not exist yet, and it pins the workspace folder and the folders holding these files so they cannot be renamed. Ordinary files and folders in the workspace stay writable. - On Linux, writes to these paths were already blocked when they existed at the start of a command. Now a root `.git` or `.env` created during a command is moved aside after the run, the same way a replaced package-store link already is. - `mari code check` now runs in the shell sandbox exactly when the CLI would run pnpm check. `mari code check -- --help` runs sandboxed, or is refused where no sandbox exists. A real help flag still shows the help text without running anything. - The sandbox-secrets regression now covers writing, renaming, deleting and creating these paths, renaming the workspace on macOS, the post-run move on every platform, and the code-check routing. ## Validation <!-- These are human tasks. Never tick a box for a check that was not actually performed. --> - [ ] `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 - On macOS, with Professor Mari in Auto mode, ask her to do ordinary shell work in the repo: list files, edit a scratch file, make and remove a folder, and run node. Manually verify that all of it still works. - On macOS, manually verify that `mari code check` from Mari's shell still completes a real `pnpm check` in a checkout that has `packages/server/data/.encryption-key`. The workspace folder and the folders holding the key are now pinned. - Manually verify that `mari code check --help` from Mari's shell still shows the code help text. - On a Linux host with bubblewrap, run `node ./scripts/run-regressions.mjs --filter scripts/regressions/professor-mari/sandbox-secrets.regression.ts` and manually verify that it passes. It was only run with macos-seatbelt. - On a Linux host with bubblewrap, in a workspace with no root `.git`, have Mari run a shell command that creates one. Manually verify that afterwards it has been renamed to `.git.unreviewed-<random>` and that the command output reports this. Agent-run checks (for reference; each regression was shown to fail without its fix): - `node ./scripts/pnpm-runner.mjs build:shared`: exit 0 - `node ./scripts/run-regressions.mjs --filter scripts/regressions/professor-mari/sandbox-secrets.regression.ts (round 2 fix)`: PASSED with macos-seatbelt - `same regression with only the round-2 source files stashed (git stash push -- workspace-shell-sandbox.ts workspace-commands.ts)`: FAILED: 'so is the workspace itself'. Source restored with git stash pop. - `scratch copy of the regression without the static root-pin assertion, on unfixed source`: FAILED: 'the moved workspace never shows the key' (the key was printed). Copy deleted. - `scratch copy with the live sandbox section skipped, on unfixed source`: FAILED: 'TypeError: quarantineNewRootSecrets is not a function'. Copy deleted. - `node ./scripts/run-regressions.mjs --filter scripts/regressions/professor-mari`: 27/27 passed - `node ./scripts/run-regressions.mjs --filter scripts/regressions/mari-guard-sibling-paths.regression.ts`: PASSED (includes the aftermath-order source assertion) - `node ./scripts/run-regressions.mjs --filter scripts/regressions/mari/`: 39/39 passed - `node ./scripts/run-regressions.mjs --filter shell-sandbox`: 1/1 passed - `node ./scripts/run-regressions.mjs --filter security`: 32/32 passed - `node ./scripts/pnpm-runner.mjs --filter @marinara-engine/server lint (tsc -b shared, tsc --noEmit, character-activity tsconfig)`: exit 0 - `npx tsc --noEmit -p tsconfig.json (root check:e2e-types)`: exit 0 Independent adversarial review: changes, 3 issue(s), all addressed in a fix-up pass. Known limits and risks: - The Linux bubblewrap branch was not run here, because no Linux host was available. The post-run quarantine is platform-independent code and was tested by calling it directly, not through a live bubblewrap run. - On Linux, a root `.git` or `.env` that a command creates exists on the host until the command ends. Host tools running at that same moment could still read it. Forbidden names created below the root are not caught on Linux. The `ponytail:` comment in `linuxBubblewrapArgs` records this limit and the upgrade path: mount placeholders over a missing root `.git` and `.env`, then remove the empty mount points afterwards. - On macOS the rules match paths. A folder moved into the workspace from the sandbox's temp folder can therefore still bring these names in below the root. The root names themselves are refused, and host git and the `.env` loader read only the root's. - The quarantine cannot tell a command's `.git` or `.env` from one another process creates at the root during the run, for example a user running `git init` at that moment. Such a file is renamed, not deleted, and the output tells Mari to ask the user. - Pinning the workspace root also refuses changes to the workspace folder's own metadata, such as chmod or touch on the folder itself. Files and folders inside it are unaffected. - On Linux, a sandboxed command can still rename `packages/server/data` inside the workspace, because the masking mounts move with the folder. The key stays masked. This behaviour is older than this PR and unchanged. - The suggested `-c core.fsmonitor=false` and `core.hooksPath` flags on unsandboxed git calls were not added. The root cause is fixed in the sandbox, and those flags cannot stop all config-driven execution, such as filter drivers. - The name rule matches ASCII case only. Existing paths are matched canonically, and ignorable code points on HFS+ volumes are not covered. - The full `pnpm check` was not run inside the new sandbox. ## Docs and release impact - [ ] No docs changes needed - [ ] Updated docs (README / CONTRIBUTING / android/README) 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) 🤖 Generated with [Claude Code](https://claude.com/claude-code) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-07 17:39:44 +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#8792
No description provided.