Skip to content

Commit 33882b9

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 8722545 commit 33882b9

File tree

2 files changed

+27
-14
lines changed

2 files changed

+27
-14
lines changed

libsql-server/src/connection/libsql.rs

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,31 @@ impl<W: Wal> Connection<W> {
694694
Ok(enabled)
695695
}
696696

697+
fn prepare_attach_query(&self, attached: &str, attached_alias: &str) -> Result<String> {
698+
let attached = attached.strip_prefix('"').unwrap_or(attached);
699+
let attached = attached.strip_suffix('"').unwrap_or(attached);
700+
if attached.contains('/') {
701+
return Err(Error::Internal(format!(
702+
"Invalid attached database name: {:?}",
703+
attached
704+
)));
705+
}
706+
let path = PathBuf::from(self.conn.path().unwrap_or("."));
707+
let dbs_path = path
708+
.parent()
709+
.unwrap_or_else(|| std::path::Path::new(".."))
710+
.parent()
711+
.unwrap_or_else(|| std::path::Path::new(".."))
712+
.canonicalize()
713+
.unwrap_or_else(|_| std::path::PathBuf::from(".."));
714+
let query = format!(
715+
"ATTACH DATABASE 'file:{}?mode=ro' AS \"{attached_alias}\"",
716+
dbs_path.join(attached).join("data").display()
717+
);
718+
tracing::trace!("ATTACH rewritten to: {query}");
719+
Ok(query)
720+
}
721+
697722
fn execute_query(
698723
&self,
699724
query: &Query,
@@ -719,19 +744,7 @@ impl<W: Wal> Connection<W> {
719744
let mut stmt = if matches!(query.stmt.kind, StmtKind::Attach) {
720745
match &query.stmt.attach_info {
721746
Some((attached, attached_alias)) => {
722-
let path = PathBuf::from(self.conn.path().unwrap_or("."));
723-
let dbs_path = path
724-
.parent()
725-
.unwrap_or_else(|| std::path::Path::new(".."))
726-
.parent()
727-
.unwrap_or_else(|| std::path::Path::new(".."))
728-
.canonicalize()
729-
.unwrap_or_else(|_| std::path::PathBuf::from(".."));
730-
let query = format!(
731-
"ATTACH DATABASE 'file:{}?mode=ro' AS \"{attached_alias}\"",
732-
dbs_path.join(attached).join("data").display()
733-
);
734-
tracing::trace!("ATTACH rewritten to: {query}");
747+
let query = self.prepare_attach_query(attached, attached_alias)?;
735748
self.conn.prepare(&query)?
736749
}
737750
None => {

libsql-server/src/query_analysis.rs

Lines changed: 1 addition & 1 deletion
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)