Debian Codegraph @

testsuite/rename-mixed-parent-symlink-race_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # Python rewrite of testsuite/rename-mixed-parent-symlink-race.test.
3 #
4 # Regression test for do_rename_at() mixed top-level/slashed paths.
5 # The 3.4.3 symlink-race hardening opened both rename parents under
6 # secure_relative_open() only when both paths contained a slash. If one
7 # side was top-level, the whole operation fell back to plain rename(),
8 # allowing the slashed side's parent symlink to escape the module.
9
10 import os
11 import subprocess
12
13 from rsyncfns import (
14 SCRATCHDIR, TOOLDIR,
15 assert_exists, assert_not_exists, rmtree, test_fail, test_skipped,
16 )
17
18
19 mod = SCRATCHDIR / 'module'
20 trap_outside = SCRATCHDIR / 'trap'
21 rmtree(mod)
22 rmtree(trap_outside)
23 (mod / 'realdir').mkdir(parents=True)
24 trap_outside.mkdir(parents=True)
25
26 (mod / 'top-to-dir').write_text("top-to-dir\n")
27 (mod / 'realdir' / 'dir-to-top').write_text("dir-to-top\n")
28 (mod / 'top-to-escape').write_text("top-to-escape\n")
29 (trap_outside / 'outside-source').write_text("outside-source\n")
30 (mod / 'realdir' / 'same-old').write_text("same-old\n")
31 (mod / 'top-old').write_text("top-old\n")
32 os.symlink('../trap', mod / 'escape_link')
33
34 proc = subprocess.run([str(TOOLDIR / 't_rename_secure'), str(mod)])
35 if proc.returncode == 77:
36 test_skipped("t_rename_secure skipped")
37 if proc.returncode != 0:
38 test_fail("t_rename_secure reported failures (see stderr above)")
39
40 assert_not_exists(trap_outside / 'new-outside',
41 f"rename escaped the module and created {trap_outside}/new-outside")
42 assert_exists(trap_outside / 'outside-source',
43 f"rename escaped the module and moved {trap_outside}/outside-source")