Skip to content

Commit 7789ebb

Browse files
authored
Merge pull request #40 from mcdruid/master
Add tests for symlink security fixes (CVE-2021-32610) [tests + patch by Drew Webber]
2 parents 4d761c5 + 8c00f3c commit 7789ebb

4 files changed

+54
-0
lines changed

tests/evil_symlink_win.phpt

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
--TEST--
2+
tests extraction of out-of-path symlink with a windows path
3+
--SKIPIF--
4+
--FILE--
5+
<?php
6+
require_once dirname(__FILE__) . '/setup.php.inc';
7+
$extract_target = dirname(__FILE__) . '/evil_symlink_win';
8+
mkdir($extract_target, 0777, TRUE);
9+
$tar = new Archive_Tar(dirname(__FILE__) . '/evil_symlink_win.tar');
10+
$tar->extract($extract_target);
11+
// On Windows dirname() will have used backslashes but the error messages do not.
12+
$extract_target = str_replace('\\', '/', $extract_target);
13+
$phpunit->assertErrors(array(array('package' => 'PEAR_Error', 'message' => 'Out-of-path file extraction {' . $extract_target . '/evil.txt --> C:\windows\system.ini}')), 'after 1');
14+
// N.B. file_exists() typically will not detect a broken symbolic link
15+
$phpunit->assertFalse(is_link($extract_target . '/evil.txt'), 'Out-of-path symlink should not have succeeded');
16+
echo 'tests done';
17+
?>
18+
--CLEAN--
19+
<?php
20+
unlink(dirname(__FILE__) . '/evil_symlink_win/evil.txt');
21+
rmdir(dirname(__FILE__) . '/evil_symlink_win');
22+
?>
23+
--EXPECT--
24+
tests done

tests/evil_symlink_win.tar

1.5 KB
Binary file not shown.

tests/out_of_path_relative.phpt

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
--TEST--
2+
tests extraction of out-of-path symlink
3+
--SKIPIF--
4+
--FILE--
5+
<?php
6+
require_once dirname(__FILE__) . '/setup.php.inc';
7+
$extract_target = dirname(__FILE__) . '/one/two/three/four';
8+
mkdir($extract_target, 0777, TRUE);
9+
file_put_contents(dirname(__FILE__) . '/one/two/secret.txt', 'password1');
10+
$tar = new Archive_Tar(dirname(__FILE__) . '/out_of_path_relative.tar');
11+
$tar->extract($extract_target);
12+
// On Windows dirname() will have used backslashes but the error messages do not.
13+
$extract_target = str_replace('\\', '/', $extract_target);
14+
$phpunit->assertErrors(array(array('package' => 'PEAR_Error', 'message' => 'Out-of-path file extraction {' . $extract_target . '/five/six/evil.txt --> ../../../../secret.txt}')), 'after 1');
15+
$phpunit->assertFileNotExists($extract_target . '/five/six/evil.txt', 'Out-of-path symlink should not have succeeded');
16+
echo 'tests done';
17+
?>
18+
--CLEAN--
19+
<?php
20+
unlink(dirname(__FILE__) . '/one/two/secret.txt');
21+
unlink(dirname(__FILE__) . '/one/two/three/four/five/six/evil.txt');
22+
rmdir(dirname(__FILE__) . '/one/two/three/four/five/six');
23+
rmdir(dirname(__FILE__) . '/one/two/three/four/five');
24+
rmdir(dirname(__FILE__) . '/one/two/three/four');
25+
rmdir(dirname(__FILE__) . '/one/two/three');
26+
rmdir(dirname(__FILE__) . '/one/two');
27+
rmdir(dirname(__FILE__) . '/one');
28+
?>
29+
--EXPECT--
30+
tests done

tests/out_of_path_relative.tar

10 KB
Binary file not shown.

0 commit comments

Comments
 (0)