Skip to content

Commit 299199c

Browse files
committed
Keep connection-string diagnostics free of input-derived secrets
1 parent 611dff1 commit 299199c

2 files changed

Lines changed: 62 additions & 8 deletions

File tree

‎src/cratis_chronicle/connection_string.py‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ def parse_connection_string(connection_string: str) -> ChronicleConnectionOption
152152
InvalidCredentialsEncodingError: The credentials are not valid percent-encoded UTF-8.
153153
"""
154154
if not isinstance(connection_string, str):
155-
raise TypeError(f"A connection string must be a str, not {type(connection_string).__name__}")
155+
raise TypeError("A connection string must be a str")
156156

157157
if any(ord(character) <= 0x20 or ord(character) == 0x7F for character in connection_string):
158158
raise MalformedConnectionStringError("The connection string must not contain whitespace or control characters")
@@ -203,8 +203,13 @@ def _parse_host_and_port(host_and_port: str) -> tuple[str, int]:
203203
port_text = remainder[1:] if remainder else None
204204
try:
205205
ipaddress.IPv6Address(host)
206-
except ValueError as error:
207-
raise InvalidHostError("The bracketed host is not a valid IPv6 address") from error
206+
except ValueError:
207+
invalid_ipv6 = True
208+
else:
209+
invalid_ipv6 = False
210+
if invalid_ipv6:
211+
# Do not retain an exception whose message can include the input host.
212+
raise InvalidHostError("The bracketed host is not a valid IPv6 address")
208213
else:
209214
if "," in host_and_port:
210215
raise InvalidHostError("Multiple hosts are not supported")
@@ -267,7 +272,7 @@ def _parse_query(query: str, *, has_credentials: bool) -> bool:
267272
elif name == _AUTH:
268273
raise UnsupportedOptionError("The 'auth' option is not supported yet")
269274
else:
270-
raise UnsupportedOptionError(f"The query parameter '{name}' is not supported yet")
275+
raise UnsupportedOptionError("The query parameter is not supported yet")
271276
return skip_tls_validation
272277

273278

‎tests/test_connection_string.py‎

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

44
import dataclasses
5+
import traceback
56

67
import pytest
78

@@ -22,6 +23,7 @@
2223
UnsupportedSchemeError,
2324
parse_connection_string,
2425
)
26+
from cratis_chronicle.connection_string import _parse_host_and_port
2527

2628

2729
def test_parses_host_port_and_credentials() -> None:
@@ -374,6 +376,17 @@ def test_rejects_a_value_that_is_not_a_string() -> None:
374376
parse_connection_string(None) # type: ignore[arg-type]
375377

376378

379+
def test_the_type_error_does_not_expose_an_input_derived_type_name() -> None:
380+
secret = "super-secret"
381+
value = type(secret, (), {})()
382+
with pytest.raises(TypeError) as caught:
383+
parse_connection_string(value)
384+
385+
assert str(caught.value) == "A connection string must be a str"
386+
assert secret not in repr(caught.value)
387+
assert secret not in "".join(traceback.format_exception(caught.value))
388+
389+
377390
def test_every_error_is_a_connection_string_error_and_a_value_error() -> None:
378391
with pytest.raises(ConnectionStringError) as caught:
379392
parse_connection_string("http://localhost")
@@ -390,14 +403,26 @@ def test_every_error_is_a_connection_string_error_and_a_value_error() -> None:
390403
"chronicle://id:super-secret-value@localhost?skipTlsValidation=maybe",
391404
"chronicle://id:super-secret-value@local host",
392405
"chronicle://id:super%FFsecret-value@localhost",
406+
"http://id:super-secret-value@localhost",
407+
"chronicle://id:super-secret-value@local!host",
408+
"chronicle://id:super-secret-value@[not-an-address]",
409+
"chronicle://id:super-secret-value@localhost/super-secret-value",
410+
"chronicle://id:super-secret-value@localhost#super-secret-value",
411+
"chronicle://id:super-secret-value@localhost?auth=super-secret-value",
412+
"chronicle://localhost?apiKey=super-secret-value",
413+
"chronicle://:super-secret-value@localhost",
414+
"chronicle://id:super-secret-value:extra@localhost",
393415
],
394416
)
395417
def test_error_messages_never_expose_the_secret(connection_string: str) -> None:
396418
with pytest.raises(ConnectionStringError) as caught:
397419
parse_connection_string(connection_string)
398420

399-
assert "super" not in str(caught.value)
400-
assert "secret-value" not in str(caught.value)
421+
for rendering in (str(caught.value), repr(caught.value), "".join(traceback.format_exception(caught.value))):
422+
assert "super" not in rendering
423+
assert "secret-value" not in rendering
424+
assert caught.value.__cause__ is None
425+
assert caught.value.__context__ is None
401426

402427

403428
@pytest.mark.parametrize("connection_string", ["chronicle://@localhost:35000", "chronicle://:@localhost:35000"])
@@ -419,8 +444,6 @@ def test_invalid_credential_encoding_error_carries_no_decoder_cause_or_context()
419444

420445

421446
def test_a_malformed_url_error_does_not_leak_the_credentials() -> None:
422-
import traceback
423-
424447
secret = "super-secret"
425448
with pytest.raises(MalformedConnectionStringError) as caught:
426449
parse_connection_string(f"chronicle://id:{secret}@host\uff0fbad")
@@ -459,3 +482,29 @@ def test_reports_a_port_of_thousands_of_zeros_as_an_invalid_port() -> None:
459482
parse_connection_string(f"chronicle://id:secret@localhost:{port}")
460483

461484
assert str(caught.value) == "The port must be an integer between 1 and 65535"
485+
486+
487+
@pytest.mark.parametrize("query_name", ["super-secret@localhost", "super%2Dsecret%40localhost"])
488+
def test_an_unsupported_query_error_does_not_expose_the_query_name(query_name: str) -> None:
489+
secret = "super-secret"
490+
with pytest.raises(UnsupportedOptionError) as caught:
491+
parse_connection_string(f"chronicle://id:35000?{query_name}")
492+
493+
assert str(caught.value) == "The query parameter is not supported yet"
494+
for rendering in (str(caught.value), repr(caught.value), "".join(traceback.format_exception(caught.value))):
495+
assert secret not in rendering
496+
assert query_name not in rendering
497+
assert caught.value.__cause__ is None
498+
assert caught.value.__context__ is None
499+
500+
501+
def test_an_invalid_ipv6_host_error_does_not_retain_the_input_in_its_chain() -> None:
502+
secret = "super-secret"
503+
with pytest.raises(InvalidHostError) as caught:
504+
_parse_host_and_port(f"[{secret}]")
505+
506+
assert str(caught.value) == "The bracketed host is not a valid IPv6 address"
507+
assert secret not in repr(caught.value)
508+
assert secret not in "".join(traceback.format_exception(caught.value))
509+
assert caught.value.__cause__ is None
510+
assert caught.value.__context__ is None

0 commit comments

Comments
 (0)