1
0
mirror of synced 2024-11-25 18:54:22 +01:00

add --unknown-members argument to mat2

This allows the user to make use of parser.unknown_member_policy for
archive formats.

At the suggestion of @jvoisin, it also prints a scary warning if the
user explicitly chooses 'keep'.
This commit is contained in:
Daniel Kahn Gillmor 2018-08-31 21:33:42 -04:00
parent 4192a2daa3
commit 10d60bd398
2 changed files with 17 additions and 5 deletions

16
mat2
View File

@ -41,6 +41,9 @@ def create_arg_parser():
help='check if MAT2 has all the dependencies it needs') help='check if MAT2 has all the dependencies it needs')
parser.add_argument('-V', '--verbose', action='store_true', parser.add_argument('-V', '--verbose', action='store_true',
help='show more verbose status information') help='show more verbose status information')
parser.add_argument('-u', '--unknown-members', metavar='POLICY', default='abort',
help='how to handle unknown members of archive-style files ' +
'(POLICY should be abort, omit, or keep)')
info = parser.add_mutually_exclusive_group() info = parser.add_mutually_exclusive_group()
@ -67,8 +70,8 @@ def show_meta(filename: str):
except UnicodeEncodeError: except UnicodeEncodeError:
print(" %s: harmful content" % k) print(" %s: harmful content" % k)
def clean_meta(params: Tuple[str, bool]) -> bool: def clean_meta(params: Tuple[str, bool, str]) -> bool:
filename, is_lightweight = params filename, is_lightweight, unknown_member_policy = params
if not __check_file(filename, os.R_OK|os.W_OK): if not __check_file(filename, os.R_OK|os.W_OK):
return False return False
@ -76,6 +79,7 @@ def clean_meta(params: Tuple[str, bool]) -> bool:
if p is None: if p is None:
print("[-] %s's format (%s) is not supported" % (filename, mtype)) print("[-] %s's format (%s) is not supported" % (filename, mtype))
return False return False
p.unknown_member_policy = unknown_member_policy
if is_lightweight: if is_lightweight:
return p.remove_all_lightweight() return p.remove_all_lightweight()
return p.remove_all() return p.remove_all()
@ -133,9 +137,15 @@ def main():
return 0 return 0
else: else:
if args.unknown_members == 'keep':
logging.warning('Keeping unknown member files may leak metadata in the resulting file!')
elif args.unknown_members not in ['omit', 'abort']:
logging.warning('Undefined policy for handling unknown member files: "%s"',
args.unknown_members)
p = multiprocessing.Pool() p = multiprocessing.Pool()
mode = (args.lightweight is True) mode = (args.lightweight is True)
l = zip(__get_files_recursively(args.files), itertools.repeat(mode)) l = zip(__get_files_recursively(args.files), itertools.repeat(mode),
itertools.repeat(args.unknown_members))
ret = list(p.imap_unordered(clean_meta, list(l))) ret = list(p.imap_unordered(clean_meta, list(l)))
return 0 if all(ret) else -1 return 0 if all(ret) else -1

View File

@ -8,12 +8,14 @@ class TestHelp(unittest.TestCase):
def test_help(self): def test_help(self):
proc = subprocess.Popen(['./mat2', '--help'], stdout=subprocess.PIPE) proc = subprocess.Popen(['./mat2', '--help'], stdout=subprocess.PIPE)
stdout, _ = proc.communicate() stdout, _ = proc.communicate()
self.assertIn(b'usage: mat2 [-h] [-v] [-l] [-c] [-V] [-s | -L] [files [files ...]]', stdout) self.assertIn(b'usage: mat2 [-h] [-v] [-l] [-c] [-V] [-u POLICY] [-s | -L] [files [files ...]]',
stdout)
def test_no_arg(self): def test_no_arg(self):
proc = subprocess.Popen(['./mat2'], stdout=subprocess.PIPE) proc = subprocess.Popen(['./mat2'], stdout=subprocess.PIPE)
stdout, _ = proc.communicate() stdout, _ = proc.communicate()
self.assertIn(b'usage: mat2 [-h] [-v] [-l] [-c] [-V] [-s | -L] [files [files ...]]', stdout) self.assertIn(b'usage: mat2 [-h] [-v] [-l] [-c] [-V] [-u POLICY] [-s | -L] [files [files ...]]',
stdout)
class TestVersion(unittest.TestCase): class TestVersion(unittest.TestCase):