Reject a path that escapes a temporary workspace
A workspace check must compare normalized paths, not merely look for .. in supplied text. This example creates a TemporaryDirectory , resolves its root, and then resolves two candidate paths. The first candidate enters reports , returns with .. , and ends at report.txt inside the workspace. The second ends at a sibling location and must be rejected. Path.relative_to() returns the child portion when its receiver is beneath the supplied base; otherwise it raises ValueError . The assertion therefore verifies the accepted candidate’s relative name, while the except branch explicitly records rejection of the escaping candidate. Path.resolve() is important here because it eliminates .. components and follows existing symbolic links before comparison. See the official pathlib documentation and relative_to reference . This is suitable for a Python 3.6+ baseline, which includes Path.resolve() . It is a path-validation step, not a complete authorization system. In particular, filesyst...