Debian Codegraph @

testsuite/daemon-copylinks-parent-target-regression_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 """Daemon --copy-links must follow a safe parent-relative symlink target."""
3
4 import os
5
6 from rsyncfns import (
7 SCRATCHDIR, forced_protocol, makepath, rmtree, test_fail, test_skipped,
8 write_daemon_conf,
9 )
10 from stdio_daemon import finish_stdio_daemon, start_stdio_daemon
11
12 # The hand-rolled client below speaks protocol 30 and greets with it, so a run
13 # that pins the daemon lower cannot complete the handshake and just times out.
14 # The behaviour under test is not protocol-specific -- the sibling
15 # daemon-copylinks-parent-escape drives the same paths with the real client at
16 # whatever protocol the run selects.
17 _proto = forced_protocol()
18 if _proto is not None and _proto < 30:
19 test_skipped(f'the stdio_daemon client speaks protocol 30 (forced {_proto})')
20
21 PAYLOAD = 'safe-parent-relative-target\n'
22
23 base = SCRATCHDIR / 'daemon-copylinks-parent'
24 module = base / 'module'
25 dest = base / 'dest'
26 rmtree(base)
27 makepath(module / 'sub', dest)
28 (module / 'target').write_text(PAYLOAD)
29 (module / 'sub' / 'control').write_text('control\n')
30 os.symlink('../target', module / 'sub' / 'parent-link')
31
32 conf = write_daemon_conf([
33 ('m', {'path': module, 'read only': 'yes', 'hosts allow': '*'}),
34 ])
35 client, daemon = start_stdio_daemon(conf)
36 failure = None
37 try:
38 client.handshake(
39 'm', ['--server', '--sender', '-rLe.LsfxCIu', '.', 'm/sub/'],
40 greeting_version=30,
41 )
42 client.pull(str(dest), preserve_times=False, preserve_perms=False)
43 except Exception as exc: # noqa: BLE001 - retain daemon stderr for the oracle
44 failure = repr(exc)
45 daemon_stderr = finish_stdio_daemon(client, daemon)
46
47 got = dest / 'parent-link'
48 if failure or not got.is_file() or got.is_symlink() or got.read_text() != PAYLOAD:
49 test_fail(
50 'daemon -rL failed to dereference an in-module parent-relative '
51 f'symlink (client_error={failure}, exists={got.exists()}, '
52 f'stderr={daemon_stderr.strip()!r})'
53 )
54
55 print('daemon-copylinks-parent-target: safe ../ target was dereferenced')