Skip to content

fix(cli): ASTRYX_NO_PROJECT_CODE=1 keeps the CLI on built-in data - #5531

Closed
bhamodi wants to merge 1 commit into
meta:mainfrom
bhamodi:fix/cli-no-project-code-gate
Closed

bhamodi wants to merge 1 commit into
meta:mainfrom
bhamodi:fix/cli-no-project-code-gate

Conversation

@bhamodi

@bhamodi bhamodi commented Aug 25, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Discovery-backed commands (component, search, docs, discover, template, doctor, layout, ...) load the checkout's astryx.config.*, the integration manifests it names (and the installed ones autolink finds), doc modules, template specs, codemods, and theme sources. All of those execute on import. That is the right trade in a workspace the operator trusts and the wrong one in CI, triage, and agent runs over arbitrary checkouts. This adds the opt-out: ASTRYX_NO_PROJECT_CODE=1 keeps the whole CLI on built-in data.

Changes

  • importUserModule (the chokepoint every user-authored module flows through) refuses under the gate before any of its loaders (jiti, fresh jiti, CommonJS require, native import) runs, with an error naming the variable. It carves out the CLI's own package root (isCliShippedPath) so built-in topics, templates, and command modules keep loading. isProjectCodeGated and projectCodeGateMessage are exported for the two loaders that need a different jiti.
  • Project.load checks the gate before touching a config: a present astryx.config.* is acknowledged once on stderr and skipped, and no integration manifest is loaded (configured, autolinked, or the package being authored), so read commands degrade to built-ins instead of erroring.
  • Every remaining checkout-path loader routes through the gate: loadDocs (component-loader), the docs catalog detail and list loaders, search's doc loader, string-utils' keyword scan, and doctor's config re-import (which now reports "config present but not loaded" instead of executing the file it just said it ignored). template-adapter keeps its JSX-capable jiti but asks the gate itself and skips external blocks entirely.
  • astryx theme build refuses a theme source outside the CLI before any loader runs (jiti sync and async paths, the family preloader, the legacy eval fallback): exit 1, ERR_THEME_LOAD, one-line message, nothing written. Covers --check, --family, and --watch.
  • README section "Running in checkouts you don't trust" describes exactly what is gated, and that the CLI's own runtime dependencies (@astryxdesign/core, jscodeshift) are not in scope.
  • Default behavior (variable unset) is unchanged.

Audit of checkout-backed import sites in packages/cli

Non-test, non-dist code. "Gated" means the site cannot execute a checkout module under ASTRYX_NO_PROJECT_CODE=1.

Site Status
foundation/fs/module-loader.mjs importUserModule (jiti, fresh jiti, CommonJS require, native import) Gated at entry, before any loader path
foundation/fs/module-loader.mjs loadModuleWithParser Gated via importUserModule
foundation/config/project.mjs config load, autolinkIntegrations, loadLocalIntegration Gated before load: config nulled with a stderr notice; autolink and local package skipped
foundation/integrations/integrations.mjs manifest import Gated via importUserModule; never reached under the gate
foundation/integrations/autolink.mjs Calls loadIntegrations; skipped by Project.load under the gate
foundation/discovery/component-loader.mjs loadComponentDoc Gated via importUserModule (already so on main)
foundation/discovery/component-loader.mjs loadDocs Now gated (was a bare import())
foundation/discovery/docs-discovery.mjs topic import Gated via importUserModule; only reached for integrations, which never load under the gate
foundation/discovery/template-adapter.mjs loadIntegrationDoc Gated via loadModuleWithParser
foundation/discovery/template-adapter.mjs loadDocModule (JSX jiti and import()) Now gated: returns null for a non-shipped path; shipped core templates still load
foundation/discovery/template-adapter.mjs discoverExternalBlocks Now gated: returns [] under the gate
foundation/text/string-utils.mjs keyword scan Now gated via the loader (imported dynamically so the browser bundle stays free of node builtins)
api/docs/_adapter.mjs topic and translation import Now gated via importUserModule
api/docs/list/list.mjs description import Now gated (defense in depth; the catalog holds no project topic under the gate)
api/search/search.mjs loadModuleDoc Now gated
api/doctor/doctor.mjs config re-import Now gated: info row returned before the import; the re-import itself goes through importUserModule
api/doctor/doctor.mjs _require.resolve peer-dep checks Not applicable: resolution only, nothing executes
api/theme/build/build.mjs jiti theme import, evalModule, eval fallback, family registry import(), preloadFamilySource; core-interception.mjs createRequire(fromFile)(specifier) Gated upstream by refuseGatedThemeSource before any loader is created
api/theme/build/build.mjs createJiti(...).resolve calls Not applicable: resolution only
api/theme/build/build.mjs import('@astryxdesign/core/theme'); import('jscodeshift') in codemods, upgrade, theme-discovery; babel plugin require in authoring/shadcn Not applicable: CLI runtime dependencies resolved by package resolution, not checkout-authored code (stated as a limit in the README)
assets/codemods/integration-discovery.mjs codemod load Gated via loadModuleWithParser; integrations never load under the gate
api/upgrade/_adapter.mjs post-codemod hook execFile The command comes from a codemod module, which is gated above
api/gap-report/gap-report-worker.mjs handler import Gated via importUserModule; the worker shares process.env, and handler entries derive from the config and loaded integrations, both empty under the gate
api/gap-report/gap-report.mjs execFile('gh', ...), api/swizzle/_github.mjs Not applicable: fixed external binary, not checkout code
clients/cli/index.mjs command module imports, commands/template.mjs lazy imports, foundation/fs/paths.mjs pnpapi Not applicable: the CLI's own modules and the Yarn PnP runtime
scripts/*.mjs, test/drift/docs-drift.mjs Not applicable: dev scripts, not CLI runtime
clients/cli/lib/resolve-theme.mjs require of package.json astryx.theme / ASTRYX_THEME Not modified here; owned by #5525, which adds its own check of the variable. Follow-up there: use isProjectCodeGated from module-loader.mjs so the carve-out and message stay shared

Test plan

  • packages/cli/clients/cli/commands/no-project-code.test.mjs (12 end-to-end probes): every fixture module sets a global when it executes; the real CLI runs in-process against it and the global must stay unset, so a loader that slips past the gate fails the suite. Covers doctor (text and --json), component <Name> from a repo-layout checkout, the fuzzy-match path (component Buton), search (with and without --type), component and component --list with a hostile integration that is both configured and declared as a dependency (so autolink is exercised), docs list plus a built-in detail, template --list with a hostile external block, theme build (text, --json code, --family with a preloaded intermediary), plus a control proving the same theme fixture executes and builds with the variable unset.
  • Unit probes: module-loader.test.mjs (refusal for .mjs, .js, and .ts including fresh, shipped-path carve-out, exact-value check), project.test.mjs (config skip with stderr notice, no autolink, no local integration; spies prove the loaders are not even called), integrationDocs.test.mjs (loadReferenceDocs refuses a project topic and still loads a built-in with --lang dense).
  • Each guard was mutation-tested: removing it fails the corresponding test(s). Removing the importUserModule gate fails 6, the config skip 1, the autolink guard 1, the local-integration guard 1, the doctor early return 2 (also with the old raw import restored), the loadDocs, search, string-utils, and docs-adapter rewrites 1 each, the external-blocks guard 1, the theme member gate 2, both theme gates 3. The family-level loop in themeBuildFamily is redundant with the member-level refusal (the plan always orders a selected member first) and is kept as belt-and-braces.
  • CI-equivalent checks run locally: pnpm -F @astryxdesign/cli typecheck:strict, typecheck:template-docs, node .github/scripts/cli-api-types-verify.mjs (regenerates the api types), ASTRYX_STRICT_LINT=1 eslint and plain eslint on every changed file, pnpm check:cli-structure, pnpm check:changesets, readme:check, the copyright check, and pnpm vitest run --project node packages/cli (4031 passed, 6 skipped; the only failures were environment-specific pnpm exec tsc invocations and load timeouts that pass on rerun).

Notes

  • The gate covers code the checkout authors or contributes. A hostile package installed under node_modules that the CLI itself depends on (for example @astryxdesign/core) is out of scope, since refusing it would break every command; the README says so.
  • Read commands that can only be answered by importing a project module fail with the loader's error (ERR_UNKNOWN in the JSON envelope). A dedicated error code is a small follow-up if wanted; theme build uses the existing ERR_THEME_LOAD.
  • Changeset included (@astryxdesign/cli patch).

@vercel

vercel Bot commented Aug 25, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
astryx Ready Ready Preview Sep 20, 2026 5:11am UTC

Request Review

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Aug 25, 2026
@github-actions github-actions Bot added community Authored by a community contributor (not on the eng/design team) needs:code-review High-risk change (new package/component/API) — needs human code review before merge labels Aug 25, 2026

@cixzhang cixzhang left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this — the trade you're naming is real and importUserModule is the right place for the switch.

It isn't holding yet. astryx doctor re-imports the config directly at doctor.mjs:328, so under the gate it prints "ignoring …; running on built-in data only" and then executes that same file. astryx component <Name> runs a doc module from the checkout through loadDocs at component-loader.mjs:141 — its neighbour loadComponentDoc goes through the gate, loadDocs doesn't. About nine bare await import()s of checkout paths remain.

ASTRYX_NO_PROJECT_CODE=1 astryx doctor   # config runs anyway

Ideally I'd like those loaders routed through importUserModule too, so the README sentence is true when it ships. Also CI is red on a missing @param for noteConfigSkipped (project.mjs:43).

Is routing them all through the one loader workable, or is something in loadDocs making that hard?

If you'd rather talk it through with someone, we're in Discord.

Full review

[Reviewed by Robohands]


/** Say once per process why a present config is being ignored. */
let configSkipNoted = false;
function noteConfigSkipped(configPath) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs a @param {string} configPath.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added, and the helper typechecks now.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still in place on the rebased head: noteConfigSkipped carries @param {string} configPath and typecheck:strict passes.

@cixzhang cixzhang left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding this safety switch. We previously asked for every checkout-backed loader to honor it and for the new helper to typecheck; this exact head has not changed, so both are still open.

With ASTRYX_NO_PROJECT_CODE=1, astryx doctor still executes a fixture config after saying it was ignored, and astryx component still executes a fixture doc module. An agent can therefore opt out and still run checkout code. CI also still fails because noteConfigSkipped(configPath) has an implicit any.

Could you cover every project-code import path, add end-to-end probes for these two commands, and add the missing @param?

If you'd rather talk it through with someone, we're in Discord.

[Reviewed by Robohands]

@bhamodi

bhamodi commented Aug 27, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for running it — you were right, the switch wasn't connected to most of the loaders. Reworked so the README sentence is true:

  • importUserModule now carves out the CLI's own package root (isCliShippedPath): shipped modules (built-in doc topics, templates, command modules) always load, everything else refuses under the gate. That let every checkout-path loader route through it — loadDocs (component-loader), the docs catalog detail/list/translation loaders, search's doc loader, theme build's target-doc loader, and string-utils' keyword scan. The nine bare await import()s of checkout paths are gone.
  • astryx doctor no longer re-imports the config under the gate — its config check returns an info row ("config present but not loaded") instead of executing the file it just said it ignored.
  • template-adapter keeps its JSX-capable jiti (importUserModule's isn't), but now checks the gate + carve-out itself and treats a gated spec like a missing file.
  • Added the end-to-end probes you asked for (no-project-code.test.mjs): fixture config and doc modules plant a global on execution; astryx doctor and astryx component Button run for real via the in-process harness and the globals stay unset. Both fail without the rework.
  • noteConfigSkipped has its @param.

Full CLI suite is green locally except build.test.mjs/build-theme.variants.test.mjs, which fail identically on clean main in my environment (they need built theme dists).

@cixzhang cixzhang left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for routing doctor and component docs through the gate — both new probes pass, and the missing JSDoc is fixed. We previously asked for every checkout-backed loader; that part is still incomplete.

ASTRYX_NO_PROJECT_CODE=1 astryx theme build hostile-theme.mjs still executes the file and emits output because importThemeModule calls jiti directly. The rewrite also leaves exact-head CI red: API type generation reports 24 errors and strict lint reports two unused imports.

Could you gate the theme source before either loader runs and get CI green? If you'd rather talk it through with someone, we're in Discord.

[Reviewed by Robohands]

@bhamodi
bhamodi force-pushed the fix/cli-no-project-code-gate branch from ae4d15f to 5dd9e88 Compare August 31, 2026 22:09
@github-actions
github-actions Bot requested a review from cixzhang August 31, 2026 22:09
github-actions Bot added a commit that referenced this pull request Aug 31, 2026
@github-actions

github-actions Bot commented Aug 31, 2026 •

Copy link
Copy Markdown
Contributor

PR Analysis Report

📚 Storybook Preview

View Storybook for this PR
GitHub Pages may take up to a minute to hydrate after deploy.

🧪 Sandbox Preview

View Sandbox for this PR
GitHub Pages may take up to a minute to hydrate after deploy.

No new or modified components detected.

Bundle Size Summary

No component packages changed.

Accessibility Audit

Status: No accessibility violations detected.


Generated by PR Enrichment workflow | Storybook | Sandbox | View full report

Discovery-backed commands load the checkout's astryx.config.*, the
integration manifests it names (and the installed ones autolink finds),
doc modules, template specs, codemods, and theme sources. All of those
execute on import, which is the right trade in a workspace the operator
trusts and the wrong one in CI, triage, and agent runs over arbitrary
checkouts. ASTRYX_NO_PROJECT_CODE=1 opts out.

The gate lives in importUserModule, the chokepoint every user-authored
module flows through, and carves out the CLI's own package root so
shipped topics, templates, and command modules keep loading. Every
loader that did not go through it now does (component-loader's loadDocs,
the docs catalog detail/list loaders, search, string-utils' keyword
scan, doctor's config re-import), or asks the gate itself where a
different jiti is needed (template-adapter's JSX jiti, external blocks).
Project.load skips a present config with a single stderr notice and
loads no manifest: configured, autolinked, or the package being
authored. `theme build` refuses a theme source outside the CLI before
jiti, the family preloader, or the eval fallback runs, exiting 1 with
ERR_THEME_LOAD and writing nothing.

Default behavior with the variable unset is unchanged.
@bhamodi
bhamodi force-pushed the fix/cli-no-project-code-gate branch from 5dd9e88 to a0348cf Compare September 20, 2026 05:06
@bhamodi

bhamodi commented Sep 20, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the precise repro. Rebased onto current main and closed the remaining gaps:

  • theme build: the source is now refused in themeBuildInternal right after the file-exists check, before jiti (sync or async), the family preloader, or the eval fallback is created. themeBuildFamily also refuses every planned source up front. The command exits 1 with ERR_THEME_LOAD, prints one line naming the variable, and writes nothing. Covered end-to-end for the text, --json, and --family paths, plus a control showing the same fixture executes and builds with the variable unset.
  • Audit: every import(), jiti, or require of a checkout path in packages/cli now either goes through importUserModule or asks the same gate (template-adapter's JSX jiti, external blocks). Project.load also stops autolink and the local-package manifest, which main added since the first round. The full table of sites is in the PR body. The one intentionally untouched site is resolve-theme.mjs, which fix(cli): theme loading resolves from the project and obeys the safe-mode gate #5525 owns.
  • The end-to-end suite grew to 12 probes (doctor, component including the fuzzy path, search, docs, template, theme build). I mutation-tested each guard: removing it fails its test.
  • CI: typecheck:strict, the api-types verify script, strict lint, and the structure and changeset checks are green locally. The README now describes exactly what is gated.

@astracat-bot astracat-bot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for hardening the CLI against executing code from untrusted checkouts. Approval is waiting for a maintainer to provide or identify the current spec governing the new ASTRYX_NO_PROJECT_CODE=1 contract. Three implementation gaps also remain:

  • Text astryx component still loads a theme selected by the checkout's package.json through an ungated require(); please gate it here or make the separate fix a required prerequisite and cover the command path.
  • Core component docs resolve outside the CLI package root and are refused, so the documented astryx component Button built-in example does not work under the gate.
  • Multiple astryx.config.* files throw before safe mode reaches its skip path.

Please preserve trusted Core docs as built-in data and handle multiple config names without executing them, with focused side-effect and success probes.

[Automated review]

@cixzhang cixzhang left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The trust-boundary goal is right, but the current implementation is not safe or compatible enough to land at exact head a0348cf9cc621bf3e027517ef94f246f0cccac24. The opt-in environment switch still allows theme execution on text component --list, refuses legitimate installed Core docs needed by ordinary inspection, and lets duplicate config files fail before the safe-mode decision. Please wait for PR 6491 to settle discoverability/environment-control policy, then rework the boundary so supported safe inspection is discoverable, trusted installed/version-matched Astryx data is distinguished from checkout code, project code is not executed on the safe path, and duplicate project config is ignored rather than parsed there. The static theme-metadata direction from PR 5525 should remove the theme execution path rather than add another gate.

@astracat-bot astracat-bot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for hardening the CLI against executing code from untrusted checkouts — that trust-boundary goal is worth keeping. It cannot land in this shape, because the governing spec has now landed on main in #6491 and differs from this implementation in a large way.

AST-017 FR14 states: "The CLI MUST NOT define or read an Astryx-owned environment variable for any purpose." The new claims also require supported behaviour to be discoverable in generated help and the machine-readable manifest, any global scope to be proven with a manifest-derived command matrix (FR15), a cross-cutting guarantee to fail closed for new extension points rather than cover only audited loaders (FR16), suppressed work to be identified in every successful machine-readable result (FR17), and programmatic control to be an explicit per-call input rather than ambient process state (FR18).

Please rework safe inspection onto a documented, scoped, per-call surface with observable reduced-capability results. In the same rework, and coordinated with #5525 for theme loading:

  • Text component output still loads a checkout-selected theme through an ungated require(); remove theme execution from the safe path (static metadata direction) and add a command-level side-effect probe.
  • Core component docs resolve outside the CLI package root and are refused, so the documented astryx component Button built-in example does not work; preserve trusted installed, version-matched Core docs as built-in data.
  • Multiple astryx.config.* files throw before the safe path can skip them; ignore duplicate project config on that path rather than throwing or parsing it.

[Automated review]

This branch was successfully deployed

1 active deployment
Preview — a0348cf9 Deployed Sep 20, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Meta Open Source bot. community Authored by a community contributor (not on the eng/design team) needs:code-review High-risk change (new package/component/API) — needs human code review before merge

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants