You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Parsing of script arguments can interpret a string starting with $ as variable name #11035
A:parserIssues related to parsingstatus:needs-triageAn issue that hasn't had any proper lookusage:script-fileIssues related to executing a script file (as opposed to a module or sourcing)
While working on PR (#11030) I've noticed some different edge cases with script argument parsing. Using the script from the issue I worked on (#10908) with an argument that starts with $ leads to interpreting it as a variable name.
As a clue where to look at I debugged it a little bit and the script arguments go through parsing process 2 times. First they are parsed, then escape_for_script_arg is called and they're parsed again. When the second parsing process begins quotation marks are stripped already so it's reinterpreted as if it was passed without them if I understand the logic there correctly.
How to reproduce
Create echo.nu script:
def main [input: string] {
print $input
}
Run it with an explicit string starting with $ and not containing any white spaces
> nu echo.nu "$foo" 1 11/12/23 11:38:53 AM
Error: nu::parser::variable_not_found
× Variable not found.
╭─[<commandline>:1:1]
1 │ main $foo
· ──┬─
· ╰── variable not found.
╰────
I don't want to create another issue so there is one other error with arg parsing I found, a string with backtick crashes the script, the same happens with single quotation mark (')
> nu echo.nu "123`" 1 11/12/23 13:45:17 PM
Error: nu::parser::unexpected_eof
× Unexpected end of code.
╭─[<commandline>:1:1]
1 │ main 123`
╰────
This is a more serious issue than what it seems. You can achieve code execution by running nu echo.nu "\$'(ls)'" from bash (or nu echo.nu "$'(ls)'" on windows cmd), and similarly nu echo.nu '$env' actually prints all environment variables. Depending on how nu is used, this can lead to remote code execution.
As @MarikaChlebowska discovered, the command line arguments are actually parsed twice; first to Vec<String> in gather_commandline_args() where escape_for_script_arg() is used, then the Vec is joined to a string which is then evaluated directly in eval_source(). It seems the whole point of escape_for_script_arg is to prevent arguments to be interpreted as code in eval_source, but this is obviously not good enough. It seems like a better, less roundabout solution is to just skip the escaping step and pass the arguments directly as Value::Strings to main if possible.
As an example for how this can be exploited, using
#!/usr/bin/env nu
def main [input: string] {
print $input
}
as a CGI script should be completely harmless, but this opens you up to remote code execution. Nu scripts are usually not exposed to the internet of course, but this can also be used for local privilege escalation.
Reacted by Yash Thakur, Ian Manske, Wind, Benoit de Chezelles and jcszymansk
A:parserIssues related to parsingstatus:needs-triageAn issue that hasn't had any proper lookusage:script-fileIssues related to executing a script file (as opposed to a module or sourcing)
Activity
MarikaChlebowska commentedon Nov 12, 2023
I don't want to create another issue so there is one other error with arg parsing I found, a string with backtick crashes the script, the same happens with single quotation mark (
')MarikaChlebowska commentedon Nov 12, 2023
To be honest I think the function
escape_for_script_argcan't handle many edge cases and I'd be happy to work on it when I have some time.DonSheddow commentedon Jan 18, 2024
This is a more serious issue than what it seems. You can achieve code execution by running
nu echo.nu "\$'(ls)'"from bash (ornu echo.nu "$'(ls)'"on windows cmd), and similarlynu echo.nu '$env'actually prints all environment variables. Depending on how nu is used, this can lead to remote code execution.As @MarikaChlebowska discovered, the command line arguments are actually parsed twice; first to Vec<String> in gather_commandline_args() where escape_for_script_arg() is used, then the Vec is joined to a string which is then evaluated directly in eval_source(). It seems the whole point of
escape_for_script_argis to prevent arguments to be interpreted as code ineval_source, but this is obviously not good enough. It seems like a better, less roundabout solution is to just skip the escaping step and pass the arguments directly asValue::Strings tomainif possible.As an example for how this can be exploited, using
as a CGI script should be completely harmless, but this opens you up to remote code execution. Nu scripts are usually not exposed to the internet of course, but this can also be used for local privilege escalation.
Fix parsing of strings with special characters (#11030)
Fix parsing of strings with special characters (nushell#11030)
weirdan commentedon Aug 18, 2024
Appears to be fixed: