| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
import os |
| 8 |
import subprocess |
| 9 |
|
| 10 |
from rsyncfns import TOOLDIR, test_fail |
| 11 |
|
| 12 |
|
| 13 |
t_unsafe = str(TOOLDIR / 't_unsafe') |
| 14 |
|
| 15 |
er |
| 16 |
|
| 17 |
pwd = os.getcwd() |
| 18 |
|
| 19 |
". |
| 20 |
CASES = [ |
| 21 |
('file', 'from', 'safe'), |
| 22 |
('dir/file', 'from', 'safe'), |
| 23 |
('dir/./file', 'from', 'safe'), |
| 24 |
('dir/.', 'from', 'safe'), |
| 25 |
('dir/', 'from', 'safe'), |
| 26 |
|
| 27 |
('/etc/passwd', 'from', 'unsafe'), |
| 28 |
('//../etc/passwd', 'from', 'unsafe'), |
| 29 |
('//./etc/passwd', 'from', 'unsafe'), |
| 30 |
|
| 31 |
('./foo', 'from', 'safe'), |
| 32 |
('../foo', 'from', 'unsafe'), |
| 33 |
('./../foo', 'from', 'unsafe'), |
| 34 |
('.//../foo', 'from', 'unsafe'), |
| 35 |
('./../foo', 'from/..', 'unsafe'), |
| 36 |
('../dest', 'from/dir', 'safe'), |
| 37 |
('../../dest', 'from//dir', 'unsafe'), |
| 38 |
('..//../dest', 'from/dir', 'unsafe'), |
| 39 |
|
| 40 |
('..', 'from/file', 'safe'), |
| 41 |
('../..', 'from/file', 'unsafe'), |
| 42 |
('..//..', 'from//file', 'unsafe'), |
| 43 |
('dir/..', 'from', 'unsafe'), |
| 44 |
('dir/../..', 'from', 'unsafe'), |
| 45 |
('dir/..//..', 'from', 'unsafe'), |
| 46 |
|
| 47 |
('', 'from', 'unsafe'), |
| 48 |
|
| 49 |
|
| 50 |
('../../unsafe/unsafefile', 'from/safe', 'unsafe'), |
| 51 |
('..//../unsafe/unsafefile', 'from/safe', 'unsafe'), |
| 52 |
('../files/file1', 'from/safe', 'safe'), |
| 53 |
|
| 54 |
('../../unsafe/unsafefile', 'safe', 'unsafe'), |
| 55 |
('../files/file1', 'safe', 'unsafe'), |
| 56 |
|
| 57 |
('../../unsafe/unsafefile', f'{pwd}/from/safe', 'safe'), |
| 58 |
('../files/file1', f'{pwd}/from/safe', 'safe'), |
| 59 |
] |
| 60 |
|
| 61 |
|
| 62 |
failures = [] |
| 63 |
for target, curdir, expected in CASES: |
| 64 |
proc = subprocess.run( |
| 65 |
[t_unsafe, target, curdir], |
| 66 |
capture_output=True, text=True, |
| 67 |
) |
| 68 |
if proc.returncode != 0: |
| 69 |
test_fail(f"Failed to check {target!r} {curdir!r}: exit {proc.returncode}") |
| 70 |
got = proc.stdout.strip() |
| 71 |
if got != expected: |
| 72 |
failures.append( |
| 73 |
f"t_unsafe {target!r} {curdir!r} returned {got!r}, expected {expected!r}" |
| 74 |
) |
| 75 |
|
| 76 |
if failures: |
| 77 |
test_fail("\n".join(failures)) |