Rack::UTF8Sanitizer is a Rack middleware which cleans up invalid UTF8 characters in request URI and headers.
Add this line to your application's Gemfile:
And then execute:
Or install it yourself as:
$ gem install rack-utf8_sanitizer
For Rails, add this to your
config.middleware.insert 0, Rack::UTF8Sanitizer
For Rack apps, add this to
Rack::UTF8Sanitizer divides all keys in the Rack environment in two distinct groups: keys which contain raw data and the ones with percent-encoded data. The fields which are treated as percent-encoded are:
The generic sanitization algorithm is as follows:
- Force the encoding to UTF-8.
- If the result contains invalid characters:
- Force the encoding to ASCII8-BIT.
- Re-encode it as UTF-8, replacing invalid and undefined characters as U+FFFD.
For fields with "raw data", the algorithm is applied once and the (UTF-8 encoded) result is left in the environment.
For fields with "percent-encoded data", the algorithm is applied twice to catch both invalid characters appearing as-is and invalid characters appearing in the percent encoding. The percent encoded, ASCII-8BIT encoded result is left in the environment.
Sanitizable content types
To add sanitizable content types to the list of defaults, pass the
additional_content_types options when using Rack::UTF8Sanitizer, e.g.
config.middleware.insert 0, Rack::UTF8Sanitizer, additional_content_types: ['application/vnd.api+json']
To explicitly set sanitizable content types and override the defaults, use the
config.middleware.insert 0, Rack::UTF8Sanitizer, sanitizable_content_types: ['application/vnd.api+json']
- Fork it
- Create your feature branch (
git checkout -b my-new-feature)
- Commit your changes (
git commit -am 'Add some feature')
- Push to the branch (
git push origin my-new-feature)
- Create new Pull Request
To run the tests, run
rake spec in the project directory.