expand .jars in the xpi and whitelist .css and .jar (bug 563389)

This commit is contained in:
Andy McKay 2011-04-15 12:32:58 -07:00
Родитель 7de61448e7
Коммит 0228a08265
4 изменённых файлов: 71 добавлений и 15 удалений

Двоичные данные
apps/files/fixtures/files/recurse.xpi Normal file

Двоичный файл не отображается.

Просмотреть файл

@ -50,11 +50,11 @@ class FileViewer:
Raises error on nasty files.
"""
try:
os.makedirs(self.dest)
except OSError:
return
os.makedirs(os.path.dirname(self.dest))
except OSError, err:
pass
try:
extract_xpi(self.src, self.dest)
extract_xpi(self.src, self.dest, expand=True)
except Exception, err:
task_log.error('Error (%s) extracting %s' % (err, self.src))
@ -71,7 +71,7 @@ class FileViewer:
"""Uses the filename to see if the file can be shown in HTML or not."""
if mimetype:
major, minor = mimetype.split('/')
if major == 'text' and minor in ['plain', 'html']:
if major == 'text' and minor in ['plain', 'html', 'css']:
return False
elif minor in ['xml', 'rdf+xml', 'javascript', 'x-javascript',
'xml-dtd', 'vnd.mozilla.xul+xml']:

Просмотреть файл

@ -12,23 +12,25 @@ import test_utils
from amo.urlresolvers import reverse
from files.helpers import FileViewer, DiffHelper
from files.utils import extract_zip
root = os.path.join(settings.ROOT, 'apps/files/fixtures/files')
dictionary = '%s/dictionary-test.xpi' % root
recurse = '%s/recurse.xpi' % root
class TestFileHelper(test_utils.TestCase):
def setUp(self):
dictionary = 'apps/files/fixtures/files/dictionary-test.xpi'
src = os.path.join(settings.ROOT, dictionary)
file_obj = Mock()
file_obj.id = file_obj.pk = 1
file_obj.file_path = src
self.viewer = FileViewer(file_obj)
file_obj.file_path = dictionary
self.old_tmp = settings.TMP_PATH
settings.TMP_PATH = tempfile.mkdtemp()
self.viewer = FileViewer(file_obj)
def tearDown(self):
self.viewer.cleanup()
settings.TMP_PATH = self.old_tmp
@ -40,6 +42,20 @@ class TestFileHelper(test_utils.TestCase):
self.viewer.extract()
eq_(self.viewer.is_extracted, True)
def test_recurse_extract(self):
self.viewer.src = recurse
self.viewer.extract()
eq_(self.viewer.is_extracted, True)
def test_recurse_contents(self):
self.viewer.src = recurse
self.viewer.extract()
files = self.viewer.get_files()
for name in ['chrome/test-root.txt', 'chrome/test.jar',
'chrome/test.jar/test/test.text']:
eq_(name in files, True, 'File %r not extracted' % name)
eq_(files['chrome/test.jar/test']['directory'], True)
def test_cleanup(self):
self.viewer.extract()
self.viewer.cleanup()

Просмотреть файл

@ -135,13 +135,53 @@ def parse_search(filename, addon=None):
'version': datetime.now().strftime('%Y%m%d')}
def extract_xpi(xpi, path):
zip = zipfile.ZipFile(xpi)
def extract_zip(source, remove=False):
"""Extracts the zip file. If remove is given, removes the source file."""
tempdir = tempfile.mkdtemp()
zip = zipfile.ZipFile(source)
for f in zip.namelist():
if '..' in f or f.startswith('/'):
log.error('Extraction error, Invalid archive: %s' % xpi)
log.error('Extraction error, Invalid archive: %s' % source)
raise forms.ValidationError(_('Invalid archive.'))
zip.extractall(path)
zip.extractall(tempdir)
if remove:
os.remove(source)
return tempdir
def copy_over(source, dest):
"""
Copies from the source to the destination, removing the destination
if it exists and is a directory.
"""
if os.path.exists(dest) and os.path.isdir(dest):
shutil.rmtree(dest)
shutil.copytree(source, dest)
shutil.rmtree(source)
def extract_xpi(xpi, path, expand=False):
"""
If expand is given, will look inside the expanded file
and find anything in the whitelist and try and expand it as well.
Currently only does one iteration of this.
It will replace the expanded file with a directory and the expanded
contents. If you have 'foo.jar', that contains 'some-image.jpg', then
it will create a folder, foo.jar, with an image inside.
"""
expand_whitelist = ['.jar']
tempdir = extract_zip(xpi)
if expand:
for root, dirs, files in os.walk(tempdir):
for name in files:
if os.path.splitext(name)[1] in expand_whitelist:
src = os.path.join(root, name)
dest = extract_zip(src, remove=True)
copy_over(dest, src)
copy_over(tempdir, path)
def parse_xpi(xpi, addon=None):