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
+14 -8
View File
@@ -10,9 +10,9 @@ Decided in `../revamp.md`, "Decided".
| Section | What |
|---|---|
| `[server]` | where it listens, the certificate files, the database, its topics, how pushes are flushed, attachments, retention's pace |
| `[server]` | where it listens, the certificate files, the database, how pushes are flushed, attachments, retention's pace |
| `[retention]` | how long each topic's messages are kept |
| `[replication]` | the servers it sends to and takes from, the topics replicated at all, retry and sync pace, batch size |
| `[replication]` | the servers it sends to and takes from, retry and sync pace, batch size |
| `[roles.<name>]` | what certificates with that role may do |
Unknown keys are refused, so a typo or a key of the old ini is caught.
@@ -45,6 +45,14 @@ What a role gives:
No `AllowRead` / `AllowWrite`: a list that's empty allows nothing. No
default topic: a push always names its topic (protocol 7 has none).
**The roles are the only lists of topics** (decided 2026-11-16): no
`[server] topics`, no `[replication] replicated_topics`, both refused as
unknown keys. A topic exists from its first message, pushed by a client
whose `push_topics` name it or taken from a peer whose `accept_topics`
do; it's then read, listed, sent on and aged like any other, each by the
roles that name it. A pattern (`push_topics = ["sensors.*"]`) lets its
clients make topics: names for a closed set.
## Peers
Who replicates with whom is each server's config, by address (not the
@@ -62,10 +70,8 @@ dialed takes them. What's sent and taken is still the roles': the other
server's certificate's roles here, their `send_*` for what's sent to it,
their `accept_*` for what's taken from it.
`[replication] replicated_topics`, optional, filters every peer's
replication whatever its roles say: an edge node that keeps some topics to
itself. Without it, no filter; `[]` replicates nothing. Pushing and reading
aren't filtered.
An edge node that keeps some topics to itself leaves them out of its
peers' `send_topics`.
## Names and values
@@ -95,7 +101,7 @@ server; each server has its own.
| `PrivateKey`, `[BakaShrot] Node` | `ca`, `certificate`, `key`: the node is the certificate's name |
| `[BakaShrot.Client.*]`: keys, `Node`, `Allow*` | `[roles.*]`, the node and roles from the certificate |
| `[BakaShrot.Peer.*]`: `Host`, `Port`, `Node`, `PublicKey`, topic and node lists | `[replication] push_to` and `accept_from` (addresses), the lists in the peer's roles |
| `ReplicatingTopics` | each peer role's `send_topics` / `accept_topics`; `replicated_topics` filters them all |
| `ReplicatingTopics` | each peer role's `send_topics` / `accept_topics` |
| topic and node ids, pins, ranges, `[BakaShrot.Nodes]` | names only: ids are the database's |
| `DefaultTopic` | dropped: a push always names its topic |
| `Host` + `Port` | `listen` |
@@ -108,7 +114,7 @@ server; each server has its own.
`crates/bakashrot-config/tests/config.rs`: the example, the defaults,
every setting, retention's order, permissions (several roles, none known,
replication per role), `replicated_topics`, paths from the config's
replication per role), the topic lists that are gone, paths from the config's
folder, and the refusals, each with its line: TOML errors, unknown keys
(the old ini's too), every value that's wrong, names twice (without case),
unquoted dotted names.
+92
View File
@@ -50,6 +50,98 @@ network**, not exposed to the internet. So:
between committing and answering) is stored twice if retried. A client
that minds reads the latest message before retrying.
## Found by reading the code, not reproduced
From a read of the source on 2026-11-15: no test shows any of these yet.
Reproduce one before fixing it, and move it up (or drop it) once known.
- **The server's writes have no timeout.** The 5 min of silence only
covers reads (`connection.rs`, `serve`): a client that stops reading
in the middle of an answer (an attachment's parts, `get_attachment`)
holds its task and its read connection's turn for as long as TCP
keeps the connection. Fix: a timeout around `send`, as `PART_TIMEOUT`
does for reads.
- **`sync_interval` over 5 min.** The server dialed closes the idle link
(`IDLE_TIMEOUT`), so every check-in fails once ("closed the link",
warned), then the link is dialed again after `retry_delay`. Nothing
in the config caps `sync_interval`. Fix: cap it under the idle
timeout, or have an idle link ask its `SyncState` more often.
- **Resuming a big replicated attachment.** Before answering `Continue`,
the receiver reads back and hashes every part it already has
(`accept_from.rs`, `replicate_attachment`, `hash_parts`), while the
sender waits 30 s for that answer (`push_to.rs`, `ANSWER_TIMEOUT`). On
a slow disk, hundreds of parts may take longer: the link drops, and
the same happens at every retry. Fix: a longer wait for that answer,
or the hash's state kept with the attachment.
- **Two peers sending the same attachment at once.** Both can be
answered `Continue` for the same `from_part` (the check and the first
part written aren't one step). Their parts then interleave in the
writer's queue: each part is stored once, from whichever came first,
and each sender has one refused (`InvalidRequest`). With few parts (4
or fewer in flight, `PARTS_IN_FLIGHT`), all of them can be stored
while both senders were refused: neither sends `FinishReplicated`,
and the attachment stays `replicating` with `received = size`, asked
for and refused at every check-in until a restart (`clean_up` starts
it over). Needs two parts at least and a narrow timing: servers that
take from two others having the same attachment. Fix: finish it
whenever the last part is stored, or one receiver at a time per
attachment.
- **`SyncState` in one packet** (old item 9). Every head the peer may
send goes in one frame of 16 MiB at most: with very many topic and
node pairs (hundreds of thousands) it doesn't fit, the packet can't
be sent and replication stops. Fix: heads in several packets, or only
those that changed.
- **Peers with several roles.** `SyncState` lists the union of its
roles' `accept_topics` and the union of their `accept_nodes`
(`Permissions::accepted`), while what's taken is each role on its
own: the sender can offer a topic of one role with a node of another,
refused (`NotAllowed`), and sent again, a whole batch, at every
check-in. Fix: the pairs per role in `SyncState`, or the sender
remembering what was refused.
- **A peer can use up a topic and node's ids.** A replicated message
may have any id up to the highest (2^43 - 1, `MAX_MESSAGE_ID`), and
the stream's last id follows it (`batch.rs`, `replicate`). One such
message from a server in `accept_from`, for any topic and node its
roles accept, and every later push to that topic by that node is
refused ("no message id left", `insert_pushed`); it replicates on to
the other servers. Nothing lowers a last id again. Part of "Peers
are trusted" above, but one message is enough and a bug can do it.
Fix: refuse a batch that doesn't follow the last id here (also closes
the gaps), or one that jumps further than some bound.
- **A pair refused for good is sent again at every check-in**, a whole
batch each time (`push_to.rs`, `send_messages`: a refusal only
leaves it for the next check-in): the database's topic and node
pairs all used, a message id past the last one. Fix: with "Peers
with several roles" above.
- **Patterns in `push_topics` let clients make topics.** The roles are
the only lists of topics since 2026-11-16 (`config.md`): a client
with `push_topics = ["*"]` makes as many topic and node pairs as it
likes, up to the database's 2^20 - 1, after which no new pair is
taken from anyone. Listing topics also only shows those with
messages: a client can't ask what it may push to. Fine for the
threat model; names in `push_topics` where the set is closed.
- **Every client sees every node's name.** `ListNames` for nodes
answers every node with messages (`list_names`, `reader.nodes()`),
whatever the client's roles read, to push-only clients too. Topics
are filtered by the roles. Fix: only the nodes of the topics it may read, or none
without `read_topics` or `query`.
- **A CRL file that isn't there.** The server takes it as nothing
revoked (`load_revocations`, `bakashrot-server/src/lib.rs`), the
`bakashrot` tool refuses to start when `client.toml`'s `crl` names a
missing file. Either may be right (a server that starts before its
first CRL; a client that must not skip the check): choose one and
say it in the docs. A server whose CRL file was deleted by mistake
revokes nothing, silently.
- **No limits per client.** Any number of connections (each a task and
its buffers), any amount pushed: an allowed client can fill the disk
or the server's memory. Fine for the threat model; first thing to
add if it changes (a cap on connections, a quota per node).
- **Small ones:** the client's connect timeout is per socket
operation, not for the whole connection; `bakashrot-cert ca` writes
`ca.crt` before failing when only `ca.key` is there already; a
server whose clock is before 1970 stamps `pushed_at` 0, which
readers refuse when decoding.
## Ideas for later
From the old benchmarks and suggestions (`OldSource/Design/Suggestions.md`),
+4 -5
View File
@@ -50,7 +50,6 @@ certificate = "server-1.crt"
key = "server-1.key"
crl = "ca.crl" # optional; a missing file is fine, it's watched
database = "bakashrot.sqlite"
topics = ["lab.temps", "lab.events"]
[retention]
"*" = "7d"
@@ -71,8 +70,8 @@ send_topics = ["lab.*"]
accept_topics = ["lab.*"]
```
- `topics` are names, not patterns (`"lab.*"` there is refused, with its
line); the roles take patterns.
- The roles are the only lists of topics, names or patterns: a topic
exists from its first message.
- Every setting, with its default: `server.example.toml` (`config.md`).
- Open the port: `New-NetFirewallRule -DisplayName BakaShrot -Direction
Inbound -Protocol TCP -LocalPort 7000 -Action Allow` on Windows, the
@@ -139,11 +138,11 @@ Saved 6 bytes as got.txt
{"columns":["topic","node","message_id","c"],"rows":[["lab.events","kitchen","1",null],["lab.temps","kitchen","1","21.5"]]}
```
**Refusals.** A topic not in the config:
**Refusals.** A topic none of its roles names:
```
> bakashrot push other "{}"
Refused by the server (UnknownName): unknown topic 'other'
Refused by the server (NotAllowed): kitchen may not push to 'other'
```
A certificate whose roles aren't in the config
+3 -2
View File
@@ -69,8 +69,9 @@ format, its values are checked all the same.)
- **Topics and nodes are names**, everywhere: 1-64 of `A-Z a-z 0-9 _ . -`,
not only digits, compared without case (`names`). A name that isn't one
is malformed (the client libraries check before sending); one the server
doesn't have is refused (`UnknownName`). Ids are each server's own, in
is malformed (the client libraries check before sending); a node the
server has no message of is refused (`UnknownName`), a topic is only
ever allowed or not (the roles). Ids are each server's own, in
its database, never on the wire.
- **Patterns** (in a `SyncState`): names with `*` (any characters, none
included) and `?` (one).
+16 -13
View File
@@ -118,8 +118,8 @@ otherwise:
one (topics and nodes, patterns included), its last id in each topic
and node, and the attachments it lacks (256 at most).
2. **The messages:** for each topic and node here that it takes and that
its roles here may be sent (`send_topics`, `send_nodes`,
`replicated_topics`), and where it's behind, the messages after its
its roles here may be sent (`send_topics`, `send_nodes`), and where
it's behind, the messages after its
last id, `batch_size` (1000) at a time and about 1 MiB at most (a
bigger one alone), each batch answered before the next. A refusal
leaves that topic and node for the next check-in.
@@ -132,7 +132,8 @@ otherwise:
**Receiving** (`accept_from.rs`): replication requests are answered only
if the connection comes from `accept_from` (a hostname looked up at its
first request, `NotAllowed` otherwise), and only for what its roles'
`accept_topics` / `accept_nodes` allow (`NotAllowed`). Messages keep
`accept_topics` / `accept_nodes` allow (`NotAllowed`); a topic nobody
pushed here is there from then on, like any other. Messages keep
their ids and `pushed_at`; ones already here are kept as they are; one
past this server's age is only counted. An attachment comes after its
message (`replicating`, `NotReady` to readers), from the part this server
@@ -172,8 +173,9 @@ the same ids with different messages, undetected.
## Requests so far
- **`PushMessage`:** the topic must be one of the config's (`UnknownName`),
the roles must allow pushing to it (`NotAllowed`), the message at most
- **`PushMessage`:** the roles must allow pushing to the topic
(`NotAllowed`; there's no other list of topics, `config.md`), the
message at most
`MAX_MESSAGE_SIZE` (`TooBig`); stored under the client's node, stamped
with the server's time; answered `MessagePushed` once committed.
- **With an attachment:** also at most the config's `max_attachment_size`
@@ -187,19 +189,20 @@ the same ids with different messages, undetected.
connection that closes, or sends anything else than the next part, is
dropped. Not pushed, the upload goes (`storage.md`, "Attachments").
- **`GetAttachment`:** the roles must allow reading the topic
(`NotAllowed`), the topic must be one of the config's (`UnknownName`).
A message without one, or none of that id: `NotFound`; one still on its
(`NotAllowed`). A message without one, or none of that id (or of that
topic): `NotFound`; one still on its
way from a peer: `NotReady`; one this server doesn't keep: `TooBig`.
Then `Attachment` (its size, its SHA-256) and the parts from
`from_part`. A part that can't be read closes the connection.
- **`GetMessages`:** the roles must allow reading the topic
(`NotAllowed`), the topic must be one of the config's and the node one
with messages in any topic, or the client's own (`UnknownName`); a known
topic and node without messages answer none. At most `limit` messages and about 1 MB
(`NotAllowed`), the node must be one with messages in any topic, or
the client's own (`UnknownName`); a topic and node without messages
answer none. At most `limit` messages and about 1 MB
(`MESSAGES_BYTES`, a bigger message alone), `more` if others follow.
- **`ListNames`:** topics are the config's, those the roles may push to or
read (every one with `query`, which reads them all anyway); nodes are
every one with messages. `truncated` past 65,536.
- **`ListNames`:** topics are those with messages (pushed here or
replicated), those the roles may push to or read (every one with
`query`, which reads them all anyway); nodes are every one with
messages. `truncated` past 65,536.
- **`Query`:** the roles must allow it (`NotAllowed`); read-only SQL,
refused with SQLite's message (`QueryFailed`, with a hint to add
`is_json = 1` on "malformed JSON"), over 10,000 rows or 16 MB
+6 -2
View File
@@ -109,7 +109,7 @@ read while the writer writes. Answers are the protocol's own types:
A topic and node with no messages read as none, unless the node has none
in any topic: `Error::UnknownName`, as the old server refused unknown
nodes. Whether a topic is known is the server's (the config's topics).
nodes. A topic is never unknown: it's allowed or not, by the roles.
## Attachments
@@ -140,7 +140,11 @@ afterwards, `purge_attachments(max_parts)` a batch at a time.
Clients' `Query` runs on `Database::query_runner()`, read-only three ways:
the connection is `query_only`, the statement must be one SQLite says only
reads, and one statement only (more is refused, not ignored). The result is
reads, and one statement only (more is refused, not ignored). PRAGMAs are
refused as they're prepared ("not authorized", SQLite's authorizer), the
`pragma_*` table functions too: some take effect at that point, and
`PRAGMA query_only = 0`, though refused, turned `query_only` off for the
connection's next queries (found 2026-11-15). The result is
`{"columns":[...],"rows":[[...]]}`, every value a string or null (blobs in
hex), 10,000 rows and 16 MB at most (`TooBig`). A failing row fails it all,
with SQLite's message: never a partial result. "malformed JSON" (a JSON