Skip to content
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
40 changes: 0 additions & 40 deletions sentry_sdk/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import base64
Comment thread
sentrivana marked this conversation as resolved.
import copy
import json
import linecache
Expand Down Expand Up @@ -85,8 +84,6 @@

_installed_modules = None

BASE64_ALPHABET = re.compile(r"^[a-zA-Z0-9/+=]*$")

FALSY_ENV_VALUES = frozenset(("false", "f", "n", "no", "off", "0"))
TRUTHY_ENV_VALUES = frozenset(("true", "t", "y", "yes", "on", "1"))
_AWS_SIGV4_SIGNING_ALGORITHMS = frozenset(
Expand Down Expand Up @@ -1620,43 +1617,6 @@ def run(self) -> None:
)


def to_base64(original: str) -> "Optional[str]":
"""
Convert a string to base64, via UTF-8. Returns None on invalid input.
"""
base64_string = None

try:
utf8_bytes = original.encode("UTF-8")
base64_bytes = base64.b64encode(utf8_bytes)
base64_string = base64_bytes.decode("UTF-8")
except Exception as err:
logger.warning("Unable to encode {orig} to base64:".format(orig=original), err)

return base64_string


def from_base64(base64_string: str) -> "Optional[str]":
"""
Convert a string from base64, via UTF-8. Returns None on invalid input.
"""
utf8_string = None

try:
only_valid_chars = BASE64_ALPHABET.match(base64_string)
assert only_valid_chars

base64_bytes = base64_string.encode("UTF-8")
utf8_bytes = base64.b64decode(base64_bytes)
utf8_string = utf8_bytes.decode("UTF-8")
except Exception as err:
logger.warning(
"Unable to decode {b64} from base64:".format(b64=base64_string), err
)

return utf8_string


Components = namedtuple("Components", ["scheme", "netloc", "path", "query", "fragment"])


Expand Down
55 changes: 0 additions & 55 deletions tests/utils/test_general.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@
Dsn,
exceptions_from_error_tuple,
filename_for_module,
from_base64,
iter_event_stacktraces,
safe_repr,
set_in_app_in_frames,
strip_string,
to_base64,
)

try:
Expand Down Expand Up @@ -550,59 +548,6 @@ def test_iter_stacktraces():
) == {1, 2, 3}


@pytest.mark.parametrize(
("original", "base64_encoded"),
[
# ascii only
("Dogs are great!", "RG9ncyBhcmUgZ3JlYXQh"),
# emoji
("🐶", "8J+Qtg=="),
# non-ascii
(
"Καλό κορίτσι, Μάιζεϊ!",
"zprOsc67z4wgzrrOv8+Bzq/PhM+DzrksIM6czqzOuc62zrXPiiE=",
),
# mix of ascii and non-ascii
(
"Of margir hundar! Ég geri ráð fyrir að ég þurfi stærra rúm.",
"T2YgbWFyZ2lyIGh1bmRhciEgw4lnIGdlcmkgcsOhw7AgZnlyaXIgYcOwIMOpZyDDvnVyZmkgc3TDpnJyYSByw7ptLg==",
),
],
)
def test_successful_base64_conversion(original, base64_encoded):
# all unicode characters should be handled correctly
assert to_base64(original) == base64_encoded
assert from_base64(base64_encoded) == original

# "to" and "from" should be inverses
assert from_base64(to_base64(original)) == original
assert to_base64(from_base64(base64_encoded)) == base64_encoded


@pytest.mark.parametrize(
"input",
[
1231, # incorrect type
True, # incorrect type
[], # incorrect type
{}, # incorrect type
None, # incorrect type
"yayfordogs", # wrong length
"#dog", # invalid ascii character
"🐶", # non-ascii character
],
)
def test_failed_base64_conversion(input):
# conversion from base64 should fail if given input of the wrong type or
# input which isn't a valid base64 string
assert from_base64(input) is None

# any string can be converted to base64, so only type errors will cause
# failures
if not isinstance(input, str):
assert to_base64(input) is None


@pytest.mark.parametrize(
"input,max_length,result",
[
Expand Down
Loading