| 1 | #!/usr/bin/env python3 |
| 2 | # Python rewrite of testsuite/secure-relpath-validation.test. |
| 3 | # |
| 4 | # Regression test for codex audit Finding 5: secure_relative_open()'s |
| 5 | # front-door input check rejects "../foo" and "foo/../bar" but missed |
| 6 | # bare "..", "subdir/..", and other variants whose "/"-split components |
| 7 | # contain a literal "..". RESOLVE_BENEATH equivalents catch these in |
| 8 | # the kernel, but the per-component O_NOFOLLOW fallback (on NetBSD, |
| 9 | # OpenBSD, Solaris, Cygwin, pre-5.6 Linux) does not -- so the |
| 10 | # validation must happen at the front door. |
| 11 | # |
| 12 | # The t_secure_relpath helper runs each suspect input through |
| 13 | # secure_relative_open() and confirms it gets back -1/EINVAL (the |
| 14 | # marker that the front-door check kicked in, not the kernel). |
| 15 | |
| 16 | import subprocess |
| 17 | |
| 18 | from rsyncfns import SCRATCHDIR, TOOLDIR, rmtree, test_fail |
| 19 | |
| 20 | |
| 21 | testdir = SCRATCHDIR / 'relpath-test' |
| 22 | rmtree(testdir) |
| 23 | testdir.mkdir(parents=True) |
| 24 | |
| 25 | proc = subprocess.run([str(TOOLDIR / 't_secure_relpath'), str(testdir)]) |
| 26 | if proc.returncode != 0: |
| 27 | test_fail( |
| 28 | "t_secure_relpath rejected one or more inputs incorrectly " |
| 29 | "(see stderr above for the specific case)" |
| 30 | ) |