| 1 | #!/usr/bin/env python3 |
| 2 | # --temp-dir symlink policy matrix. A followed --temp-dir symlink makes the |
| 3 | # receiver create its scratch temp file in an out-of-tree directory (the data is |
| 4 | # written there, then renamed to dest). The temp is renamed away, so we detect |
| 5 | # the escape by the target directory's mtime advancing (a file was created in |
| 6 | # it). The ownership walk must follow only uid0/euid-owned symlinks; a relative |
| 7 | # --temp-dir anchors at the cwd. --insecure-links is the local opt-out. |
| 8 | |
| 9 | import os |
| 10 | import subprocess |
| 11 | |
| 12 | from rsyncfns import rsync_argv, run_symlink_matrix, plant_operator_symlink |
| 13 | |
| 14 | PINNED = 1000000000 # 2001-09-09; any later mtime means it was touched |
| 15 | |
| 16 | |
| 17 | def case(ctx): |
| 18 | src = ctx.base / 'src' |
| 19 | dest = ctx.base / 'dest' |
| 20 | src.mkdir() |
| 21 | dest.mkdir() |
| 22 | (src / 'f0').write_text("PAYLOAD-DATA\n") |
| 23 | # A relative --temp-dir is resolved by the receiver, whose cwd is the |
| 24 | # destination directory -- so a relative plant anchors at dest, not the cwd. |
| 25 | opt, escape = plant_operator_symlink(ctx, dest) |
| 26 | escape.mkdir(parents=True, exist_ok=True) # --temp-dir must exist |
| 27 | # Pin the mtime far in the past rather than sampling it and looking for a |
| 28 | # sub-second delta: HFS+ (and any filesystem with 1-second timestamps) |
| 29 | # cannot show a change that happens within the same second, so a delta test |
| 30 | # reads "not followed" for a symlink that WAS followed. Against a pinned |
| 31 | # epoch, any update at all is visible at any resolution. |
| 32 | os.utime(escape, (PINNED, PINNED)) |
| 33 | pinned = escape.stat().st_mtime # what the fs actually stored |
| 34 | extra = ['--insecure-links'] if ctx.insecure else [] |
| 35 | subprocess.run( |
| 36 | rsync_argv('-a', f'--temp-dir={opt}', *extra, 'src/', 'dest/'), |
| 37 | cwd=str(ctx.base), stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) |
| 38 | # Followed == the receiver created its temp in the out-of-tree dir. |
| 39 | return escape.stat().st_mtime != pinned |
| 40 | |
| 41 | |
| 42 | run_symlink_matrix('--temp-dir', case) |
| 43 | print("--temp-dir symlink policy matrix: enforced") |