Skip to content

components/discover.ts imports *.component.ts files unconditionally, no --sandbox option (same class as #1045's resolveProjectLexicons bypass) #1051

Closed

Description

@lex00

Follow-up from #1045 Phase 2 review (PR #1050). While fixing the resolveProjectLexicons bypass (a full, unsandboxed discover() call that executed every project source file before the sandboxed build ever ran), the same reviewer asked for a broader audit of pre-build paths that import project source. chant lint and watch mode came back clean; components/discover.ts did not.

The finding

discoverComponents() (packages/core/src/components/discover.ts) always does:

exports = await import(pathToFileURL(resolve(filePath)).href);

for every discovered *.component.ts file — unconditionally, in the CLI's own process, with no --fold/--sandbox equivalent at all. A *.component.ts file is project source the same way a src/*.ts infra file is; a malicious one (e.g. console.error(readFileSync("/etc/passwd")) at module top level, plus a real Component export so discovery doesn't just error out) executes with full filesystem/network/environment access the moment discoverComponents reaches it.

Call sites (all unsandboxed today):

  • packages/core/src/components/cli-support.ts
  • packages/core/src/cli/handlers/components.ts
  • packages/core/src/cli/handlers/graph.ts
  • packages/core/src/cli/handlers/lifecycle.ts
  • packages/core/src/lifecycle/status.ts
  • packages/core/src/lint/component-checks.ts (chant lint's COMP* checks)

Why this wasn't folded into #1045 Phase 2 / PR #1050

Sandboxing this properly touches at least six call sites plus new CLI flag plumbing, and deserves its own differential-style verification the way #1045 Phases 1–2 got for the resource-entity discovery path — not something to bolt on under time pressure onto an already-large security PR. resolveProjectLexicons (the reported bug) is fixed and regression-tested in PR #1050; this is a distinct, analogous gap in a different discovery convention.

Why this one should be materially EASIER than #1045's entity sandbox

Component (packages/core/src/components/component.ts) is plain JSON data, not a Declarable — no AttrRef/live-object cross-references, no entity-wire codec needed. isComponent() is a duck-type check (name/dependsOn/deploy shape), and projectToJson() already proves a Component round-trips through a bare JSON.parse(JSON.stringify(...)). That means the child process in a sandboxed variant can hand back plain JSON directly — no EntitySetWire-equivalent needed, no AttrRef reconstruction. The one thing to preserve carefully: discoverComponents's existing duplicate-name check compares by object identity (existing.component !== value, tolerating the same object re-exported from multiple bindings) — that check needs to run INSIDE the child, over live objects, before anything is serialized, mirroring how collectEntities runs inside chant#1045 Phase 2's sandboxed child today (see packages/core/src/discovery/sandbox/run.ts/driver.ts).

Suggested shape

  1. Refactor discoverComponents to separate "import each file" from "collect + duplicate-check the already-imported exports" (mirrors collect.ts's collectEntities taking pre-imported modules).
  2. Add a sandboxed variant reusing packages/core/src/discovery/sandbox/bundle.ts's bundleDriver (generic — doesn't care what the driver does) with a new, much simpler driver template: import each *.component.ts file via a literal-string dynamic import (same lazy-init trick), run the collect+duplicate-check step, JSON.stringify the result, send over IPC.
  3. Thread a sandbox option through discoverComponents(path, { sandbox }) and wire --sandbox through the CLI call sites listed above.
  4. Hostile-fixture regression test analogous to packages/core/src/cli/security-boundary.test.ts, but for *.component.ts.

Related, smaller finding worth a look in the same pass

chant lint's own file-linting is already execution-free (runLint works over file text/AST, not live imports) — confirmed safe. But loadPluginRules/loadLocalRules (packages/core/src/cli/commands/lint.ts) import project-specified lint-plugin files (chant.config.ts's lint.plugins, .chant/rules/*.ts) unconditionally too. This is arguably closer to chant.config.ts itself (an explicit, project-authored extension point, not an automatically-discovered infra file) than to the resolveProjectLexicons bug, but it's the same class of "no isolation option exists here at all" gap and is worth deciding on explicitly rather than by omission.

Activity

  1. lex00 commented on Jul 26, 2026

    @lex00
    ContributorAuthor

    Closed by #1053 (d5b94031).

    Verified independently with a hostile *.component.ts driven through the real CLI, evidence routed through the component name:

    discovered name wrote to /tmp
    no --sandbox readu-writeok-spawnok-env77 yes
    --sandbox blkerraccessdenied-…-…-env2 no

    Read, write and spawn all denied; environment cut from 77 variables to 2; component still discovered correctly.

    The duplicate-name identity check runs inside the child before serialization, as the issue required. The related loadPluginRules/loadLocalRules question got an explicit recorded decision rather than being settled by omission — not sandboxed, because a LintRule.check() is invoked inline per file across the whole lint run rather than being a one-shot data producer, so isolating the import alone would not contain it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions