# Possibly bulletproof eval()

**URL:** <https://www.ruby-forum.com/t/possibly-bulletproof-eval/109596>\
**Category:** Ruby\
**Created:** [July 17, 2007, 7:53am UTC](https://www.ruby-forum.com/t/possibly-bulletproof-eval/109596 "2007-07-17T07:53:57Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![Giles\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/848f3c/32.png) [@Giles\_B](https://www.ruby-forum.com/u/Giles_B)\
**Post date:** [July 17, 2007, 7:53am UTC](https://www.ruby-forum.com/t/possibly-bulletproof-eval/109596/1 "2007-07-17T07:53:57Z")

</div>

I’ve got a class which loads files and turns them into ActiveRecord DB  
rows. I’m converting images on a filesystem into blobs in a database.

class ImageFile \< ActiveRecord::Base  
class \<\< self  
def import\_from\_hash(hash)  
%w{medium square thumb lsquare lthumb tiny}.each do |suffix|  
filename = “public/item/photos/” + hash[0…2] + “/” + hash +  
“\_#{suffix}.jpg”  
if File.exists?(filename)  
File.open(filename, “r”) do |file|  
image\_file = ImageFile.new  
eval (“image\_file.#{suffix} = file.read”)  
end  
end  
end  
end  
end  
end

As you can see the whole thing depends massively on eval(). Yet I  
think it’s safe. First, it’s only supposed to be run from irb. Second,  
the eval’s guarded by an array which essentially acts as a filter. It  
only ever evals code generated by this code and literals from the  
array. (No user input.)

The suffixes match existing suffixes on the filesystem. They  
correspond to different sizes of the same image. I’m essentially  
refactoring an architecture here. That’s why the redundant data in the  
DB. Each image now being represented on the filesystem with  
“xyz\_square.jpg” will soon become the square attribute of an ImageFile  
object. This isn’t the cleanest design, but it beats the filesystem  
version for my purposes (load-balance-ability and hardware stability).

Anyway, I think in addition to being safe, this code also shows  
something which would end up **significantly** less concise without  
eval(). It’s kind of deliberately Lispy and functional-programmy. I  
would never have written anything like this before reading  
“Higher-Order Perl.” I think it’s a good example of eval() being  
useful and safe. But, you know, please knock me down (if you see me  
getting high and mighty).

–  
Giles B.

Blog: [http://gilesbowkett.blogspot.com](http://gilesbowkett.blogspot.com)  
Portfolio: [http://www.gilesgoatboy.org](http://www.gilesgoatboy.org)

---

<div class="post-metadata">

**Author:** ![Giles\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/848f3c/32.png) [@Giles\_B](https://www.ruby-forum.com/u/Giles_B)\
**Post date:** [July 17, 2007, 8:41am UTC](https://www.ruby-forum.com/t/possibly-bulletproof-eval/109596/2 "2007-07-17T08:41:04Z")

</div>

On 7/17/07, Giles B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Anyway, I think in addition to being safe, this code also shows  
> something which would end up **significantly** less concise without  
> eval().  
> Hmm I dislike eval very strongly, therefore:

not tested but I think

image\_file.send “#{suffix}=”, file.read  
or even (it it is an accessor)  
image\_file.instance\_variable\_set, “@” \<\< suffix, file.read

should work too

It’s kind of deliberately Lispy and functional-programmy. I

> would never have written anything like this before reading  
> “Higher-Order Perl.” I think it’s a good example of eval() being  
> useful and safe. But, you know, please knock me down (if you see me  
> getting high and mighty).  
> Who said that 😉 ?
> 
> –  
> Giles B.
> 
> Blog: [http://gilesbowkett.blogspot.com](http://gilesbowkett.blogspot.com)  
> Portfolio: [http://www.gilesgoatboy.org](http://www.gilesgoatboy.org)

Cheers  
Robert

---

<div class="post-metadata">

**Author:** ![Giles\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/848f3c/32.png) [@Giles\_B](https://www.ruby-forum.com/u/Giles_B)\
**Post date:** [July 17, 2007, 10:36am UTC](https://www.ruby-forum.com/t/possibly-bulletproof-eval/109596/3 "2007-07-17T10:36:36Z")

</div>

On Jul 16, 2007, at 22:51, Giles B. wrote:

> ```
> File.open(filename, "r") do |file|
> 
> ```
> 
> think it’s safe.  
> echo ‘system “rm -rf /”’ \> public/item/photos/XX/Y\_medium.jpg

---

<div class="post-metadata">

**Author:** ![Giles\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/848f3c/32.png) [@Giles\_B](https://www.ruby-forum.com/u/Giles_B)\
**Post date:** [July 17, 2007, 10:39am UTC](https://www.ruby-forum.com/t/possibly-bulletproof-eval/109596/4 "2007-07-17T10:39:36Z")

</div>

On Jul 17, 2007, at 01:18, Eric H. wrote:

> > “\_#{suffix}.jpg”
> > 
> > As you can see the whole thing depends massively on eval(). Yet I  
> > think it’s safe.
> 
> echo ‘system “rm -rf /”’ \> public/item/photos/XX/Y\_medium.jpg

Hrm, sorry, no. Too tired to notice no #{} around file.read.

Still, far too dangerous, use #send instead.

---

<div class="post-metadata">

**Author:** ![Giles\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/848f3c/32.png) [@Giles\_B](https://www.ruby-forum.com/u/Giles_B)\
**Post date:** [July 17, 2007, 4:18pm UTC](https://www.ruby-forum.com/t/possibly-bulletproof-eval/109596/5 "2007-07-17T16:18:28Z")

</div>

On Jul 17, 2007, at 12:51 AM, Giles B. wrote:

> As you can see the whole thing depends massively on eval().

I see you’ve already got the answers on eval(), which I completely  
agree with.

> Each image now being represented on the filesystem with  
> “xyz\_square.jpg” will soon become the square attribute of an ImageFile  
> object. This isn’t the cleanest design, but it beats the filesystem  
> version for my purposes (load-balance-ability and hardware stability).

My database teacher use to harp on and on about how file data  
belonged in the file system (not a database). His primary reasoning  
was that you can’t meaningfully query it so you don’t gain anything  
from the database structure. I’ve also found that things get  
complicated as soon as I have a large enough file stored in a database.

Just my two cents.

James Edward G. II

---

<div class="post-metadata">

**Author:** ![Giles\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/848f3c/32.png) [@Giles\_B](https://www.ruby-forum.com/u/Giles_B)\
**Post date:** [July 17, 2007, 5:49pm UTC](https://www.ruby-forum.com/t/possibly-bulletproof-eval/109596/6 "2007-07-17T17:49:41Z")

</div>

On Jul 17, 2007, at 10:42 AM, Ryan D. wrote:

> Agreed, but you can’t tell from his code sample that he’s not doing  
> that already. We’ve got a similar model class (tree of classes  
> actually) whose @content doesn’t go the db but gets saved in a  
> after\_save handler instead. He might just be storing the useful  
> stuff in the db (dimensions, size, mimetype, and just having a row  
> so tagging and other referential things can happen).

That’s true. I handle it just like you do.

I made my assumption about how he was doing it from his comment, “I’m  
converting images on a filesystem into blobs in a database.”

James Edward G. II

---

<div class="post-metadata">

**Author:** ![Giles\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/848f3c/32.png) [@Giles\_B](https://www.ruby-forum.com/u/Giles_B)\
**Post date:** [July 17, 2007, 5:43pm UTC](https://www.ruby-forum.com/t/possibly-bulletproof-eval/109596/7 "2007-07-17T17:43:40Z")

</div>

On Jul 17, 2007, at 06:42 , James Edward G. II wrote:

> My database teacher use to harp on and on about how file data  
> belonged in the file system (not a database). His primary  
> reasoning was that you can’t meaningfully query it so you don’t  
> gain anything from the database structure. I’ve also found that  
> things get complicated as soon as I have a large enough file stored  
> in a database.

Agreed, but you can’t tell from his code sample that he’s not doing  
that already. We’ve got a similar model class (tree of classes  
actually) whose @content doesn’t go the db but gets saved in a  
after\_save handler instead. He might just be storing the useful stuff  
in the db (dimensions, size, mimetype, and just having a row so  
tagging and other referential things can happen).

---

<div class="post-metadata">

**Author:** ![Giles\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/848f3c/32.png) [@Giles\_B](https://www.ruby-forum.com/u/Giles_B)\
**Post date:** [July 17, 2007, 7:51pm UTC](https://www.ruby-forum.com/t/possibly-bulletproof-eval/109596/8 "2007-07-17T19:51:57Z")

</div>

> image\_file.send “#{suffix}=”, file.read

That’s probably the way I should do it actually. I always forget about  
send for some reason.

I had to put tons of comments like “warning do not ever copy/paste  
this!” in the code near the eval. Using send would get me around that.

–  
Giles B.

Blog: [http://gilesbowkett.blogspot.com](http://gilesbowkett.blogspot.com)  
Portfolio: [http://www.gilesgoatboy.org](http://www.gilesgoatboy.org)

---

<div class="post-metadata">

**Author:** ![Giles\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/848f3c/32.png) [@Giles\_B](https://www.ruby-forum.com/u/Giles_B)\
**Post date:** [July 17, 2007, 7:50pm UTC](https://www.ruby-forum.com/t/possibly-bulletproof-eval/109596/9 "2007-07-17T19:50:57Z")

</div>

> > Each image now being represented on the filesystem with  
> > “xyz\_square.jpg” will soon become the square attribute of an ImageFile  
> > object. This isn’t the cleanest design, but it beats the filesystem  
> > version for my purposes (load-balance-ability and hardware stability).
> 
> My database teacher use to harp on and on about how file data  
> belonged in the file system (not a database). His primary reasoning  
> was that you can’t meaningfully query it so you don’t gain anything  
> from the database structure. I’ve also found that things get  
> complicated as soon as I have a large enough file stored in a database.

I have to admit, storing the images in the DB is kind of a lame hack.  
We’re doing it because we need to be able to load-balance the app  
across three servers, and storing images on the filesystem the way the  
app currently does it blocks the load-balancing. It could probably be  
done more correctly if we had a dedicated image server, but there are  
budget questions there.

–  
Giles B.

Blog: [http://gilesbowkett.blogspot.com](http://gilesbowkett.blogspot.com)  
Portfolio: [http://www.gilesgoatboy.org](http://www.gilesgoatboy.org)

---

<div class="post-metadata">

**Author:** ![Giles\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/848f3c/32.png) [@Giles\_B](https://www.ruby-forum.com/u/Giles_B)\
**Post date:** [July 17, 2007, 8:13pm UTC](https://www.ruby-forum.com/t/possibly-bulletproof-eval/109596/10 "2007-07-17T20:13:00Z")

</div>

On Jul 17, 2007, at 10:48 , Giles B. wrote:

> I have to admit, storing the images in the DB is kind of a lame hack.  
> We’re doing it because we need to be able to load-balance the app  
> across three servers, and storing images on the filesystem the way the  
> app currently does it blocks the load-balancing. It could probably be  
> done more correctly if we had a dedicated image server, but there are  
> budget questions there.

image server or look into using mogilefs

---

<div class="post-metadata">

**Author:** ![Giles\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/848f3c/32.png) [@Giles\_B](https://www.ruby-forum.com/u/Giles_B)\
**Post date:** [July 17, 2007, 7:51pm UTC](https://www.ruby-forum.com/t/possibly-bulletproof-eval/109596/11 "2007-07-17T19:51:57Z")

</div>

> Agreed, but you can’t tell from his code sample that he’s not doing  
> that already. We’ve got a similar model class (tree of classes  
> actually) whose @content doesn’t go the db but gets saved in a  
> after\_save handler instead. He might just be storing the useful stuff  
> in the db (dimensions, size, mimetype, and just having a row so  
> tagging and other referential things can happen).

I wish! See the response about load balancing.

–  
Giles B.

Blog: [http://gilesbowkett.blogspot.com](http://gilesbowkett.blogspot.com)  
Portfolio: [http://www.gilesgoatboy.org](http://www.gilesgoatboy.org)

---

<div class="post-metadata">

**Author:** ![Giles\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/848f3c/32.png) [@Giles\_B](https://www.ruby-forum.com/u/Giles_B)\
**Post date:** [September 25, 2007, 11:03pm UTC](https://www.ruby-forum.com/t/possibly-bulletproof-eval/109596/12 "2007-09-25T23:03:32Z")

</div>

Giles B. wrote:

> ```
> File.open(filename, "r") do |file|
> 
> ```
> 
> think it’s safe. First, it’s only supposed to be run from irb. Second,  
> version for my purposes (load-balance-ability and hardware stability).
> 
> Anyway, I think in addition to being safe, this code also shows  
> something which would end up **significantly** less concise without  
> eval(). It’s kind of deliberately Lispy and functional-programmy. I  
> would never have written anything like this before reading  
> “Higher-Order Perl.” I think it’s a good example of eval() being  
> useful and safe. But, you know, please knock me down (if you see me  
> getting high and mighty).

ActiveRecord has the write\_attribute and read\_attribute methods  
which is a better choice for what you are doing. You can also  
use [] and []= as aliases for write\_attribute and read\_attribute

[http://ar.rubyonrails.org/classes/ActiveRecord/Base.html](http://ar.rubyonrails.org/classes/ActiveRecord/Base.html)

Suffixes = %w{medium square thumb lsquare lthumb tiny}

class ImageFile \< ActiveRecord::Base

```
  def ImageFile.import_from_hash(hash)
     Suffixes.each do |suffix|
         filename =
         "public/item/photos/" + hash[0..2] + "/" + hash +

```

“\_#{suffix}.jpg”  
if File.exists?(filename)  
File.open(filename, “r”) do |file|  
image\_file = ImageFile.new  
self[suffix]=file.read  
end  
end  
end  
end  
end

---

<div class="post-metadata">

**Author:** ![Giles\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/848f3c/32.png) [@Giles\_B](https://www.ruby-forum.com/u/Giles_B)\
**Post date:** [September 25, 2007, 11:04pm UTC](https://www.ruby-forum.com/t/possibly-bulletproof-eval/109596/13 "2007-09-25T23:04:04Z")

</div>

Giles B. wrote:

> > ActiveRecord has the write\_attribute and read\_attribute methods  
> > which is a better choice for what you are doing. You can also  
> > use [] and []= as aliases for write\_attribute and read\_attribute
> 
> It’s a class method which creates new instances. I don’t think you can  
> get to write\_attribute that way, because it’s a private method.

Not a problem! Just use use [] and []= public aliases for the protected  
methods.

From the RDoc for ActiveRecord.

[http://ar.rubyonrails.org/classes/ActiveRecord/Base.html#M000402](http://ar.rubyonrails.org/classes/ActiveRecord/Base.html#M000402)

## Public Instance Methods

Returns the value of the attribute identified by attr\_name after it has  
been typecast (for example, “2004-12-12” in a data column is cast to a  
date object, like Date.new(2004, 12, 12)). (Alias for the protected  
read\_attribute method).

[show source]

```
   # File lib/active_record/base.rb, line 1489

```

1489: def   
1490: read\_attribute(attr\_name)  
1491: end

[]=(attr\_name, value)

Updates the attribute identified by attr\_name with the specified value.  
(Alias for the protected write\_attribute method).

[show source]

```
   # File lib/active_record/base.rb, line 1495

```

1495: def []=(attr\_name, value)  
1496: write\_attribute(attr\_name, value)  
1497: end

---

<div class="post-metadata">

**Author:** ![Giles\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/848f3c/32.png) [@Giles\_B](https://www.ruby-forum.com/u/Giles_B)\
**Post date:** [September 25, 2007, 11:05pm UTC](https://www.ruby-forum.com/t/possibly-bulletproof-eval/109596/14 "2007-09-25T23:05:00Z")

</div>

> ActiveRecord has the write\_attribute and read\_attribute methods  
> which is a better choice for what you are doing. You can also  
> use [] and []= as aliases for write\_attribute and read\_attribute

It’s a class method which creates new instances. I don’t think you can  
get to write\_attribute that way, because it’s a private method.

–  
Giles B.

Blog: [http://gilesbowkett.blogspot.com](http://gilesbowkett.blogspot.com)  
Portfolio: [http://www.gilesgoatboy.org](http://www.gilesgoatboy.org)

---

<div class="post-metadata">

**Author:** ![Giles\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/848f3c/32.png) [@Giles\_B](https://www.ruby-forum.com/u/Giles_B)\
**Post date:** [July 17, 2007, 9:59pm UTC](https://www.ruby-forum.com/t/possibly-bulletproof-eval/109596/15 "2007-07-17T21:59:25Z")

</div>

On 7/17/07, Giles B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > image\_file.send “#{suffix}=”, file.read  
> > Well, good, but as I do not know AR I might have missed out on the AR  
> > specific solutions, maybe you should check out Brad’s last post.
> 
> That’s probably the way I should do it actually. I always forget about  
> send for some reason.
> 
> I had to put tons of comments like “warning do not ever copy/paste  
> this!” in the code near the eval. Using send would get me around that.  
> That for sure is a bad sign ;).  
> Robert
