1+ use crate :: redaction:: main:: redact_error_text:: redact_error_text;
12use crate :: redaction:: main:: redact_text:: redact_text;
23use crate :: redaction:: tests:: test_types:: RedactTestCase ;
34
@@ -19,6 +20,56 @@ fn given_diagnostic_text_when_redacting_then_matches_python_substitutions() {
1920 text : "nothing to see" ,
2021 expected_text : "nothing to see" ,
2122 } ,
23+ RedactTestCase {
24+ description : "a key glued to a prefix with a separator" ,
25+ text : "db_password=orders-pass host=warehouse" ,
26+ expected_text : "db_password=[REDACTED] host=warehouse" ,
27+ } ,
28+ RedactTestCase {
29+ description : "a key glued to a prefix without a separator" ,
30+ text : "PGPASSWORD=orders-pass psql" ,
31+ expected_text : "PGPASSWORD=[REDACTED] psql" ,
32+ } ,
33+ RedactTestCase {
34+ description : "near miss: a key that only starts with a secret word keeps its value" ,
35+ text : "password_policy=strict max_tokens=5 tokenizer: words passwords: 3 tries" ,
36+ expected_text : "password_policy=strict max_tokens=5 tokenizer: words passwords: 3 tries" ,
37+ } ,
38+ RedactTestCase {
39+ description : "near miss: pwd stays a whole word, so working directories are kept" ,
40+ text : "OLDPWD=/srv/orders cwd=/srv/orders pwd=orders-pass" ,
41+ expected_text : "OLDPWD=/srv/orders cwd=/srv/orders pwd=[REDACTED]" ,
42+ } ,
43+ RedactTestCase {
44+ description : "a quoted PASSWORD in DDL" ,
45+ text : "CREATE ROLE orders_app WITH LOGIN PASSWORD 'orders-pass' VALID UNTIL 'infinity'" ,
46+ expected_text : "CREATE ROLE orders_app WITH LOGIN PASSWORD [REDACTED] VALID UNTIL 'infinity'" ,
47+ } ,
48+ RedactTestCase {
49+ description : "IDENTIFIED BY with a quoted and a bare password" ,
50+ text : "CREATE USER orders IDENTIFIED BY 'orders-pass'; ALTER USER stock IDENTIFIED BY stockpass" ,
51+ expected_text : "CREATE USER orders IDENTIFIED BY [REDACTED]; ALTER USER stock IDENTIFIED BY [REDACTED]" ,
52+ } ,
53+ RedactTestCase {
54+ description : "near miss: prose about passwords and IDENTIFIED WITH keep their words" ,
55+ text : "password authentication failed for user \" orders\" ; IDENTIFIED WITH caching_sha2_password" ,
56+ expected_text : "password authentication failed for user \" orders\" ; IDENTIFIED WITH caching_sha2_password" ,
57+ } ,
58+ RedactTestCase {
59+ description : "an unencoded @ inside a URI password" ,
60+ text : "url=postgres://orders:p@ss@warehouse.example.test/orders" ,
61+ expected_text : "url=postgres://orders:[REDACTED]@warehouse.example.test/orders" ,
62+ } ,
63+ RedactTestCase {
64+ description : "near miss: an @ after the host belongs to the path or query, not the password" ,
65+ text : "postgres://orders:pw@warehouse.example.test/orders?contact=support@example.com, https://api.example.test/v1" ,
66+ expected_text : "postgres://orders:[REDACTED]@warehouse.example.test/orders?contact=support@example.com, https://api.example.test/v1" ,
67+ } ,
68+ RedactTestCase {
69+ description : "two URIs in a list each lose their password" ,
70+ text : "postgres://orders:one@h1.example.test,postgres://stock:two@h2.example.test" ,
71+ expected_text : "postgres://orders:[REDACTED]@h1.example.test,postgres://stock:[REDACTED]@h2.example.test" ,
72+ } ,
2273 ] ;
2374
2475 for test_case in test_cases {
@@ -30,3 +81,38 @@ fn given_diagnostic_text_when_redacting_then_matches_python_substitutions() {
3081 ) ;
3182 }
3283}
84+
85+ #[ test]
86+ fn given_error_text_with_cut_uris_when_redacting_then_position_tells_a_port_from_a_password ( ) {
87+ let test_cases = [
88+ RedactTestCase {
89+ description : "an all-digit password in a URI cut before its host" ,
90+ text : "could not parse 'postgres://orders:1234" ,
91+ expected_text : "could not parse 'postgres://orders:[REDACTED]" ,
92+ } ,
93+ RedactTestCase {
94+ description : "a password cut at a closing quote" ,
95+ text : "dsn 'postgres://orders:orders-pass' rejected" ,
96+ expected_text : "dsn 'postgres://orders:[REDACTED]' rejected" ,
97+ } ,
98+ RedactTestCase {
99+ description : "near miss: a port the authority ends after stays" ,
100+ text : "GET http://warehouse.example.test:8080/health failed; retry http://warehouse.example.test:8443?attempt=2" ,
101+ expected_text : "GET http://warehouse.example.test:8080/health failed; retry http://warehouse.example.test:8443?attempt=2" ,
102+ } ,
103+ RedactTestCase {
104+ description : "a complete URI is redacted once and a second pass changes nothing" ,
105+ text : "postgres://orders:[REDACTED]@warehouse.example.test:5432/orders" ,
106+ expected_text : "postgres://orders:[REDACTED]@warehouse.example.test:5432/orders" ,
107+ } ,
108+ ] ;
109+
110+ for test_case in test_cases {
111+ assert_eq ! (
112+ redact_error_text( test_case. text) ,
113+ test_case. expected_text,
114+ "{}" ,
115+ test_case. description
116+ ) ;
117+ }
118+ }
0 commit comments