From 5ec695e159f94d30516a102e50e9b748e388de1a Mon Sep 17 00:00:00 2001 From: "tinycloud-bot[bot]" <348376441+tinycloud-bot[bot]@users.noreply.github.com> Date: Sat, 14 Nov 2026 02:50:41 +0000 Subject: [PATCH 1/3] test(TC-3035): read_only caveat vs non-read statements for admin; is_plain_read rule --- tinycloud-core/src/duckdb/parser.rs | 109 ++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) diff --git a/tinycloud-core/src/duckdb/parser.rs b/tinycloud-core/src/duckdb/parser.rs index 1919bff54..be2c15bf8 100644 --- a/tinycloud-core/src/duckdb/parser.rs +++ b/tinycloud-core/src/duckdb/parser.rs @@ -25,6 +25,26 @@ pub struct ParsedQuery { pub is_ddl: bool, } +impl ParsedQuery { + /// Whether the statements are plain reads: queries, `EXPLAIN TABLE`, and + /// `EXPLAIN` without `ANALYZE`, none of which writes. Anything else, + /// including statements that run a stored body or change settings, is + /// not. Transactions are not either: `is_read_only` lets a read tier + /// open one, but a caller deciding whether a request can write should + /// treat it like any non-read statement. + pub fn is_plain_read(&self) -> bool { + self.is_read_only + && self.statements.iter().all(|statement| { + matches!( + statement, + Statement::Query(_) + | Statement::ExplainTable { .. } + | Statement::Explain { analyze: false, .. } + ) + }) + } +} + fn ascii_prefix(value: &str, prefix: &str) -> bool { value .as_bytes() @@ -1265,4 +1285,93 @@ mod tests { vec![TouchedTables::supported(vec!["users".to_string()])] ); } + + const ADMIN: &str = "tinycloud.duckdb/admin"; + + fn read_only_caveat() -> Option { + Some(DuckDbCaveats { + tables: None, + columns: None, + statements: None, + read_only: Some(true), + }) + } + + /// A `read_only` caveat holds for the admin tier too: a statement that can + /// change data, settings or files, or that runs a stored body, is refused + /// whether or not the parser models its effect. + #[test] + fn read_only_caveat_refuses_what_is_not_a_plain_read_for_admin() { + // These must parse, and be refused for being writes. + for sql in [ + "INSERT INTO t VALUES (1)", + "PREPARE p AS INSERT INTO t VALUES (1)", + "PREPARE p AS UPDATE t SET a = 1", + "PREPARE p AS DELETE FROM t", + "EXECUTE p", + "DEALLOCATE p", + "EXPLAIN ANALYZE INSERT INTO t VALUES (1)", + ] { + assert!( + matches!( + validate_sql(sql, &read_only_caveat(), ADMIN), + Err(DuckDbError::ReadOnlyViolation) + ), + "{sql}" + ); + } + // These may also be refused earlier, by the parser; never allowed. + for sql in [ + "CALL checkpoint()", + "COPY t TO 'out.csv'", + "CREATE SCHEMA s", + "CREATE SEQUENCE q", + "ATTACH ':memory:' AS m", + ] { + assert!( + validate_sql(sql, &read_only_caveat(), ADMIN).is_err(), + "{sql}" + ); + } + } + + #[test] + fn read_only_caveat_still_allows_plain_reads_for_admin() { + for sql in [ + "SELECT 1", + "SELECT * FROM t", + "WITH c AS (SELECT 1) SELECT * FROM c", + "EXPLAIN SELECT * FROM t", + "EXPLAIN ANALYZE SELECT * FROM t", + "BEGIN", + "COMMIT", + ] { + let parsed = validate_sql(sql, &read_only_caveat(), ADMIN) + .unwrap_or_else(|error| panic!("{sql}: {error}")); + assert!(parsed.is_read_only, "{sql}"); + } + } + + /// `is_plain_read` is the rule a request that can't write must meet: + /// queries and non-`ANALYZE` plans only. + #[test] + fn only_queries_and_plans_are_plain_reads() { + for (sql, plain) in [ + ("SELECT 1", true), + ("WITH c AS (SELECT 1) SELECT * FROM c", true), + ("EXPLAIN SELECT * FROM t", true), + ("EXPLAIN ANALYZE SELECT * FROM t", false), + ("BEGIN", false), + ("COMMIT", false), + ("INSERT INTO t VALUES (1)", false), + ("WITH c AS (SELECT 1) INSERT INTO t SELECT * FROM c", false), + ("PREPARE p AS INSERT INTO t VALUES (1)", false), + ("EXECUTE p", false), + ("DEALLOCATE p", false), + ] { + let parsed = + validate_sql(sql, &None, ADMIN).unwrap_or_else(|error| panic!("{sql}: {error}")); + assert_eq!(parsed.is_plain_read(), plain, "{sql}"); + } + } } From f05d2a73534b1eebfa59a36544784a4f39ae0ec9 Mon Sep 17 00:00:00 2001 From: "tinycloud-bot[bot]" <348376441+tinycloud-bot[bot]@users.noreply.github.com> Date: Sat, 14 Nov 2026 02:50:41 +0000 Subject: [PATCH 2/3] fix(TC-3035): DuckDB read_only caveat allows only plain reads --- CHANGELOG.md | 1 + tinycloud-core/src/duckdb/parser.rs | 15 +++++++++++---- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ca9632fb0..9ff53a52a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## [Unreleased] ### Changes +- DuckDB: a delegation with the `read_only` caveat allows only queries, `EXPLAIN` and transaction statements, whatever its ability tier. Any other statement (for example `PREPARE`, `EXECUTE`, `CALL`, `COPY`) is refused with a read-only violation. Details: TC-3035. - SQLite stamp replay deadlines now use the same timestamp encoding as cleanup (TC-2920). - PostgreSQL database artifacts use uncompressed external storage for new writes to avoid repeated decompression during chunked reads (TC-2917). Existing large artifacts require an operator re-toast; verify both storage settings after rollout and follow `scripts/tc2917-README.md` if lock contention deferred the migration. - Encryption: v2 column format with key ids, per-purpose keys and recorded format metadata. Optional strict reads are off by default. Details: TC-2516, TC-2517. diff --git a/tinycloud-core/src/duckdb/parser.rs b/tinycloud-core/src/duckdb/parser.rs index be2c15bf8..7c4324869 100644 --- a/tinycloud-core/src/duckdb/parser.rs +++ b/tinycloud-core/src/duckdb/parser.rs @@ -665,10 +665,17 @@ fn classify_statement( *is_ddl = true; extract_tables_from_statement(stmt, tables); } - Statement::SetVariable { .. } => { - *is_read_only = false; - } - _ => {} + // Plans and transactions don't write, and a read tier may use them. + Statement::ExplainTable { .. } + | Statement::StartTransaction { .. } + | Statement::Commit { .. } + | Statement::Rollback { .. } => {} + // Everything else is not known to be a read: `SET`, and the admin + // tier's statements that run a stored body, change data or settings, + // or touch files (`PREPARE`, `EXECUTE`, `CALL`, `COPY`, `ATTACH`, + // `CREATE SCHEMA`, ...). A statement the parser learns later is + // refused under a `read_only` caveat until it is classified here. + _ => *is_read_only = false, } } From 081bb8055f5e6202cfb7dd90f14717cb4a40905f Mon Sep 17 00:00:00 2001 From: "tinycloud-bot[bot]" <348376441+tinycloud-bot[bot]@users.noreply.github.com> Date: Sat, 14 Nov 2026 03:10:42 +0000 Subject: [PATCH 3/3] test(TC-3035): pin EXPLAIN ANALYZE of EXECUTE, UPDATE and DELETE under read_only --- tinycloud-core/src/duckdb/parser.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tinycloud-core/src/duckdb/parser.rs b/tinycloud-core/src/duckdb/parser.rs index 7c4324869..8fb71ce26 100644 --- a/tinycloud-core/src/duckdb/parser.rs +++ b/tinycloud-core/src/duckdb/parser.rs @@ -1318,6 +1318,8 @@ mod tests { "EXECUTE p", "DEALLOCATE p", "EXPLAIN ANALYZE INSERT INTO t VALUES (1)", + "EXPLAIN ANALYZE UPDATE t SET a = 1", + "EXPLAIN ANALYZE DELETE FROM t", ] { assert!( matches!( @@ -1342,6 +1344,22 @@ mod tests { } } + /// `EXPLAIN ANALYZE` runs what it wraps, so wrapping `EXECUTE` is the + /// same bypass shape as `EXECUTE` itself. It is a read-only violation if + /// the parser accepts it, and a parse refusal otherwise; never allowed. + #[test] + fn read_only_caveat_refuses_explain_analyze_of_execute() { + let result = validate_sql("EXPLAIN ANALYZE EXECUTE p", &read_only_caveat(), ADMIN); + assert!( + matches!( + result, + Err(DuckDbError::ReadOnlyViolation) | Err(DuckDbError::ParseError(_)) + ), + "{:?}", + result.map(|parsed| parsed.is_read_only) + ); + } + #[test] fn read_only_caveat_still_allows_plain_reads_for_admin() { for sql in [