From: Bill Kelly Date: 2003-08-13T00:58:27+09:00 Subject: Re: $SAFE = 5 and Safe Ruby Misleading? Hi, From: "Dave Fayram" > Basically, can I evaluate something in a $SAFE = 4 thread, then get it > out and work with it meaningfully? The only way I can think of is to > try analyzing it in a $SAFE = 3 thread, where an object can't untaint > itself, but doing so for an object would be pretty much impossible. Not sure if this is sophisticated enough for your purpose - I just needed arbitrary code to be evaluated "safely" at $SAFE = 4, and have it be able to modify an object passed to it and return [some] result. def safe_eval(_eval_str, *_args) th = Thread.new(_eval_str, *_args) do |eval_str, *args| res, warning = nil, nil begin $SAFE = 4 res = eval(eval_str) rescue Exception => ex warning = ex.to_s end [res, warning] end th.value end The calling code in my case is $SAFE = 1... not that the safe-level of the calling code should matter, I believe. For some reason, looking at my calling code, I wanted to pass in a tainted object so it could be modified directly by the $SAFE = 4 thread via mutator methods (i.e. gsub!)... Though if one didn't want to allow any object passed to it to be modified, the evaluated code could presumably dup the object, modify the copy, and return the modified object as a result... (Unless that's asking too much of $SAFE = 4... I don't remember ) Anyway what I'm doing is essentially: some_object_to_be_modified.taint # allow mutator methods in high SAFE levels res, warning = safe_eval(untrusted_code, some_object_to_be_modified) if warning log warning log untrusted_code end Hope this helps, Bill