diff options
| -rw-r--r-- | lib/archive.py | 31 | ||||
| -rw-r--r-- | lib/mat.py | 22 | ||||
| -rw-r--r-- | lib/parser.py | 6 |
3 files changed, 49 insertions, 10 deletions
diff --git a/lib/archive.py b/lib/archive.py index 6378cab..c8203c9 100644 --- a/lib/archive.py +++ b/lib/archive.py | |||
| @@ -1,6 +1,33 @@ | |||
| 1 | import tarfile | ||
| 2 | import sys | ||
| 1 | import parser | 3 | import parser |
| 4 | import mat | ||
| 2 | 5 | ||
| 3 | class TarStripper(parser.Generic_parser): | 6 | class TarStripper(parser.Generic_parser): |
| 4 | def remove_all(self): | 7 | def remove_all(self): |
| 5 | for file in self.editor.array("file"): | 8 | if not tarfile.is_tarfile(self.filename): |
| 6 | print file.name | 9 | print('%s is not a valid tar file' % self.filename) |
| 10 | sys.exit(1) | ||
| 11 | |||
| 12 | tarin = tarfile.open(self.filename, 'r') | ||
| 13 | tarout = tarfile.open(self.filename + parser.POSTFIX, 'w') | ||
| 14 | |||
| 15 | for current_file in tarin.getmembers(): | ||
| 16 | tarin.extract(current_file) | ||
| 17 | if current_file.type is '0': #is current_file a regular file ? | ||
| 18 | #no backup file | ||
| 19 | class_file = mat.create_class_file(current_file.name, False) | ||
| 20 | class_file.remove_all() | ||
| 21 | tarout.add(current_file.name) | ||
| 22 | |||
| 23 | #meta from the tar itself | ||
| 24 | tarout.mtime = None | ||
| 25 | |||
| 26 | tarout.close() | ||
| 27 | tarin.close() | ||
| 28 | |||
| 29 | def is_clean(self): | ||
| 30 | return False | ||
| 31 | |||
| 32 | |||
| 33 | |||
| @@ -14,7 +14,7 @@ import hachoir_editor | |||
| 14 | import images | 14 | import images |
| 15 | import audio | 15 | import audio |
| 16 | import misc | 16 | import misc |
| 17 | #import archive | 17 | import archive |
| 18 | 18 | ||
| 19 | __version__ = "0.1" | 19 | __version__ = "0.1" |
| 20 | __author__ = "jvoisin" | 20 | __author__ = "jvoisin" |
| @@ -24,24 +24,34 @@ strippers = { | |||
| 24 | hachoir_parser.image.PngFile: images.PngStripper, | 24 | hachoir_parser.image.PngFile: images.PngStripper, |
| 25 | hachoir_parser.audio.MpegAudioFile: audio.MpegAudioStripper, | 25 | hachoir_parser.audio.MpegAudioFile: audio.MpegAudioStripper, |
| 26 | hachoir_parser.misc.PDFDocument: misc.PdfStripper, | 26 | hachoir_parser.misc.PDFDocument: misc.PdfStripper, |
| 27 | #hachoir_parser.archive.TarFile: archive.TarStripper, | 27 | hachoir_parser.archive.TarFile: archive.TarStripper, |
| 28 | } | 28 | } |
| 29 | 29 | ||
| 30 | def create_class_file(name, backup): | 30 | def is_secure(filename): |
| 31 | ''' | 31 | ''' |
| 32 | return a $FILETYPEStripper() class, | 32 | Prevent shell injection |
| 33 | corresponding to the filetype of the given file | ||
| 34 | ''' | 33 | ''' |
| 35 | if not(os.path.isfile(name)): #check if the file exist | 34 | if not(os.path.isfile(name)): #check if the file exist |
| 36 | print("Error: %s is not a valid file" % name) | 35 | print("Error: %s is not a valid file" % name) |
| 37 | sys.exit(1) | 36 | sys.exit(1) |
| 37 | filename.strip('\s') #separations | ||
| 38 | filename.strip('`') #injection `rm / -Rf` | ||
| 39 | filename.strip('\$(.*)')#injection $(rm / -Rf) | ||
| 40 | filename.strip(';')#injection $filename;rm / -Rf | ||
| 41 | |||
| 42 | def create_class_file(name, backup): | ||
| 43 | ''' | ||
| 44 | return a $FILETYPEStripper() class, | ||
| 45 | corresponding to the filetype of the given file | ||
| 46 | ''' | ||
| 47 | #is_secure(name) | ||
| 38 | 48 | ||
| 39 | filename = "" | 49 | filename = "" |
| 40 | realname = name | 50 | realname = name |
| 41 | filename = hachoir_core.cmd_line.unicodeFilename(name) | 51 | filename = hachoir_core.cmd_line.unicodeFilename(name) |
| 42 | parser = hachoir_parser.createParser(filename) | 52 | parser = hachoir_parser.createParser(filename) |
| 43 | if not parser: | 53 | if not parser: |
| 44 | print("Unable to parse the file %s : sorry" % filename) | 54 | print("Unable to parse the file %s with hachoir-parser." % filename) |
| 45 | sys.exit(1) | 55 | sys.exit(1) |
| 46 | 56 | ||
| 47 | editor = hachoir_editor.createEditor(parser) | 57 | editor = hachoir_editor.createEditor(parser) |
diff --git a/lib/parser.py b/lib/parser.py index d629619..12ef15a 100644 --- a/lib/parser.py +++ b/lib/parser.py | |||
| @@ -6,6 +6,7 @@ import hachoir_core.error | |||
| 6 | import hachoir_parser | 6 | import hachoir_parser |
| 7 | import hachoir_editor | 7 | import hachoir_editor |
| 8 | import sys | 8 | import sys |
| 9 | import os | ||
| 9 | import shutil | 10 | import shutil |
| 10 | 11 | ||
| 11 | POSTFIX = ".cleaned" | 12 | POSTFIX = ".cleaned" |
| @@ -25,6 +26,7 @@ class Generic_parser(): | |||
| 25 | #FIXME : not secure at all ! | 26 | #FIXME : not secure at all ! |
| 26 | try: | 27 | try: |
| 27 | shutil.rmtree(self.filename) | 28 | shutil.rmtree(self.filename) |
| 29 | #shutil.subprocess('shutil' , '--remove', 'self.filename') | ||
| 28 | except: | 30 | except: |
| 29 | print('Unable to remove %s' % self.filename) | 31 | print('Unable to remove %s' % self.filename) |
| 30 | 32 | ||
| @@ -47,7 +49,7 @@ class Generic_parser(): | |||
| 47 | hachoir_core.field.writeIntoFile(self.editor, self.filename + POSTFIX) | 49 | hachoir_core.field.writeIntoFile(self.editor, self.filename + POSTFIX) |
| 48 | if self.backup is False: | 50 | if self.backup is False: |
| 49 | self.secure_remove() #remove the old file | 51 | self.secure_remove() #remove the old file |
| 50 | shutil.rename(self.filename+ POSTFIX, self.filename)#rename the new | 52 | os.rename(self.filename+ POSTFIX, self.filename)#rename the new |
| 51 | 53 | ||
| 52 | def remove_all_ugly(self): | 54 | def remove_all_ugly(self): |
| 53 | ''' | 55 | ''' |
| @@ -57,7 +59,7 @@ class Generic_parser(): | |||
| 57 | In a perfect world, with nice fileformat, | 59 | In a perfect world, with nice fileformat, |
| 58 | this method does not exist. | 60 | this method does not exist. |
| 59 | ''' | 61 | ''' |
| 60 | raise NotImplementedError() | 62 | self.remove_all() |
| 61 | 63 | ||
| 62 | 64 | ||
| 63 | def _remove(self, field): | 65 | def _remove(self, field): |
