Skip to content

Commit 0ce1455

Browse files
authored
strip leading periods (#257)
* prevent traversal * tests * strip periods entirely
1 parent 096cd37 commit 0ce1455

2 files changed

Lines changed: 33 additions & 1 deletion

File tree

beaker/util.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ def encoded_path(root, identifiers, extension=".enc", depth=3,
233233
else:
234234
ident = sha1(ident).hexdigest()
235235

236-
ident = os.path.basename(ident)
236+
ident = os.path.basename(ident).replace('.', '')
237237

238238
tokens = []
239239
for d in range(1, depth):

tests/test_encoded_path.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from beaker.util import encoded_path
2+
import pathlib
3+
4+
def test_strips_leading_periods(tmp_path):
5+
"""
6+
Ensure that leading periods in the identifier are stripped when
7+
digest_filenames=False to prevent limited traversal
8+
"""
9+
10+
out = encoded_path(
11+
root=tmp_path,
12+
identifiers=["..poc"],
13+
digest_filenames=False
14+
)
15+
16+
p = pathlib.Path(out)
17+
18+
# The resulting filename must not begin with a dot
19+
assert not p.name.startswith("."), "Leading periods should be stripped"
20+
21+
# After stripping leading dots, the stem should match
22+
assert p.stem == "poc", "Filename should preserve content minus leading dots"
23+
24+
# And extension should still be present
25+
assert p.suffix == ".enc"
26+
27+
# encoded path should be a child of input, ./po/p/poc.enc
28+
assert p.is_relative_to(tmp_path)
29+
30+
# check no traversal has put the encoded directory back to the input
31+
assert str(p.parent) != str(tmp_path.absolute())
32+

0 commit comments

Comments
 (0)