Forcing file operations under a directory

M

Michael Schuerig

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
 
A

ara.t.howard

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



Dir.chdir '/var/tmp/safe_place' do

....

end


a @ http://codeforpeople.com/
 
X

Xavier Noria

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
 
X

Xavier Noria

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
 
M

Michael Schuerig

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top