# $SAFE = 5 and Safe Ruby Misleading?

**URL:** <https://rubytalk.org/t/safe-5-and-safe-ruby-misleading/7369>\
**Category:** ruby-talk\
**Created:** [12 August 2003 15:30 UTC](https://rubytalk.org/t/safe-5-and-safe-ruby-misleading/7369 "2003-08-12T15:30:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dave\_Fayram2](https://avatars.discourse-cdn.com/v4/letter/d/f0a364/32.png) [@Dave\_Fayram2](https://rubytalk.org/u/Dave_Fayram2)\
**Post date:** [12 August 2003 15:30 UTC](https://rubytalk.org/t/safe-5-and-safe-ruby-misleading/7369/1 "2003-08-12T15:30:17Z")

</div>

Well, what if I took in code, say from a semi-trusted source. Let’s say  
an IRC bot (because I helped a friend write one). Originally, the bot  
would have been able to accept strings from arbitrary users which would  
be evaluated as Ruby. The strings would return some value. It was a  
really interesting idea for such a lame domain.

But, you can’t do that. They don’t even have to modify core classes.  
They need to modify one instance one one class. This is the case where  
$SAFE = 4 is misleading. It also makes me wonder, what is the point of  
it? If you can’t output from such a block in any way that wouldn’t risk  
the integrity of the system, what does it DO? It seems to me the  
PURPOSE of $SAFE = 4 is to make it so you can run untrusted code  
without fear of it modifying the core environment.

For example, in the Programming Ruby book’s CGI example, what’s to say  
that the string didn’t look innocuous, but return an object who’s  
.to\_html or .to\_str didn’t just rm \* again? I suppose with such a  
simple expression, it’s easier, but more complex it would be  
impossible.

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.

Anyone have any suggestions on how I mi

> **···**
>
> On Monday, August 11, 2003, at 4:35 PM, Brian Candler wrote:
> 
> > So someone cannot send you a general Ruby object, unless you were to  
> > explicitly unmarshal it, and they cannot directly touch your Ruby  
> > run-time  
> > environment. If they could, they could redefine ENV to return whatever  
> > they  
> > like, or they could simply override methods in your classes to do  
> > whatever  
> > they liked.
> 
> ## – Dave Fayram [kirindave@lensmen.net](mailto:kirindave@lensmen.net) Developer / Idealist

---

<div class="post-metadata">

**Author:** ![ts1](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@ts1](https://rubytalk.org/u/ts1)\
**Post date:** [12 August 2003 15:53 UTC](https://rubytalk.org/t/safe-5-and-safe-ruby-misleading/7369/2 "2003-08-12T15:53:30Z")

</div>

> 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.

No, the only way to do it is to make the analysis at $SAFE = 4 and return  
an object with a known class.

Never try to interpret at a lower level the result of a block evaluated at  
level 4.

You have an example of such mechanism in plruby (but it's written in  
C). When plruby run with a timeout, it has 3 threads :  
&nbsp;&nbsp;&nbsp;\* main thread (level 3)  
&nbsp;&nbsp;&nbsp;\* timeout thread (level 3)  
&nbsp;&nbsp;&nbsp;\* user thread (level 4)

All evaluations are made in the user thread, and the user thread must  
return a valid object which is valided at level 4. The main thread never  
try to evaluate some code, it just expect a valid object.

Guy Decoux

---

<div class="post-metadata">

**Author:** ![Bill\_Kelly](https://avatars.discourse-cdn.com/v4/letter/b/cc9497/32.png) [@Bill\_Kelly](https://rubytalk.org/u/Bill_Kelly)\
**Post date:** [12 August 2003 15:58 UTC](https://rubytalk.org/t/safe-5-and-safe-ruby-misleading/7369/3 "2003-08-12T15:58:27Z")

</div>

Hi,

> 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

> **···**
>
> From: “Dave Fayram” [kirindave@lensmen.net](mailto:kirindave@lensmen.net)

---

<div class="post-metadata">

**Author:** ![Yukihiro\_Matsumoto2](https://avatars.discourse-cdn.com/v4/letter/y/b9e5f3/32.png) [@Yukihiro\_Matsumoto2](https://rubytalk.org/u/Yukihiro_Matsumoto2)\
**Post date:** [12 August 2003 15:59 UTC](https://rubytalk.org/t/safe-5-and-safe-ruby-misleading/7369/4 "2003-08-12T15:59:05Z")

</div>

Hi,

> Basically, can I evaluate something in a $SAFE = 4 thread, then get it  
> out and work with it meaningfully?

The untrusted thread can return value from it. For example,

def safe\_eval(str)  
Thread.start {  
$SAFE=4  
eval(str) # the last evaluated value  
}.value # can be retrieved using “value” method  
end

Besides, the thread can push data to the queue, then data is read by  
the trusted code, validated, then output.

```
						matz.

```

> **···**
>
> In message “Re: $SAFE = 5 and Safe Ruby Misleading?” on 03/08/13, Dave Fayram [kirindave@lensmen.net](mailto:kirindave@lensmen.net) writes:
