Skip to content

fix(toolkit-lib): reject discovered Windows paths containing %VAR% - #1924

Open
iankhou wants to merge 2 commits into
mainfrom
iankhou-windows-var-path-rejection
Open

fix(toolkit-lib): reject discovered Windows paths containing %VAR%#1924
iankhou wants to merge 2 commits into
mainfrom
iankhou-windows-var-path-rejection

Conversation

@iankhou

@iankhou iankhou commented Sep 1, 2026

Copy link
Copy Markdown
Contributor

Follow-up to #1763 / #1849.

Summary

quoteShellPart() (toolkit-lib) — cmd.exe expands %VAR% even inside double quotes, and a cmd /c command line (how runUserCommandLine reaches the shell on Windows) cannot reliably escape a percent (doubling only works in batch files). A filesystem-discovered path carrying a %...% reference spliced into the user's app command line would therefore be silently rewritten — an environment variable expanded into the path. It is now refused loudly (ToolkitError('UnsafeWindowsPath', ...)) rather than executing something other than what is on disk.

Demonstration of the problem

Ran these commands on a Windows machine in cmd.

# USERNAME should be interpreted as a variable, as expansion occurs inside double quotes
D:\Users\ianhou>echo "C:\proj\%USERNAME%\app"
"C:\proj\ianhou\app"

# Here, we can't successfully escape USERNAME using %USERNAME%
D:\Users\ianhou>cmd /c echo "%USERNAME%"
"ianhou"

# And we can't do it using %%USERNAME%% either
D:\Users\ianhou>cmd /c echo "%%USERNAME%%"
"%ianhou%"

# Here is the only case where we escape it
D:\Users\ianhou>cmd /c echo "^%USERNAME^%"
"^%USERNAME^%"

Testing

  • Unit test in environment.test.ts forcing platform=win32 and asserting a discovered %VAR% path is rejected.

This test demonstrates that given a %VAR% path on Windows, guessExecutable throws UnsafeWindowsPath instead of returning a quoted string. What it does NOT demonstrate is the problem itself - that cmd.exe actually expands %VAR% inside double quotes. However, we demonstrated that in the section "Demonstration of the problem" above.

Checklist

  • Unit tests added/updated
  • No manual edits to generated files

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license.

cmd.exe expands %VAR% even inside double quotes, and a cmd /c command line
cannot reliably escape a percent (doubling only works in batch files). A
filesystem-discovered path carrying a %...% reference spliced into the user's
app command line would therefore be silently rewritten (an env var expanded
into the path). quoteShellPart now refuses such a path loudly instead of
executing something other than what is on disk.

Split out of #1905, which now carries only the resolveExecutable (PATH-vs-cwd)
hardening. Follow-up to #1763 / #1849.
@iankhou
iankhou deployed to automation September 1, 2026 15:44 — with GitHub Actions Active
@github-actions github-actions Bot added the p2 label Sep 1, 2026
iankhou added a commit that referenced this pull request Sep 1, 2026
The quoteShellPart %VAR% guard is an independent fix on the shell path and now
lives in its own PR (#1924). This PR is scoped to the resolveExecutable
(PATH-vs-cwd) hardening on the no-shell run() path.
@iankhou
iankhou deployed to no-approval September 1, 2026 15:45 — with GitHub Actions Active
@aws-cdk-automation
aws-cdk-automation requested a review from a team September 1, 2026 15:49
@iankhou iankhou self-assigned this Sep 3, 2026
@iankhou
iankhou marked this pull request as ready for review September 3, 2026 17:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants