Skip to content

Commit

Permalink
FIX: displaying wrong avatar and letter avatar
Browse files Browse the repository at this point in the history
correct regression where params and env is reused in production
  • Loading branch information
SamSaffron committed Nov 27, 2017
1 parent 5a715b7 commit ca7af7b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
20 changes: 18 additions & 2 deletions lib/hijack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,26 @@ module Hijack

def hijack(&blk)
controller_class = self.class
request = self.request

#env = request.env.dup
#request_copy = ActionDispatch::Request.new(env)
request_copy = self.request

if hijack = request.env['rack.hijack']
io = hijack.call

# in prd the env object is re-used
# make a copy of all strings
env_copy = {}
request.env.each do |k, v|
env_copy[k] = v if String === v
end

request_copy = ActionDispatch::Request.new(env_copy)

# params is generated per request so we can simply reuse it
params_copy = params

Scheduler::Defer.later("hijack work") do

begin
Expand All @@ -24,7 +39,8 @@ def hijack(&blk)
instance = controller_class.new
response = ActionDispatch::Response.new
instance.response = response
instance.request = request
instance.request = request_copy
instance.params = params_copy

begin
instance.instance_eval(&blk)
Expand Down
18 changes: 16 additions & 2 deletions spec/components/hijack_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ class Hijack::Tester < ApplicationController

def initialize
@io = StringIO.new
self.request = ActionController::TestRequest.new(
{ "rack.hijack" => lambda { @io } },
self.request = ActionController::TestRequest.new({
"rack.hijack" => lambda { @io },
"rack.input" => StringIO.new
},
nil,
nil
)
Expand All @@ -27,6 +29,18 @@ def hijack_test(&blk)
Hijack::Tester.new
end

it "dupes the request params and env" do
orig_req = tester.request
copy_req = nil

tester.hijack_test do
copy_req = request
render body: "hello world", status: 200
end

expect(copy_req.object_id).not_to eq(orig_req.object_id)
end

it "handles expires_in" do
tester.hijack_test do
expires_in 1.year
Expand Down

1 comment on commit ca7af7b

@discoursebot
Copy link

Choose a reason for hiding this comment

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

This commit has been mentioned on Discourse Meta. There might be relevant details there:

https://meta.discourse.org/t/wrong-account-avatar-after-upgrade/74886/12

Please sign in to comment.