Skip to content

fix(cli): confine every swizzle write, not just the output base - #6573

Merged
josephfarina merged 1 commit into
mainfrom
cli-spec/component-swizzle/swizzle-confine-writes
Sep 24, 2026
Merged

josephfarina merged 1 commit into
mainfrom
cli-spec/component-swizzle/swizzle-confine-writes

Conversation

@josephfarina

Copy link
Copy Markdown
Contributor

User impact

Anyone who runs astryx swizzle <Component> in a project whose output folder (default components/astryx/) contains a symlink. If the component's folder, or one of the files it would write, is a symlink that points outside the project, the copy follows the link and writes outside the project root. The command exits 0 and its receipt names the in-project path, so neither a person nor an agent can tell. With --overwrite, an existing symlinked file overwrites the file it points to, wherever that file is. After this change the command refuses with ERR_PATH_TRAVERSAL before it writes anything.

Problem and solution fit

  • Why does it happen? swizzleCopy runs --output through assertWithin, which resolves symlinks for that one path. It then joins the component name and each file name onto that base and writes, and it never checks those joined paths.
  • Why does it hurt the task? Swizzle is a write command that an agent runs without a person watching. An escape is silent, and it can replace files outside the project.
  • Why does it matter? The CLI promises that every write stays inside the project, and callers depend on that promise when they run the CLI unattended.

The fix runs every destination file through the same assertWithin check before the first mkdir or write, and reports an escape as ERR_PATH_TRAVERSAL, the code the architecture assigns to a path escape.

Expected behavior and authority

docs/architecture/cli-surface.md (current), INV10:

INV10 — Every write is confined. Output paths pass assertWithin, which canonicalises symlinks and rejects a NUL byte; an escape is ERR_PATH_TRAVERSAL.

Compatibility (spec:AST-017 FR5): this restores the documented contract. No valid use changes. Writing outside the project through a symlink was never a supported result.

Smallest restoration

  • api/swizzle/copy/copy.mjs: one small helper, confineWrite. It calls assertWithin and maps PathSafetyError to ERR_PATH_TRAVERSAL, as the existing --output check already did. The helper is used for --output and for each destination file, all before any mkdir or write. Nothing else moves.
  • api/swizzle/swizzle.doc.mjs: the ERR_PATH_TRAVERSAL entry names the symlink case.

A dangling symlink, whose target does not exist yet, is not resolved by assertWithin today. That belongs to the shared guard and is fixed separately there. With that fix, this change covers the dangling case too, because every destination now passes the guard.

Evidence

  • Reproduction before the change: in a project with components/astryx/Button -> ../../../outside, astryx --json swizzle Button exits 0, reports outputDir: components/astryx/Button, and writes outside/Button.tsx. With components/astryx/Button/Button.tsx symlinked to an outside file, astryx --json swizzle Button --overwrite exits 0 and replaces that outside file's contents.
  • Result after the change: both runs exit 1 with {"code": "ERR_PATH_TRAVERSAL"}, and nothing outside the project changes.
  • Representative unchanged path: a normal swizzle into a fresh or existing real folder, --overwrite over real files, and the existing --output ../x and absolute-path refusals (swizzle.path-safety.test.mjs, copy.test.mjs, swizzle.routing.test.mjs, all green).
  • Regression test: two new cases in clients/cli/commands/swizzle.path-safety.test.mjs (a symlinked component folder, and a symlinked existing file with --overwrite). Both fail on main. They also fail when only the per-file check is removed, and pass with the fix.

Scope

  • One defect is restored.
  • Separable contradictory or unsettled tagalongs were removed or split.
  • No new public API, visual representation, default, or interaction model is hidden under the bug-fix label.
  • Public text and artifacts contain no internal Meta context.

Testing

pnpm -F @astryxdesign/cli exec tsc --project tsconfig.api-dts.json
pnpm check:cli-structure
pnpm -F @astryxdesign/cli readme:check
pnpm -F @astryxdesign/cli typecheck:json-api
pnpm -F @astryxdesign/cli typecheck:strict
ASTRYX_STRICT_LINT=1 pnpm exec eslint packages/cli/api/swizzle/copy/copy.mjs packages/cli/api/swizzle/swizzle.doc.mjs packages/cli/clients/cli/commands/swizzle.path-safety.test.mjs
pnpm check:repo
pnpm exec vitest run packages/cli    # 4300 passed, 5 skipped

swizzle checked --output with assertWithin but then wrote the component folder
and files without checking them. A symlinked component folder or file inside
the output base sent the copy outside the project, with exit 0.
@vercel

vercel Bot commented Sep 24, 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 24, 2026 7:55am UTC

Request Review

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Sep 24, 2026
@astracat-bot

astracat-bot Bot commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

AI review status for this pull request.

Review status Updated
🟢 Reviewed (for maintainers only) Sep 24, 2026, 10:03 AM UTC

@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.

This change correctly confines astryx swizzle writes to the project. Previously only --output was checked, so a symlinked component folder or destination file could silently write — or overwrite with --overwrite — outside the project while reporting success. Now every destination file is checked before any write and an escape is refused with ERR_PATH_TRAVERSAL, matching the documented confined-write contract. The new regression tests cover both the symlinked folder and the symlinked file cases, and normal swizzle paths are unchanged.

One scope note: dangling symlinks are not yet resolved by the shared path guard itself, so coverage for that case depends on the separate guard fix — this change already routes every destination through the guard, so no further swizzle change should be needed once that lands.

[Automated review]

@josephfarina
josephfarina marked this pull request as ready for review September 24, 2026 20:56
@josephfarina
josephfarina merged commit 63d3eda into main Sep 24, 2026
38 checks passed
@github-actions
github-actions Bot deleted the cli-spec/component-swizzle/swizzle-confine-writes branch September 25, 2026 06:23

This branch was successfully deployed

1 active deployment
Preview — 012ba4ca Deployed Sep 24, 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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant