fix(auth): secure MySQL and PostgreSQL database selection (#10382)

* fix(mysql): authorize the default database at login

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

* fix(auth): validate MySQL handshake scope and SQL USE

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

* fix(auth): authorize PostgreSQL schema selection

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

* fix(servers): defer PostgreSQL database lookup until authorized

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

* test(servers): propagate catalog lookup errors in protocol tests

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

* fix(postgres): skip unusable search path candidates

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

* fix(auth): log database authorization and selection outcomes

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

* fix(auth): preserve search path responses and schema boundaries

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

* fix(auth): clarify database selection diagnostics

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

* fix(mysql): format handshake username in structured logs

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

* fix(auth): secure database-less sessions and failed startup

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

* fix(mysql): retain authorized default database selection

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

* refactor(auth): separate database selection from request dispatch

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

* test(mysql): align schema validation with database-less login

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

* docs(auth): explain database-less session restrictions

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

---------

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>
This commit is contained in:
dennis zhuang
2026-11-13 08:07:57 +00:00
committed by GitHub
parent f88af153ec
commit 6e025eef61
16 changed files with 1375 additions and 159 deletions
+40 -15
View File
@@ -26,23 +26,36 @@ pub struct SetVariables {
}
impl SetVariables {
/// Returns the first supported `search_path` value.
pub fn search_path(&self) -> Option<&str> {
/// Returns the schema candidates for a supported `search_path` assignment.
pub fn search_path(&self) -> Option<Vec<String>> {
let [ObjectNamePart::Identifier(variable)] = self.variable.0.as_slice() else {
return None;
};
if variable.quote_style.is_some() || !variable.value.eq_ignore_ascii_case("search_path") {
if !variable.value.eq_ignore_ascii_case("search_path") {
return None;
}
match self.value.first()? {
Expr::Value(ValueWithSpan {
value: Value::SingleQuotedString(value) | Value::DoubleQuotedString(value),
..
}) => Some(value),
Expr::Identifier(value) => Some(&value.value),
_ => None,
if let [Expr::Identifier(value)] = self.value.as_slice()
&& value.quote_style.is_none()
&& value.value.eq_ignore_ascii_case("default")
{
return Some(vec!["$user".to_string(), "public".to_string()]);
}
self.value
.iter()
.map(|value| match value {
Expr::Value(ValueWithSpan {
value: Value::SingleQuotedString(value) | Value::DoubleQuotedString(value),
..
}) => Some(value.clone()),
Expr::Identifier(value) if value.quote_style.is_none() => {
Some(value.value.to_lowercase())
}
Expr::Identifier(value) => Some(value.value.clone()),
_ => None,
})
.collect()
}
}
@@ -95,11 +108,20 @@ SET delayed_insert_timeout = 300"#,
#[test]
fn test_search_path() {
for (sql, expected) in [
("SET search_path TO public", Some("public")),
("SET SEARCH_PATH TO 'private'", Some("private")),
("SET search_path TO \"quoted\"", Some("quoted")),
("SET \"search_path\" TO private", None),
("SET search_path TO public", Some(vec!["public"])),
("SET search_path TO DEFAULT", Some(vec!["$user", "public"])),
(r#"SET search_path TO "default""#, Some(vec!["default"])),
("SET search_path TO 'default'", Some(vec!["default"])),
("SET SEARCH_PATH TO 'private'", Some(vec!["private"])),
("SET search_path TO \"quoted\"", Some(vec!["quoted"])),
("SET \"search_path\" TO private", Some(vec!["private"])),
("SET timezone TO 'UTC'", None),
(
r#"SET search_path TO "$user", MISSING, "Mixed", 'a,b'"#,
Some(vec!["$user", "missing", "Mixed", "a,b"]),
),
("SET search_path TO ''", Some(vec![""])),
("SET search_path TO public, 42", None),
] {
let statements = ParserContext::create_with_dialect(
sql,
@@ -110,7 +132,10 @@ SET delayed_insert_timeout = 300"#,
let [Statement::SetVariables(set)] = statements.as_slice() else {
unreachable!()
};
assert_eq!(expected, set.search_path());
assert_eq!(
expected.map(|v| v.into_iter().map(str::to_owned).collect::<Vec<_>>()),
set.search_path()
);
}
}
}