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

Validate header parameters #122

Merged
merged 2 commits into from
Mar 27, 2018

Conversation

tzmfreedom
Copy link
Contributor

@tzmfreedom tzmfreedom commented Nov 7, 2017

On current version of committee, parameters in header is not validated when parameter is defined in header on OpenAPI 2.0 format.

So the following OpenAPI description is not working because current version does't extract and validate header values.

...
properties:
  name:
    type: string
    in: header
...

This pull request solve them.

  • If allow_header_params is enabled, header values is extracted as parameter.
  • On OpenAPI driver, header parameter is uppercased because we treat header key as CGI header values, that is uppercased and converted hyphen to underscore.

Copy link
Member

@brandur brandur left a comment

Choose a reason for hiding this comment

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

Hi Makoto,

Sorry about the very delayed review here :/ and thank you for the work.

I left a couple comments below.

@@ -133,6 +133,9 @@ def call
param_schema.items = param_data["items"]
end

# if parameter is defined in header, uppercase name parameter to recognize CGI header.
param_data["name"].upcase! if param_data["in"] == "header"
Copy link
Member

Choose a reason for hiding this comment

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

Can we just perform the upcase when doing the validation? I think having all the logic in one place is just going to make everything in a little more readable.

@coerce_form_params = options[:coerce_form_params]
@optimistic_json = options[:optimistic_json]
@schema = options[:schema]
@allow_form_params = options[:allow_form_params]
Copy link
Member

Choose a reason for hiding this comment

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

What do you think about having allow_header_params on by default? As you noticed, it seems to be part of the spec, and the reason that allow_query_params and allow_form_params exist is that they're doing something that's a little abnormal from the standard Committee conventions. Header parameters are almost entirely orthogonal to other types of parameters, and I suspect it's fine just to always allow them.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for your pointing it out.
I think allow_header_params should be true by default too.
In the same way as other parameter, I'll set default parameter on RequestValidation's constructor.

headers[headerized_key] = env[key]
headers
end
end
Copy link
Member

Choose a reason for hiding this comment

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

I'm a tad bit concerned about the potential for different types of parameters to start colliding. For example, you could have a param called Encoding that appeared as both a header and as a form parameter, and Committee would have a hard time differentiating between the two.

What do you think about separating headers out into separate buckets? Maybe the best solution would be if RequestUnpacker returned a [headers, params] where each element could be validated separately, but a cheaper solution might be to just have a special __committee_headers key in the params hash for now that's extracted during validation. Thoughts?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As you pointed out, I think header and parameter are orthogonal too.
In current PR implementation, collision is occured and this is not good for us.
So separating headers out into separate buckets is good idea.

I think the best solution is separeting params and headers on RequestUnpacker#call too.

The following is idea for implementation.

  • Committee::Drivers::OpenAPI2::Link has new header_schema attribute that is JsonSchema::Schema Object to validate header.
    • Committee::Drivers::HyperSchema does't have header_schema. This attribute only be in OpenAPI.
  • RequestUnpacker returns array, params and headers.
  • RequestValidator validates params and headers separately. params are validated with Link#schema, headers are validated with Link#header_schema.

If the above solution looks good, I'll implement it.

Copy link
Member

Choose a reason for hiding this comment

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

Thanks for thinking it through so thoroughly! Yes, that sounds good to me.

@tzmfreedom tzmfreedom force-pushed the validate_header_parameters branch 3 times, most recently from 09f9e50 to 7231af9 Compare March 15, 2018 14:04
@tzmfreedom tzmfreedom force-pushed the validate_header_parameters branch from 7231af9 to 719db39 Compare March 15, 2018 14:07
@tzmfreedom
Copy link
Contributor Author

I revised.
2eeec41

And, I fixed some test because of redundant variable assignment.
719db39

Please review it and let me know if there are any comments.

@brandur
Copy link
Member

brandur commented Mar 27, 2018

Thank you for the significant chunk of work here @tzmfreedom! Once again, apologies for the delay in getting back to you on it — if it happens again in the future, feel free to bump the thread to get it back to the top of my inbox.

This looks good me!

@brandur brandur merged commit c21294a into interagent:master Mar 27, 2018
@brandur
Copy link
Member

brandur commented Mar 27, 2018

Released as 2.1.0.

@tzmfreedom tzmfreedom deleted the validate_header_parameters branch August 5, 2018 03:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants