Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix jsonschema validation crash with unknown required properties (fixes #1243) #1245

Merged
merged 3 commits into from
May 31, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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