Re:No one expects command execution
by krylon on Friday July 03, 2015 @01:11PM
(#9826729)
Perl has a special "taint" mode (Ruby does, too, I think, but I don't know about the differences or similarties), where any data coming from outside the program - environment variables, network connections, files, etc. - is considered "tainted", i.e. treated as bad, until you validate it using regular expressions (or something along those lines). I have to admit I never tried it, but it sounds like a good idea, assuming it works as advertised, when dealing with potentially malicious input or paranoia.
[ Parent
| on HN ]
Re:No one expects command execution
by JonnieCache on Friday July 03, 2015 @01:17PM
(#9826766)
Rails has ActiveSupport::SafeBuffer for doing XSS protection, but ruby doesn't track external strings as dirty like that.
> but ruby doesn't track external strings as dirty like that.
Yes, it does. E.g:
x = STDIN.gets
puts x.tainted?
y = "hello "+x
puts y.tainted?
z = "foo"
puts z.tainted?
The two first will return "true", and the last will print "false". If you set the safe-level appropriately some methods will be disallowed for x and y above (e.g. eval()), though I would not trust that as comprehensive, but it's a lot better than nothing.> but ruby doesn't track external strings as dirty like that.
It does:
irb(main):001:0> gets.tainted?
foo
=> true
You can even set $SAFE to 1 or higher to disallow the use of tainted strings in potentially dangerous commands: freya:~ flgr$ ruby -e '$SAFE = 1; system "tar bla bla #{gets}"'
foo
-e:1:in `system': Insecure operation: -r (SecurityError)
from -e:1:in `<main>'
See http://phrogz.net/programmingruby/taint.html for more information.