manifest: validate() fixes

We need to be a bit more careful with our types.

For example, the string "not-a-manifest" can be loaded as YAML, and
evaluates to... itself. And the string 'manifest' sure is in the
string 'not-a-manifest', so the "'manifest' not in data" check in
validate() doesn't catch the invalidity.

Then we try to index ['manifest'] in a string, which doesn't work,
because string indexes must be integers. Boom.

Fix it with extra type checking.

Signed-off-by: Martí Bolívar <marti.bolivar@nordicsemi.no>
This commit is contained in:
Martí Bolívar 2020-01-15 17:04:34 -08:00 committed by Marti Bolivar
parent 7822a4c315
commit cd55fcc255
1 changed files with 5 additions and 0 deletions

View File

@ -79,7 +79,12 @@ def validate(data):
:param data: YAML manifest data as a string or object :param data: YAML manifest data as a string or object
''' '''
if isinstance(data, str): if isinstance(data, str):
as_str = data
data = yaml.safe_load(data) data = yaml.safe_load(data)
if not isinstance(data, dict):
raise MalformedManifest(f'{as_str} is not a YAML dictionary')
elif not isinstance(data, dict):
raise TypeError(f'data type {type(data)} must be str or dict')
if 'manifest' not in data: if 'manifest' not in data:
raise MalformedManifest('manifest data contains no "manifest" key') raise MalformedManifest('manifest data contains no "manifest" key')