Skip to content

Commit

Permalink
Merge pull request #1245 from Kinto/1243-json-validation-crash
Browse files Browse the repository at this point in the history
Fix jsonschema validation crash with unknown required properties (fixes #1243)
  • Loading branch information
leplatrem authored May 31, 2017
2 parents 121e4df + 42b5cb9 commit 5e2a13b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ This document describes changes between each past release.
- Fix removal of timestamps when parent object is deleted (fixes #1233)
- Do not allow to reuse deletion tokens (fixes #1171)
- ``accounts`` plugin: fix exception on authentication. (#1224)
- Fix crash with JSONSchema validation of unknown required properties (fixes #1243)


7.0.1 (2017-05-17)
Expand Down
8 changes: 4 additions & 4 deletions kinto/views/records.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ def process_record(self, new, old=None):
stripped.pop(self.schema_field, None)
jsonschema.validate(stripped, schema)
except jsonschema_exceptions.ValidationError as e:
try:
field = e.path.pop() if e.path else e.validator_value.pop()
except AttributeError:
field = None
if e.validator_value:
field = e.validator_value[-1]
else:
field = e.schema_path[-1]
raise_invalid(self.request, name=field, description=e.message)

new[self.schema_field] = collection_timestamp
Expand Down
19 changes: 19 additions & 0 deletions tests/test_views_collections_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,25 @@ def test_schema_should_be_json_schema(self):
error_msg = "'Washmachine' is not valid under any of the given schemas"
self.assertIn(error_msg, resp.json['message'])

def test_extra_unknown_required_property(self):
schema = {**SCHEMA, "required": ["unknown"]}
self.app.put_json(COLLECTION_URL,
{'data': {'schema': schema}},
headers=self.headers)

record = {'title': 'bug 1243'}
r = self.app.post_json(RECORDS_URL,
{'data': record},
headers=self.headers,
status=400)
self.assertEqual("'unknown' is a required property", r.json["message"])

# With bug Kinto/kinto#1243, the second call would crash.
self.app.post_json(RECORDS_URL,
{'data': record},
headers=self.headers,
status=400)


class RecordsValidationTest(BaseWebTestWithSchema, unittest.TestCase):
def setUp(self):
Expand Down

0 comments on commit 5e2a13b

Please sign in to comment.