Skip to content

fix: always quote the resolved next bin path - #3494

Merged
lariciamota merged 4 commits into
vtex:devfrom
angeloevangelista:fix/scape-next-bin-path
Sep 28, 2026
Merged

lariciamota merged 4 commits into
vtex:devfrom
angeloevangelista:fix/scape-next-bin-path

Conversation

@angeloevangelista

@angeloevangelista angeloevangelista commented Sep 18, 2026 •

Copy link
Copy Markdown
Contributor

What's the purpose of this pull request?

The CLI writes the next scripts of .faststore/package.json (build, serve, dev, dev-only) as node <nextBin> ..., where nextBin is the Next binary resolved from @faststore/core, relative to .faststore.

That binary is resolved through its realpath. When next is a symlink target elsewhere on disk, the relative path climbs out of .faststore and keeps every directory it does not share with it, e.g. ../../../../john doe/Area de trabalho/repo/node_modules/next/dist/bin/next. Unquoted, the shell splits that path on each space, node receives only the first piece, and the script fails with Cannot find module.

How it works?

The resolved path is now always wrapped in double quotes: node "<nextBin>" .... Double quotes are the quoting both sh and cmd.exe understand; single quotes or a backslash escape would break the script on Windows. This matches how the absolute-path version of this script was written before 4.8.0.

The comment above it was rewritten as well: it claimed the relative path only spans node_modules segments, which is the assumption this bug disproves.

Tests:

  • Added a case that builds nextBin the way the CLI does, from a .faststore and a Next binary that do not share the spaced directory, and asserts all four scripts use the quoted path.
  • Updated the existing expectations to the quoted form.

How to test it?

A store directory that merely contains spaces does not reproduce the bug: path.relative drops the directory .faststore shares with next, so the script stays node "../node_modules/next/dist/bin/next". To reproduce it, next has to resolve to a directory outside the store whose path has a space:

  • Set up a store whose next binary resolves, through a symlink, to a path such as /Users/john doe/Area de trabalho/repo/node_modules/next
  • Run faststore generate and check that the build, serve, dev and dev-only scripts in .faststore/package.json are node "<path containing the spaces>" ...
  • Run yarn dev / yarn build and confirm Next starts, where the previous version failed with Cannot find module
  • Unit tests: cd packages/cli && pnpm vitest run src/utils/generate.test.ts

Starters Deploy Preview

References

Checklist

You may erase this after checking them all 😉

PR Title and Commit Messages

  • PR title and commit messages follow the Conventional Commits specification
    • Available prefixes: feat, fix, chore, docs, style, refactor, ci and test

PR Description

  • Added a label according to the PR goal - breaking change, bug, contributing, performance, documentation..

Dependencies

  • Committed the pnpm-lock.yaml file when there were changes to the packages

Documentation

  • PR description
  • For documentation changes, ping @Mariana-Caetano to review and update (Or submit a doc request)

Summary by CodeRabbit

Summary by CodeRabbit

  • Bug Fixes
    • Generated Next.js commands now quote resolved executable paths, allowing them to run when paths contain spaces, including paths outside .faststore.
    • When a resolved path contains characters that could interfere with command execution—such as quotes, dollar signs, backticks, or percent signs—the generated scripts fall back to the next command.

@angeloevangelista
angeloevangelista requested a review from a team as a code owner September 18, 2026 19:02
@angeloevangelista
angeloevangelista requested review from gabpaladino and lucasfp13 and removed request for a team September 18, 2026 19:02
@coderabbitai

coderabbitai Bot commented Sep 18, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Navigate logical layers of code changes, visualize relationships, and explore their blast radius.

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository: vtex/faststore/.coderabbit.yaml

Review profile: CHILL

Plan: Advanced

Run ID: 2bd40761-6cc2-495f-9e80-291852711ef5

📥 Commits

Reviewing files that changed from the base of the PR and between dd52ffb and e7dcff1.

📒 Files selected for processing (1)
  • packages/cli/src/utils/generate.test.ts

Included review availability: This review used your included allowance. Your plan provides up to 2 included reviews per hour; 1 remain after this review.


Walkthrough

Generated scripts quote resolved Next.js binary paths when invoking them through Node. relativeNextBin returns undefined for paths containing specified shell characters. Tests cover paths with spaces and paths that trigger the bare next fallback.

Changes

Next.js path quoting

Layer / File(s) Summary
Quote Next.js command paths
packages/cli/src/utils/generate.ts, packages/cli/src/utils/generate.test.ts
Generated scripts quote eligible resolved Next.js paths. relativeNextBin rejects paths containing ", $, backticks, or %. Tests cover paths with spaces and paths that trigger the bare next fallback.

Priority: ⬇️ Low

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Suggested reviewers: eduardoformiga

Merge Risk: ⚪ Minimal · up to e7dcf

The generated scripts now quote eligible Next paths and use the bare command for rejected paths. The additional path failure identified already existed before this change, so no PR-introduced merge risk remains.

Architecture Summary

Architecture risk: 🔵 Low · up to e7dcf

The change affects 1 system.

Changed systems: packages/cli

Architecture concerns
No architecture-level concerns identified.

Review details

Systems and components

  • observed — packages/cli (library) was modified; 2 changed files map to changed impact.

Before / after behavior

  • observed — Modified behavior in packages/cli/src/utils/generate.ts: The generated script command now wraps nextBin in double quotes when invoking it through Node. The documentation replaces the former assertion that the relative path only spans node_modules segments with the possibility that it climbs outside .faststore, includes spaces, and is quoted for sh and cmd.exe.
  • observed — Modified behavior in packages/cli/src/utils/generate.ts: Adds UNQUOTABLE_CHARS for ", $, backticks, and %, and updates relativeNextBin’s documentation to include paths containing those characters among the cases that return undefined.
  • observed — Modified behavior in packages/cli/src/utils/generate.ts: relativeNextBin now returns the relative path only if it is nonempty, non-absolute, and contains none of the characters in UNQUOTABLE_CHARS; previously it accepted any nonempty, non-absolute relative path.
  • observed — Modified behavior in packages/cli/src/utils/generate.test.ts: Updated expected scripts to wrap the resolved Next binary path in double quotes for build, serve, dev, and dev-only.
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: quoting the resolved Next binary path in generated CLI scripts.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 2 functions across 2 files.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR

Comment @coderabbitai help to get the list of available commands.

@codesandbox-ci

codesandbox-ci Bot commented Sep 18, 2026 •

Copy link
Copy Markdown

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

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

Two updates before merge:

The title scope cli is not in the Lint PR allowlist (.github/workflows/pr.yml on dev). That check did not run on this commit; the next push will fail it. Recent CLI fixes omit the scope. fix: quote the next bin path when it contains spaces matches them.

The test plan does not reproduce the failure. A project directory that contains spaces still generates ../node_modules/next/dist/bin/next, because path.relative drops the shared prefix. The failing path is the one that climbs out of .faststore into the realpath of next, for example ../../../../john doe/Area de trabalho/.../next/dist/bin/next. Worth one sentence in "How to test it?" so the fix is verified against that path.

Comment thread packages/cli/src/utils/generate.ts Resolved
Comment thread packages/cli/src/utils/generate.test.ts Resolved
@angeloevangelista angeloevangelista changed the title fix(cli): adjust next bin path to support spaces fix: always quote the resolved next bin path Sep 28, 2026
Comment thread packages/cli/src/utils/generate.ts Resolved
Comment thread packages/cli/src/utils/generate.test.ts Resolved
Comment thread packages/cli/src/utils/generate.test.ts Resolved
@dk-pr-review

dk-pr-review Bot commented Sep 28, 2026 •

Copy link
Copy Markdown

DK Review — Audit Summary

Verdict: ⚠️ RESTRICTED

Severity Count
BLOCK 0
RESTRICT 1
SUGGEST 2

Only BLOCK and RESTRICT findings anchored to a changed line are commented inline on the diff. The other 2 findings below open no review thread.

Scenarios evaluated: feature-flags, general-review, quality-ratchet
Scenarios skipped: dependency-governance (no matching files), pipeline-config (no matching files), agent-skills-review (no matching files)


📋 Findings (3)

RESTRICT

  1. [Functional.Check] packages/cli/src/utils/generate.ts:139 — relativeNextBin only rejects relative paths containing '"', '$', '', or '%', but does not guard against a resolved path whose first character is '-'. That value is spliced into node "${nextBin}" build --webpack; shell quoting only affects word-splitting, not how node's own argv parser treats a leading-dash token as a CLI flag. A resolved relative path like -e/dist/bin/nextturns the generated script intonode "-e/dist/bin/next" build --webpack, which node parses as the -e` eval flag with the rest of the quoted string as the code argument, and 'build' becomes the next argv entry — breaking the build instead of invoking Next. This is the same threat model the PR is hardening (an oddly-named or attacker-influenced directory reaching the generated script unescaped), just via argument injection rather than shell metacharacter injection, and it isn't covered by UNQUOTABLE_CHARS or the new test cases.
    → Also fall back to undefined when the first segment of the relative path starts with '-', or prefix the path with './' before quoting so it can never be parsed as a flag.
    Source: packages/cli/src/utils/generate.ts lines 111-144 (UNQUOTABLE_CHARS / relativeNextBin)

SUGGEST

  1. [Evolvability.Textual] packages/cli/src/utils/generate.test.ts:106 — The doc comment on 'quotes a resolved Next path that leaves the store through a spaced directory' claims the test exercises realpath/symlink resolution ('The binary is resolved through its realpath, so when next is a symlink target outside the store...'), but the test body never calls relativeNextBin or creates a symlink — it hand-builds the nextBin string with path.relative and feeds it straight into buildFaststorePackageJson. The comment misrepresents what is actually verified, which can mislead future readers into thinking the realpath-resolution path is covered here.
    → Either reword the comment to say this only checks that buildFaststorePackageJson quotes a space-containing path, or rewrite the test to actually drive it through relativeNextBin with a real symlink (as the later describe('relativeNextBin', ...) block does) so the comment matches the behavior under test.
  2. [quality.new-logic-enforcement] packages/cli/src/utils/generate.test.ts:116 — There is no end-to-end test covering the 'benign' resolved-symlink case: a relativeNextBin result that legitimately escapes .faststore through a directory containing a space (but no dangerous characters) being passed into buildFaststorePackageJson and correctly quoted. The suite tests the dangerous-character fallback (relativeNextBin returns undefined) via symlinks, and separately tests quoting via a hand-constructed string, but never combines the two to confirm the real integration path works for the non-fallback, space-only case this PR is meant to fix.
    → Add a test that creates a symlink to a next binary under a space-containing (but otherwise safe) outside-the-store directory, calls relativeNextBin on it, and asserts the returned path flows into a correctly quoted buildFaststorePackageJson script — mirroring the existing symlink-based fallback tests instead of hardcoding the expected relative path.

DK Review v1.0.0 | To dismiss a finding: reply /dk-review dismiss [reason] under its comment

Double quotes stop the shell from splitting the path on spaces, but not from expanding $(...), backticks or $VAR; a " ends the quoting and cmd.exe still expands %VAR%. By returning undefined for those cases we fall back to the bare next
*/
const next = nextBin ? `node ${nextBin}` : 'next'
const next = nextBin ? `node "${nextBin}"` : 'next'

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

[Functional.Check] 🟡 RESTRICT

buildFaststorePackageJson interpolates nextBin into a shell command string (node "${nextBin}") without validating it itself. The only guard against ", $, backtick, or % in the path is the UNQUOTABLE_CHARS check inside the separate relativeNextBin function. Any caller that passes a raw resolved path to buildFaststorePackageJson without first routing it through relativeNextBin (the test file itself does exactly this) reintroduces the shell/command injection this PR aims to close.

Action: Validate for unquotable characters inside buildFaststorePackageJson itself before interpolating, or encode the invariant in the type system (e.g. a branded SanitizedRelativePath type only producible by relativeNextBin) so safety doesn't depend on every caller remembering to pre-sanitize.

To dismiss: reply /dk-review dismiss [reason] under this 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.

I'll take this in a follow-up PR. The production caller already goes through relativeNextBin, so the generated script is covered. Moving the check into buildFaststorePackageJson keeps a future caller from skipping it.

Comment thread packages/cli/src/utils/generate.test.ts Resolved
Comment thread packages/cli/src/utils/generate.test.ts Resolved

return relative && !path.isAbsolute(relative) ? relative : undefined
return relative &&
!path.isAbsolute(relative) &&

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

[Functional.Check] 🟡 RESTRICT

relativeNextBin only rejects relative paths containing '"', '$', '', or '%', but does not guard against a resolved path whose first character is '-'. That value is spliced into node "${nextBin}" build --webpack; shell quoting only affects word-splitting, not how node's own argv parser treats a leading-dash token as a CLI flag. A resolved relative path like -e/dist/bin/nextturns the generated script intonode "-e/dist/bin/next" build --webpack, which node parses as the -e` eval flag with the rest of the quoted string as the code argument, and 'build' becomes the next argv entry — breaking the build instead of invoking Next. This is the same threat model the PR is hardening (an oddly-named or attacker-influenced directory reaching the generated script unescaped), just via argument injection rather than shell metacharacter injection, and it isn't covered by UNQUOTABLE_CHARS or the new test cases.

Action: Also fall back to undefined when the first segment of the relative path starts with '-', or prefix the path with './' before quoting so it can never be parsed as a flag.
Source: packages/cli/src/utils/generate.ts lines 111-144 (UNQUOTABLE_CHARS / relativeNextBin)

To dismiss: reply /dk-review dismiss [reason] under this 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.

Not needed here. A relative path that leaves .faststore starts with .., and Node only treats the argument as a flag when the argument itself starts with -. A - later in the path is part of the file name.

@sonar-workflows

Copy link
Copy Markdown

@lariciamota
lariciamota merged commit 407a05a into vtex:dev Sep 28, 2026
10 checks passed
lariciamota added a commit that referenced this pull request Sep 28, 2026
…3503)

## Summary
- `buildFaststorePackageJson` now applies the same guard as
`relativeNextBin` before writing the Next script. A path containing `"`,
`$`, a backtick, or `%` falls back to the bare `next` command instead of
being interpolated into `node "<path>"`.
- This covers a caller that passes `nextBin` without going through
`relativeNextBin`. The production path already did; the check now lives
where the shell string is written.
- Follow-up to #3494.

## Test plan
- [ ] `cd packages/cli && pnpm vitest run src/utils/generate.test.ts`
- [ ] Confirm a path with a space is still quoted (`node "<path>"`)
- [ ] Confirm a path with `"`, `$`, a backtick, or `%` produces `next
build --webpack` (and the same for `serve`, `dev`, and `dev-only`)


Made with [Cursor](https://cursor.com)

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

* **Bug Fixes**
* Improved command generation for projects with Next.js executable paths
containing special characters. In these cases, generated scripts use the
standard `next` command instead, avoiding shell expansion issues.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->

Co-authored-by: Cursor <[email protected]>
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.

3 participants