Skip to content

refactor(security): reconcile containment guards and cell-timeout parsing - #1160

Merged
Astro-Han merged 8 commits into
mainfrom
refactor/security-unify-containment-guards
Jul 17, 2026
Merged

Astro-Han merged 8 commits into
mainfrom
refactor/security-unify-containment-guards

Conversation

@Astro-Han

Copy link
Copy Markdown
Contributor

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 private isInside in workspace-executor.ts / filesystem-worker/operations.ts, plus pathWithinRoot in additional-permissions.ts, now call isPathInside.
  • isInside (the non-injectable separator-aware copy) is removed; isPathInside itself moves into path-containment.ts from system-prompt/workspace-instructions.ts, which now imports it from there.
  • headless isInsidePosix becomes isPathInside(root, target, <posix pathApi>), reusing the injectable primitives for POSIX sandbox paths.
  • desktop main (skills.ts, managed-skill-sources.ts, explore-agent-tool.ts, office-document-tool.ts) switches from isContainedPath / isInside to isPathInside.
  • A repo-wide contract test forbids re-introducing the retired names (isInside, isContainedPath, isInsidePosix, pathWithinRoot) outside path-containment.ts.

..foo edge 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. ..foo is a legitimate directory name, not a parent reference; skill-id safety is enforced separately by isSafeSkillId. The ..rules case is locked by path-containment.test.ts.

MAKA_CELL_TIMEOUT_SEC syntax unified. lenientPositiveIntEnv narrows to a decimal positive integer literal, matching what the Harbor adapter's Python int() 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. new containment-guard-contract), 7 skipped
  • npm --workspace @maka/headless run test:dist — 1080 pass, 1 skipped
  • desktop main skills / managed-skill-sources / explore-agent-tool / office-document-tool / office-documents-capability / bundled-skill-catalog tests — 80 pass
  • typecheck: @maka/runtime, @maka/headless, desktop main — pass
  • biome lint on all 16 changed files — clean
  • npm run check:stale — dist is fresh
  • Python adapter _cell_timeout_sec behavior unchanged (no maka_agent.py edit); 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 (..foo now inside) tightens correctness, not the escape boundary: a real .. parent reference is still rejected, and identifier safety stays with isSafeSkillId.

Breaking change

@maka/runtime public surface drops isContainedPath and isInside. All in-repo callers migrate in the same PR; the four commits are each independently buildable (move isPathInside → migrate callers + drop retired variants → contract test → cell-timeout narrowing).

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
@Astro-Han
Astro-Han merged commit 3cbde0f into main Jul 17, 2026
3 checks passed
@Astro-Han
Astro-Han deleted the refactor/security-unify-containment-guards branch July 24, 2026 09:05
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.

refactor(security): reconcile the containment-guard variants left out of #1136

1 participant