Skip to content

Commit

Permalink
Handle cookies with valid JSON of invalid type
Browse files Browse the repository at this point in the history
  • Loading branch information
knarewski committed Sep 12, 2022
1 parent 4e4599f commit 2314122
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/split/persistence/cookie_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ def delete_cookie_header!(header, key, value)
def hash
@hash ||= if cookies = @cookies[:split.to_s]
begin
JSON.parse(cookies)
parsed = JSON.parse(cookies)
parsed.is_a?(Hash) ? parsed : {}
rescue JSON::ParserError
{}
end
Expand Down
16 changes: 16 additions & 0 deletions spec/persistence/cookie_adapter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,22 @@
subject["my_key"] = "my_value"
expect(subject["my_key"]).to eq("my_value")
end

it "ignores valid JSON of invalid type (integer)" do
context.request.cookies["split"] = "2"

expect(subject["my_key"]).to be_nil
subject["my_key"] = "my_value"
expect(subject["my_key"]).to eq("my_value")
end

it "ignores valid JSON of invalid type (array)" do
context.request.cookies["split"] = "[\"foo\", \"bar\"]"

expect(subject["my_key"]).to be_nil
subject["my_key"] = "my_value"
expect(subject["my_key"]).to eq("my_value")
end
end

describe "#delete" do
Expand Down

0 comments on commit 2314122

Please sign in to comment.