Skip to content

fix(cli): theme loading resolves from the project and obeys the safe-mode gate - #5525

Closed
bhamodi wants to merge 1 commit into
meta:mainfrom
bhamodi:fix/cli-theme-package-names-only
Closed

bhamodi wants to merge 1 commit into
meta:mainfrom
bhamodi:fix/cli-theme-package-names-only

Conversation

@bhamodi

@bhamodi bhamodi commented Aug 25, 2026 •

Copy link
Copy Markdown
Contributor

Summary

resolveTheme() had two problems: its createRequire was bound to the CLI's install location, so package themes resolved from the wrong node_modules, and it loaded checkout files on every read command with no way to opt out. This keeps every documented setup working exactly as on main, including the theme guide's {"astryx": {"theme": "./src/theme.ts"}}, and puts all theme loading, file and package alike, behind the one safe-mode switch.

Changes

  • File specifiers load through the shared importUserModule (the documented .ts setup now uses the same jiti path configs do, instead of relying on Node's type stripping).
  • Package specifiers resolve with a createRequire bound to the project (<cwd>/package.json), so astryx.theme names packages out of the project's own node_modules.
  • Both are project code, so both obey ASTRYX_NO_PROJECT_CODE=1: no theme module loads (file, package, or ASTRYX_THEME), a one-line notice printed once per process explains why, and rendering degrades theme-less. The variable is read inline so this change stands alone; it is the same switch fix(cli): ASTRYX_NO_PROJECT_CODE=1 keeps the CLI on built-in data #5531's projectCodeAllowed reads.
  • Every warning that prints the configured specifier routes it through printable(), which replaces control and format characters (\p{Cc}, \p{Cf}) with U+FFFD, so a checkout-controlled value cannot write escape sequences, newlines, or bidi overrides to the terminal.
  • resolveTheme is now async; its one call site awaits it.

Test plan

19 tests in resolve-theme.test.mjs, each verified to fail when the guard it protects is removed:

  • The documented file setup loads relative to cwd, including ./src/theme.ts with TypeScript syntax; a child-process test uses an enum (rejected by Node's built-in type stripping) so only the shared jiti loader can pass it.
  • A package fixture that exists only in the project's node_modules resolves (fails against a CLI-bound require).
  • ASTRYX_THEME still loads.
  • Under the gate, file, package, and ASTRYX_THEME setups all return null with a probe global proving the module never executed; the notice prints once across repeated reads; any other value of the variable leaves loading on.
  • Specifiers carrying OSC/CSI sequences, newlines, CR, and U+202E reach none of the five warn sites unescaped (package, file, bare-name, and gate-notice paths covered).

Also: clients/cli lib + component suites green (181 tests), typecheck:strict, eslint strict, check:changesets, check:cli-structure, and a manual run of astryx component Button against a fixture project with a .ts theme, a project-local package theme, and ASTRYX_THEME, each with and without ASTRYX_NO_PROJECT_CODE=1.

Notes

@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:10am 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
@github-actions

github-actions Bot commented Aug 25, 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

github-actions Bot added a commit that referenced this pull request 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 — the diagnosis is right, and the field really is read on every astryx component.

A package name still reaches code the checkout controls: a local package named in astryx.theme can execute through astryx component. Restricting the config value alone therefore does not establish the trust boundary.

[Reviewed by Robohands]

@@ -22,6 +29,13 @@ import {createRequire} from 'node:module';

const _require = createRequire(import.meta.url);

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.

createRequire is bound here, not to cwd. Might be worth routing through module-loader.mjs instead.

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.

Right on both counts — bound to the CLI, and the wrong place to bind it. File specifiers now go through the shared module loader, and package specifiers use a require bound to <cwd>/package.json, with a regression that only passes when resolution is project-bound (the fixture package exists only in the fixture's node_modules).

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 holds on current main: file specifiers use the shared importUserModule, package specifiers use a require bound to <cwd>/package.json. The regression only passes when resolution is project-bound (rebinding to the CLI's location fails it).

@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 — the public theme guide explicitly tells builders to use "astryx": {"theme":"./src/theme.ts"}. That setup prints the custom variant in published 0.5.0; this head drops it and warns, while astryx doctor can still report the field as wired.

The package-name check also does not complete the stated trust boundary: a package selected by the checkout executes before and after. Could you preserve the documented path behavior and add a regression proving every project-code theme load requires the same explicit opt-in?

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

[Reviewed by Robohands]

@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 — I rechecked this unchanged head against the two prior Robohands reviews. The second corrected the first and asked to preserve the documented path setup while gating every project-code theme load. Both blockers still reproduce: the public theme guide's {"astryx":{"theme":"./src/theme.ts"}} works on current main and published 0.5.0, but this head drops its variants while astryx doctor still reports it wired; a package selected by the same checkout still executes.

Could you preserve the documented path behavior and make both path and package theme loading obey one shared safe-mode gate? If you'd rather talk it through with someone, we're in Discord.

[Reviewed by Robohands]

@bhamodi
bhamodi force-pushed the fix/cli-theme-package-names-only branch from 68f0228 to d841cab Compare August 27, 2026 09:09
@github-actions
github-actions Bot requested a review from cixzhang August 27, 2026 09:09
@bhamodi bhamodi changed the title fix(cli): astryx.theme takes npm package names only fix(cli): theme loading resolves from the project and obeys the safe-mode gate Aug 27, 2026
@bhamodi

bhamodi commented Aug 27, 2026

Copy link
Copy Markdown
Contributor Author

Reworked per your second review — you were right that the name check broke the documented setup without establishing the boundary. The restriction is gone: the theme guide's {"astryx": {"theme": "./src/theme.ts"}} works exactly as on published 0.5.0 (and now loads through the same jiti path configs use, rather than relying on Node's type stripping). In its place, every theme load — file, package, and ASTRYX_THEME alike — obeys the one safe-mode switch: under ASTRYX_NO_PROJECT_CODE=1 no theme module loads at all, with a one-line notice.

Regressions added for each: the documented file setup loading, project-bound package resolution (fails against a CLI-bound require), and probe-based proof that nothing executes under the gate for all three sources. Pairs with #5531, which routes the config/doc/integration loaders through the same switch; either merge order works since this reads the variable inline.

github-actions Bot added a commit that referenced this pull request Aug 27, 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 — I rechecked the two prior Robohands asks. This head now preserves the documented TypeScript theme path, resolves packages from the project, and keeps file, package, and ASTRYX_THEME modules unexecuted under the gate.

One security edge remains: resolve-theme.mjs:107 writes the checkout-controlled theme value directly to the terminal. A value containing a newline or ANSI control sequence can spoof the output in the untrusted-checkout mode; I reproduced both reaching the warning unchanged. Could we escape or omit that value so the notice stays one inert line? If you'd rather talk it through with someone, we're in Discord.

[Reviewed by Robohands]

if (gateNoted) return;
gateNoted = true;
console.warn(
`⚠ theme: ASTRYX_NO_PROJECT_CODE=1 — not loading theme "${specifier}"; rendering without theme data`,

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.

Could we escape or omit this checkout-controlled value? Newlines and terminal controls pass through.

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.

Done — added a printable() helper that replaces control characters (\p{Cc}: C0, DEL, C1) with U+FFFD before a specifier reaches the terminal, and routed all five warn sites through it (the gate notice plus the four resolve-failure warnings, which print the same checkout-controlled value). Test added: a specifier carrying an OSC sequence and a newline warns with no control character surviving into the message.

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.

Done: routed through printable() (control and format characters to U+FFFD), and the notice now also prints once per process. Tests at the gate-notice, file, package, and bare-name sites, each failing if the escape is removed.

@bhamodi
bhamodi force-pushed the fix/cli-theme-package-names-only branch from d841cab to f391b45 Compare August 31, 2026 22:09
@github-actions
github-actions Bot requested a review from cixzhang August 31, 2026 22:10
github-actions Bot added a commit that referenced this pull request Aug 31, 2026
…mode gate

resolveTheme required through a createRequire bound to the CLI's own
location, so package themes resolved from the wrong node_modules, and it
loaded checkout files on every read command with no way to opt out.

Now file specifiers (the documented {"astryx":{"theme":"./src/theme.ts"}}
setup, unchanged by default and loaded through the same jiti path configs
use) go through the shared module loader, package specifiers resolve with
a require bound to the project, and BOTH sit behind the one safe-mode
switch: under ASTRYX_NO_PROJECT_CODE=1 no theme module (file, package, or
ASTRYX_THEME) loads at all and a one-line notice, printed once per
process, says why. Theme data is project code whichever way it is named.

Every warning that prints the configured specifier routes it through
printable(), which replaces control and format characters (Cc, Cf) with
U+FFFD, so a checkout-controlled value cannot write escape sequences,
newlines, or bidi overrides to the operator's terminal.

The variable is read inline so this change stands alone; it is the same
switch the module loader's broader gate reads.
@bhamodi
bhamodi force-pushed the fix/cli-theme-package-names-only branch from f391b45 to cb5aaef Compare September 20, 2026 05:06
@bhamodi

bhamodi commented Sep 20, 2026

Copy link
Copy Markdown
Contributor Author

Rebased onto current main and re-verified the earlier asks against it. The escape is in place: every warn site (the gate notice plus the four resolve-failure warnings) prints the specifier through printable(), which replaces \p{Cc} and now also \p{Cf} (bidi overrides) with U+FFFD. Tests cover the package, file, bare-name, and gate-notice paths with OSC/CSI sequences, newline, CR, and U+202E, and each fails if printable becomes the identity. The documented ./src/theme.ts setup is also now proven to go through the jiti loader by a child-process test using an enum, which Node's type stripping rejects; a plain import() or the old CLI-bound require fails it.

@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 project-relative resolver fix is useful, but the safe-mode design needs to change at exact head cb5aaef66af3eab487b3a5971da1fe036e30490c. A read-only astryx component command should not execute project theme code behind an opt-out environment variable; the command only needs static custom-variant metadata, and the current resolver does not reliably derive that from normal defineTheme() output anyway. Please split or rework this so trusted astryx theme build emits versioned, schema-validated variant JSON and astryx component reads it without executing project code, falling back to built-in docs when metadata is missing, invalid, or stale. Keep the resolver correctness repair separate if useful. PR 6491 may further settle the general CLI environment-variable policy.

@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 — the resolution repair itself looks right: theme packages now resolve from the project's own node_modules, the documented ./src/theme.ts setup loads through the same shared loader configs use, and every warning now prints the configured specifier with control and format characters replaced.

One part needs to change before this can land: the ASTRYX_NO_PROJECT_CODE gate.

  • Current CLI policy does not allow a new Astryx-owned environment variable as a behavior control; supported behavior needs a documented command, option, configuration surface, or programmatic API instead.
  • The gate also only covers the theme. On the astryx component path, the project configuration and integration manifests load before theme resolution is reached, so someone running the command in an untrusted checkout with the variable set can still execute project code — the switch promises more safety than it delivers.

Please split the gate out of this PR and land the resolution fix on its own. Safe mode for read commands is still worth pursuing separately, ideally in the direction discussed on this PR: a trusted build step emitting static, validated variant data that the component command can read without executing project code at all. One smaller note: the current tests only exercise theme objects with a top-level variants map, while a normal defineTheme() result does not carry one, so the variant benefit for real themes is not yet demonstrated — worth a fixture whichever PR carries the safe-mode rework.

[Automated review]

@cixzhang

cixzhang commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

(Posted by Cindy's agent.) Thank you, @bhamodi! This report shaped both the CLI's rules for controls (no environment-variable gates) and the generated theme-module path that shipped in #6874 for installed themes, so the resolution and the gate are covered there. The remaining hardening, sanitizing the configured theme value in warnings, will land separately with credit to you, and the static-metadata safe mode from the review is tracked as follow-up work. Closing this one as superseded.

@cixzhang cixzhang closed this Nov 9, 2026
cixzhang added a commit that referenced this pull request Nov 9, 2026
…rs (#10764)

fix(cli): a theme warning prints as one line without control characters

The configured astryx.theme value and the generated theme record come
from the checkout. Every theme warning now replaces control and format
characters with U+FFFD, so a newline or an escape sequence cannot
rewrite the terminal.

Carries the warning hardening from #5525.

Co-authored-by: Baraa Hamodi <[email protected]>
@github-actions
github-actions Bot deleted the fix/cli-theme-package-names-only branch November 9, 2026 14:50

This branch was successfully deployed

1 active deployment
Preview — cb5aaef6 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