I have the following Ruby code:
Testcase.rb:
filename = "/absolute/path/开发者_JS百科to/Untrusted.rb"
thread = Thread.new do
$SAFE = 4
Kernel::load(filename, true)
end
thread.join
Untrusted.rb
#Untrusted code
puts "Untrusted!"
However, I get an error when I try to run Testcase.rb:
/Volumes/Data/Users/mike/Desktop/Testcase.rb:4:in `write': Insecure operation `write' at level 4 (SecurityError)
from /Volumes/Data/Users/mike/Desktop/Testcase.rb:7:in `join'
from /Volumes/Data/Users/mike/Desktop/Testcase.rb:7
Removing $SAFE=4
solves the issue, but I want to be able to safely run untrusted code. Any ideas about why this isn't working?
I tried your code and got the same result as you. Then I changed the $SAFE level to 3, and got this warning:
Insecure world writable dir /tmp in LOAD_PATH, mode 041777
I moved the file being loaded from /tmp to a directory that isn't world-writable and the warning went away. Changing the $SAFE level to 4 then worked.
So, try making sure that the loaded file's directory isn't world writable. Also try a lower safe level and see if you get a useful warning.
If you are running in a sandbox, it doesn't allow unsafe code to be run, because a sandbox has its purpose, to keep you from doing something that's not allowed or unsafe.
精彩评论