Ruby/Class/tainted

Материал из Wiki.crossplatform.ru

Перейти к: навигация, поиск

Check if an object is considered tainted by using the tainted? method:

x = "Hello, world!"
puts x.tainted?
y = [x, x, x]
puts y.tainted?
z = 20 + 50
puts z.tainted?
a = File.open("somefile").readlines.first
puts a.tainted?
b = ENV["PATH"]
puts b.tainted?
c = [a, b]
puts c.tainted?



Force an object to be seen as untainted by calling the untaint method on the object.

while x = gets
  next if x.tainted?
  puts "=> #{eval(x)}"
end



Tell if a certain operation is safe:

def code_is_safe?(code)
  code =~ /[`;*-]/ ? false : true
end
while x = gets
  x.untaint if code_is_safe?(x)
  next if x.tainted?
  puts "=> #{eval(x)}"
end