summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/archive.py31
-rw-r--r--lib/mat.py22
-rw-r--r--lib/parser.py6
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 @@
1import tarfile
2import sys
1import parser 3import parser
4import mat
2 5
3class TarStripper(parser.Generic_parser): 6class 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
diff --git a/lib/mat.py b/lib/mat.py
index 6abcd64..156c683 100644
--- a/lib/mat.py
+++ b/lib/mat.py
@@ -14,7 +14,7 @@ import hachoir_editor
14import images 14import images
15import audio 15import audio
16import misc 16import misc
17#import archive 17import 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
30def create_class_file(name, backup): 30def 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
42def 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
6import hachoir_parser 6import hachoir_parser
7import hachoir_editor 7import hachoir_editor
8import sys 8import sys
9import os
9import shutil 10import shutil
10 11
11POSTFIX = ".cleaned" 12POSTFIX = ".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):