Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix decoding data encoded using a non-String purpose #49669

Merged
merged 1 commit into from Nov 1, 2023

Conversation

intrip
Copy link
Contributor

@intrip intrip commented Oct 17, 2023

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.

Checklist

Before submitting the PR make sure the following are checked:

  • This Pull Request is related to one change. Changes that are unrelated should be opened in separate PRs.
  • Commit message has a detailed description of what changed and why. If this PR fixes a related issue include it in the commit message. Ex: [Fix #issue-number]
  • Tests are added or updated if you fix a bug or add a feature.
  • CHANGELOG files are updated for the changed libraries if there is a behavior change or additional feature. Minor bug fixes and documentation changes should not be included.

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.
@intrip intrip force-pushed the fix-message-metadata-non-str branch from 788e669 to cebdf71 Compare October 17, 2023 08:02
@@ -82,7 +82,7 @@ def extract_from_metadata_envelope(envelope, purpose: nil)
throw :invalid_message_content, "expired"
end

if hash["pur"] != purpose&.to_s
if hash["pur"].to_s != purpose.to_s
Copy link
Member

@p8 p8 Oct 17, 2023

Choose a reason for hiding this comment

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

Wouldn't hash["pur"].to_s result in matching purpose for "1" and 1?

Copy link
Contributor Author

@intrip intrip Oct 18, 2023

Choose a reason for hiding this comment

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

Correct, the new serialization already transforms the purpose in a String so hash["pur"] is always a String if serialized with the new logic.
The old serialization, on the other hand doesn't do it and this introduces a mismatch.
Isn't fine to always compare purpose as a String? We are already doing it after all.

Copy link
Member

@p8 p8 Oct 18, 2023

Choose a reason for hiding this comment

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

It seems like the old comparison implementation was also calling to_s:
https://github.com/rails/rails/blame/ebc3b660e51c5bdfd639639bb3c605d522d1a728/activesupport/lib/active_support/messages/metadata.rb#L67

So this looks good to me.

@byroot byroot merged commit 9ade3f9 into rails:main Nov 1, 2023
4 checks passed
@zzak
Copy link
Member

zzak commented Nov 3, 2023

Should this be backported?

byroot added a commit that referenced this pull request Nov 4, 2023
Fix decoding data encoded using a non-String purpose
@byroot
Copy link
Member

byroot commented Nov 4, 2023

Backported as 02e3f69

@intrip intrip deleted the fix-message-metadata-non-str branch November 30, 2023 10:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants