22# Licensed under the MIT license. See LICENSE file in the project root for full license information.
33
44import dataclasses
5+ import traceback
56
67import pytest
78
2223 UnsupportedSchemeError ,
2324 parse_connection_string ,
2425)
26+ from cratis_chronicle .connection_string import _parse_host_and_port
2527
2628
2729def 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+
377390def 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)
395417def 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
421446def 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\uff0f bad" )
@@ -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