# Re: Strange safe level change

**URL:** <https://www.ruby-forum.com/t/re-strange-safe-level-change/81539>\
**Category:** Ruby\
**Created:** [January 9, 2007, 7:41pm UTC](https://www.ruby-forum.com/t/re-strange-safe-level-change/81539 "2007-01-09T19:41:23Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gennady\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c57346/32.png) [@Gennady\_B](https://www.ruby-forum.com/u/Gennady_B)\
**Post date:** [January 9, 2007, 7:41pm UTC](https://www.ruby-forum.com/t/re-strange-safe-level-change/81539/1 "2007-01-09T19:41:23Z")

</div>

Eric H. wrote:

> $SAFE is switched when you assign to it, never at any other time.  
> Is it so? Consider the following code:

puts RUBY\_VERSION, RUBY\_PLATFORM

class Test  
def show\_safe  
puts $SAFE  
end  
end

Test.new.show\_safe  
Test.new.method(:show\_safe).taint.call

It produces:

:!ruby safe.rb  
1.8.4  
i686-linux  
0  
safe.rb:5:in `write': Insecure operation`write’ at level 4  
(SecurityError)  
from safe.rb:5:in `show\_safe’  
from safe.rb:12

> You have code like $SAFE = 4 in your program somwhere. Use grep to  
> find it.

Not necessarily. See above.

> Typically $SAFE = 4 is only set in a spawned thread since its so  
> strict its rarely useful outside of sandboxing dangerous code.

Gennady.

---

<div class="post-metadata">

**Author:** ![Gennady\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c57346/32.png) [@Gennady\_B](https://www.ruby-forum.com/u/Gennady_B)\
**Post date:** [January 9, 2007, 9:41pm UTC](https://www.ruby-forum.com/t/re-strange-safe-level-change/81539/2 "2007-01-09T21:41:46Z")

</div>

On Jan 9, 2007, at 10:40, Gennady B. wrote:

> > 
> 
> i686-linux  
> 0  
> safe.rb:5:in `write': Insecure operation `write’ at level 4  
> (SecurityError)  
> from safe.rb:5:in `show\_safe’  
> from safe.rb:12

Interesting.

You are right. From method\_call:

```
 if (OBJ_TAINTED(method)) {
     safe = NOEX_WITH(data->safe_level, 4)|NOEX_TAINTED;
 }

```

$SAFE is only changed for the method invocation, it does not leak  
into the surrounding process. puts $SAFE afterward shows the  
original safe level.

–  
Eric H. - removed\_email\_address@domain.invalid - [http://blog.segment7.net](http://blog.segment7.net)

I LIT YOUR GEM ON FIRE!
