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

Adds json schema parsing check during body validation #214

Merged
merged 3 commits into from
Jun 27, 2019

Conversation

artem-zakharchenko
Copy link
Contributor

Performs given JSON Schema parsing during body validation to prevent processing of invalid or malformed JSON Schema. Note that this performs no actual validation either a JSON Schema is a valid JSON Schema.

GitHub

lib/units/validateBody.js Outdated Show resolved Hide resolved
@artem-zakharchenko
Copy link
Contributor Author

@kylef, could you please review it once more?

  • Adjusted the developer-friendly message to suggest to check the given JSON's validity (not the JSON Schema validity)
  • Error message now includes only the parsing error (error.message) instead of the entire stack

*/
const validateJsonSchema = (schema) => {
try {
JSON.parse(schema);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think at other places in Gavel we use json-parse-helpfulerror to provide even more helpful error messages than what JSON.parse() provides

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was looking for how the message body was done, didn't find it with JSON.parse.

src/mixins/validatable-http-message.coffee:jph = require 'json-parse-helpfulerror'
src/mixins/validatable-http-message.coffee:        jph.parse @body
src/mixins/validatable-http-message.coffee:        jph.parse @body
src/mixins/validatable-http-message.coffee:          parsed = jph.parse @expected.bodySchema
src/mixins/validatable-http-message.coffee:          jph.parse @expected.body
src/mixins/validatable-http-message.coffee:          jph.parse @expected.body

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While looking over the changes yesterday I've spotted that we already have a part of the body validation that attempts to parse a JSON Schema:

try {
jph.parse(bodySchema);
return [null, jsonSchemaType];
} catch (exception) {
const error = `Can't validate: expected body JSON Schema is not a parseable JSON:\n${exception.message}`;
return [error, null];
}

Which means that mentioned issue scenario somehow doesn't trigger this logic. I would need to investigate. Hopefully, we don't need any new functions/checks, just need to fix the old ones.

Copy link
Contributor Author

@artem-zakharchenko artem-zakharchenko Jun 26, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, it looks like with the current founding we must treat malformed JSON Schemas like an exception. Shall we change this to an exception then?

const error = `Can't validate: expected body JSON Schema is not a parseable JSON:\n${exception.message}`; 
  
return [error, null]; 

At the moment it's being tolerated as a validation error, not stopping the process, and ending up in the public results.

Copy link
Contributor Author

@artem-zakharchenko artem-zakharchenko Jun 26, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I confirm that changing that soft handling to throwing an exception produces the same behavior:

  • Stops Gavel validation to prevent operating on malformed data
  • Propagates user-friendly error + JSON parsing error from jph

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think exception is all right. If Gavel is given schema, the user's intention is to validate according to it. If Gavel cannot parse it, then it shouldn't be silent or just warning. There's no way for Gavel to continue in that case, otherwise it could lead to false positives in validation.

@artem-zakharchenko artem-zakharchenko merged commit 4012ccc into master Jun 27, 2019
@artem-zakharchenko artem-zakharchenko deleted the malformed-json-schema branch June 27, 2019 10:23
@ApiaryBot
Copy link
Collaborator

🎉 This PR is included in version 6.1.3 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

validateBody() throws in case it is provided with no schema
4 participants