Skip to content

Commit bdacec2

Browse files
committed
fix(redaction): keep only digit ports in cut URIs and cover IDENTIFIED WITH plugin BY
A value after scheme://name: stays only when it is all digits and the authority ends after it, so a cut password containing / is redacted again. IDENTIFIED WITH <plugin> BY '...' is redacted like IDENTIFIED BY.
1 parent 5d8dc93 commit bdacec2

3 files changed

Lines changed: 16 additions & 6 deletions

File tree

‎crates/sqlbuild-core/src/redaction/_helpers/patterns.rs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,15 @@ pub(crate) fn substituted(
4040
}
4141
}
4242

43-
/// By position: a value the authority ends after (`/`, `?`, `#`) is a port; a cut-off value may be a password.
43+
/// A port is all digits and the authority ends after it (`/`, `?`, `#`); anything else may be a password.
4444
pub(crate) fn bare_userinfo_redacted(text: &str) -> String {
4545
match BARE_USERINFO.as_ref() {
4646
Ok(regex) => regex
4747
.replace_all(text, |found: &Captures<'_>| {
48-
if found[3].is_empty() {
49-
format!("{}[REDACTED]", &found[1])
50-
} else {
48+
if !found[3].is_empty() && found[2].bytes().all(|byte| byte.is_ascii_digit()) {
5149
found[0].to_owned()
50+
} else {
51+
format!("{}[REDACTED]{}", &found[1], &found[3])
5252
}
5353
})
5454
.into_owned(),

‎crates/sqlbuild-core/src/redaction/constants.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ pub(crate) const URI_CREDENTIAL_PATTERN: &str = concat!(
3030
r"(?i)\b([a-z][a-z0-9+.-]*://[^\s:/@]+:)([^\s]+?)",
3131
r#"(@[^\s@/?#,;'"]*)([\s/?#,;'"]|$)"#,
3232
);
33-
/// SQL that sets a password: `PASSWORD '…'` (quoted only, so prose such as "password authentication" stays) and `IDENTIFIED BY …`.
33+
/// SQL that sets a password: `PASSWORD '…'` (quoted only, so prose such as "password authentication" stays) and `IDENTIFIED [WITH plugin] BY …`.
3434
pub(crate) const DDL_PASSWORD_PATTERN: &str = concat!(
3535
r#"(?i)\b(password)(\s+)("[^"\r\n]{0,512}"|'[^'\r\n]{0,512}')|"#,
36-
r#"\b(identified\s+by)(\s+)("[^"\r\n]{0,512}"|'[^'\r\n]{0,512}'|[^\s,;]{1,512})"#,
36+
r#"\b(identified\s+(?:with\s+\w+\s+)?by)(\s+)("[^"\r\n]{0,512}"|'[^'\r\n]{0,512}'|[^\s,;]{1,512})"#,
3737
);
3838
pub(crate) const DDL_REPLACEMENT: &str = "${1}${2}${4}${5}[REDACTED]";
3939
pub(crate) const PAIR_REPLACEMENT: &str = "${1}${2}[REDACTED]";

‎crates/sqlbuild-core/src/redaction/tests/test_redact_text.rs‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ fn given_diagnostic_text_when_redacting_then_matches_python_substitutions() {
5050
text: "CREATE USER orders IDENTIFIED BY 'orders-pass'; ALTER USER stock IDENTIFIED BY stockpass",
5151
expected_text: "CREATE USER orders IDENTIFIED BY [REDACTED]; ALTER USER stock IDENTIFIED BY [REDACTED]",
5252
},
53+
RedactTestCase {
54+
description: "IDENTIFIED WITH a plugin BY a password, as ClickHouse writes it",
55+
text: "CREATE USER orders IDENTIFIED WITH sha256_password BY 'orders-pass' HOST ANY",
56+
expected_text: "CREATE USER orders IDENTIFIED WITH sha256_password BY [REDACTED] HOST ANY",
57+
},
5358
RedactTestCase {
5459
description: "near miss: prose about passwords and IDENTIFIED WITH keep their words",
5560
text: "password authentication failed for user \"orders\"; IDENTIFIED WITH caching_sha2_password",
@@ -95,6 +100,11 @@ fn given_error_text_with_cut_uris_when_redacting_then_position_tells_a_port_from
95100
text: "dsn 'postgres://orders:orders-pass' rejected",
96101
expected_text: "dsn 'postgres://orders:[REDACTED]' rejected",
97102
},
103+
RedactTestCase {
104+
description: "a cut password containing / is not a port",
105+
text: "cut 'postgres://orders:pa/ss",
106+
expected_text: "cut 'postgres://orders:[REDACTED]/ss",
107+
},
98108
RedactTestCase {
99109
description: "near miss: a port the authority ends after stays",
100110
text: "GET http://warehouse.example.test:8080/health failed; retry http://warehouse.example.test:8443?attempt=2",

0 commit comments

Comments
 (0)