diff --git a/tools/proto_format/format_api.py b/tools/proto_format/format_api.py index fc9972d8e2..80c40a8c82 100644 --- a/tools/proto_format/format_api.py +++ b/tools/proto_format/format_api.py @@ -245,12 +245,50 @@ def format_api(mode, outfile, xformed, printed): printed_dir = dst_dir.joinpath("printed") printed_dir.mkdir() with tarfile.open(printed) as tar: - tar.extractall(printed_dir) + def is_within_directory(directory, target): + + abs_directory = os.path.abspath(directory) + abs_target = os.path.abspath(target) + + prefix = os.path.commonprefix([abs_directory, abs_target]) + + return prefix == abs_directory + + def safe_extract(tar, path=".", members=None, *, numeric_owner=False): + + for member in tar.getmembers(): + member_path = os.path.join(path, member.name) + if not is_within_directory(path, member_path): + raise Exception("Attempted Path Traversal in Tar File") + + tar.extractall(path, members, numeric_owner=numeric_owner) + + + safe_extract(tar, printed_dir) xformed_dir = dst_dir.joinpath("xformed") xformed_dir.mkdir() with tarfile.open(xformed) as tar: - tar.extractall(xformed_dir) + def is_within_directory(directory, target): + + abs_directory = os.path.abspath(directory) + abs_target = os.path.abspath(target) + + prefix = os.path.commonprefix([abs_directory, abs_target]) + + return prefix == abs_directory + + def safe_extract(tar, path=".", members=None, *, numeric_owner=False): + + for member in tar.getmembers(): + member_path = os.path.join(path, member.name) + if not is_within_directory(path, member_path): + raise Exception("Attempted Path Traversal in Tar File") + + tar.extractall(path, members, numeric_owner=numeric_owner) + + + safe_extract(tar, xformed_dir) paths = [] dst_src_paths = defaultdict(list) diff --git a/tools/proto_format/proto_sync.py b/tools/proto_format/proto_sync.py index 4d329947b5..787ed4e4f3 100755 --- a/tools/proto_format/proto_sync.py +++ b/tools/proto_format/proto_sync.py @@ -52,7 +52,26 @@ def sync(api_root, formatted, mode, is_ci): # b - ideally formatted version of api directory according to bazel dst_dir = tmp_path.joinpath("b") with tarfile.open(formatted) as tar: - tar.extractall(dst_dir) + def is_within_directory(directory, target): + + abs_directory = os.path.abspath(directory) + abs_target = os.path.abspath(target) + + prefix = os.path.commonprefix([abs_directory, abs_target]) + + return prefix == abs_directory + + def safe_extract(tar, path=".", members=None, *, numeric_owner=False): + + for member in tar.getmembers(): + member_path = os.path.join(path, member.name) + if not is_within_directory(path, member_path): + raise Exception("Attempted Path Traversal in Tar File") + + tar.extractall(path, members, numeric_owner=numeric_owner) + + + safe_extract(tar, dst_dir) # These support files are handled manually. for f in ['envoy/annotations/resource.proto', 'envoy/annotations/deprecation.proto', diff --git a/tools/protoprint/protoprint_test.py b/tools/protoprint/protoprint_test.py index c353ce0c8a..b2accb123d 100644 --- a/tools/protoprint/protoprint_test.py +++ b/tools/protoprint/protoprint_test.py @@ -128,9 +128,47 @@ def main(): golden = tmp.joinpath("golden") with tarfile.open(parsed.formatted) as tar: - tar.extractall(formatted) + def is_within_directory(directory, target): + + abs_directory = os.path.abspath(directory) + abs_target = os.path.abspath(target) + + prefix = os.path.commonprefix([abs_directory, abs_target]) + + return prefix == abs_directory + + def safe_extract(tar, path=".", members=None, *, numeric_owner=False): + + for member in tar.getmembers(): + member_path = os.path.join(path, member.name) + if not is_within_directory(path, member_path): + raise Exception("Attempted Path Traversal in Tar File") + + tar.extractall(path, members, numeric_owner=numeric_owner) + + + safe_extract(tar, formatted) with tarfile.open(parsed.golden) as tar: - tar.extractall(golden) + def is_within_directory(directory, target): + + abs_directory = os.path.abspath(directory) + abs_target = os.path.abspath(target) + + prefix = os.path.commonprefix([abs_directory, abs_target]) + + return prefix == abs_directory + + def safe_extract(tar, path=".", members=None, *, numeric_owner=False): + + for member in tar.getmembers(): + member_path = os.path.join(path, member.name) + if not is_within_directory(path, member_path): + raise Exception("Attempted Path Traversal in Tar File") + + tar.extractall(path, members, numeric_owner=numeric_owner) + + + safe_extract(tar, golden) os.chdir(PATH)