From fee5cde00be7342dc6c00d0b0a0276d09e5252e3 Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada Date: Tue, 29 Oct 2019 22:39:30 +0900 Subject: Fix tests for CVE-2018-6914 Since the current working directory is not involved in `Tempfile` and `Dir.mktmpdir` (except for the last resort), it is incorrect to derive the traversal path from it. Also, since the rubyspec temporary directory is created under the build directory, this is not involved in the target method. Fixed sporadic errors in test-spec. --- test/test_tmpdir.rb | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) (limited to 'test/test_tmpdir.rb') diff --git a/test/test_tmpdir.rb b/test/test_tmpdir.rb index 1e633d233b..42bcbc00a8 100644 --- a/test/test_tmpdir.rb +++ b/test/test_tmpdir.rb @@ -65,22 +65,29 @@ class TestTmpdir < Test::Unit::TestCase } end - TRAVERSAL_PATH = Array.new(Dir.pwd.split('/').count, '..').join('/') + Dir.pwd + '/' - TRAVERSAL_PATH.delete!(':') if /mswin|mingw/ =~ RUBY_PLATFORM - def test_mktmpdir_traversal - expect = Dir.glob(TRAVERSAL_PATH + '*').count - Dir.mktmpdir(TRAVERSAL_PATH + 'foo') do - actual = Dir.glob(TRAVERSAL_PATH + '*').count - assert_equal expect, actual + assert_mktmpdir_traversal do |traversal_path| + Dir.mktmpdir(traversal_path + 'foo') do |actual| + actual + end end end def test_mktmpdir_traversal_array - expect = Dir.glob(TRAVERSAL_PATH + '*').count - Dir.mktmpdir([TRAVERSAL_PATH, 'foo']) do - actual = Dir.glob(TRAVERSAL_PATH + '*').count - assert_equal expect, actual + assert_mktmpdir_traversal do |traversal_path| + Dir.mktmpdir([traversal_path, 'foo']) do |actual| + actual + end + end + end + + def assert_mktmpdir_traversal + Dir.mktmpdir do |target| + target = target.chomp('/') + '/' + traversal_path = target.sub(/\A\w:/, '') # for DOSISH + traversal_path = Array.new(target.count('/')-2, '..').join('/') + traversal_path + actual = yield traversal_path + assert_not_send([File.absolute_path(actual), :start_with?, target]) end end end -- cgit v1.2.1