Skip to content

Commit

Permalink
Handle non-ASCII-only in AS::MessagePack#signature?
Browse files Browse the repository at this point in the history
Because `ActiveSupport::MessagePack::Serializer::SIGNATURE` includes a
non-ASCII-only byte (`"\xCC"`), it raises `Encoding::CompatibilityError`
when compared with another string that is not encoded with
`Encoding::BINARY` and also includes a non-ASCII-only byte.

To prevent that, this commit changes `AS::MessagePack#signature?` to
directly compare the first two bytes of both strings.

Fixes #48196.
  • Loading branch information
jonathanhefner committed May 12, 2023
1 parent d954155 commit 76ea586
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
Expand Up @@ -25,7 +25,7 @@ def load(dumped)
end

def signature?(dumped)
dumped.start_with?(SIGNATURE)
dumped.getbyte(0) == SIGNATURE.getbyte(0) && dumped.getbyte(1) == SIGNATURE.getbyte(1)
end

def message_pack_factory
Expand Down
5 changes: 5 additions & 0 deletions activesupport/test/message_pack/shared_serializer_tests.rb
Expand Up @@ -41,6 +41,11 @@ module MessagePackSharedSerializerTests
assert_not serializer.signature?("{}")
end

test "#signature? handles non-ASCII-only non-binary-encoded strings" do
assert serializer.signature?(dump("ümlaut").force_encoding(Encoding::UTF_8))
assert_not serializer.signature?("ümlaut")
end

test "roundtrips Symbol" do
assert_roundtrip :some_symbol
end
Expand Down

0 comments on commit 76ea586

Please sign in to comment.