1
0
mirror of https://github.com/aziascreations/BakaShrot.git synced 2026-11-23 20:58:46 +01:00

Made the roles the only lists of topics, Refused PRAGMAs in queries

- Removed `[server] topics` and `[replication] replicated_topics`: a push
  is taken by its roles' `push_topics`, a replicated message by the
  peer's `accept_topics`; a topic exists from its first message.
- Topics that only came from a peer are read and listed like any other;
  `ListNames` lists the topics with messages, filtered by the roles.
- A push to a topic no role names is `NotAllowed`; `UnknownName` is only
  for nodes.
- Clients' queries: every PRAGMA refused as it's prepared (SQLite's
  authorizer), `PRAGMA query_only = 0` used to take effect.
- Noted the leads found by reading the code in `docs/known-issues.md`.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
2026-11-16 01:28:24 +01:00
parent cef99102f9
commit cf8264b21d
34 changed files with 304 additions and 227 deletions
+3 -2
View File
@@ -16,8 +16,9 @@ bakashrot-proto = { path = "../bakashrot-proto" }
bakashrot-config = { path = "../bakashrot-config" }
# SQLite itself compiled in (`bundled`): the same version everywhere, with
# its JSON functions, no system library to install. `fallible_uint`: u64
# in and out, an error past i64::MAX instead of a wrap.
rusqlite = { version = "0.42.0", features = ["bundled", "fallible_uint"] }
# in and out, an error past i64::MAX instead of a wrap. `hooks`: the
# authorizer that refuses PRAGMAs in clients' queries.
rusqlite = { version = "0.42.0", features = ["bundled", "fallible_uint", "hooks"] }
# Query results as JSON.
serde_json = "1.0.154"
+11 -2
View File
@@ -3,6 +3,7 @@
use crate::{Error, QueryRunner, Reader, Result, SCHEMA_VERSION, Writer};
use bakashrot_config::Synchronous;
use rusqlite::hooks::{AuthAction, AuthContext, Authorization};
use rusqlite::{Connection, OptionalExtension, TransactionBehavior};
use std::path::{Path, PathBuf};
use std::time::Duration;
@@ -126,10 +127,18 @@ impl Database {
Ok(Reader::new(self.connect()?))
}
/// A connection for clients' queries: read-only (`query_only`).
/// A connection for clients' queries: read-only (`query_only`), and
/// every PRAGMA refused from then on (SQLite's authorizer). Some take
/// effect when they're prepared, before a statement can be refused
/// for what it is: `PRAGMA query_only = 0` turned `query_only` off
/// for the connection's next queries.
pub fn query_runner(&self) -> Result<QueryRunner> {
let connection = self.connect()?;
let mut connection = self.connect()?;
connection.pragma_update(None, "query_only", true)?;
connection.authorizer(Some(|context: AuthContext<'_>| match context.action {
AuthAction::Pragma { .. } => Authorization::Deny,
_ => Authorization::Allow,
}))?;
Ok(QueryRunner::new(connection))
}
+4 -1
View File
@@ -3,7 +3,10 @@
//! Read-only three ways: the connection has `query_only` on (SQLite
//! refuses any write), the statement must be one SQLite says only reads
//! (`sqlite3_stmt_readonly`), and only one statement runs (more is an
//! error, not ignored). The result is JSON, every value a string or null
//! error, not ignored). PRAGMAs are refused as they're prepared
//! (`Database::query_runner`'s authorizer): some would change the
//! connection, `query_only` included, before the statement is looked
//! at. The result is JSON, every value a string or null
//! (as in protocol 6): `{"columns":[...],"rows":[[...],...]}`.
use crate::{Error, Result};
+19
View File
@@ -702,6 +702,25 @@ fn queries() {
] {
assert!(matches!(runner.run(sql), Err(Error::Query(_))), "{sql}");
}
// PRAGMAs are refused as they're prepared, reading ones too: one that
// changes the connection would have by the time it's looked at.
for sql in [
"PRAGMA query_only = 0",
"PRAGMA query_only",
"PRAGMA locking_mode = EXCLUSIVE",
"PRAGMA database_list",
"SELECT * FROM pragma_database_list",
] {
match runner.run(sql) {
Err(Error::Query(why)) => assert!(why.contains("not authorized"), "{sql}: {why}"),
other => panic!("{sql}: {other:?}"),
}
}
// Still read-only, still answering.
assert_eq!(
json("SELECT count(*) FROM messages")["rows"],
serde_json::json!([["3"]])
);
// A failing row fails it all, with SQLite's message.
match runner.run("SELECT message_data ->> '$.t' FROM messages") {
Err(Error::Query(why)) => assert!(why.contains("malformed JSON"), "{why}"),