This repository has been archived by the owner on Mar 15, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 235
/
validation.py
276 lines (253 loc) · 10.5 KB
/
validation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import imghdr
import json
from cStringIO import StringIO
from zipfile import BadZipfile
from django.forms import ValidationError
from django.utils.encoding import smart_unicode
from PIL import Image
from django.utils.translation import ugettext as _
from mkt.api.exceptions import ParseError
from mkt.files.utils import SafeUnzip
from mkt.site.utils import strip_bom
class ExtensionValidator(object):
"""
Firefox OS Add-on validator. If validation fails, will raise an instance of
rest_framework.exceptions.ParseError containing information about the
error.
"""
errors = {
'AUTHOR_NOT_STRING': _(u'The `author` property must be a string.'),
'AUTHOR_TOO_LONG': _(
u'The `author` property cannot be longer than 128 characters.'),
'AUTHOR_TOO_SHORT': _(
u'The `author` property must be at least 1 character'
u' long and can not consist of only whitespace characters.'),
'BAD_CONTENT_TYPE': _(
u'The file sent has an unsupported content-type'),
'DESCRIPTION_NOT_STRING': _(
u'The `description` property must be a string.'),
'DESCRIPTION_TOO_LONG': _(
u'The `description` property cannot be '
u'longer than 132 characters.'),
'ICON_INCORRECT_DIMENSIONS': _(
u'The icon file `%(icon_path)s` is not the specified dimensions '
u'of %(icon_size)s x %(icon_size)s as defined in the manifest.'),
'ICON_DOES_NOT_EXIST': _(
u'The icon file `%(icon_path)s` is referenced in the manifest but'
u' does not exist in the ZIP file.'),
'ICONS_NO_128': _(
u'If defining `icons`, you must include a 128x128 variant.'),
'ICON_NOT_A_VALID_IMAGE_OR_PNG': _(
u'The icon file `%s` is not a valid PNG.'),
'ICON_NOT_SQUARE': _(
u'The icon file `%(icon_path)s` is not square.'),
'ICON_INVALID_SIZE': _(
u'The manifest contains an invalid icon size: %(icon_size)s'),
'INVALID_JSON': _(
u"'manifest.json' in the archive is not a valid JSON"
u" file."),
'INVALID_JSON_ENCODING': _(
u"'manifest.json' in the archive is not encoded in UTF-8."),
'INVALID_ZIP': _(u'The file sent is not a valid ZIP file.'),
'NAME_MISSING': _(u'There is no `name` property in the manifest.'),
'NAME_NOT_STRING': _(u'The `name` property must be a string.'),
'NAME_TOO_LONG': _(
u'The `name` property cannot be longer than 45 '
u'characters.'),
'NAME_TOO_SHORT': _(
u'The `name` property must be at least 1 character'
u' long and can not consist of only whitespace characters.'),
'NO_MANIFEST': _(
u"The archive does not contain a 'manifest.json' "
u"file."),
'VERSION_MISSING': _(
u'There is no `version` property in the manifest.'),
'VERSION_NOT_STRING': _(u'The `version` property must be a string.'),
'VERSION_INVALID': _(
u'The `version` property must be a string'
u' containing one to four dot-separated integers each between'
u' 0 and 65535.'),
}
valid_content_types = (
'application/octet-stream',
'application/zip',
)
def __init__(self, file_obj=None):
self.file_obj = file_obj
self.zipfile = None
def error(self, key, **kwargs):
message = self.errors[key]
if kwargs:
message = self.errors[key] % kwargs
raise ParseError(detail={
'key': key,
'message': message,
'params': kwargs,
})
def validate(self):
"""
Run the full validation suite against the uploaded file:
* Ensure that it is a valid zip file.
* Ensure that it contains a valid manifest.json file.
* Validate the manifest fields against the spec.
Return the manifest contents (as dict).
"""
self.manifest = self.validate_file(self.file_obj)
self.data = self.validate_json(self.manifest)
self.validate_name(self.data)
self.validate_description(self.data)
self.validate_version(self.data)
self.validate_author(self.data)
self.validate_icons(self.data)
return self.data
def validate_file(self, file_obj):
"""
Verify that the upload is a valid zip file that contains a
manifest.json file.
"""
if file_obj.content_type not in self.valid_content_types:
self.error('BAD_CONTENT_TYPE')
try:
self.zipfile = SafeUnzip(file_obj)
try:
# Will throw ValidationError if necessary.
self.zipfile.is_valid()
except ValidationError as e:
raise ParseError(unicode(e))
except (BadZipfile, IOError):
self.error('INVALID_ZIP')
manifest = self.zipfile.extract_path('manifest.json')
except KeyError:
self.error('NO_MANIFEST')
return manifest
def validate_json(self, contents):
"""
Verify that the enclosed manifest.json is a valid and parsable JSON
file.
"""
try:
# We support only UTF-8 encoded manifests.
decoded_data = smart_unicode(strip_bom(contents))
except UnicodeDecodeError:
self.error('INVALID_JSON_ENCODING')
try:
return json.loads(decoded_data)
except ValueError:
self.error('INVALID_JSON')
def validate_name(self, manifest_json):
"""
Ensure that the name property of the manifest exists and is a string
between 1 and 45 characters long.
In addition, even though it's allowed in the spec, we consider the name
invalid (too short) if it contains only whitespace characters.
https://developer.chrome.com/extensions/manifest/name
"""
try:
name = manifest_json['name']
except KeyError:
self.error('NAME_MISSING')
if not isinstance(name, basestring):
self.error('NAME_NOT_STRING')
if len(name.strip()) < 1:
self.error('NAME_TOO_SHORT')
if len(name) > 45:
self.error('NAME_TOO_LONG')
def validate_description(self, manifest_json):
"""
Ensures that, if present, the description property is no longer than
132 characters.
https://developer.chrome.com/extensions/manifest/description
"""
if 'description' in manifest_json:
description = manifest_json['description']
if not isinstance(description, basestring):
self.error('DESCRIPTION_NOT_STRING')
if len(description.strip()) > 132:
self.error('DESCRIPTION_TOO_LONG')
def validate_version(self, manifest_json):
"""
Ensure that the version property of the manifest exists and is a string
containing one to four dot-separated integers, each between 0 and
65535, with no leading zeros."""
try:
version = manifest_json['version']
except KeyError:
self.error('VERSION_MISSING')
if not isinstance(version, basestring):
self.error('VERSION_NOT_STRING')
splitted = version.split('.')
if len(splitted) > 4:
# Too many dots.
self.error('VERSION_INVALID')
for version_component in splitted:
try:
number = int(version_component)
if version_component.startswith('0') and number != 0:
# Leading zeros are forbidden.
self.error('VERSION_INVALID')
if number < 0 or number > 65535:
# All numbers must be between 0 and 65535 inclusive.
self.error('VERSION_INVALID')
except ValueError:
# Not a valid number.
self.error('VERSION_INVALID')
def validate_author(self, manifest_json):
"""
Ensures that, if present, the author property is no longer than
128 characters.
"""
if 'author' in manifest_json:
author = manifest_json['author']
# Author must not be empty/only whitespace if present, since we'll
# use it as link text.
if not isinstance(author, basestring):
self.error('AUTHOR_NOT_STRING')
if len(author.strip()) < 1:
self.error('AUTHOR_TOO_SHORT')
if len(author) > 128:
self.error('AUTHOR_TOO_LONG')
def validate_icons(self, manifest_json):
"""
Validate the `icons` property in the manifest:
* Ensure that, if the icons property is present, a 128px icon is
provided.
* Ensure that each icon size is a valid integer.
* Ensure that each icon file is valid by calling _validate_icon_file().
"""
icons = manifest_json.get('icons', {})
if icons:
if '128' not in icons:
self.error('ICONS_NO_128')
for icon_size, icon_path in icons.iteritems():
try:
icon_size = int(icon_size)
if icon_size <= 0:
raise ValueError
except ValueError:
self.error(
'ICON_INVALID_SIZE', icon_size=unicode(icon_size))
self._validate_icon_file(icon_path, icon_size)
def _validate_icon_file(self, icon_path, icon_size):
"""
Validate a specific icon path referenced in the manifest:
* Ensure that the file exists in the zip.
* Ensure that it is a valid PNG file.
* Ensure that it is square.
* Ensure that it has the claimed dimensions.
"""
try:
icon_contents = self.zipfile.extract_path(icon_path.lstrip('/'))
except KeyError:
self.error('ICON_DOES_NOT_EXIST', icon_path=icon_path)
try:
if imghdr.what(None, icon_contents) != 'png':
raise IOError
image = Image.open(StringIO(icon_contents))
except IOError:
self.error('ICON_NOT_A_VALID_IMAGE_OR_PNG',
icon_path=icon_path)
if image.size[0] != image.size[1]:
self.error('ICON_NOT_SQUARE', icon_path=icon_path)
if icon_size != image.size[0]:
self.error('ICON_INCORRECT_DIMENSIONS',
icon_path=icon_path, icon_size=icon_size)