Skip to content

Commit cebdf71

Browse files
committed
Fix decoding data encoded using a non-String purpose
Data encoded using a non-String purpose and `use_message_serializer_for_metadata == false` was incorrectly decoded, triggering a "mismatched purpose" error during decode. Fix this by ensuring that we compare both sides as a String.
1 parent c139e52 commit cebdf71

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

activesupport/lib/active_support/messages/metadata.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def extract_from_metadata_envelope(envelope, purpose: nil)
8282
throw :invalid_message_content, "expired"
8383
end
8484

85-
if hash["pur"] != purpose&.to_s
85+
if hash["pur"].to_s != purpose.to_s
8686
throw :invalid_message_content, "mismatched purpose"
8787
end
8888

activesupport/test/messages/message_metadata_tests.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,13 @@ module MessageMetadataTests
9292
assert_roundtrip "a string", codec, { purpose: "x", expires_in: 1.year }, { purpose: "x" }
9393
end
9494

95+
test "messages with non-string purpose are readable" do
96+
each_scenario do |data, codec|
97+
message = encode(data, codec, purpose: [ "x", 1 ])
98+
assert_equal data, decode(message, codec, purpose: [ "x", 1 ])
99+
end
100+
end
101+
95102
test "messages are readable regardless of use_message_serializer_for_metadata" do
96103
each_scenario do |data, codec|
97104
message = encode(data, codec, purpose: "x")

0 commit comments

Comments
 (0)