Skip to content

Commit 633b2aa

Browse files
committed
Guard against path traversal and leading '|'
This change adds some checks against path traversal ('..') and accidentally shelling out (opening a file starting with '|').
1 parent d37c628 commit 633b2aa

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

lib/grit/git-ruby/repository.rb

+3-1
Original file line numberDiff line numberDiff line change
@@ -683,7 +683,9 @@ def self.create_initial_config(bare = false)
683683
end
684684

685685
def self.add_file(name, contents)
686-
File.open(name, 'w') do |f|
686+
path = File.join(Dir.pwd, name)
687+
raise "Invalid path: #{path}" unless File.absolute_path(path) == path
688+
File.open(path, 'w') do |f|
687689
f.write contents
688690
end
689691
end

lib/grit/git.rb

+7-2
Original file line numberDiff line numberDiff line change
@@ -117,15 +117,19 @@ def shell_escape(str)
117117
#
118118
# Returns Boolean
119119
def fs_exist?(file)
120-
File.exist?(File.join(self.git_dir, file))
120+
path = File.join(self.git_dir, file)
121+
raise "Invalid path: #{path}" unless File.absolute_path(path) == path
122+
File.exist?(path)
121123
end
122124

123125
# Read a normal file from the filesystem.
124126
# +file+ is the relative path from the Git dir
125127
#
126128
# Returns the String contents of the file
127129
def fs_read(file)
128-
File.read(File.join(self.git_dir, file))
130+
path = File.join(self.git_dir, file)
131+
raise "Invalid path: #{path}" unless File.absolute_path(path) == path
132+
File.read(path)
129133
end
130134

131135
# Write a normal file to the filesystem.
@@ -135,6 +139,7 @@ def fs_read(file)
135139
# Returns nothing
136140
def fs_write(file, contents)
137141
path = File.join(self.git_dir, file)
142+
raise "Invalid path: #{path}" unless File.absolute_path(path) == path
138143
FileUtils.mkdir_p(File.dirname(path))
139144
File.open(path, 'w') do |f|
140145
f.write(contents)

0 commit comments

Comments
 (0)