1
0
mirror of https://gitlab.com/fdroid/fdroidserver.git synced 2024-11-10 01:10:11 +01:00

Actually check that bools are valid

The metadata check was omitting booleans and lists, as it was only
taking strings. Fix this.
This commit is contained in:
Daniel Martí 2015-12-03 16:35:46 +01:00
parent 7b575e3fdd
commit 8c2e1fedfc

View File

@ -387,6 +387,8 @@ class FieldValidator():
self.matching = matching self.matching = matching
if type(matching) is str: if type(matching) is str:
self.compiled = re.compile(matching) self.compiled = re.compile(matching)
else:
self.matching = set(self.matching)
self.sep = sep self.sep = sep
self.fields = fields self.fields = fields
self.flags = flags self.flags = flags
@ -394,25 +396,23 @@ class FieldValidator():
def _assert_regex(self, values, appid): def _assert_regex(self, values, appid):
for v in values: for v in values:
if not self.compiled.match(v): if not self.compiled.match(v):
raise MetaDataException("'%s' is not a valid %s in %s. " raise MetaDataException("'%s' is not a valid %s in %s. Regex pattern: %s"
% (v, self.name, appid) + % (v, self.name, appid, self.matching))
"Regex pattern: %s" % (self.matching))
def _assert_list(self, values, appid): def _assert_list(self, values, appid):
for v in values: for v in values:
if v not in self.matching: if v not in self.matching:
raise MetaDataException("'%s' is not a valid %s in %s. " raise MetaDataException("'%s' is not a valid %s in %s. Possible values: %s"
% (v, self.name, appid) + % (v, self.name, appid, ', '.join(self.matching)))
"Possible values: %s" % (", ".join(self.matching)))
def check(self, v, appid): def check(self, v, appid):
if type(v) is not str or not v: if not v:
return return
if self.sep is not None: if type(v) == list:
values = v.split(self.sep) values = v
else: else:
values = [v] values = [v]
if type(self.matching) is list: if type(self.matching) is set:
self._assert_list(values, appid) self._assert_list(values, appid)
else: else:
self._assert_regex(values, appid) self._assert_regex(values, appid)
@ -444,12 +444,6 @@ valuetypes = {
["Litecoin"], ["Litecoin"],
[]), []),
FieldValidator("bool",
r'([Yy]es|[Nn]o|[Tt]rue|[Ff]alse)', None,
["Requires Root"],
['submodules', 'oldsdkloc', 'forceversion', 'forcevercode',
'novcheck']),
FieldValidator("Repo Type", FieldValidator("Repo Type",
['git', 'git-svn', 'svn', 'hg', 'bzr', 'srclib'], None, ['git', 'git-svn', 'svn', 'hg', 'bzr', 'srclib'], None,
["Repo Type"], ["Repo Type"],
@ -857,9 +851,9 @@ def post_metadata_parse(app):
build.__dict__[k] = re.sub(esc_newlines, '', v).lstrip().rstrip() build.__dict__[k] = re.sub(esc_newlines, '', v).lstrip().rstrip()
elif ftype == TYPE_BOOL: elif ftype == TYPE_BOOL:
# TODO handle this using <xsd:element type="xsd:boolean> in a schema # TODO handle this using <xsd:element type="xsd:boolean> in a schema
if isinstance(v, basestring) and v == 'true': if isinstance(v, basestring):
build.__dict__[k] = True build.__dict__[k] = _decode_bool(v)
elif ftype == TYPE_BOOL: elif ftype == TYPE_STRING:
if isinstance(v, bool) and v: if isinstance(v, bool) and v:
build.__dict__[k] = 'yes' build.__dict__[k] = 'yes'
@ -925,6 +919,18 @@ def _decode_dict(data):
return rv return rv
bool_true = re.compile(r'([Yy]es|[Tt]rue)')
bool_false = re.compile(r'([Nn]o|[Ff]alse)')
def _decode_bool(s):
if bool_true.match(s):
return True
if bool_false.match(s):
return False
raise MetaDataException("Invalid bool '%s'" % s)
def parse_metadata(metadatapath): def parse_metadata(metadatapath):
_, ext = common.get_extension(metadatapath) _, ext = common.get_extension(metadatapath)
accepted = common.config['accepted_formats'] accepted = common.config['accepted_formats']
@ -1038,8 +1044,7 @@ def parse_txt_metadata(metadatapath):
elif t == TYPE_STRING or t == TYPE_SCRIPT: elif t == TYPE_STRING or t == TYPE_SCRIPT:
build.set_flag(pk, pv) build.set_flag(pk, pv)
elif t == TYPE_BOOL: elif t == TYPE_BOOL:
if pv == 'yes': build.set_flag(pk, _decode_bool(pv))
build.set_flag(pk, True)
def parse_buildline(lines): def parse_buildline(lines):
v = "".join(lines) v = "".join(lines)