postgres: disable load_extension on the wire listener

tursopg --server reused DatabaseOpts::turso_cli(), which turns on SQL
load_extension. The listener has no password (NoopHandler). A client
could SELECT load_extension and run native code in the server process.

Interactive CLI still enables the flag. The wire path uses the library
default (off), matching sqlite3_enable_load_extension and #3250.

Tests: tursopg wire_load_extension_is_disabled,
       repl_load_extension_still_attempts_to_open_the_file
Signed-off-by: Sebastien Tardif <sebtardif@ncf.ca>
This commit is contained in:
Sebastien Tardif
2026-09-15 17:44:14 -07:00
committed by Pekka Enberg
parent 807e7a2a9a
commit f3309d06f4
2 changed files with 48 additions and 3 deletions

View File

@@ -100,6 +100,7 @@ fn open_database(
db_path: &str,
vfs: Option<&String>,
readonly: bool,
enable_load_extension: bool,
) -> anyhow::Result<(Arc<dyn turso_core::IO>, Connection)> {
let db_opts = DatabaseOpts::new()
.with_views(true)
@@ -109,6 +110,11 @@ fn open_database(
.with_autovacuum(true)
.with_attach(true)
.with_generated_columns(true);
let db_opts = if enable_load_extension {
db_opts.turso_cli()
} else {
db_opts
};
let flags = if readonly {
OpenFlags::default().union(OpenFlags::ReadOnly)
@@ -116,8 +122,7 @@ fn open_database(
OpenFlags::default()
};
let (io, db) =
turso_pg::open_database(db_path, vfs.map(|v| v.as_str()), flags, db_opts.turso_cli())?;
let (io, db) = turso_pg::open_database(db_path, vfs.map(|v| v.as_str()), flags, db_opts)?;
let conn = Connection::connect(&db)?;
Ok((io, conn))
}
@@ -1010,7 +1015,12 @@ fn main() -> anyhow::Result<()> {
.as_ref()
.map_or(":memory:".to_string(), |p| p.to_string_lossy().to_string());
let (io, conn) = open_database(&db_file, opts.vfs.as_ref(), opts.readonly)?;
let (io, conn) = open_database(
&db_file,
opts.vfs.as_ref(),
opts.readonly,
opts.server.is_none(),
)?;
let interrupt_count = Arc::new(AtomicUsize::new(0));
{

View File

@@ -1288,6 +1288,41 @@ fn wire_copy_from_returns_copy_n() {
server.wait().ok();
}
#[test]
fn repl_load_extension_still_attempts_to_open_the_file() {
let output = run_tursopg(b"SELECT load_extension('/nonexistent/turso-audit-ext');\n");
let combined = format!(
"{}{}",
stdout(&output),
String::from_utf8_lossy(&output.stderr)
);
assert!(
combined.contains("Extension file not found"),
"interactive CLI should still try to load the library: {combined}"
);
assert!(
!combined.contains("runtime extension loading is disabled"),
"interactive CLI must keep load_extension enabled: {combined}"
);
}
#[test]
fn wire_load_extension_is_disabled() {
with_pg_client(|client| {
client.send_query("SELECT load_extension('/nonexistent/turso-audit-ext')");
let response = client.read_until_ready();
let text = String::from_utf8_lossy(&response);
assert!(
text.contains("runtime extension loading is disabled"),
"wire listener must refuse load_extension, got: {text}"
);
assert!(
!text.contains("Extension file not found"),
"wire listener must not reach the filesystem open: {text}"
);
});
}
#[test]
fn wire_comment_on_returns_comment_tag() {
with_pg_client(|client| {