Skip to content

Commit 4a43604

Browse files
committed
attach: fix quoted db names
In libsql-server, raw db names are uuids that need to be quoted, so that needs to be supported in the ATTACH layer. As a bonus, "names" that are actually file system paths are refused to prevent abuse.
1 parent aee6f34 commit 4a43604

File tree

2 files changed

+27
-14
lines changed

2 files changed

+27
-14
lines changed

libsql-server/src/connection/libsql.rs

+26-13
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,31 @@ impl<W: Wal> Connection<W> {
677677
Ok(enabled)
678678
}
679679

680+
fn prepare_attach_query(&self, attached: &str, attached_alias: &str) -> Result<String> {
681+
let attached = attached.strip_prefix("\"").unwrap_or(attached);
682+
let attached = attached.strip_suffix("\"").unwrap_or(attached);
683+
if attached.contains('/') {
684+
return Err(Error::Internal(format!(
685+
"Invalid attached database name: {:?}",
686+
attached
687+
)));
688+
}
689+
let path = PathBuf::from(self.conn.path().unwrap_or("."));
690+
let dbs_path = path
691+
.parent()
692+
.unwrap_or_else(|| std::path::Path::new(".."))
693+
.parent()
694+
.unwrap_or_else(|| std::path::Path::new(".."))
695+
.canonicalize()
696+
.unwrap_or_else(|_| std::path::PathBuf::from(".."));
697+
let query = format!(
698+
"ATTACH DATABASE 'file:{}?mode=ro' AS \"{attached_alias}\"",
699+
dbs_path.join(attached).join("data").display()
700+
);
701+
tracing::trace!("ATTACH rewritten to: {query}");
702+
Ok(query)
703+
}
704+
680705
fn execute_query(
681706
&self,
682707
query: &Query,
@@ -702,19 +727,7 @@ impl<W: Wal> Connection<W> {
702727
let mut stmt = if matches!(query.stmt.kind, StmtKind::Attach) {
703728
match &query.stmt.attach_info {
704729
Some((attached, attached_alias)) => {
705-
let path = PathBuf::from(self.conn.path().unwrap_or("."));
706-
let dbs_path = path
707-
.parent()
708-
.unwrap_or_else(|| std::path::Path::new(".."))
709-
.parent()
710-
.unwrap_or_else(|| std::path::Path::new(".."))
711-
.canonicalize()
712-
.unwrap_or_else(|_| std::path::PathBuf::from(".."));
713-
let query = format!(
714-
"ATTACH DATABASE 'file:{}?mode=ro' AS \"{attached_alias}\"",
715-
dbs_path.join(attached).join("data").display()
716-
);
717-
tracing::trace!("ATTACH rewritten to: {query}");
730+
let query = self.prepare_attach_query(attached, attached_alias)?;
718731
self.conn.prepare(&query)?
719732
}
720733
None => {

libsql-server/src/query_analysis.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ impl Statement {
284284
}) => Some((expr.clone(), name.clone())),
285285
_ => None,
286286
};
287-
287+
tracing::info!("attachiu: {:?}", attach_info);
288288
Ok(Statement {
289289
stmt: c.to_string(),
290290
kind,

0 commit comments

Comments
 (0)