| 1 |
|
| 2 |
"""--copy-links may follow ".." inside the module, but never out of it. |
| 3 |
|
| 4 |
Allowing a parent-relative symlink target to resolve at all is a loosening: |
| 5 |
the resolver used to refuse every literal ".." at the front door, which |
| 6 |
protected the module boundary by accident while also breaking legitimate |
| 7 |
in-module targets. Now the fd-anchored walk decides, popping a held parent |
| 8 |
descriptor per ".." and refusing to pop above the module root. |
| 9 |
|
| 10 |
So both directions need holding down together, which is what this checks: |
| 11 |
|
| 12 |
in-module ../target.txt file symlink -> dereferenced (the loosening works) |
| 13 |
in-module ../targetdir dir symlink -> dereferenced |
| 14 |
escaping ../../outside/* file symlink -> refused, no content leaves |
| 15 |
escaping ../../outside/* dir symlink -> refused, no content leaves |
| 16 |
|
| 17 |
A file and a directory symlink are both covered because they take different |
| 18 |
paths through the sender -- the directory one already understood "..", the |
| 19 |
file one did not, which is the asymmetry this fix removes. |
| 20 |
""" |
| 21 |
|
| 22 |
import os |
| 23 |
import subprocess |
| 24 |
|
| 25 |
from rsyncfns import ( |
| 26 |
SCRATCHDIR, makepath, rmtree, rsync_argv, start_test_daemon, test_fail, |
| 27 |
write_daemon_conf, |
| 28 |
) |
| 29 |
|
| 30 |
PORT = 12945 |
| 31 |
|
| 32 |
IN_FILE = 'IN-MODULE-FILE\n' |
| 33 |
IN_DIRFILE = 'IN-MODULE-DIRFILE\n' |
| 34 |
SECRET = 'OUT-OF-MODULE-SECRET\n' |
| 35 |
DIRSECRET = 'OUT-OF-MODULE-DIRSECRET\n' |
| 36 |
|
| 37 |
base = SCRATCHDIR / 'copylinks-parent-escape' |
| 38 |
rmtree(base) |
| 39 |
module = base / 'module' |
| 40 |
outside = base / 'outside' |
| 41 |
dest = base / 'dest' |
| 42 |
makepath(module / 'sub', module / 'targetdir', outside / 'outdir', dest) |
| 43 |
|
| 44 |
(module / 'target.txt').write_text(IN_FILE) |
| 45 |
(module / 'targetdir' / 'f.txt').write_text(IN_DIRFILE) |
| 46 |
|
| 47 |
(outside / 'secret.txt').write_text(SECRET) |
| 48 |
(outside / 'outdir' / 's.txt').write_text(DIRSECRET) |
| 49 |
|
| 50 |
os.symlink('../target.txt', module / 'sub' / 'in_file') |
| 51 |
os.symlink('../targetdir', module / 'sub' / 'in_dir') |
| 52 |
os.symlink('../../outside/secret.txt', module / 'sub' / 'esc_file') |
| 53 |
os.symlink('../../outside/outdir', module / 'sub' / 'esc_dir') |
| 54 |
|
| 55 |
conf = write_daemon_conf([ |
| 56 |
('m', {'path': str(module), 'read only': 'yes', 'use chroot': 'no'}), |
| 57 |
], name='copylinks-parent-escape.conf') |
| 58 |
url = start_test_daemon(conf, PORT) |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
proc = subprocess.run( |
| 63 |
rsync_argv('-r', '--copy-links', f'{url}m/sub/', str(dest) + '/'), |
| 64 |
stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True) |
| 65 |
ctx = f'rc={proc.returncode}, output={proc.stdout.strip()[:400]!r}' |
| 66 |
|
| 67 |
|
| 68 |
for path in sorted(p for p in dest.rglob('*') if p.is_file()): |
| 69 |
body = path.read_text(errors='replace') |
| 70 |
if body in (SECRET, DIRSECRET): |
| 71 |
test_fail(f'--copy-links followed a symlink out of the module: {path} ' |
| 72 |
f'holds out-of-module content ({ctx})') |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
got_file = dest / 'in_file' |
| 77 |
if not got_file.is_file() or got_file.is_symlink(): |
| 78 |
test_fail(f'in-module ../target.txt was not dereferenced ({ctx})') |
| 79 |
if got_file.read_text() != IN_FILE: |
| 80 |
test_fail(f'in-module file symlink gave wrong content: ' |
| 81 |
f'{got_file.read_text()!r} ({ctx})') |
| 82 |
|
| 83 |
got_dirfile = dest / 'in_dir' / 'f.txt' |
| 84 |
if not got_dirfile.is_file() or got_dirfile.read_text() != IN_DIRFILE: |
| 85 |
test_fail(f'in-module ../targetdir was not dereferenced ({ctx})') |
| 86 |
|
| 87 |
print('copy-links followed ".." inside the module and refused it at the root') |