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
37 changes: 30 additions & 7 deletions python/tarfile-extractall-traversal.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@
import tarfile

def get_parser():
parser = argparse.ArgumentParser(description='Process zip files.')
parser.add_argument('--path', type=str, required=True,
help='Path to zip file')
parser.add_argument('--save-path', type=str, required=True,
help='Output path')
parser = argparse.ArgumentParser(description="Process tar files.")
parser.add_argument("--path", type=str, required=True, help="Path to tar file")
parser.add_argument("--save-path", type=str, required=True, help="Output path")
return parser


Expand All @@ -17,14 +15,12 @@ def extract_1(args):
with tarfile.open(path) as f:
f.extractall(save_path)


def extract_2(args):
path = args.path
save_path = args.save_path
# ruleid: tarfile-extractall-traversal
tarfile.open(path).extractall(save_path)


def extract_3(args):
path = args.path
save_path = args.save_path
Expand All @@ -33,25 +29,49 @@ def extract_3(args):
tf.extractall(save_path)

def extract_4(args, tarobj):
save_path = args.save_path
# ruleid: tarfile-extractall-traversal
tf = tarfile.open(mode='r', fileobj=None)
tf.extractall(save_path)

def extract_5(args, tarobj):
save_path = args.save_path
# ok: tarfile-extractall-traversal
tf = tarfile.open(mode='r', fileobj=None)
tf.extractall(save_path, members=safu_members())

def extract_6(args, tarobj):
save_path = args.save_path
# ok: tarfile-extractall-traversal
tf = tarfile.open(mode='r', fileobj=None)
tf.extractall(members=safu_members(), numeric_owner=True, path=save_path)

def extract_7(args, tarobj):
save_path = args.save_path
# todoruleid: tarfile-extractall-traversal
tf = tarfile.open(mode='r', fileobj=None)
tf.extractall(members=None, numeric_owner=True, path=save_path)

def extract_8(args):
path = args.path
save_path = args.save_path
# ok: tarfile-extractall-traversal
with tarfile.open(path) as f:
f.extractall(save_path, filter='data')

def extract_9(args):
path = args.path
save_path = args.save_path
# ok: tarfile-extractall-traversal
tarfile.open(path).extractall(save_path, filter='data')

def extract_10(args):
path = args.path
save_path = args.save_path
# ok: tarfile-extractall-traversal
tf = tarfile.open(path)
tf.extractall(save_path, filter='data')

def run():
parser = get_parser()
args = parser.parse_args()
Expand All @@ -62,6 +82,9 @@ def run():
extract_5(args)
extract_6(args)
extract_7(args)
extract_8(args)
extract_9(args)
extract_10(args)


if __name__ == '__main__':
Expand Down
10 changes: 10 additions & 0 deletions python/tarfile-extractall-traversal.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,13 @@ rules:
$TAR = tarfile.open(...)
...
$TAR.extractall(..., members=$MEMBERS, ...)
- pattern-not: |
with tarfile.open(...) as $TAR:
...
$TAR.extractall(..., filter=$FILTER, ...)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

per https://docs.python.org/3/library/tarfile.html#default-named-filters we may still want to have failing behavior if filter is 'fully_trusted', or None prior to 3.14, but at least this forces it to be explicit

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I agree. I think we should still detect cases where filter is set to "fully_trusted", "tar", or None. Since the default will be "data" in Python 3.14 I think that's a reasonable acknowledgment that even "tar" has some security concerns. For example, it allows links to absolute paths or paths outside of the destination.

- pattern-not: |
tarfile.open(...).extractall(..., filter=$FILTER, ...)
- pattern-not: |
$TAR = tarfile.open(...)
...
$TAR.extractall(..., filter=$FILTER, ...)
Loading