Force request encodings to be UTF-8 instead of ASCII-8BIT after a reflex #320
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Type of PR (feature, enhancement, bug fix, etc.)
Bug Fix
Description
This was a really hard one! Spent the last few hours debugging where that ASCII-8BIT came from.
Calling
Rack::MockRequest.env_for()
inlib/stimulus_reflex/reflex.rb
returns for some reason a Hash with some ASCII-8BIT strings. Specifically those are the strings which are encoded as ASCII-8BIT:This happens because
Rack::MockRequest.env_for()
encodes it's input asEncoding::BINARY
which results in ASCII-8BIT. See below for reference in rack/rack:https://github.com/rack/rack/blob/4db9d299e43dbf856664359b16e2e461c378ee1c/lib/rack/mock.rb#L155-L158
With this hash we are building a new
ActionDispatch::Request
which we then pass intoRails.application.routes.recognize_path_with_request()
.This then returns the following Hash which also contains ASCII-8BIT strings as a result:
Not quite sure if this also impacts this issue, but they are again converted to ASCII-8BIT in
RouteSet#recognize_path_with_request
. Seerails/rails
for reference:https://github.com/rails/rails/blob/60ef07093d9023568ccbbd34e862b40395ec02f4/actionpack/lib/action_dispatch/routing/route_set.rb#L866-L868.
The strings in this
@url_params
hash get passed around the whole Rails stack and finally end up ascontroller.action_name
andaction_name
in the views. This is why @asgerb encountered theCan not transliterate strings with ASCII-8BIT encoding
error when callingaction_name.parameterize
.This commit converts the values of
:action
and:controller
in@url_params
back to UTF-8 strings as they should be (this is the case if you render the view normally).I didn't find another way in the
Rack::MockRequest.env_for()
orRails.application.routes.recognize_path_with_request()
call to enforce those strings to be UTF-8 even earlier.This PR also untangles the
StimulusReflex::Reflex#request
method a little bit, which should make it easier to follow and easier to understand. There is a whole lot going on the env merging.Finally Resolves #202.
/cc @asgerb could you check if this resolves your issue? Thank you!
Why should this be added
Fixes the edge case in #202 where the
action_name
was converted to a ASCII-8BIT string.Checklist