Skip to content

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

Closed

Description

@hakula139

Bug report form

  • I have done a basic search of the issue tracker to find any existing issues that are similar.
  • I have checked that my version is at least the latest stable release available via my installation method.

Describe the bug

nu <script> <args...> turns the arguments into the source main <args> and parses that source again. escape_for_script_arg quotes an argument only when string_should_be_quoted finds a listed special character, and three argument shapes slip through, so they are parsed as code instead of being passed as strings:

  1. Flag names. For an argument starting with --, the part before the first = is never checked (L34-L46). --x;print INJECTED and --x;print INJECTED=1 both become main --x;print INJECTED..., which runs print.
  2. Redirection tokens. o>, e>>, out+err>, and the other redirection forms contain no listed character, so nu script.nu o> file redirects the script's output into file (creating or truncating it) instead of passing two strings.
  3. Spreads. ...$var does not start with $, so it stays unquoted and ...$env.PATH is evaluated and spread into the arguments.

Any wrapper that forwards untrusted strings to nu script.nu is affected. #9939 and #11035 report other symptoms of the same re-parsing, but neither covers these shapes.

How to reproduce

  1. Create the script:

    'def main [--x, ...args] { $args | to nuon }' | save test.nu
  2. From a POSIX shell, run it with each argument shape (the release 0.117.0 binary and the nightly 0.117.0-nightly.1+a481925 behave identically):

    $ nu -n test.nu a 'b c'
    [a, "b c"]
    
    $ nu -n test.nu '--x;print INJECTED'
    INJECTED
    
    $ nu -n test.nu '--x;print INJECTED=1'
    INJECTED=1
    
    $ nu -n test.nu 'o>' out.txt
    $ cat out.txt
    []
    
    $ nu -n test.nu 'out+err>' out.txt
    $ cat out.txt
    []
    
    $ nu -n test.nu 'e>>' out.txt
    []
    $ cat out.txt
    
    $ env -i PATH=/usr/bin:/bin nu -n test.nu '...$env.PATH'
    [/usr/bin, /bin]

The first call shows normal behavior. In the flag cases print runs. In the redirection cases out.txt is created and the script receives no arguments. In the spread case the script receives the contents of $env.PATH.

Expected behavior

I expected nu to pass every command-line argument to main as data, the way it handles a and 'b c' above:

  • '--x;print INJECTED' is rejected as an unknown or malformed flag, or passed as a string, and never runs print.
  • nu test.nu 'o>' out.txt prints ["o>", out.txt] and leaves out.txt untouched.
  • nu test.nu '...$env.PATH' prints ["...$env.PATH"].

A possible fix direction is to treat an argument as a flag only when it is --name or --name=value with a plain identifier name, and to quote every other argument, including redirection tokens and spreads. Building the call from parsed values instead of re-parsing source text would avoid this class of bugs.

Configuration

key value
version 0.117.0
major 0
minor 117
patch 0
branch
commit_hash a481925
build_os linux-x86_64
build_target x86_64-unknown-linux-gnu
rust_version rustc 1.97.1 (8bab26f4f 2026-07-14)
rust_channel 1.97.1-x86_64-unknown-linux-gnu
cargo_version cargo 1.97.1 (c980f4866 2026-06-30)
build_time 2026-11-09 21:15:12 +00:00
build_rust_channel release
allocator standard
features dap, default, lsp, mcp, network, plugin, rustls-tls, sqlite, static-link-openssl, trash-support
installed_plugins
experimental_options example=false, dc-glob=false, reorder-cell-paths=true, pipefail=true, enforce-runtime-annotations=true, native-clip=false, cell-path-types=false

This is the official 0.117.0 release binary, whose commit is also the current main head. The nightly 0.117.0-nightly.1+a481925 reproduces every case above.

Activity

  1. fdncred commented on Nov 10, 2026

    @fdncred
    Contributor

    Thanks for reporting this. I don't think it's a regression but definitely a bug. I'll work on it. I think we need to make escape_for_script_arg smarter.

  2. fdncred commented on Nov 10, 2026

    @fdncred
    Contributor

    Let me know if this fixes the issue for you #19486

  3. hakula139 commented on Nov 11, 2026

    @hakula139
    Author

    @fdncred Yes, this fixes the issue in my testing on Linux x86_64 with the CI artifact from run 42862116242, built from PR head 9d674d1 (CI merge commit 6363718).

    The original injection-shaped flags now arrive unchanged in $args, and print does not execute. The redirection-shaped arguments (o>, out+err>, e>>, a>e> with out.txt) arrive literally and leave the target's exact bytes untouched, and ...$env.PATH stays literal. All 28 checks against the fixture passed, including the broader parser shapes, nu -c, glob and path expansion, typed values, and valid flags. The same harness against official 0.117.0 reproduced the injections, output truncation, and PATH spread.

    Thanks for the fix.

  4. added this to the v0.118.0 milestone on Nov 12, 2026
  5. added a commit that references this issue on Nov 12, 2026
    622be20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions