Debian Codegraph @

tests/test_encoded_path.py

raw symbols SCIP
1 from beaker.util import encoded_path
2 import os
3 import pathlib
4
5 def test_strips_leading_periods(tmp_path):
6 """
7 Ensure that leading periods in the identifier are stripped when
8 digest_filenames=False to prevent limited traversal
9 """
10
11 out = encoded_path(
12 root=tmp_path,
13 identifiers=["..poc"],
14 digest_filenames=False
15 )
16
17 p = pathlib.Path(out)
18
19 # The resulting filename must not begin with a dot
20 assert not p.name.startswith("."), "Leading periods should be stripped"
21
22 # After stripping leading dots, the stem should match
23 assert p.stem == "poc", "Filename should preserve content minus leading dots"
24
25 # And extension should still be present
26 assert p.suffix == ".enc"
27
28 # encoded path should be a child of input, ./po/p/poc.enc
29 # Use os.path.relpath for Python 3.8 compatibility (is_relative_to added in 3.9)
30 relpath = os.path.relpath(p, tmp_path)
31 assert not relpath.startswith('..'), f"Path {p} is not relative to {tmp_path}"
32
33 # check no traversal has put the encoded directory back to the input
34 assert str(p.parent) != str(tmp_path.absolute())
35