当一个线程的$SAFE值为4时调用一个方法,该方法也会以相同的$SAFE级别运行:
def test_method
raise "value of $SAFE inside the method: #{$SAFE}"
end
t = Thread.new{$SAFE = 4; self.test_method}; t.join
=> RuntimeError: value of $SAFE inside the method: 4
然而,当调用一个代码块时,它似乎使用它原始上下文中的 $SAFE:
test_lambda = lambda do
raise "value of $SAFE inside the lambda: #{$SAFE}"
end
t = Thread.new{$SAFE = 4; test_lambda.call}; t.join
=> RuntimeError: value of $SAFE inside the lambda: 0
有人能解释一下为什么会这样吗?这似乎是一个安全问题。
我使用 raise
而不是 puts
的原因是在 $SAFE = 4 时 puts
不起作用。
这可以用于在看似安全的上下文中评估一个被污染的字符串:
test_lambda = lambda{|s| puts "Tainted: #{s.tainted?}"; eval s}
t = Thread.new{$SAFE = 4; test_lambda.call("puts `date`")}; t.join
=> Tainted: true
=> Fri Mar 30 03:15:33 UTC 2012
$SAFE
等级为 0 的块内执行操作吗? - Andrew Grimm