Skip to content
Snippets Groups Projects
Commit 5b6ca7b0 authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets
Browse files

Merge branch 'path_fixes' into 'master'

Path fixes

Improve the path handling in Grit to guard against path traversal and accidentally shelling out.

See merge request !8
parents a7cd2c6f 4a36e4c4
No related branches found
No related tags found
No related merge requests found
== 2.7.3
* Add guards against path traversal and leading '|'
== 2.7.2
* Make sure grit restores old timeout value even if exception occures
 
Loading
Loading
Loading
Loading
@@ -683,7 +683,9 @@ module Grit
end
 
def self.add_file(name, contents)
File.open(name, 'w') do |f|
path = File.join(Dir.pwd, name)
raise "Invalid path: #{path}" unless File.absolute_path(path) == path
File.open(path, 'w') do |f|
f.write contents
end
end
Loading
Loading
Loading
Loading
@@ -117,7 +117,9 @@ module Grit
#
# Returns Boolean
def fs_exist?(file)
File.exist?(File.join(self.git_dir, file))
path = File.join(self.git_dir, file)
raise "Invalid path: #{path}" unless File.absolute_path(path) == path
File.exist?(path)
end
 
# Read a normal file from the filesystem.
Loading
Loading
@@ -125,7 +127,9 @@ module Grit
#
# Returns the String contents of the file
def fs_read(file)
File.read(File.join(self.git_dir, file))
path = File.join(self.git_dir, file)
raise "Invalid path: #{path}" unless File.absolute_path(path) == path
File.read(path)
end
 
# Write a normal file to the filesystem.
Loading
Loading
@@ -135,6 +139,7 @@ module Grit
# Returns nothing
def fs_write(file, contents)
path = File.join(self.git_dir, file)
raise "Invalid path: #{path}" unless File.absolute_path(path) == path
FileUtils.mkdir_p(File.dirname(path))
File.open(path, 'w') do |f|
f.write(contents)
Loading
Loading
Loading
Loading
@@ -2,7 +2,7 @@ require File.dirname(__FILE__) + '/helper'
 
class TestGit < Test::Unit::TestCase
def setup
@git = Git.new(File.join(File.dirname(__FILE__), *%w[..]))
@git = Git.new(File.absolute_path(File.join(File.dirname(__FILE__), *%w[..])))
end
 
def teardown
Loading
Loading
@@ -83,6 +83,12 @@ class TestGit < Test::Unit::TestCase
assert_equal 'bar', @git.fs_read('foo')
end
 
def test_fs_read_path_traversal
assert_raise RuntimeError do
@git.fs_read('../foo')
end
end
def test_fs_write
f = stub
f.expects(:write).with('baz')
Loading
Loading
@@ -91,6 +97,12 @@ class TestGit < Test::Unit::TestCase
@git.fs_write('foo/bar', 'baz')
end
 
def test_fs_write_path_traversal
assert_raise RuntimeError do
@git.fs_read('../foo/bar')
end
end
def test_fs_delete
FileUtils.expects(:rm_rf).with(File.join(@git.git_dir, 'foo'))
@git.fs_delete('foo')
Loading
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment