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(utils): strip_string() checks text length counting bytes not chars #1711

Merged
merged 1 commit into from
Oct 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion sentry_sdk/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -841,7 +841,7 @@ def strip_string(value, max_length=None):
# This is intentionally not just the default such that one can patch `MAX_STRING_LENGTH` and affect `strip_string`.
max_length = MAX_STRING_LENGTH

length = len(value)
length = len(value.encode("utf-8"))

if length > max_length:
return AnnotatedValue(
Expand Down
21 changes: 21 additions & 0 deletions tests/utils/test_general.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
iter_event_stacktraces,
to_base64,
from_base64,
strip_string,
AnnotatedValue,
)
from sentry_sdk._compat import text_type, string_types

Expand Down Expand Up @@ -217,3 +219,22 @@ def test_failed_base64_conversion(input):
# failures
if type(input) not in string_types:
assert to_base64(input) is None


def test_strip_string():
# If value is None returns None.
assert strip_string(None) is None

# If max_length is not passed, returns the full text (up to 1024 bytes).
text_1024_long = "a" * 1024
assert strip_string(text_1024_long).count("a") == 1024

# If value exceeds the max_length, returns an AnnotatedValue.
text_1025_long = "a" * 1025
stripped_text = strip_string(text_1025_long)
assert isinstance(stripped_text, AnnotatedValue)
assert stripped_text.value.count("a") == 1021 # + '...' is 1024

# If text has unicode characters, it counts bytes and not number of characters.
text_with_unicode_character = "éê"
assert strip_string(text_with_unicode_character, max_length=2).value == "é..."