|
| 1 | +class PathTraversalTest < MiniTest::Test |
| 2 | + TEST_FILE_ROOT = File.absolute_path('test/data/jwilk-path-traversal-samples') |
| 3 | + |
| 4 | + def setup |
| 5 | + FileUtils.rm_f '/tmp/moo' # with apologies to anyone using this file |
| 6 | + end |
| 7 | + |
| 8 | + def extract_path_traversal_zip(name) |
| 9 | + Zip::File.open(File.join(TEST_FILE_ROOT, name)) do |zip_file| |
| 10 | + zip_file.each do |entry| |
| 11 | + entry.extract |
| 12 | + end |
| 13 | + end |
| 14 | + end |
| 15 | + |
| 16 | + def in_tmpdir |
| 17 | + Dir.mktmpdir do |tmp| |
| 18 | + test_path = File.join(tmp, 'test') |
| 19 | + Dir.mkdir test_path |
| 20 | + Dir.chdir(test_path) do |
| 21 | + yield |
| 22 | + end |
| 23 | + end |
| 24 | + end |
| 25 | + |
| 26 | + def test_leading_slash |
| 27 | + in_tmpdir do |
| 28 | + extract_path_traversal_zip 'absolute1.zip' |
| 29 | + assert !File.exist?('/tmp/moo') |
| 30 | + end |
| 31 | + end |
| 32 | + |
| 33 | + def test_multiple_leading_slashes |
| 34 | + in_tmpdir do |
| 35 | + extract_path_traversal_zip 'absolute2.zip' |
| 36 | + assert !File.exist?('/tmp/moo') |
| 37 | + end |
| 38 | + end |
| 39 | + |
| 40 | + def test_leading_dot_dot |
| 41 | + in_tmpdir do |
| 42 | + extract_path_traversal_zip 'relative0.zip' |
| 43 | + assert !File.exist?('../moo') |
| 44 | + end |
| 45 | + end |
| 46 | + |
| 47 | + def test_non_leading_dot_dot |
| 48 | + in_tmpdir do |
| 49 | + extract_path_traversal_zip 'relative2.zip' |
| 50 | + assert !File.exist?('../moo') |
| 51 | + end |
| 52 | + end |
| 53 | + |
| 54 | + def test_file_symlink |
| 55 | + in_tmpdir do |
| 56 | + extract_path_traversal_zip 'symlink.zip' |
| 57 | + assert File.exist?('moo') |
| 58 | + assert !File.exist?('/tmp/moo') |
| 59 | + end |
| 60 | + end |
| 61 | + |
| 62 | + def test_directory_symlink |
| 63 | + in_tmpdir do |
| 64 | + extract_path_traversal_zip 'dirsymlink.zip' |
| 65 | + assert !File.exist?('/tmp/moo') |
| 66 | + end |
| 67 | + end |
| 68 | + |
| 69 | + def test_two_directory_symlinks_a |
| 70 | + in_tmpdir do |
| 71 | + # Can't create par/moo because the symlink par is skipped. |
| 72 | + assert_raises Errno::ENOENT do |
| 73 | + extract_path_traversal_zip 'dirsymlink2a.zip' |
| 74 | + end |
| 75 | + assert File.exist?('cur') |
| 76 | + assert_equal '.', File.readlink('cur') |
| 77 | + end |
| 78 | + end |
| 79 | + |
| 80 | + def test_two_directory_symlinks_b |
| 81 | + in_tmpdir do |
| 82 | + extract_path_traversal_zip 'dirsymlink2b.zip' |
| 83 | + assert File.exist?('cur') |
| 84 | + assert_equal '.', File.readlink('cur') |
| 85 | + assert !File.exist?('../moo') |
| 86 | + end |
| 87 | + end |
| 88 | +end |
0 commit comments