Skip to content

Commit e5196b4

Browse files
committed
Reject raw unassigned Unicode characters consistently
1 parent 1ce1d0d commit e5196b4

2 files changed

Lines changed: 33 additions & 3 deletions

File tree

‎src/cratis_chronicle/connection_string.py‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
_REDACTED = "****"
4545
_HOST_LABEL = re.compile(r"[A-Za-z0-9](?:[A-Za-z0-9_-]*[A-Za-z0-9])?")
4646
_ZONE_ID = re.compile(r"[A-Za-z0-9._~-]+")
47-
_FORBIDDEN_RAW_CATEGORIES = {"Cc", "Cf", "Zs", "Zl", "Zp"}
47+
_FORBIDDEN_RAW_CATEGORIES = {"Cc", "Cf", "Cn", "Zs", "Zl", "Zp"}
4848
_PORT = re.compile(r"^[0-9]+$")
4949
_API_KEY = "apikey"
5050
_AUTH = "auth"
@@ -143,7 +143,8 @@ def parse_connection_string(connection_string: str) -> ChronicleConnectionOption
143143
144144
Raises:
145145
TypeError: ``connection_string`` is not a ``str``.
146-
MalformedConnectionStringError: The raw string contains Unicode whitespace, control or format characters.
146+
MalformedConnectionStringError: The raw string contains Unicode whitespace, control, format or unassigned
147+
characters.
147148
UnsupportedSchemeError: The scheme is not ``chronicle``.
148149
MissingHostError: No host is given.
149150
InvalidHostError: The host or its brackets are invalid, or more than one host is given.
@@ -158,7 +159,9 @@ def parse_connection_string(connection_string: str) -> ChronicleConnectionOption
158159
raise TypeError("A connection string must be a str")
159160

160161
if any(unicodedata.category(character) in _FORBIDDEN_RAW_CATEGORIES for character in connection_string):
161-
raise MalformedConnectionStringError("The connection string must not contain whitespace or control characters")
162+
raise MalformedConnectionStringError(
163+
"The connection string must not contain whitespace, control, format or unassigned characters"
164+
)
162165

163166
scheme, separator, authority_and_suffix = connection_string.partition("://")
164167
if not separator or scheme.lower() != _SCHEME:

‎tests/test_connection_string.py‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -644,6 +644,33 @@ def test_accepts_rfc6874_zone_characters(zone: str, delimiter: str) -> None:
644644
assert options.host == f"fe80::1%{zone}"
645645

646646

647+
@pytest.mark.parametrize("codepoint", [0x0378, 0x0890, 0x0891, *range(0x13439, 0x13440)])
648+
@pytest.mark.parametrize("field", ["client_id", "client_secret"])
649+
def test_rejects_raw_unassigned_or_newly_assigned_format_characters(codepoint: int, field: str) -> None:
650+
character = chr(codepoint)
651+
client_id = "id" + (character if field == "client_id" else "")
652+
client_secret = "secret" + (character if field == "client_secret" else "")
653+
connection_string = f"chronicle://{client_id}:{client_secret}@localhost"
654+
with pytest.raises(MalformedConnectionStringError) as caught:
655+
parse_connection_string(connection_string)
656+
657+
assert caught.value.__cause__ is None
658+
assert caught.value.__context__ is None
659+
660+
661+
@pytest.mark.parametrize("codepoint", [0x0378, 0x0890, 0x0891, *range(0x13439, 0x13440)])
662+
@pytest.mark.parametrize("field", ["client_id", "client_secret"])
663+
def test_preserves_percent_encoded_unassigned_or_newly_assigned_format_characters(codepoint: int, field: str) -> None:
664+
character = chr(codepoint)
665+
client_id = "id" + (quote(character) if field == "client_id" else "")
666+
client_secret = "secret" + (quote(character) if field == "client_secret" else "")
667+
options = parse_connection_string(f"chronicle://{client_id}:{client_secret}@localhost")
668+
669+
assert options.client_id == "id" + (character if field == "client_id" else "")
670+
assert options.client_secret == "secret" + (character if field == "client_secret" else "")
671+
assert str(options)
672+
673+
647674
def test_an_invalid_ipv6_host_error_does_not_retain_the_input_in_its_chain() -> None:
648675
secret = "super-secret"
649676
with pytest.raises(InvalidHostError) as caught:

0 commit comments

Comments
 (0)