Skip to content

non utf-8 encoding issue with error handlers #1350

Closed
@eguzki

Description

Sinatra 2.0.0

When post request has a json body with non utf-8 encoding characters in keys, Sinatra middleware returns 400 with the following body:

Invalid query parameters: invalid byte sequence in UTF-8

Request body example:
{ "\xf0\x90\x28\xbc": 3 }

Our Sinatra app has been configured to enable raise_errors and disable show_exceptions

enable :raise_errors
disable :show_exceptions

We would like to customize error message, so we registered a BadRequest error handler:

error Sinatra::BadRequest do
  [400, {}, ['some error message']]               
end 

This error handling causes Sinatra app to raise exception internally. After a bit of debugging, crash happens @

@params[:captures] = captures

I guess "@params" is nil because due to encoding issue "@params" was not properly initialized @

force_encoding(@params = IndifferentHash[@request.params])

Calling "params" method of request object raises exception which is handled but "@params" is left uninitialized.

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions