Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion beaker/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ def encoded_path(root, identifiers, extension=".enc", depth=3,
else:
ident = sha1(ident).hexdigest()

ident = os.path.basename(ident)
ident = os.path.basename(ident).lstrip('.')

tokens = []
for d in range(1, depth):
Expand Down
32 changes: 32 additions & 0 deletions tests/test_encoded_path.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from beaker.util import encoded_path
import pathlib

def test_strips_leading_periods(tmp_path):
"""
Ensure that leading periods in the identifier are stripped when
digest_filenames=False to prevent limited traversal
"""

out = encoded_path(
root=tmp_path,
identifiers=["..poc"],
digest_filenames=False
)

p = pathlib.Path(out)

# The resulting filename must not begin with a dot
assert not p.name.startswith("."), "Leading periods should be stripped"

# After stripping leading dots, the stem should match
assert p.stem == "poc", "Filename should preserve content minus leading dots"

# And extension should still be present
assert p.suffix == ".enc"

# encoded path should be a child of input, ./po/p/poc.enc
assert p.is_relative_to(tmp_path)

# check no traversal has put the encoded directory back to the input
assert str(p.parent) != str(tmp_path.absolute())