Skip to content

Commit 8737958

Browse files
committed
Check that tarball is valid, and show error message if not
1 parent 9c17c23 commit 8737958

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

cesium_app/handlers/dataset.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from os.path import join as pjoin
1212
import uuid
1313
import base64
14+
import tarfile
1415

1516

1617
class DatasetHandler(BaseHandler):
@@ -21,6 +22,11 @@ def post(self):
2122
return self.error('No tar file uploaded')
2223

2324
zipfile = data['tarFile']
25+
tarball_content_type_str = 'data:application/gzip;base64,'
26+
27+
if not zipfile['body'].startswith(tarball_content_type_str):
28+
return self.error('Invalid tar file - please ensure file is gzip '
29+
'format.')
2430

2531
if zipfile['name'] == '':
2632
return self.error('Empty tar file uploaded')
@@ -34,7 +40,13 @@ def post(self):
3440

3541
with open(zipfile_path, 'wb') as f:
3642
f.write(base64.b64decode(
37-
zipfile['body'].replace('data:application/gzip;base64,', '')))
43+
zipfile['body'].replace(tarball_content_type_str, '')))
44+
try:
45+
tarfile.open(zipfile_path)
46+
except tarfile.ReadError:
47+
os.remove(zipfile_path)
48+
return self.error('Invalid tar file - please ensure file is gzip '
49+
'format.')
3850

3951
# Header file is optional for unlabled data w/o metafeatures
4052
if 'headerFile' in data:

0 commit comments

Comments
 (0)