Repository navigation
refactor(security): reconcile containment guards and cell-timeout parsing - #1160
Merged
Merged
Conversation
Relocate the separator-aware `isPathInside` guard and its `PathInsideApi` injection point from `system-prompt/workspace-instructions.ts` into `path-containment.ts`, the shared home #1136 established for filesystem- containment guards. Pure move: the implementation, its cross-platform tests (migrated to `path-containment.test.ts`), and the `@maka/runtime` public re-export are unchanged in behavior and signature. This makes `isPathInside` the single authority for the separator-aware family so the bare-`startsWith('..')` variants can migrate to it next. Refs #1145
Migrate every caller of the bare-`startsWith('..')` containment family to the
separator-aware `isPathInside` and drop the retired variants, so
`path-containment.ts` is the single authority for "inside or equal to root"
checks.
- runtime: `isContainedPath` (skill reader + managed sources) and the private
`isInside` in `workspace-executor.ts` and `filesystem-worker/operations.ts`,
plus `pathWithinRoot` in `additional-permissions.ts`, now call `isPathInside`.
- headless: `workspace-executor-adapter.ts`'s `isInsidePosix` is replaced by
`isPathInside(root, target, <posix pathApi>)`, reusing the injectable
`pathApi` for the POSIX sandbox paths.
- desktop main: `skills.ts`, `managed-skill-sources.ts`, `explore-agent-tool.ts`,
and `office-document-tool.ts` switch from `isContainedPath`/`isInside` to
`isPathInside`.
- `path-containment.ts` keeps only `isPathInside` (+ `PathInsideApi`),
`isSafeSkillId`, and `toRelative`; the `@maka/runtime` public surface drops
`isContainedPath` and `isInside`.
Behavior change: a child entry whose own name begins with `..` (e.g.
`root/..foo`) is now correctly treated as inside, where the bare-`startsWith`
variant rejected it as an escape. `..foo` is a legitimate directory name, not a
parent reference, and skill-id safety is enforced separately by `isSafeSkillId`.
The `..rules` case is already locked by `path-containment.test.ts`.
Refs #1145
Add a repo-wide contract test that fails if any retired containment name (`isInside`, `isContainedPath`, `isInsidePosix`, `pathWithinRoot`) is redefined outside `path-containment.ts`, and that the shared home keeps exporting `isPathInside`. New "inside or equal to root" callers must import `isPathInside` from `@maka/runtime` instead of growing a private copy. The strict-interior family (`isInsideOrSamePath`, `isInsideCwd`) is a deliberately different semantic and stays allowed. Refs #1145
Narrow `lenientPositiveIntEnv` to a decimal positive integer literal so the TS runner accepts exactly what the Harbor adapter's Python `int()` does on `MAKA_CELL_TIMEOUT_SEC`. `"1e3"` and `"1.0"` are now a parse miss (fall back to the task timeout, then the adapter default) on both sides, instead of the prior divergence where `Number(raw)` silently took them on the TS side. Still lenient (no throw), so the TS runner stays aligned with the adapter's "a malformed value falls back to the default" contract. New TS-only env vars keep using the throwing `positiveIntEnv`. Closes the divergence deferred from #1137; refs #1145.
Two fixes from PR #1160 review: - Guard against Number() overflow: `lenientPositiveIntEnv` now requires `Number.isSafeInteger`, so an over-long digit string (`Number("9".repeat(400)) === Infinity`) no longer slips through as a valid timeout — it falls back like any other malformed value, restoring the pre-#1145 guard that `Number.isInteger(Infinity) === false` provided. - Unify accepted syntax on both sides: the TS parser already accepted only `[1-9]\d*`; `maka_agent.py::_cell_timeout_sec` now applies the same regex instead of `int()`, so `"+1800"` and `"01800"` (which Python `int()` accepted but the TS host rejected) are a parse miss on both sides, closing the asymmetry #1145 introduced when it narrowed the TS parser. Tests cover the shared TS table (`+1800`/`01800`/over-long → fallback) and the Python adapter (`+1800`/`01800` → default). Refs #1145
From PR #1160 review: - Detect `const`/`let`/`var` declarations, not just `function`, so an arrow-function revival of a retired predicate cannot slip past. - Scan the whole `packages` + `apps` tree (was four hand-listed roots), so a new private copy in any package is caught. - Drop `pathWithinRoot` from the retired list: `packages/core` has same-named helpers that do POSIX policy-string prefix matching (`trimTrailingSlashes` + `startsWith`), not `node:path`-relative containment, so the name is not a reliable cross-package signal. `isInside` / `isContainedPath` / `isInsidePosix` stay retired. Refs #1145
Fresh-eye review of PR #1160 found the Python side still diverged from the TS host on three edges: - Over-long digit strings: CPython caps integer-string conversion at 4300 digits and raises ValueError. The regex-then-int() form had no try/except, so a 5000-digit value would abort the task instead of falling back. Restore the guard. - Value range: a 400-digit value is a valid Python int but Number() overflow (Infinity) on the JS side, so the host rejected it while the adapter accepted it. Cap Python at 2^53 - 1 (Number.MAX_SAFE_INTEGER) to match Number.isSafeInteger. - Unicode digits: Python `\d` matches non-ASCII digits (`1٢` -> 12); JS `\d` is ASCII-only. Use `[1-9][0-9]*` on the Python side so both reject them. Both sides now accept the same ASCII decimal positive integer literal, capped at 2^53 - 1, lenient on a miss. Tests cover `1٢` and a 5000-digit string on both sides. Refs #1145
Fresh-eye review of PR #1160: the contract comment claimed the strict-interior family (`isInsideOrSamePath` / `isInsideCwd`) never allows root equality, but `storage/artifact-store.ts::isInsideOrSamePath` returns true for `target === root`. Stop claiming a specific equality semantic; unifying that family with `isPathInside` stays out of scope for #1145. Refs #1145
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes #1145. Two reconciliations deferred from #1084's High slices (#1136, #1137).
Containment guards → single authority. The repo carried three "inside or equal to root" containment semantics across five owners. This PR retires the duplicates and makes
path-containment.ts::isPathInside(separator-aware,pathApi-injectable, cross-platform-tested) the single authority:isContainedPath(skill reader, managed sources) and the privateisInsideinworkspace-executor.ts/filesystem-worker/operations.ts, pluspathWithinRootinadditional-permissions.ts, now callisPathInside.isInside(the non-injectable separator-aware copy) is removed;isPathInsideitself moves intopath-containment.tsfromsystem-prompt/workspace-instructions.ts, which now imports it from there.isInsidePosixbecomesisPathInside(root, target, <posix pathApi>), reusing the injectable primitives for POSIX sandbox paths.skills.ts,managed-skill-sources.ts,explore-agent-tool.ts,office-document-tool.ts) switches fromisContainedPath/isInsidetoisPathInside.isInside,isContainedPath,isInsidePosix,pathWithinRoot) outsidepath-containment.ts...fooedge decided. A child entry whose own name begins with..(e.g.root/..foo) is now correctly treated as inside, where the bare-startsWith('..')variant rejected it as an escape...foois a legitimate directory name, not a parent reference; skill-id safety is enforced separately byisSafeSkillId. The..rulescase is locked bypath-containment.test.ts.MAKA_CELL_TIMEOUT_SECsyntax unified.lenientPositiveIntEnvnarrows to a decimal positive integer literal, matching what the Harbor adapter's Pythonint()accepts."1e3"/"1.0"are now a parse miss (fall back to task timeout, then adapter default) on both sides, closing the divergence deferred from #1137. Still lenient (no throw), aligned with the adapter's "malformed → default" contract.Strict-interior family left as-is.
isInsideOrSamePath/isInsideCwd(rel !== '', target may not equal root) are a deliberately different semantic and out of scope; the contract test allows them by name.Verification
npm --workspace @maka/runtime run test— 2039 pass (incl. newcontainment-guard-contract), 7 skippednpm --workspace @maka/headless run test:dist— 1080 pass, 1 skippedskills/managed-skill-sources/explore-agent-tool/office-document-tool/office-documents-capability/bundled-skill-catalogtests — 80 pass@maka/runtime,@maka/headless, desktop main — passbiome linton all 16 changed files — cleannpm run check:stale— dist is fresh_cell_timeout_secbehavior unchanged (nomaka_agent.pyedit); the table test now mirrors it on the TS side.Security
Security-relevant path-containment code converges on the single tested
isPathInside(cross-drive guard + separator-aware..handling). The one behavior change (..foonow inside) tightens correctness, not the escape boundary: a real..parent reference is still rejected, and identifier safety stays withisSafeSkillId.Breaking change
@maka/runtimepublic surface dropsisContainedPathandisInside. All in-repo callers migrate in the same PR; the four commits are each independently buildable (moveisPathInside→ migrate callers + drop retired variants → contract test → cell-timeout narrowing).