Skip to content

Commit 0dec6be

Browse files
committed
fix(handlers): fix edge cases in tar handler (empty name, too long name).
tarfile python library does not perform any kind of defensive programming and will trigger OS level errors like ENAMETOOLONG trying to write files that have long names. This commit adds two checks to our safe implementation of tarfile so that we log the issue with a warning and skip the file. Note: tar files with such entries (long name, empty name) do not happen naturally and must be synthesized. That's why decided to skip them, in a similar fashion than GNU tar and 7zip.
1 parent 5c886cc commit 0dec6be

File tree

5 files changed

+21
-0
lines changed

5 files changed

+21
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
version https://git-lfs.github.com/spec/v1
2+
oid sha256:8b5233dfb3a4a23a3bf291bfff8ccfb371fbb2136cb093247d30af090d1e4276
3+
size 10240
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
version https://git-lfs.github.com/spec/v1
2+
oid sha256:bd002ebcec73917b4294602e2999809e4119f5209a74f92fc0f70bff838bca69
3+
size 10240
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
version https://git-lfs.github.com/spec/v1
2+
oid sha256:303980bcb9e9e6cdec515230791af8b0ab1aaa244b58a8d99152673aa22197d0
3+
size 6
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
version https://git-lfs.github.com/spec/v1
2+
oid sha256:303980bcb9e9e6cdec515230791af8b0ab1aaa244b58a8d99152673aa22197d0
3+
size 6

unblob/handlers/archive/_safe_tarfile.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
logger = get_logger()
1010

1111
RUNNING_AS_ROOT = os.getuid() == 0
12+
MAX_PATH_LEN = 255
1213

1314

1415
class SafeTarFile(TarFile):
@@ -18,6 +19,14 @@ def extract(
1819
path_as_path = Path(str(path))
1920
member_name_path = Path(str(member.name))
2021

22+
if not member.name:
23+
logger.warning("File with empty filename in tar archive. Skipping")
24+
return
25+
26+
if len(member.name) > MAX_PATH_LEN:
27+
logger.warning("File with filename too long in tar archive. Skipping")
28+
return
29+
2130
if not RUNNING_AS_ROOT and (member.ischr() or member.isblk()):
2231
logger.warning(
2332
"missing elevated permissions, skipping block and character device creation",

0 commit comments

Comments
 (0)