-
Notifications
You must be signed in to change notification settings - Fork 124
fix: harden runWrangler against shell injection and silence DEP0190 #1248
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kirkouimet
wants to merge
7
commits into
opennextjs:main
Choose a base branch
from
kirkouimet:fix-runwrangler-shell-injection
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
726e3ef
fix: harden runWrangler against shell injection and silence DEP0190
kirkouimet 5df6c61
fix: split multi-word args at every runWrangler call site
kirkouimet 227371e
fix: address review feedback on runWrangler shell injection PR
kirkouimet 9f39728
fix: address review feedback round 2 from @vicb and @james-elicx
kirkouimet 382e5d1
fix: silence DEP0190 on Windows too via cmd.exe /c invocation
kirkouimet 229c45f
fix: correct quoteShellMeta Windows escaping algorithm
kirkouimet 5a5a768
docs: address review feedback on comments
kirkouimet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@opennextjs/cloudflare": patch | ||
| --- | ||
|
|
||
| fix: harden `runWrangler` against shell injection and silence Node.js DEP0190. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
packages/cloudflare/src/cli/commands/utils/helpers.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| import { afterEach, beforeEach, describe, expect, it } from "vitest"; | ||
|
|
||
| import { quoteShellMeta } from "./helpers.js"; | ||
|
|
||
| describe("quoteShellMeta", () => { | ||
| describe("on Windows", () => { | ||
| let originalPlatform: PropertyDescriptor | undefined; | ||
|
|
||
| beforeEach(() => { | ||
| originalPlatform = Object.getOwnPropertyDescriptor(process, "platform"); | ||
| Object.defineProperty(process, "platform", { value: "win32" }); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| if (originalPlatform) { | ||
| Object.defineProperty(process, "platform", originalPlatform); | ||
| } | ||
| }); | ||
|
|
||
| it("escapes empty strings", () => { | ||
| expect(quoteShellMeta("")).toBe('^"^"'); | ||
| }); | ||
|
|
||
| it("escapes simple alphanumeric values", () => { | ||
| expect(quoteShellMeta("simple-value")).toBe('^"simple-value^"'); | ||
| }); | ||
|
|
||
| it("does not leave bare carets in front of metacharacters inside the wrapping quotes", () => { | ||
| // Regression: an earlier implementation produced `"...^(...^)..."` which made the | ||
| // carets literal characters inside cmd.exe quotes, corrupting SQL with parens. | ||
| // The correct algorithm caret-escapes the wrapping quotes themselves, so the inner | ||
| // metacharacters stay un-escaped. | ||
| const sql = "CREATE TABLE foo (x INT);"; | ||
| const escaped = quoteShellMeta(sql); | ||
| expect(escaped).toBe('^"CREATE^ TABLE^ foo^ ^(x^ INT^)^;^"'); | ||
| // No bare `^(` or `^)` sitting inside an un-caret-escaped `"..."`. | ||
| expect(escaped).not.toMatch(/(?<!\^)"[^"]*\^[()][^"]*(?<!\^)"/); | ||
| }); | ||
|
|
||
| it("escapes paths containing spaces", () => { | ||
| expect(quoteShellMeta("C:\\Users\\Some User\\chunk.json")).toBe( | ||
| '^"C:\\Users\\Some^ User\\chunk.json^"' | ||
| ); | ||
| }); | ||
|
|
||
| it("escapes JSON values containing quotes and braces", () => { | ||
| expect(quoteShellMeta('KEY:{"foo":"bar baz"}')).toBe('^"KEY:{\\^"foo\\^":\\^"bar^ baz\\^"}^"'); | ||
| }); | ||
|
|
||
| it("doubles backslashes that precede a quote", () => { | ||
| // A backslash immediately before a quote needs doubling so the receiving program's | ||
| // argument parser sees a single literal backslash followed by an escaped quote. | ||
| expect(quoteShellMeta('\\"')).toBe('^"\\\\\\^"^"'); | ||
| }); | ||
| }); | ||
|
|
||
| describe("on POSIX", () => { | ||
| let originalPlatform: PropertyDescriptor | undefined; | ||
|
|
||
| beforeEach(() => { | ||
| originalPlatform = Object.getOwnPropertyDescriptor(process, "platform"); | ||
| Object.defineProperty(process, "platform", { value: "linux" }); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| if (originalPlatform) { | ||
| Object.defineProperty(process, "platform", originalPlatform); | ||
| } | ||
| }); | ||
|
|
||
| it("single-quotes values containing whitespace", () => { | ||
| expect(quoteShellMeta("hello world")).toBe("'hello world'"); | ||
| }); | ||
|
|
||
| it("double-quotes values containing both single and double quotes", () => { | ||
| expect(quoteShellMeta(`it's "quoted"`)).toBe(`"it's \\"quoted\\""`); | ||
| }); | ||
|
|
||
| it("backslash-escapes shell metacharacters in bare values", () => { | ||
| expect(quoteShellMeta("a;b")).toBe("a\\;b"); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,8 @@ import path from "node:path"; | |
|
|
||
| import { compareSemver } from "@opennextjs/aws/build/helper.js"; | ||
|
|
||
| import { quoteShellMeta } from "./helpers.js"; | ||
|
|
||
| export type PackagerDetails = { | ||
| /** The name of the package manager. */ | ||
| packager: "npm" | "pnpm" | "yarn" | "bun"; | ||
|
|
@@ -96,39 +98,68 @@ export function runWrangler( | |
| ): WranglerCommandResult { | ||
| const noLogs = wranglerOpts.logging === "none"; | ||
| const shouldPipeLogs = wranglerOpts.logging === "error"; | ||
| const isWin = process.platform === "win32"; | ||
|
|
||
| const packagerArgs = [ | ||
| options.packager === "bun" ? "x" : "exec", | ||
| "wrangler", | ||
| ...injectPassthroughFlagForArgs(options, [ | ||
| ...args, | ||
| ...(wranglerOpts.environment ? ["--env", wranglerOpts.environment] : []), | ||
| ...(wranglerOpts.configPath ? ["--config", wranglerOpts.configPath] : []), | ||
| ...(wranglerOpts.target === "remote" ? ["--remote"] : []), | ||
| ...(wranglerOpts.target === "local" ? ["--local"] : []), | ||
| ]), | ||
| ]; | ||
|
|
||
| // Always pipe stderr so that we can capture it for inspection. | ||
| // Keep stdin and stdout as "inherit" when not piping logs to maintain TTY detection | ||
| // (wrangler checks `process.stdin.isTTY && process.stdout.isTTY` for interactive mode). | ||
| const stdio: ("inherit" | "ignore" | "pipe")[] = | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you please re-add the former comment on this |
||
| shouldPipeLogs || noLogs ? ["ignore", "pipe", "pipe"] : ["inherit", "inherit", "pipe"]; | ||
|
|
||
| const env = { | ||
| ...process.env, | ||
| ...wranglerOpts.env, | ||
| // `.env` files are handled by the adapter. | ||
| // Wrangler would load `.env.<wrangler env>` while we should load `.env.<process.env.NEXTJS_ENV>` | ||
| // See https://opennext.js.org/cloudflare/howtos/env-vars | ||
| CLOUDFLARE_LOAD_DEV_VARS_FROM_DOT_ENV: "false", | ||
| }; | ||
|
|
||
| const result = spawnSync( | ||
| options.packager, | ||
| [ | ||
| options.packager === "bun" ? "x" : "exec", | ||
| "wrangler", | ||
| ...injectPassthroughFlagForArgs( | ||
| options, | ||
| // We use `shell: false` everywhere to avoid DEP0190. | ||
| // See https://nodejs.org/api/deprecations.html#dep0190-passing-args-to-nodechild-process-execfilespawn-with-shell-option | ||
| // | ||
| // On POSIX we can spawn the package manager binary directly: each value is passed verbatim | ||
| // as its own argv entry. | ||
| // | ||
| // On Windows the package manager is typically a `.cmd` shim. Per Node.js docs | ||
| // (https://nodejs.org/api/child_process.html#spawning-bat-and-cmd-files-on-windows) the | ||
| // recommended way to invoke a `.cmd`/`.bat` without `shell: true` is to spawn `cmd.exe` | ||
| // directly with `/c`. We pre-escape every token with `quoteShellMeta` and pass the joined | ||
| // command line as a single quoted string with `windowsVerbatimArguments: true` so Node | ||
| // does not re-escape it. This mirrors how `cross-spawn` handles non-`.exe` commands. | ||
| const result = isWin | ||
| ? spawnSync( | ||
| "cmd.exe", | ||
| [ | ||
| ...args, | ||
| wranglerOpts.environment && `--env ${wranglerOpts.environment}`, | ||
| wranglerOpts.configPath && `--config ${wranglerOpts.configPath}`, | ||
| wranglerOpts.target === "remote" && "--remote", | ||
| wranglerOpts.target === "local" && "--local", | ||
| ].filter((v): v is string => !!v) | ||
| ), | ||
| ], | ||
| { | ||
| shell: true, | ||
| // Always pipe stderr so that we can capture it for inspection. | ||
| // Keep stdin and stdout as "inherit" when not piping logs to maintain TTY detection | ||
| // (wrangler checks `process.stdin.isTTY && process.stdout.isTTY` for interactive mode). | ||
| stdio: shouldPipeLogs || noLogs ? ["ignore", "pipe", "pipe"] : ["inherit", "inherit", "pipe"], | ||
| env: { | ||
| ...process.env, | ||
| ...wranglerOpts.env, | ||
| // `.env` files are handled by the adapter. | ||
| // Wrangler would load `.env.<wrangler env>` while we should load `.env.<process.env.NEXTJS_ENV>` | ||
| // See https://opennext.js.org/cloudflare/howtos/env-vars | ||
| CLOUDFLARE_LOAD_DEV_VARS_FROM_DOT_ENV: "false", | ||
| }, | ||
| } | ||
| ); | ||
| "/d", | ||
| "/s", | ||
| "/c", | ||
| `"${[options.packager, ...packagerArgs].map((token) => quoteShellMeta(token)).join(" ")}"`, | ||
| ], | ||
| { | ||
| shell: false, | ||
| windowsVerbatimArguments: true, | ||
| stdio, | ||
| env, | ||
| } | ||
| ) | ||
| : spawnSync(options.packager, packagerArgs, { | ||
| shell: false, | ||
| stdio, | ||
| env, | ||
| }); | ||
|
|
||
| const success = result.status === 0; | ||
| const stdout = result.stdout?.toString() ?? ""; | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.