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

refs #53 handle invalid content-type headers #55

Merged
merged 7 commits into from
May 28, 2020

Conversation

gingerlime
Copy link
Collaborator

  • added a test to simulate the issue
  • unfortunately, not entirely sure how to catch this exception and
    handle it yet ... so this test currently fails

* added a test to simulate the issue
* unfortunately, not entirely sure how to catch this exception and
  handle it yet ...
@yuki24
Copy link
Owner

yuki24 commented May 27, 2020

You could probably sanitize the Content-Type header by doing:

begin
  request.content_type
rescue Mime::Type::InvalidMimeType
  request.env["MALFORMED_HTTP_CONTENT_TYPE"], request.env["HTTP_CONTENT_TYPET"] = request.env["HTTP_CONTENT_TYPE"], "*/*"
end

And you can probably add it to either before or after where the Accept header is sannitized:

begin
request.accepts
rescue Mime::Type::InvalidMimeType
request.env["MALFORMED_HTTP_ACCEPT"], request.env["HTTP_ACCEPT"] = request.env["HTTP_ACCEPT"], "*/*"
end

@gingerlime
Copy link
Collaborator Author

gingerlime commented May 27, 2020

Thank @yuki24 ! I tried something similar. The problem I see is that request.POST then fails, even if I update the request content_type beforehand... I'll push another commit that fixes it, but I'm not entirely sure if it's the best solution still (at least you can hopefully see what I mean)

Update: see 53ea75e although I'm a bit confused by the travis results... tests are passing for me locally

Update2: Ah, I think I see how tests are run now, so hopefully can fix the response codes for each rails version

Co-authored-by: Yuki Nishijima <yk.nishijima@gmail.com>
@@ -59,12 +59,16 @@ def process_action(*)
request.GET
rescue ActionController::BadRequest
request.env["MALFORMED_QUERY_STRING"], request.env["QUERY_STRING"] = request.env["QUERY_STRING"], ""
rescue Mime::Type::InvalidMimeType
request.env["MALFORMED_CONTENT_TYPE"], request.env["CONTENT_TYPE"] = request.env["CONTENT_TYPE"], "text/plain"
Copy link
Owner

@yuki24 yuki24 May 27, 2020

Choose a reason for hiding this comment

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

Each one of the elements should be sanitized independently to cover a case where both the query string and the content type header are malformed. You could probably do this at the beginning of this method:

def process_action(*)
  begin
    request.content_mime_type
  rescue Mime::Type::InvalidMimeType
    request.env["MALFORMED_CONTENT_TYPE"], request.env["CONTENT_TYPE"] = request.env["CONTENT_TYPE"], "*/*"
  end

  ...

I picked up HTTP_CONTENT_TYPET in my previous comment, which was wrong, and you seem to have figured it out correctly, so the code above should work now.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

cool. this seems to work! I'll update it. Thanks @yuki24

end

begin
request.POST
rescue ActionController::BadRequest
request.env["MALFORMED_BODY"], request.env["rack.input"] = request.env["rack.input"], StringIO.new
rescue Mime::Type::InvalidMimeType
request.env["MALFORMED_CONTENT_TYPE"], request.env["CONTENT_TYPE"] = request.env["CONTENT_TYPE"], "text/plain"
Copy link
Owner

Choose a reason for hiding this comment

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


assert_equal 406, response.status
assert_equal "The requested content type is not acceptable.\n", response.body
end
Copy link
Owner

Choose a reason for hiding this comment

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

@@ -67,12 +93,12 @@ def without_layouts
`mv error.html.erb test/fake_app/app/views/layouts/`
end

def get(path)
def get(path, headers: {})
Copy link
Owner

@yuki24 yuki24 May 27, 2020

Choose a reason for hiding this comment

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

I think this is a good change, but it would be great if you could send a separate pull request for that.

def get(path, params: {}, headers: {})
  ...
end

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

That's a good idea. Although I do need it in order to have more flexibility testing with GET since you overwrote it. I'll create a separate PR, and then see if I can rebase this PR off the other one.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

see #56 (merged onto this PR)

@gingerlime gingerlime changed the title refs #53 test with invalid content-type header refs #53 handle invalid content-type headers May 28, 2020
@yuki24
Copy link
Owner

yuki24 commented May 28, 2020

The test failure seems to be caused by the extra test which I think we can remove. I'll just go ahead nad merge since this pull request looks good as it is.

@yuki24 yuki24 merged commit a625fa3 into yuki24:master May 28, 2020
@gingerlime
Copy link
Collaborator Author

That's great. Thanks so much @yuki24 and for your patience helping me work through this.

@gingerlime gingerlime deleted the mime_types branch May 28, 2020 14:42
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