Skip to content

Stop script arguments from running as code - #19486

Merged
fdncred merged 1 commit into
nushell:mainfrom
fdncred:script_arg_escaping
Nov 12, 2026
Merged

fdncred merged 1 commit into
nushell:mainfrom
fdncred:script_arg_escaping

Conversation

@fdncred

@fdncred fdncred commented Nov 10, 2026

Copy link
Copy Markdown
Contributor

Description

nu script.nu <args> and nu -c <commands> <args> turn the arguments into the source text main <args> and parse it. escape_for_script_arg quotes the arguments that would parse as something other than a plain word, but it missed three shapes, so they ran as code.

Before and after

Script: def main [--x, ...args] { $args | to json --raw }. Main is a481925.

nu test.nu ... main this PR
a 'b c' ["a","b c"] ["a","b c"]
'--x;print INJECTED' prints INJECTED ["--x;print INJECTED"]
'--x;print INJECTED=1' prints INJECTED=1 ["--x;print INJECTED=1"]
'--help;print INJECTED' prints INJECTED ["--help;print INJECTED"]
'o>' out.txt writes [] to out.txt ["o>","out.txt"]
'out+err>' out.txt writes [] to out.txt ["out+err>","out.txt"]
'e>>' out.txt creates empty out.txt ["e>>","out.txt"]
'...$env.PATH' (PATH=/usr/bin:/bin) ["/usr/bin","/bin"] ["...$env.PATH"]
'0..$nu.pid' ["0..248"] ["0..$nu.pid"]
'=' parse_mismatch: expected right hand side of assignment ["="]
'&&' shell_andand ["&&"]
'a)' unbalanced_delimiter ["a)"]
--x b ["b"] ["b"]

Script: def main [p: path] { $p | to json --raw }.

nu test.nu ... main this PR
... "../.." "../.."
'~/a$b' "/Users/me/a$b" "/Users/me/a$b"

Script: def main [g: glob] { ls $g | get name | sort | to json --raw }, in a directory holding a$1.txt, a$2.txt, a)1.txt and a)2.txt.

nu test.nu ... main this PR
'a$*.txt' ["a$1.txt","a$2.txt"] ["a$1.txt","a$2.txt"]
'a)*.txt' unbalanced_delimiter ["a)1.txt","a)2.txt"]

User-facing changes (Release notes)

  • Fixed nu script.nu <args> and nu -c <commands> <args> running some arguments as code. An argument such as --help;print hi, a redirection such as o> file, or a variable read such as ...$env.PATH or 0..$nu.pid now reaches main as a string.
  • Script arguments =, &&, and arguments containing ) no longer fail to parse.

Additional notes

@github-actions github-actions Bot added the A:parser Issues related to parsing label Nov 10, 2026
@fdncred fdncred added status:wait-until-after-nushell-release notes:fixes Noted in "Bug fixes" section notes:ready Indicates Ready for Release notes and removed status:wait-until-after-nushell-release labels Nov 11, 2026
@fdncred
fdncred merged commit 622be20 into nushell:main Nov 12, 2026
12 checks passed
@fdncred
fdncred deleted the script_arg_escaping branch November 12, 2026 12:09
@github-actions github-actions Bot added this to the v0.118.0 milestone Nov 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A:parser Issues related to parsing notes:fixes Noted in "Bug fixes" section notes:ready Indicates Ready for Release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Script arguments run as code: escape_for_script_arg leaves flag names, redirections, and spreads unquoted

1 participant