solidb 1.2.1

A lightweight, high-performance structured database server written in Rust.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
//! Per-database authorization middleware.
//!
//! `auth_middleware` (authentication) runs first and inserts `Claims` into
//! the request extensions; this layer then enforces that the principal is
//! actually allowed to touch the database named in the path. Routes without
//! a `{db}` path parameter pass through untouched (global endpoints do their
//! own `check_permission` calls in the handlers).
//!
//! Rollout switch: `SOLIDB_DB_AUTHZ_MODE=warn` logs would-be denials on the
//! `audit` target and lets requests through, so existing deployments can do
//! a dry-run release before flipping to the default `enforce`.

use axum::body::Body;
use axum::extract::{RawPathParams, State};
use axum::http::{Method, Request};
use axum::middleware::Next;
use axum::response::Response;

use crate::error::{DbError, DbResult};
use crate::server::auth::Claims;
use crate::server::authorization::{AuthorizationService, PermissionAction};
use crate::server::handlers::AppState;

/// How authorization failures are handled.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum AuthzMode {
    Enforce,
    Warn,
}

fn authz_mode() -> AuthzMode {
    static MODE: std::sync::OnceLock<AuthzMode> = std::sync::OnceLock::new();
    *MODE.get_or_init(|| match std::env::var("SOLIDB_DB_AUTHZ_MODE").as_deref() {
        Ok("warn")
            if std::env::var("SOLIDB_DB_AUTHZ_ALLOW_WARN")
                .map(|v| v == "1" || v.eq_ignore_ascii_case("true"))
                .unwrap_or(false) =>
        {
            tracing::warn!(
                "SOLIDB_DB_AUTHZ_MODE=warn: per-database authorization failures are \
                     logged but NOT enforced"
            );
            AuthzMode::Warn
        }
        Ok("warn") => {
            tracing::error!(
                "SOLIDB_DB_AUTHZ_MODE=warn is ignored without SOLIDB_DB_AUTHZ_ALLOW_WARN=1; \
                 enforcing authorization"
            );
            AuthzMode::Enforce
        }
        _ => AuthzMode::Enforce,
    })
}

/// Check `action` on `database` for `claims`, honoring the rollout mode.
/// Shared by this middleware and the handler-level checks (cursor
/// continuation, offline sync, mutating-query upgrade, WebSocket subscribe).
pub async fn enforce(
    claims: &Claims,
    state: &AppState,
    action: PermissionAction,
    database: Option<&str>,
) -> DbResult<()> {
    match AuthorizationService::check_permission(claims, state, action.clone(), database).await {
        Ok(()) => Ok(()),
        Err(e) => match authz_mode() {
            AuthzMode::Enforce => Err(e),
            AuthzMode::Warn => {
                tracing::warn!(
                    target: "audit",
                    user = %claims.sub,
                    database = database.unwrap_or("<global>"),
                    action = ?action,
                    "authz dry-run: request would be denied ({})",
                    e
                );
                Ok(())
            }
        },
    }
}

/// Synchronous variant of [`enforce`] for call sites that already resolved
/// the principal's permission set (e.g. per-item filtering loops). Returns
/// `true` if the action is allowed under the current rollout mode.
pub fn enforce_raw(
    permissions: &std::collections::HashSet<crate::server::authorization::Permission>,
    action: PermissionAction,
    database: Option<&str>,
    scoped_databases: Option<&[String]>,
    subject: &str,
) -> bool {
    match AuthorizationService::check_permission_raw(
        permissions,
        action.clone(),
        database,
        scoped_databases,
    ) {
        Ok(()) => true,
        Err(e) => match authz_mode() {
            AuthzMode::Enforce => false,
            AuthzMode::Warn => {
                tracing::warn!(
                    target: "audit",
                    user = subject,
                    database = database.unwrap_or("<global>"),
                    action = ?action,
                    "authz dry-run: request would be denied ({})",
                    e
                );
                true
            }
        },
    }
}

/// Map an HTTP request on a `{db}`-scoped route to the permission it needs.
///
/// **`path` must be the matched route *template*** (`/_api/database/{db}/
/// document/{collection}/{key}`), never the concrete request path. Every rule
/// below keys off path structure, and on a concrete path the last segment is
/// usually caller-supplied — a document key, a collection name, an index name.
/// Classifying `PUT /_api/database/app/document/settings/query` by its
/// concrete path matched the `/query` read-suffix and let a read-only
/// principal overwrite the document named `query`; the same trick reached
/// `DELETE .../document/c/search` and `DELETE .../index/users/cursor`.
/// Templates contain only literals and `{param}` placeholders, so no request
/// input can reach these comparisons.
///
/// Defaults: GET/HEAD/OPTIONS → Read, everything else → Write. Overrides:
/// - POST endpoints with read semantics (query execution, explain, search)
///   only need Read; the query handlers upgrade to Write themselves when the
///   parsed query contains mutating clauses.
/// - Irreversible bulk destruction (truncate, drop collection) and the Lua
///   REPL (arbitrary code execution) require Admin.
fn required_action(method: &Method, path: &str) -> PermissionAction {
    // Admin overrides
    if path.ends_with("/truncate") {
        return PermissionAction::Admin;
    }
    if path.ends_with("/repl") {
        return PermissionAction::Admin;
    }
    if is_script_or_service_mutation(method, path) {
        return PermissionAction::Admin;
    }
    if *method == Method::DELETE && is_collection_drop(path) {
        return PermissionAction::Admin;
    }

    if matches!(*method, Method::GET | Method::HEAD | Method::OPTIONS) {
        return PermissionAction::Read;
    }

    // Read-semantics POST/PUT endpoints
    const READ_SUFFIXES: [&str; 9] = [
        "/cursor",
        "/explain",
        "/nl",
        "/near",
        "/within",
        "/search",
        "/aggregate",
        "/sql",
        "/_verify",
    ];
    if READ_SUFFIXES.iter().any(|s| path.ends_with(s)) {
        return PermissionAction::Read;
    }
    // Columnar read query: /_api/database/{db}/columnar/{collection}/query
    // and transactional query: .../transaction/{tx_id}/query
    if path.ends_with("/query") {
        return PermissionAction::Read;
    }

    PermissionAction::Write
}

/// `DELETE /_api/database/{db}/collection/{name}` and
/// `DELETE /_api/database/{db}/columnar/{collection}` drop a whole
/// collection; deeper paths (documents, indexes, schema, ...) do not.
/// Creating or changing Lua services/scripts is equivalent to installing
/// server-side code; listing them stays a Read.
fn is_script_or_service_mutation(method: &Method, path: &str) -> bool {
    if matches!(*method, Method::GET | Method::HEAD | Method::OPTIONS) {
        return false;
    }
    let Some(rest) = path
        .strip_prefix("/_api/database/")
        .and_then(|r| r.split_once('/'))
        .map(|(_, rest)| rest)
    else {
        return false;
    };
    rest == "scripts"
        || rest.starts_with("scripts/")
        || rest == "services"
        || rest.starts_with("services/")
}

fn is_collection_drop(path: &str) -> bool {
    let Some(rest) = path
        .strip_prefix("/_api/database/")
        .and_then(|r| r.split_once('/'))
        .map(|(_, rest)| rest)
    else {
        return false;
    };
    match rest.split_once('/') {
        Some(("collection", tail)) | Some(("columnar", tail)) => !tail.contains('/'),
        _ => false,
    }
}

/// Permission from the HTTP method alone: GET/HEAD/OPTIONS read, everything
/// else writes.
///
/// The fallback when the route template is unavailable. It deliberately keeps
/// none of the read-suffix overrides: those may only *downgrade* a request to
/// Read, and downgrading on evidence we could not verify is exactly the bug
/// this module now guards against.
fn method_default_action(method: &Method) -> PermissionAction {
    if matches!(*method, Method::GET | Method::HEAD | Method::OPTIONS) {
        PermissionAction::Read
    } else {
        PermissionAction::Write
    }
}

/// Axum middleware enforcing per-database permissions on routes that carry a
/// `{db}` path parameter. Must run *after* `auth_middleware`.
pub async fn db_authz_middleware(
    State(state): State<AppState>,
    params: RawPathParams,
    req: Request<Body>,
    next: Next,
) -> Result<Response, DbError> {
    let db = params
        .iter()
        .find(|(k, _)| *k == "db")
        .map(|(_, v)| v.to_string());

    // Global routes (no {db} param) do their own permission checks.
    let Some(db) = db else {
        return Ok(next.run(req).await);
    };

    let claims = req
        .extensions()
        .get::<Claims>()
        .cloned()
        .ok_or_else(|| DbError::Forbidden("Missing authentication context".to_string()))?;

    // Classify on the matched route template, not the concrete path: the last
    // segment of a concrete path is caller-supplied on most routes, and the
    // read-suffix overrides below would otherwise be selected by a document
    // key or collection name (a key literally named `query` downgraded a PUT
    // to Read). `MatchedPath` is set by the router before any layer runs, so
    // the fallback is unreachable in practice; it degrades to method-only
    // defaults rather than trusting the URI.
    let action = match req.extensions().get::<axum::extract::MatchedPath>() {
        Some(matched) => required_action(req.method(), matched.as_str()),
        None => {
            tracing::warn!(
                target: "audit",
                path = %req.uri().path(),
                "authz: no matched route template; falling back to method defaults"
            );
            method_default_action(req.method())
        }
    };
    enforce(&claims, &state, action, Some(&db)).await?;

    Ok(next.run(req).await)
}

#[cfg(test)]
mod tests {
    use super::*;

    // `required_action` is fed the matched route *template*, so every path in
    // these tests is written the way the router reports it.

    #[test]
    fn test_required_action_defaults() {
        let get = Method::GET;
        let post = Method::POST;
        let put = Method::PUT;
        let delete = Method::DELETE;

        assert_eq!(
            required_action(&get, "/_api/database/{db}/document/{collection}/{key}"),
            PermissionAction::Read
        );
        assert_eq!(
            required_action(&post, "/_api/database/{db}/document/{collection}"),
            PermissionAction::Write
        );
        assert_eq!(
            required_action(&delete, "/_api/database/{db}/document/{collection}/{key}"),
            PermissionAction::Write
        );
        assert_eq!(
            required_action(&put, "/_api/database/{db}/collection/{name}/properties"),
            PermissionAction::Write
        );
    }

    #[test]
    fn test_required_action_read_overrides() {
        let post = Method::POST;
        for path in [
            "/_api/database/{db}/cursor",
            "/_api/database/{db}/explain",
            "/_api/database/{db}/nl",
            "/_api/database/{db}/geo/{collection}/{field}/near",
            "/_api/database/{db}/geo/{collection}/{field}/within",
            "/_api/database/{db}/vector/{collection}/{index}/search",
            "/_api/database/{db}/hybrid/{collection}/search",
            "/_api/database/{db}/columnar/{collection}/aggregate",
            "/_api/database/{db}/columnar/{collection}/query",
            "/_api/database/{db}/transaction/{tx_id}/query",
            "/_api/database/{db}/sql",
            "/_api/database/{db}/document/{collection}/_verify",
        ] {
            assert_eq!(
                required_action(&post, path),
                PermissionAction::Read,
                "expected Read for {}",
                path
            );
        }
    }

    /// A document key, collection name or index name is the last segment of
    /// many mutating routes. Classifying on the concrete path let a key named
    /// after a read suffix downgrade the request: `PUT .../document/settings/
    /// query` matched `/query` and a read-only principal overwrote it.
    /// Templates have no such segment, so the same requests classify as Write.
    #[test]
    fn test_read_suffix_cannot_be_forged_by_a_document_key() {
        for (method, template) in [
            (
                Method::PUT,
                "/_api/database/{db}/document/{collection}/{key}",
            ),
            (
                Method::DELETE,
                "/_api/database/{db}/document/{collection}/{key}",
            ),
            (Method::POST, "/_api/database/{db}/document/{collection}"),
            (
                Method::DELETE,
                "/_api/database/{db}/index/{collection}/{index_name}",
            ),
        ] {
            assert_eq!(
                required_action(&method, template),
                PermissionAction::Write,
                "{} {} must stay a write",
                method,
                template
            );
        }
    }

    /// The fallback used when the router did not record a template never
    /// downgrades to Read on anything but a genuinely safe method.
    #[test]
    fn test_method_default_action_never_downgrades_writes() {
        assert_eq!(method_default_action(&Method::GET), PermissionAction::Read);
        assert_eq!(method_default_action(&Method::HEAD), PermissionAction::Read);
        for method in [Method::POST, Method::PUT, Method::PATCH, Method::DELETE] {
            assert_eq!(
                method_default_action(&method),
                PermissionAction::Write,
                "{} must default to Write",
                method
            );
        }
    }

    #[test]
    fn test_required_action_admin_overrides() {
        let put = Method::PUT;
        let post = Method::POST;
        let delete = Method::DELETE;

        assert_eq!(
            required_action(&put, "/_api/database/{db}/collection/{name}/truncate"),
            PermissionAction::Admin
        );
        assert_eq!(
            required_action(&delete, "/_api/database/{db}/collection/{name}"),
            PermissionAction::Admin
        );
        assert_eq!(
            required_action(&delete, "/_api/database/{db}/columnar/{collection}"),
            PermissionAction::Admin
        );
        assert_eq!(
            required_action(&post, "/_api/database/{db}/repl"),
            PermissionAction::Admin
        );
        assert_eq!(
            required_action(&post, "/_api/database/{db}/scripts"),
            PermissionAction::Admin
        );
        assert_eq!(
            required_action(&put, "/_api/database/{db}/services/{key}"),
            PermissionAction::Admin
        );
        assert_eq!(
            required_action(&delete, "/_api/database/{db}/scripts/{script_id}"),
            PermissionAction::Admin
        );

        // Deeper DELETE paths are plain writes, not drops
        assert_eq!(
            required_action(&delete, "/_api/database/{db}/collection/{name}/schema"),
            PermissionAction::Write
        );
        assert_eq!(
            required_action(
                &delete,
                "/_api/database/{db}/columnar/{collection}/index/{column}"
            ),
            PermissionAction::Write
        );
    }
}