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

strip_string trims by number of chars instead of bytes #2634

Closed
sentrivana opened this issue Jan 11, 2024 · 0 comments · Fixed by #2657
Closed

strip_string trims by number of chars instead of bytes #2634

sentrivana opened this issue Jan 11, 2024 · 0 comments · Fixed by #2657
Labels
Type: Bug Something isn't working

Comments

@sentrivana
Copy link
Contributor

sentrivana commented Jan 11, 2024

How do you use Sentry?

Sentry Saas (sentry.io)

Version

1.39.2

Issue

The strip_string function isn't working properly.

Here we calculate the size of the string in bytes as length. But then when we actually determine that the string needs trimming, we trim length characters from the string instead of length bytes. We also then potentially report the wrong number in the metadata.

from sentry_sdk.utils import strip_string

strip_string("éê", 2)  # == AnnotatedValue(value="éê", ...)

Both é and ê are two-byte large, making the string "éê" 4 bytes long. Yet strip_string will not strip it to two bytes.

  1. It'll get encoded into bytes here.
  2. The size of the encoded version is 4, so length will be set to 4.
  3. This check will be True, because 4 > 2.
  4. But when we actually try to trim here, we're trimming the string "éê" to two (characters/code points), as opposed to the encoded bytes representation.

Solution

Probably something to the effect of

string.encode("utf-8")[: max_bytes - 3].decode("utf-8", errors="ignore")

The [: max_bytes - 3] part might end up cutting a code point in two; .decode with errors="ignore" will ignore any malformed codepoints.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant