Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add exclusion patterns for tarfile-extractall-traversal #72

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
if filter arg is passed to extractall() dont trigger rule
  • Loading branch information
cjenn-aviatrix committed Dec 27, 2024
commit dbfbc02f407ce9ccf201d2da952a01b0af336597
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

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