Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: @ARGV ignores quotes

by Anonymous Monk
on Jun 20, 2021 at 06:44 UTC ( [id://11134052]=note: print w/replies, xml ) Need Help??


in reply to @ARGV ignores quotes

I want to get user's command and execute it at some point.

Is it supposed to be a shell command (a single string which may contain shell-special character combinations like &&), or an executable followed by an argument list, to be executed by system PROGRAM LIST with no processing by the shell?

In the former case, any shell-special characters will be interpreted by the shell. In a way, you'd be introducing a shell injection vulnerability into your program. If the user passes types "echo hello world \`rm -rf whatever\`" as the command line argument, the string reaching the shell would be echo hello world `rm -rf whatever`, making the shell dutifully run rm -rf whatever before running echo hello world. Maybe that's what you want: after all, no security boundaries are crossed here, and the user is always able to rm -rf anything they want by typing the command instead of making your program do it.

The downside is having to quote anything that might contain spaces, dollar signs, single and double quotes and everything else. If you want to supply a file name I'm eating in bed.ogg to mpv, you have to first quote it "I'm eating in bed.ogg", then quote the quotes: "mpv \"I'm eating in bed.ogg\"". There, now it's safe to pass to your program -- if it's intended to use the shell to run the command lines. Note that you can't use single quotes here because of escaping rules, but you could if the file name didn't contain an apostrophe. Add a few layers of that ("I need to pass this file name to a program that runs a command line that runs another command line..."), and therein lies madness.

In the latter case, the arguments end up more or less directly passed to the execve system call, but they have to survive being an array of the arguments and not a single string. This way you can pass arbitrary NUL-terminated strings to the child process, no matter their contents, because a shell doesn't even come near them, so there's no danger in it interpreting `rm -rf whatever` and running it as a program, if you had a file named like that.

Replies are listed 'Best First'.
Re^2: @ARGV ignores quotes
by afoken (Chancellor) on Jun 20, 2021 at 09:47 UTC

    See also The problem of "the" default shell.

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11134052]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2024-04-19 17:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found