# Forcing file operations under a directory

**URL:** <https://www.ruby-forum.com/t/forcing-file-operations-under-a-directory/120943>\
**Category:** Ruby\
**Created:** [November 22, 2007, 12:16am UTC](https://www.ruby-forum.com/t/forcing-file-operations-under-a-directory/120943 "2007-11-22T00:16:24Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![wickie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ba9def/32.png) [@wickie](https://www.ruby-forum.com/u/wickie)\
**Post date:** [November 22, 2007, 12:16am UTC](https://www.ruby-forum.com/t/forcing-file-operations-under-a-directory/120943/1 "2007-11-22T00:16:24Z")

</div>

I’m looking for a way to force file operations under a given root  
directory. Somewhat similar to chroot, but purely in Ruby.

For the surface syntax I have in mind something like this

File.with\_root ‘/var/tmp/safe\_place’ do  
File.open(’…/…/etc/passwd’, ‘w’) do |f|  
f.puts ‘Let’s try it…’ # No! -\> Exception  
end  
end

I have, unfortunately, no clear idea how to implement File#with\_root.  
I’m not even sure it’s possible, or possible without an inordinate  
amount of work.

My concrete problem is rather more mundane and can probably be solved  
easier. I have uploaded file data and paths where they ought to be  
stored. I’d like to make sure that they don’t escape from underneath  
the top-level directory where they are supposed to stay.

Michael

---

<div class="post-metadata">

**Author:** ![wickie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ba9def/32.png) [@wickie](https://www.ruby-forum.com/u/wickie)\
**Post date:** [November 22, 2007, 1:10am UTC](https://www.ruby-forum.com/t/forcing-file-operations-under-a-directory/120943/2 "2007-11-22T01:10:13Z")

</div>

On Nov 21, 2007, at 4:15 PM, Michael S. wrote:

> end  
> Michael
> 
> –  
> Michael S.  
> [mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [Michael Schürig | Sentenced to making sense](http://www.schuerig.de/michael/)

Dir.chdir ‘/var/tmp/safe\_place’ do

…

end

a @ [http://codeforpeople.com/](http://codeforpeople.com/)

---

<div class="post-metadata">

**Author:** ![wickie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ba9def/32.png) [@wickie](https://www.ruby-forum.com/u/wickie)\
**Post date:** [November 22, 2007, 1:28am UTC](https://www.ruby-forum.com/t/forcing-file-operations-under-a-directory/120943/3 "2007-11-22T01:28:20Z")

</div>

On Nov 22, 2007, at 1:09 AM, ara.t.howard wrote:

> Dir.chdir ‘/var/tmp/safe\_place’ do
> 
> …
> 
> end

That changes the cwd, the OP wants the block to believe that /var/tmp/  
safe\_place is /. Dir.entries("/") should list /var/tmp/safe\_place,  
system(“ls /”) I guess should do the same.

I it needs a system-level solution.

– fxn

---

<div class="post-metadata">

**Author:** ![wickie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ba9def/32.png) [@wickie](https://www.ruby-forum.com/u/wickie)\
**Post date:** [November 22, 2007, 1:37am UTC](https://www.ruby-forum.com/t/forcing-file-operations-under-a-directory/120943/4 "2007-11-22T01:37:41Z")

</div>

On Nov 22, 2007, at 12:15 AM, Michael S. wrote:

> My concrete problem is rather more mundane and can probably be solved  
> easier. I have uploaded file data and paths where they ought to be  
> stored. I’d like to make sure that they don’t escape from underneath  
> the top-level directory where they are supposed to stay.

To accomplish this you sanitize the filename, then compute  
File.expand\_path inside a Dir.chdir block (if relative filenames are  
allowed), and check whether the result is out of the root via String  
comparisons on the names (regexps, etc.)

– fxn

---

<div class="post-metadata">

**Author:** ![wickie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ba9def/32.png) [@wickie](https://www.ruby-forum.com/u/wickie)\
**Post date:** [November 23, 2007, 12:24am UTC](https://www.ruby-forum.com/t/forcing-file-operations-under-a-directory/120943/5 "2007-11-23T00:24:02Z")

</div>

On Thursday 22 November 2007, Xavier N. wrote:

> On Nov 22, 2007, at 12:15 AM, Michael S. wrote:
> 
> > My concrete problem is rather more mundane and can probably be  
> > solved easier. I have uploaded file data and paths where they ought  
> > to be stored. I’d like to make sure that they don’t escape from  
> > underneath the top-level directory where they are supposed to stay.
> 
> To accomplish this you sanitize the filename, then compute  
> File.expand\_path inside a Dir.chdir block (if relative filenames are  
> allowed), and check whether the result is out of the root via String  
> comparisons on the names (regexps, etc.)

Yes, thanks, that’s more or less what I’m doing now and relative  
filenames are disallowed anyway.

Michael
