From 95f5606fa7f9787b618174505237c0909c4544d0 Mon Sep 17 00:00:00 2001 From: Jono Yang Date: Tue, 14 Jun 2022 16:11:24 -0700 Subject: [PATCH] Add tests for extract_tar Signed-off-by: Jono Yang --- src/container_inspector/utils.py | 20 ++++++--- tests/data/tar/absolute_path.tar | Bin 0 -> 10240 bytes tests/data/tar/colon.tar.xz | Bin 0 -> 188 bytes tests/data/tar/tar_relative.tar | Bin 0 -> 10240 bytes tests/test_utils.py | 75 +++++++++++++++++++++++++++++++ 5 files changed, 90 insertions(+), 5 deletions(-) create mode 100644 tests/data/tar/absolute_path.tar create mode 100644 tests/data/tar/colon.tar.xz create mode 100644 tests/data/tar/tar_relative.tar create mode 100644 tests/test_utils.py diff --git a/src/container_inspector/utils.py b/src/container_inspector/utils.py index 7c0edbe..740d0ae 100755 --- a/src/container_inspector/utils.py +++ b/src/container_inspector/utils.py @@ -97,36 +97,46 @@ def extract_tar(location, target_dir, skip_symlinks=True): with tarfile.open(location) as tarball: # never extract character device, block and fifo files: # we extract dirs, files and links only - to_extract = [] + error_messages = [] for tarinfo in tarball: if TRACE: logger.debug(f'_extract_tar: {tarinfo}') if tarinfo.isdev() or tarinfo.ischr() or tarinfo.isblk() or tarinfo.isfifo() or tarinfo.sparse: + msg = f'_extract_tar: skipping unsupported {tarinfo} file type: block, chr, dev or sparse file' + error_messages.append(msg) if TRACE: - logger.debug(f'_extract_tar: skipping unsupported {tarinfo} file type: block, chr, dev or sparse file') + logger.debug(msg) continue if '..' in tarinfo.name: - if TRACE: logger.debug(f'_extract_tar: skipping unsupported {tarinfo} with relative path') + msg = f'_extract_tar: skipping unsupported {tarinfo} with relative path' + error_messages.append(msg) + if TRACE: + logger.debug(msg) continue if tarinfo.islnk() or tarinfo.issym(): try: target = tarball._find_link_target(tarinfo) if not target: + msg = f'_extract_tar: skipping link with missing target: {tarinfo}' + error_messages.append(msg) if TRACE: - logger.debug(f'_extract_tar: skipping link with missing target: {tarinfo}') + logger.debug(msg) continue except Exception: import traceback + msg = f'_extract_tar: skipping link with missing target: {tarinfo}: {traceback.format_exc()}' + error_messages.append(msg) if TRACE: - logger.debug(f'_extract_tar: skipping link with missing target: {tarinfo}: {traceback.format_exc()}') + logger.debug(msg) continue tarinfo.mode = 0o755 tarinfo.name = tarinfo.name.lstrip('/') tarball.extract(member=tarinfo, path=target_dir, set_attrs=False,) + return error_messages def extract_tar_with_symlinks(location, target_dir): diff --git a/tests/data/tar/absolute_path.tar b/tests/data/tar/absolute_path.tar new file mode 100644 index 0000000000000000000000000000000000000000..0b168da538ab9a466b06f31a9d2ca85fbb218f83 GIT binary patch literal 10240 zcmeIyOAdlC5P;#XJw;C-v`{!tQHUh?2-N8DEx53R#9fJze>UwDQpz_Bi96qt;a(lv zJ`s^3^-&rlrm=XBem>>nnh&9r)|oPCo@tRPUHT759?^9V}N+Ht-XIX5y@%mfYv|YVhu&}TV zfw2FpU+Dko|4;v~_4QTs|60^M2LS{SKmY**5I_I{1Q0*~0R#|0009ILKmY**5I_I{ L1Q0*~foK9R;hsut literal 0 HcmV?d00001 diff --git a/tests/data/tar/colon.tar.xz b/tests/data/tar/colon.tar.xz new file mode 100644 index 0000000000000000000000000000000000000000..e8481c9d0a801606e767c8d1b948866542b6b5cb GIT binary patch literal 188 zcmV;t07L)%H+ooF000E$*0e?f03iVu0001VFXf})2mb(hT>vqg$N& z*e=+F66_D+%jGGkz9*+9E>NZG3X>-d?0b#LMD=mVu?LRMsHOL*=z%ICUB<6S>XQRI z9LBF!xU*##W2M(YqbgeOu{$jZ{KXfAT;%+fM{(U5m?`$kNV)-8pqy?xo4pACNZJK< qHPZk90001^xl4oFsT14)0hIxO5C8zyZ{b3*#Ao{g000001X)@t=2GAQ literal 0 HcmV?d00001 diff --git a/tests/data/tar/tar_relative.tar b/tests/data/tar/tar_relative.tar new file mode 100644 index 0000000000000000000000000000000000000000..0caff864506dd5c31a8b108fe68f77c2e28faf32 GIT binary patch literal 10240 zcmeIz@2bKe9LDioN>|{f?a$?iBTwf=WVqj`%g?QdLr|O-%l6_c1I5YI4?a$*(%F-X z{wVe*9Ckh`xk#~SsZwd%wwUTvp6k@r7FwIybXwQjG)^~Lql-xd;s@EdHLZxX!U9 zqVezi*Cyq^Ynq1q=Qsy*{`(v=F~=GOQuuGb{c;%ld#ksP_P6rinTq`9I4|Tshca^@ ofB*srAb