mirror of
https://github.com/GreptimeTeam/greptimedb.git
synced 2026-11-21 10:14:06 +00:00
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:
@@ -27,6 +27,17 @@ pub fn ensure_admin_privilege(
|
||||
func_ctx: &FunctionContext,
|
||||
fn_name: &str,
|
||||
) -> datafusion_common::Result<()> {
|
||||
// Database-less MySQL sessions have no authorized default scope. Admin targets
|
||||
// are runtime function arguments, so frontend relation checks cannot authorize
|
||||
// them, even when qualified. Require database selection before invoking a handler.
|
||||
if func_ctx.query_ctx.channel() == session::context::Channel::Mysql
|
||||
&& func_ctx.query_ctx.current_schema().is_empty()
|
||||
{
|
||||
return Err(DataFusionError::External(Box::new(BoxedError::new(
|
||||
session::SessionError::NoDatabaseSelected {},
|
||||
))));
|
||||
}
|
||||
|
||||
let admin_stmt = Statement::Admin(Admin::Func(sql::ast::Function {
|
||||
name: ObjectName::from(vec![Ident::new(fn_name)]),
|
||||
uses_odbc_syntax: false,
|
||||
|
||||
@@ -59,7 +59,9 @@ macro_rules! impl_current_schema_function {
|
||||
let func_ctx = find_function_context(&args)?;
|
||||
let db = func_ctx.query_ctx.current_schema();
|
||||
|
||||
Ok(ColumnarValue::Scalar(ScalarValue::Utf8View(Some(db))))
|
||||
Ok(ColumnarValue::Scalar(ScalarValue::Utf8View(
|
||||
(!db.is_empty()).then_some(db),
|
||||
)))
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@@ -90,7 +90,9 @@ impl Function for CurrentSchemaFunction {
|
||||
let func_ctx = find_function_context(&args)?;
|
||||
let db = func_ctx.query_ctx.current_schema();
|
||||
|
||||
Ok(ColumnarValue::Scalar(ScalarValue::Utf8View(Some(db))))
|
||||
Ok(ColumnarValue::Scalar(ScalarValue::Utf8View(
|
||||
(!db.is_empty()).then_some(db),
|
||||
)))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -158,11 +160,15 @@ impl Function for CurrentSchemasFunction {
|
||||
&self,
|
||||
args: ScalarFunctionArgs,
|
||||
) -> datafusion_common::Result<ColumnarValue> {
|
||||
let func_ctx = find_function_context(&args)?;
|
||||
let schema = func_ctx.query_ctx.current_schema();
|
||||
let args = ColumnarValue::values_to_arrays(&args.args)?;
|
||||
let input = as_boolean_array(&args[0]);
|
||||
|
||||
// Create a UTF8 array with a single value
|
||||
let mut values = vec!["public"];
|
||||
let mut values = Vec::new();
|
||||
if !schema.is_empty() {
|
||||
values.push(schema.as_str());
|
||||
}
|
||||
// include implicit schemas
|
||||
if input.value(0) {
|
||||
values.push(INFORMATION_SCHEMA_NAME);
|
||||
|
||||
Reference in New Issue
Block a user