rack-cache icon indicating copy to clipboard operation
rack-cache copied to clipboard

invalid byte sequence in UTF-8 in Rack::Cache::Key

Open tc opened this issue 12 years ago • 8 comments

I'm seeing a lot of these errors in our logs:

cache error: invalid byte sequence in UTF-8
/data/apps/production/web/shared/bundle/ruby/1.9.1/gems/rack-cache-1.1/lib/rack/cache/key.rb:46:in `split'
/data/apps/production/web/shared/bundle/ruby/1.9.1/gems/rack-cache-1.1/lib/rack/cache/key.rb:46:in `block in query_string'
/data/apps/production/web/shared/bundle/ruby/1.9.1/gems/rack-cache-1.1/lib/rack/cache/key.rb:45:in `map'
/data/apps/production/web/shared/bundle/ruby/1.9.1/gems/rack-cache-1.1/lib/rack/cache/key.rb:45:in `query_string'
    def query_string
      return nil if @request.query_string.nil?

      @request.query_string.split(/[&;] */n).
46:        map { |p| unescape(p).split('=', 2) }.
        sort.
        map { |k,v| "#{escape(k)}=#{escape(v)}" }.
        join('&')
    end

tc avatar Jan 12 '12 01:01 tc

I'm getting the same errors on our logs on Heroku (Cedar stack).

anderslemke avatar Jan 27 '12 11:01 anderslemke

Removing the my entire bundle & cache fixed this for me.

barttenbrinke avatar Feb 24 '12 10:02 barttenbrinke

I'm seeing this as well. Looks like it's possibly for any request with URL encoded Latin-1 in it, e.g.

cache error: invalid byte sequence in UTF-8
…
cache: [GET /kalix?q=sn%F6slunga] pass

henrik avatar Jul 12 '12 11:07 henrik

Okay, I ended up doing this for now. Admittedly a kludge (and only for Ruby 1.9):

# Make Rack::Cache not break on Latin-1 query params.
# https://github.com/rtomayko/rack-cache/issues/47
class Rack::Cache::Key
  def unescape(x)
    super(x).encode("UTF-8", "ISO8859-1")
  end

  def escape(x)
    super(x.encode("ISO8859-1", "UTF-8")).encode("UTF-8", "ISO8859-1")
  end
end

In my case, I can rely on (I hope) query parameters to always be Latin-1 (ISO 8859-1), so this solution should work. Shouldn't be that hard to come up with a general solution, though – may give it a go.

henrik avatar Jul 12 '12 12:07 henrik

Anyone have a general solution for this problem? We're also seeing this issue.

joe1chen avatar Feb 04 '13 18:02 joe1chen

Am also seeing this in our unicorn logs. Very strange.

jamsi avatar Mar 05 '13 00:03 jamsi

See same error a lot in the unicorn log file.

jun1st avatar Apr 19 '14 07:04 jun1st

I feel like I've seen this, but not in a while. Have you tried adding the # encoding: utf-8 magic comment to the top of your config.ru or other relevant files?

xentek avatar Apr 21 '14 17:04 xentek