On Tue, Apr 6, 2010 at 12:52 PM, Brandon McCaig <bamcc...@gmail.com> wrote:
> The single quotes are necessary to prevent the '~' and '|' characters
> from being interpreted by the shell. Essentially, you can never trust
> the user when writing programs and always have to assume there will be
> malicious users trying to do bad things. You always want to do
> everything in your power to prevent this. In the case of open, that
> means making sure the program always opens a file and never runs a
> command, regardless of what the EXPR argument is.

Actually, forget the malicious users. If a script is being invoked
directly by a user then it'll probably not be malicious (who would
remove their home directory, and if they were going to why would they
use your script to do it?). However, consider my `touch' example.

>  touch 'rm -fR ~ |'

What if that was instead your script.

  your_script 'rm -fR ~ |'

If your script claimed to expect a file then I would expect that to be
interpreted as a file, just as it was with touch. If touch was written
with Perl and used the two argument open syntax then it would actually
remove my home directory, which is not what I wanted at all!
Similarly, so would your_script if it used Perl's two argument open
syntax. That's essentially why you should use the three argument
version when you're expecting to just open a file. Or you can document
the argument so that the user knows how to use it, but that's still
risky if they fail to read the documentation or aren't familiar with
programming (even though 'rm -fR ~ |' is an unlikely filename).

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to