Skip to content
Snippets Groups Projects
Commit 633b2aaa authored by Jacob Vosmaer's avatar Jacob Vosmaer
Browse files

Guard against path traversal and leading '|'

This change adds some checks against path traversal ('..') and
accidentally shelling out (opening a file starting with '|').
parent d37c628f
No related branches found
No related tags found
No related merge requests found
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
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