Skip to content

Commit

Permalink
Add jwilk's path traversal tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jdleesmiller committed Aug 26, 2018
1 parent 0586329 commit 9c468f3
Show file tree
Hide file tree
Showing 10 changed files with 93 additions and 0 deletions.
5 changes: 5 additions & 0 deletions test/data/jwilk-path-traversal-samples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Path Traversal Samples

Copied from https://github.com/jwilk/path-traversal-samples on 2018-08-26.

License: MIT
Binary file added test/data/jwilk-path-traversal-samples/absolute1.zip
Binary file not shown.
Binary file added test/data/jwilk-path-traversal-samples/absolute2.zip
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added test/data/jwilk-path-traversal-samples/relative0.zip
Binary file not shown.
Binary file added test/data/jwilk-path-traversal-samples/relative2.zip
Binary file not shown.
Binary file added test/data/jwilk-path-traversal-samples/symlink.zip
Binary file not shown.
88 changes: 88 additions & 0 deletions test/path_traversal_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
class PathTraversalTest < MiniTest::Test
TEST_FILE_ROOT = File.absolute_path('test/data/jwilk-path-traversal-samples')

def setup
FileUtils.rm_f '/tmp/moo' # with apologies to anyone using this file
end

def extract_path_traversal_zip(name)
Zip::File.open(File.join(TEST_FILE_ROOT, name)) do |zip_file|
zip_file.each do |entry|
entry.extract
end
end
end

def in_tmpdir
Dir.mktmpdir do |tmp|
test_path = File.join(tmp, 'test')
Dir.mkdir test_path
Dir.chdir(test_path) do
yield
end
end
end

def test_leading_slash
in_tmpdir do
extract_path_traversal_zip 'absolute1.zip'
assert !File.exist?('/tmp/moo')
end
end

def test_multiple_leading_slashes
in_tmpdir do
extract_path_traversal_zip 'absolute2.zip'
assert !File.exist?('/tmp/moo')
end
end

def test_leading_dot_dot
in_tmpdir do
extract_path_traversal_zip 'relative0.zip'
assert !File.exist?('../moo')
end
end

def test_non_leading_dot_dot
in_tmpdir do
extract_path_traversal_zip 'relative2.zip'
assert !File.exist?('../moo')
end
end

def test_file_symlink
in_tmpdir do
extract_path_traversal_zip 'symlink.zip'
assert File.exist?('moo')
assert !File.exist?('/tmp/moo')
end
end

def test_directory_symlink
in_tmpdir do
extract_path_traversal_zip 'dirsymlink.zip'
assert !File.exist?('/tmp/moo')
end
end

def test_two_directory_symlinks_a
in_tmpdir do
# Can't create par/moo because the symlink par is skipped.
assert_raises Errno::ENOENT do
extract_path_traversal_zip 'dirsymlink2a.zip'
end
assert File.exist?('cur')
assert_equal '.', File.readlink('cur')
end
end

def test_two_directory_symlinks_b
in_tmpdir do
extract_path_traversal_zip 'dirsymlink2b.zip'
assert File.exist?('cur')
assert_equal '.', File.readlink('cur')
assert !File.exist?('../moo')
end
end
end

0 comments on commit 9c468f3

Please sign in to comment.