Skip to main content

uni_common/
config.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2024-2026 Dragonscale Team
3
4use std::path::{Path, PathBuf};
5use std::thread;
6use std::time::Duration;
7
8#[derive(Clone, Debug)]
9pub struct CompactionConfig {
10    /// Enable background compaction (default: true)
11    pub enabled: bool,
12
13    /// Max uncompacted flush generations before triggering compaction (default: 8)
14    pub max_l1_runs: usize,
15
16    /// Max L1 size in bytes before compaction (default: 256MB)
17    pub max_l1_size_bytes: u64,
18
19    /// Max age of oldest L1 run before compaction (default: 1 hour)
20    pub max_l1_age: Duration,
21
22    /// Background check interval (default: 10s)
23    pub check_interval: Duration,
24
25    /// Number of compaction worker threads (default: 1)
26    pub worker_threads: usize,
27
28    /// Number of frozen L0-csr overlay segments that must accumulate before
29    /// `AdjacencyManager::compact` is spawned post-flush (default: 2).
30    ///
31    /// Each frozen segment adds per-read overhead until merged back into the
32    /// Main CSR. Lowering this triggers compaction sooner; higher values
33    /// batch more segments per compaction at the cost of slower reads while
34    /// they accumulate. The default of 2 keeps the read-side overhead
35    /// bounded across a wide range of write rates. See issue #55.
36    pub frozen_segments_compact_threshold: usize,
37}
38
39impl Default for CompactionConfig {
40    fn default() -> Self {
41        Self {
42            enabled: true,
43            max_l1_runs: 8,
44            max_l1_size_bytes: 256 * 1024 * 1024,
45            max_l1_age: Duration::from_secs(3600),
46            check_interval: Duration::from_secs(10),
47            worker_threads: 1,
48            frozen_segments_compact_threshold: 2,
49        }
50    }
51}
52
53/// Configuration for background index rebuilding.
54#[derive(Clone, Debug)]
55pub struct IndexRebuildConfig {
56    /// Maximum number of retry attempts for failed index builds (default: 3).
57    pub max_retries: u32,
58
59    /// Delay between retry attempts (default: 60s).
60    pub retry_delay: Duration,
61
62    /// How often to check for pending index rebuild tasks (default: 5s).
63    pub worker_check_interval: Duration,
64
65    /// Row growth ratio to trigger rebuild (default: 0.5 = 50%). Set 0.0 to disable.
66    pub growth_trigger_ratio: f64,
67
68    /// Max index age before rebuild. `None` disables the time-based trigger.
69    pub max_index_age: Option<Duration>,
70
71    /// Enable post-flush automatic rebuild scheduling (default: false).
72    pub auto_rebuild_enabled: bool,
73}
74
75impl Default for IndexRebuildConfig {
76    fn default() -> Self {
77        Self {
78            max_retries: 3,
79            retry_delay: Duration::from_secs(60),
80            worker_check_interval: Duration::from_secs(5),
81            growth_trigger_ratio: 0.5,
82            max_index_age: None,
83            auto_rebuild_enabled: false,
84        }
85    }
86}
87
88#[derive(Clone, Copy, Debug)]
89pub struct WriteThrottleConfig {
90    /// Uncompacted flush generations to start throttling (default: 16)
91    pub soft_limit: usize,
92
93    /// Uncompacted flush generations to stop writes entirely (default: 32)
94    pub hard_limit: usize,
95
96    /// Base delay when throttling (default: 10ms)
97    pub base_delay: Duration,
98}
99
100impl Default for WriteThrottleConfig {
101    fn default() -> Self {
102        Self {
103            soft_limit: 16,
104            hard_limit: 32,
105            base_delay: Duration::from_millis(10),
106        }
107    }
108}
109
110#[derive(Clone, Debug)]
111pub struct ObjectStoreConfig {
112    pub connect_timeout: Duration,
113    pub read_timeout: Duration,
114    pub write_timeout: Duration,
115    pub max_retries: u32,
116    pub retry_backoff_base: Duration,
117    pub retry_backoff_max: Duration,
118}
119
120impl Default for ObjectStoreConfig {
121    fn default() -> Self {
122        Self {
123            connect_timeout: Duration::from_secs(10),
124            read_timeout: Duration::from_secs(30),
125            write_timeout: Duration::from_secs(60),
126            max_retries: 3,
127            retry_backoff_base: Duration::from_millis(100),
128            retry_backoff_max: Duration::from_secs(10),
129        }
130    }
131}
132
133/// Security configuration for file system operations.
134/// Controls which paths can be accessed by BACKUP, COPY, and EXPORT commands.
135///
136/// Disabled by default for backward compatibility in embedded mode.
137/// MUST be enabled for server mode with untrusted clients.
138#[derive(Clone, Debug, Default)]
139pub struct FileSandboxConfig {
140    /// If true, file operations are restricted to allowed_paths.
141    /// If false, all paths are allowed (NOT RECOMMENDED for server mode).
142    pub enabled: bool,
143
144    /// List of allowed base directories for file operations.
145    /// Paths must be absolute and canonical.
146    /// File operations are only allowed within these directories.
147    pub allowed_paths: Vec<PathBuf>,
148}
149
150/// Deployment mode for the database.
151///
152/// Used to determine appropriate security defaults.
153#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
154pub enum DeploymentMode {
155    /// Embedded/library mode where the host application controls access.
156    /// File sandbox is disabled by default for backward compatibility.
157    #[default]
158    Embedded,
159    /// Server mode with untrusted clients.
160    /// File sandbox is enabled by default with restricted paths.
161    Server,
162}
163
164/// HTTP server configuration.
165///
166/// Controls CORS, authentication, and other HTTP-related security settings.
167///
168/// # Security
169///
170/// **CWE-942 (Overly Permissive CORS)**, **CWE-306 (Missing Authentication)**:
171/// Production deployments should configure explicit `allowed_origins` and
172/// enable API key authentication.
173#[derive(Clone, Debug)]
174pub struct ServerConfig {
175    /// Allowed CORS origins.
176    ///
177    /// - Empty vector: No CORS headers (most restrictive)
178    /// - `["*"]`: Allow all origins (NOT RECOMMENDED for production)
179    /// - Explicit list: Only allow specified origins (RECOMMENDED)
180    ///
181    /// # Security
182    ///
183    /// **CWE-942**: Using `["*"]` allows any website to make requests to
184    /// your server, potentially exposing sensitive data.
185    pub allowed_origins: Vec<String>,
186
187    /// Optional API key for request authentication.
188    ///
189    /// When set, all API requests must include the header:
190    /// `X-API-Key: <key>`
191    ///
192    /// # Security
193    ///
194    /// **CWE-306**: Without authentication, any client can execute queries.
195    /// Enable this for any deployment accessible beyond localhost.
196    pub api_key: Option<String>,
197
198    /// Whether to require API key for metrics endpoint.
199    ///
200    /// Default: false (metrics are public for observability tooling)
201    pub require_auth_for_metrics: bool,
202}
203
204impl Default for ServerConfig {
205    fn default() -> Self {
206        Self {
207            // Default to localhost-only origin for development safety
208            allowed_origins: vec!["http://localhost:3000".to_string()],
209            api_key: None,
210            require_auth_for_metrics: false,
211        }
212    }
213}
214
215impl ServerConfig {
216    /// Create a permissive config for local development only.
217    ///
218    /// # Security
219    ///
220    /// **WARNING**: Do not use in production. This config allows all CORS origins
221    /// and has no authentication.
222    #[must_use]
223    pub fn development() -> Self {
224        Self {
225            allowed_origins: vec!["*".to_string()],
226            api_key: None,
227            require_auth_for_metrics: false,
228        }
229    }
230
231    /// Create a production config with explicit origins and required API key.
232    ///
233    /// # Panics
234    ///
235    /// Panics if `api_key` is empty.
236    #[must_use]
237    pub fn production(allowed_origins: Vec<String>, api_key: String) -> Self {
238        assert!(
239            !api_key.is_empty(),
240            "API key must not be empty for production"
241        );
242        Self {
243            allowed_origins,
244            api_key: Some(api_key),
245            require_auth_for_metrics: true,
246        }
247    }
248
249    /// Returns a security warning if the config is insecure.
250    pub fn security_warning(&self) -> Option<&'static str> {
251        if self.allowed_origins.contains(&"*".to_string()) && self.api_key.is_none() {
252            Some(
253                "Server config has permissive CORS (allow all origins) and no API key. \
254                 This is insecure for production deployments.",
255            )
256        } else if self.allowed_origins.contains(&"*".to_string()) {
257            Some(
258                "Server config has permissive CORS (allow all origins). \
259                 Consider restricting to specific origins for production.",
260            )
261        } else if self.api_key.is_none() {
262            Some(
263                "Server config has no API key authentication. \
264                 Enable api_key for production deployments.",
265            )
266        } else {
267            None
268        }
269    }
270}
271
272impl FileSandboxConfig {
273    /// Creates a sandboxed config that only allows operations in the specified directories.
274    pub fn sandboxed(paths: Vec<PathBuf>) -> Self {
275        Self {
276            enabled: true,
277            allowed_paths: paths,
278        }
279    }
280
281    /// Creates a config with appropriate defaults for the deployment mode.
282    ///
283    /// # Security
284    ///
285    /// - **Embedded mode**: Sandbox disabled (host application controls access)
286    /// - **Server mode**: Sandbox enabled with default paths `/var/lib/uni/data` and
287    ///   `/var/lib/uni/backups`
288    ///
289    /// **CWE-22 (Path Traversal)**: Server deployments MUST enable the sandbox to
290    /// prevent arbitrary file read/write via BACKUP, COPY, and EXPORT commands.
291    pub fn default_for_mode(mode: DeploymentMode) -> Self {
292        match mode {
293            DeploymentMode::Embedded => Self {
294                enabled: false,
295                allowed_paths: vec![],
296            },
297            DeploymentMode::Server => Self {
298                enabled: true,
299                allowed_paths: vec![
300                    PathBuf::from("/var/lib/uni/data"),
301                    PathBuf::from("/var/lib/uni/backups"),
302                ],
303            },
304        }
305    }
306
307    /// Returns a security warning message if the sandbox is disabled.
308    ///
309    /// Call this at startup to alert administrators about potential security risks.
310    /// Returns `Some(message)` if a warning should be displayed, `None` otherwise.
311    ///
312    /// # Security
313    ///
314    /// **CWE-22 (Path Traversal)**, **CWE-73 (External Control of File Name)**:
315    /// Disabled sandbox allows unrestricted filesystem access for BACKUP, COPY,
316    /// and EXPORT commands, which can lead to:
317    /// - Arbitrary file read/write in server deployments
318    /// - Data exfiltration to attacker-controlled paths
319    /// - Potential privilege escalation via file overwrites
320    ///
321    /// # Example
322    ///
323    /// ```ignore
324    /// if let Some(warning) = config.file_sandbox.security_warning() {
325    ///     tracing::warn!(target: "uni_db::security", "{}", warning);
326    /// }
327    /// ```
328    pub fn security_warning(&self) -> Option<&'static str> {
329        if !self.enabled {
330            Some(
331                "File sandbox is DISABLED. This allows unrestricted filesystem access \
332                 for BACKUP, COPY, and EXPORT commands. Enable sandbox for server \
333                 deployments: file_sandbox.enabled = true",
334            )
335        } else {
336            None
337        }
338    }
339
340    /// Returns whether the sandbox is in a potentially insecure state.
341    ///
342    /// Returns `true` if the sandbox is disabled or enabled with no allowed paths.
343    pub fn is_potentially_insecure(&self) -> bool {
344        !self.enabled || self.allowed_paths.is_empty()
345    }
346
347    /// Validate that a path is within the allowed sandbox.
348    /// Returns Ok(canonical_path) if allowed, Err if not.
349    pub fn validate_path(&self, path: &str) -> Result<PathBuf, String> {
350        if !self.enabled {
351            // Sandbox disabled - allow all paths
352            return Ok(PathBuf::from(path));
353        }
354
355        if self.allowed_paths.is_empty() {
356            return Err("File sandbox is enabled but no allowed paths configured".to_string());
357        }
358
359        // Resolve the path to canonical form to prevent traversal attacks
360        let input_path = Path::new(path);
361
362        // For paths that don't exist yet (e.g., export destinations), we need to
363        // check their parent directory exists and is within allowed paths
364        let canonical = if input_path.exists() {
365            input_path
366                .canonicalize()
367                .map_err(|e| format!("Failed to canonicalize path: {}", e))?
368        } else {
369            // Path doesn't exist - check parent
370            let parent = input_path
371                .parent()
372                .ok_or_else(|| "Invalid path: no parent directory".to_string())?;
373            if !parent.exists() {
374                return Err(format!(
375                    "Parent directory does not exist: {}",
376                    parent.display()
377                ));
378            }
379            let canonical_parent = parent
380                .canonicalize()
381                .map_err(|e| format!("Failed to canonicalize parent: {}", e))?;
382            // Reconstruct with canonical parent + original filename
383            let filename = input_path
384                .file_name()
385                .ok_or_else(|| "Invalid path: no filename".to_string())?;
386            canonical_parent.join(filename)
387        };
388
389        // Check if the canonical path is within any allowed directory
390        for allowed in &self.allowed_paths {
391            // Ensure allowed path is canonical too
392            let canonical_allowed = if allowed.exists() {
393                allowed.canonicalize().unwrap_or_else(|_| allowed.clone())
394            } else {
395                allowed.clone()
396            };
397
398            if canonical.starts_with(&canonical_allowed) {
399                return Ok(canonical);
400            }
401        }
402
403        Err(format!(
404            "Path '{}' is outside allowed sandbox directories. Allowed: {:?}",
405            path, self.allowed_paths
406        ))
407    }
408}
409
410#[derive(Clone, Debug)]
411pub struct UniConfig {
412    /// Maximum adjacency cache size in bytes (default: 1GB)
413    pub cache_size: usize,
414
415    /// Number of worker threads for parallel execution
416    pub parallelism: usize,
417
418    /// Size of each data morsel/batch (number of rows)
419    pub batch_size: usize,
420
421    /// Maximum size of traversal frontier before pruning
422    pub max_frontier_size: usize,
423
424    /// Auto-flush threshold for L0 buffer (default: 10_000 mutations)
425    pub auto_flush_threshold: usize,
426
427    /// Auto-flush interval for L0 buffer (default: 5 seconds).
428    /// Flush triggers if time elapsed AND mutation count >= auto_flush_min_mutations.
429    /// Set to None to disable time-based flush.
430    pub auto_flush_interval: Option<Duration>,
431
432    /// Minimum mutations required before the time-based flush triggers
433    /// (default: 1).
434    ///
435    /// Prevents unnecessary flushes when activity is minimal. Raising this
436    /// (e.g., to 1000) lets small bursts coalesce into one flush — useful
437    /// for benchmark workloads — but for active databases with high write
438    /// rates, raising it reduces flush frequency and lets the active overlay
439    /// grow larger between flushes, which can hurt read latency. Tune with
440    /// `compaction.frozen_segments_compact_threshold` together. See issue
441    /// #55 for the trade-off discussion.
442    pub auto_flush_min_mutations: usize,
443
444    /// Enable write-ahead logging (default: true)
445    pub wal_enabled: bool,
446
447    /// Compaction configuration
448    pub compaction: CompactionConfig,
449
450    /// Write throttling configuration
451    pub throttle: WriteThrottleConfig,
452
453    /// File sandbox configuration for BACKUP/COPY/EXPORT commands.
454    /// MUST be enabled with allowed paths in server mode to prevent arbitrary file access.
455    pub file_sandbox: FileSandboxConfig,
456
457    /// Default query execution timeout (default: 30s)
458    pub query_timeout: Duration,
459
460    /// Maximum wall time a transaction commit may take before it is aborted with
461    /// `CommitTimeout` (default: 5s). This guards against a commit blocking on the
462    /// writer/flush lock, but it also bounds the commit's own compute time — so
463    /// workloads that commit very large transactions in a single shot (bulk-history
464    /// backfills, or unoptimized debug builds) may need to raise it.
465    pub commit_timeout: Duration,
466
467    /// Default maximum memory per query (default: 1GB)
468    pub max_query_memory: usize,
469
470    /// Maximum transaction buffer memory in bytes (default: 1GB).
471    /// Limits memory usage during transactions to prevent OOM.
472    pub max_transaction_memory: usize,
473
474    /// Maximum rows for in-memory compaction (default: 5M, ~725MB at 145 bytes/row).
475    /// Configurable OOM guard to prevent memory exhaustion during compaction.
476    pub max_compaction_rows: usize,
477
478    /// Enable in-memory VID-to-labels index for O(1) lookups (default: true).
479    /// Memory cost: ~42 bytes per vertex (1M vertices ≈ 42MB).
480    pub enable_vid_labels_index: bool,
481
482    /// Maximum iterations for recursive CTE evaluation (default: 1000).
483    pub max_recursive_cte_iterations: usize,
484
485    /// Object store resilience configuration
486    pub object_store: ObjectStoreConfig,
487
488    /// Background index rebuild configuration
489    pub index_rebuild: IndexRebuildConfig,
490
491    /// When true, reject writes that reference labels or edge types not declared
492    /// in the schema. Default: false (schemaless mode — any label or edge type
493    /// is accepted and dynamically registered).
494    pub strict_schema: bool,
495
496    /// Enable Lance `MergeInsert` for SET-only flushes (default: false).
497    ///
498    /// When true, `Writer::insert_vertex_partial` records the touched
499    /// property keys into L0 and the flush emits a partial-column source
500    /// to Lance via `MergeInsertBuilder` — skipping the read of (and write
501    /// of) the unchanged columns. Wide-row schemas with vector indexes
502    /// benefit most (~17 ms/row → ~3 ms/row on the issue #72 ingest
503    /// workload). See the Round-11 plan section in
504    /// `plan-and-implement-a-valiant-flame.md`.
505    pub partial_lance_writes: bool,
506
507    /// When true, auto-embedding for vertex writes is deferred from the
508    /// per-row `insert_vertex_*` path to the next L1 flush, where the
509    /// existing `process_embeddings_for_batch` issues one model call for
510    /// the whole flush batch instead of N per-row calls.
511    ///
512    /// Trade-off: in-tx reads of the embedding column on a freshly
513    /// SET/inserted vertex see the OLD storage value (or no value, for
514    /// new vertices) until flush. Existing behavior is identical to
515    /// today's `process_embeddings_impl(target_prop present)` short-circuit
516    /// (writer.rs:2727) — updating only the source text never refreshes
517    /// the embedding mid-tx, deferred or not. Opt-in for workloads that
518    /// don't read embeddings between write and commit.
519    ///
520    /// Default: `false` (preserves bit-for-bit compatibility with
521    /// pre-Phase-B releases).
522    pub defer_embeddings: bool,
523
524    /// Per-fork L1 fragment-count threshold above which a `tracing::warn!`
525    /// fires once per crossing during fork flush. Long-lived heavy-write
526    /// forks accumulate fragments because fork compaction is deferred to
527    /// Phase 5; this surfaces the risk operationally. Default: 256.
528    pub fork_fragment_warn_threshold: usize,
529
530    /// Per-transaction VID/EID reservoir refill size. Each `Transaction`
531    /// pre-reserves this many IDs at a time from the global `IdAllocator`,
532    /// amortizing its `tokio::Mutex` over `N` allocations. Tradeoff:
533    /// larger = fewer global-mutex acquisitions but more wasted IDs on
534    /// short transactions (capped at `batch_size - 1` per tx). u64 ID space
535    /// makes the waste negligible. Default: 16.
536    pub tx_id_reservoir_batch: usize,
537
538    /// When `true`, `check_flush` on the commit path dispatches via the
539    /// async path (`flush_to_l1_async`): rotate L0 under `flush_lock`,
540    /// then spawn the streaming + finalize work on a background task.
541    /// Concurrent committers no longer queue on the flush's long I/O.
542    ///
543    /// When `false` (default for now), `check_flush` calls the original
544    /// synchronous `flush_to_l1` and holds `flush_lock` across the full
545    /// L1-streaming write. This is the kill-switch.
546    pub async_flush_enabled: bool,
547
548    /// Maximum number of L0→L1 flushes that may be in-flight simultaneously
549    /// when `async_flush_enabled` is true. The (N+1)th rotate blocks until
550    /// one of the in-flight flushes finalizes. Bounds WAL retention and
551    /// memory growth. Default: 2.
552    pub max_pending_flushes: usize,
553
554    /// Maximum time `drop_fork` will wait for pending async flushes on
555    /// that fork before failing with `PendingFlushTimeout`. Only meaningful
556    /// when `async_flush_enabled` is true. Default: 10s.
557    pub drop_fork_drain_timeout: Duration,
558
559    /// Phase 4a: cap on total fork count (Active + Pending + Tombstoned).
560    /// `None` = unbounded. When set, `Session::fork(name).await` errors
561    /// with `UniError::ForkBudgetExceeded` once the cap is reached.
562    /// Tombstoned forks count because they still hold branch state on
563    /// disk until recovery completes; counting them prevents churn-thrash.
564    pub max_forks: Option<usize>,
565
566    /// Phase 4a: default TTL applied to forks when the user does not
567    /// supply one via `session.fork(name).ttl(...)`. `None` = no TTL.
568    /// The background sweeper drops forks whose `ttl_expires_at` is in
569    /// the past via `drop_fork_cascade`.
570    pub fork_default_ttl: Option<Duration>,
571
572    /// Phase 4a: how often the background TTL sweeper polls the
573    /// registry for expired forks. Default: 60 seconds.
574    pub fork_sweeper_interval: Duration,
575
576    /// Phase 4a: skip spawning the TTL sweeper. Tests should set this
577    /// to `true` when they want deterministic control over fork
578    /// lifetimes; production should leave it `false`.
579    pub disable_fork_sweeper: bool,
580
581    /// Phase 5a: minimum per-fork row count (per label) before the
582    /// background `IndexRebuildManager` schedules a fork-local index
583    /// build. Below this threshold, fork reads inherit primary's
584    /// indexes through Lance `base_paths`; above it, the planner
585    /// switches to `FusedIndexScan` once the build completes. Default
586    /// 10,000 rows per spec §8.
587    pub fork_index_build_threshold: u64,
588
589    /// Phase 5a-impl Step 7: how often the background fork index
590    /// builder polls active forks for build candidates. Default
591    /// 30 seconds.
592    pub fork_index_builder_interval: Duration,
593
594    /// Phase 5a-impl Step 7: skip spawning the background fork index
595    /// builder. Tests that exercise the manual `Session::build_fork_local_index`
596    /// trigger should set this to `true` so timing isn't dependent on
597    /// the polling cadence.
598    pub disable_fork_index_builder: bool,
599
600    /// Enable Serializable Snapshot Isolation and optimistic concurrency
601    /// control (default: `true`).
602    ///
603    /// When `true`, read-write transactions read from a pinned L0 snapshot,
604    /// track an item-level read/write-set, and validate at commit under
605    /// `flush_lock`: a write-write or read-write conflict against a commit
606    /// landed since the transaction's snapshot aborts with
607    /// `UniError::SerializationConflict`, a duplicate concurrent `MERGE` on a
608    /// unique key aborts with `UniError::ConstraintConflict`, and `FOR UPDATE`
609    /// acquires per-key row locks. Callers should wrap contended writes in
610    /// `Session::transact_with_retry`, which re-runs retriable conflicts.
611    ///
612    /// When `false`, the engine reverts to last-writer-wins: concurrent
613    /// read-modify-write transactions can silently lose updates, concurrent
614    /// `MERGE` can create duplicate unique keys, and `FOR UPDATE` is a no-op
615    /// (a `tracing::warn!` is emitted when a query requests it). Reads run
616    /// against the live L0 with no snapshot pinning. This reproduces the
617    /// pre-SSI behavior bit-for-bit and skips the (near-zero, but non-nil)
618    /// read-set/validation overhead — appropriate only for single-writer
619    /// workloads or callers that guard read-modify-write externally.
620    ///
621    /// Defaults to `true` because silent lost updates are a correctness hazard
622    /// for any concurrent-writer workload.
623    pub ssi_enabled: bool,
624}
625
626impl Default for UniConfig {
627    fn default() -> Self {
628        let parallelism = thread::available_parallelism()
629            .map(|n| n.get())
630            .unwrap_or(4);
631
632        Self {
633            cache_size: 1024 * 1024 * 1024, // 1GB
634            parallelism,
635            batch_size: 1024, // Default morsel size
636            max_frontier_size: 1_000_000,
637            auto_flush_threshold: 10_000,
638            auto_flush_interval: Some(Duration::from_secs(5)),
639            auto_flush_min_mutations: 1,
640            wal_enabled: true,
641            compaction: CompactionConfig::default(),
642            throttle: WriteThrottleConfig::default(),
643            file_sandbox: FileSandboxConfig::default(),
644            query_timeout: Duration::from_secs(30),
645            commit_timeout: Duration::from_secs(5),
646            max_query_memory: 1024 * 1024 * 1024,       // 1GB
647            max_transaction_memory: 1024 * 1024 * 1024, // 1GB
648            max_compaction_rows: 5_000_000,             // 5M rows
649            enable_vid_labels_index: true,              // Enable by default
650            max_recursive_cte_iterations: 1000,
651            object_store: ObjectStoreConfig::default(),
652            index_rebuild: IndexRebuildConfig::default(),
653            strict_schema: false,
654            partial_lance_writes: false,
655            defer_embeddings: false,
656            fork_fragment_warn_threshold: 256,
657            tx_id_reservoir_batch: 16,
658            // Default ON as of the Item-B-deep-fix landing (per-table
659            // write serialization + Lance Table cache removed + drain
660            // in flush_to_l1). Validated by full UNI_ASYNC_FLUSH=1
661            // cross-crate nextest: 1754/1754 pass.
662            //
663            // `UNI_ASYNC_FLUSH=0` / `=false` / `=no` (case-insensitive)
664            // explicitly DISABLES async flush — useful for bisecting
665            // suspected async-flush regressions and for the sync-only
666            // benchmarks in `flush_pressure.rs`. Unset = default
667            // behavior (true).
668            async_flush_enabled: std::env::var("UNI_ASYNC_FLUSH")
669                .ok()
670                .map(|v| {
671                    let v = v.to_ascii_lowercase();
672                    !(v == "0" || v == "false" || v == "no")
673                })
674                .unwrap_or(true),
675            max_pending_flushes: 2,
676            drop_fork_drain_timeout: Duration::from_secs(10),
677            max_forks: None,
678            fork_default_ttl: None,
679            fork_sweeper_interval: Duration::from_secs(60),
680            disable_fork_sweeper: false,
681            fork_index_build_threshold: 10_000,
682            fork_index_builder_interval: Duration::from_secs(30),
683            disable_fork_index_builder: false,
684            // Correctness-first default: SSI/OCC on. See the field docs for
685            // the behavioral contract and the migration note (concurrent
686            // writers now observe aborts instead of silent lost updates;
687            // wrap them in `Session::transact_with_retry`).
688            ssi_enabled: true,
689        }
690    }
691}
692
693/// Cloud storage backend configuration.
694///
695/// Supports Amazon S3, Google Cloud Storage, and Azure Blob Storage.
696/// Each variant contains the credentials and connection parameters for
697/// its respective cloud provider.
698///
699/// # Examples
700///
701/// ```ignore
702/// // Create S3 configuration from environment variables
703/// let config = CloudStorageConfig::s3_from_env("my-bucket");
704///
705/// // Create explicit S3 configuration for LocalStack testing
706/// let config = CloudStorageConfig::S3 {
707///     bucket: "test-bucket".to_string(),
708///     region: Some("us-east-1".to_string()),
709///     endpoint: Some("http://localhost:4566".to_string()),
710///     access_key_id: Some("test".to_string()),
711///     secret_access_key: Some("test".to_string()),
712///     session_token: None,
713///     virtual_hosted_style: false,
714/// };
715/// ```
716#[derive(Clone, Debug)]
717pub enum CloudStorageConfig {
718    /// Amazon S3 storage configuration.
719    S3 {
720        /// S3 bucket name.
721        bucket: String,
722        /// AWS region (e.g., "us-east-1"). Uses AWS_REGION env var if None.
723        region: Option<String>,
724        /// Custom endpoint URL for S3-compatible services (MinIO, LocalStack).
725        endpoint: Option<String>,
726        /// AWS access key ID. Uses AWS_ACCESS_KEY_ID env var if None.
727        access_key_id: Option<String>,
728        /// AWS secret access key. Uses AWS_SECRET_ACCESS_KEY env var if None.
729        secret_access_key: Option<String>,
730        /// AWS session token for temporary credentials.
731        session_token: Option<String>,
732        /// Use virtual-hosted-style requests (bucket.s3.region.amazonaws.com).
733        virtual_hosted_style: bool,
734    },
735    /// Google Cloud Storage configuration.
736    Gcs {
737        /// GCS bucket name.
738        bucket: String,
739        /// Path to service account JSON key file.
740        service_account_path: Option<String>,
741        /// Service account JSON key content (alternative to path).
742        service_account_key: Option<String>,
743    },
744    /// Azure Blob Storage configuration.
745    Azure {
746        /// Azure container name.
747        container: String,
748        /// Azure storage account name.
749        account: String,
750        /// Azure storage account access key.
751        access_key: Option<String>,
752        /// Azure SAS token for limited access.
753        sas_token: Option<String>,
754    },
755}
756
757impl CloudStorageConfig {
758    /// Creates an S3 configuration using environment variables.
759    ///
760    /// Reads credentials from standard AWS environment variables:
761    /// - `AWS_ACCESS_KEY_ID`
762    /// - `AWS_SECRET_ACCESS_KEY`
763    /// - `AWS_SESSION_TOKEN` (optional)
764    /// - `AWS_REGION` or `AWS_DEFAULT_REGION`
765    /// - `AWS_ENDPOINT_URL` (optional, for S3-compatible services)
766    #[must_use]
767    pub fn s3_from_env(bucket: &str) -> Self {
768        Self::S3 {
769            bucket: bucket.to_string(),
770            region: std::env::var("AWS_REGION")
771                .or_else(|_| std::env::var("AWS_DEFAULT_REGION"))
772                .ok(),
773            endpoint: std::env::var("AWS_ENDPOINT_URL").ok(),
774            access_key_id: std::env::var("AWS_ACCESS_KEY_ID").ok(),
775            secret_access_key: std::env::var("AWS_SECRET_ACCESS_KEY").ok(),
776            session_token: std::env::var("AWS_SESSION_TOKEN").ok(),
777            virtual_hosted_style: false,
778        }
779    }
780
781    /// Creates a GCS configuration using environment variables.
782    ///
783    /// Reads service account path from `GOOGLE_APPLICATION_CREDENTIALS`.
784    #[must_use]
785    pub fn gcs_from_env(bucket: &str) -> Self {
786        Self::Gcs {
787            bucket: bucket.to_string(),
788            service_account_path: std::env::var("GOOGLE_APPLICATION_CREDENTIALS").ok(),
789            service_account_key: None,
790        }
791    }
792
793    /// Creates an Azure configuration using environment variables.
794    ///
795    /// Reads credentials from Azure environment variables:
796    /// - `AZURE_STORAGE_ACCOUNT`
797    /// - `AZURE_STORAGE_ACCESS_KEY` (optional)
798    /// - `AZURE_STORAGE_SAS_TOKEN` (optional)
799    ///
800    /// # Panics
801    ///
802    /// Panics if `AZURE_STORAGE_ACCOUNT` is not set.
803    #[must_use]
804    pub fn azure_from_env(container: &str) -> Self {
805        Self::Azure {
806            container: container.to_string(),
807            account: std::env::var("AZURE_STORAGE_ACCOUNT")
808                .expect("AZURE_STORAGE_ACCOUNT environment variable required"),
809            access_key: std::env::var("AZURE_STORAGE_ACCESS_KEY").ok(),
810            sas_token: std::env::var("AZURE_STORAGE_SAS_TOKEN").ok(),
811        }
812    }
813
814    /// Returns the bucket/container name for this configuration.
815    #[must_use]
816    pub fn bucket_name(&self) -> &str {
817        match self {
818            Self::S3 { bucket, .. } => bucket,
819            Self::Gcs { bucket, .. } => bucket,
820            Self::Azure { container, .. } => container,
821        }
822    }
823
824    /// Returns a URL-style identifier for this storage location.
825    #[must_use]
826    pub fn to_url(&self) -> String {
827        match self {
828            Self::S3 { bucket, .. } => format!("s3://{bucket}"),
829            Self::Gcs { bucket, .. } => format!("gs://{bucket}"),
830            Self::Azure {
831                container, account, ..
832            } => format!("az://{account}/{container}"),
833        }
834    }
835}
836
837#[cfg(test)]
838mod security_tests {
839    use super::*;
840
841    /// Tests for CWE-22 (Path Traversal) prevention in file sandbox.
842    mod file_sandbox {
843        use super::*;
844
845        #[test]
846        fn test_sandbox_disabled_allows_all_paths() {
847            let config = FileSandboxConfig::default();
848            assert!(!config.enabled);
849            // When disabled, all paths are allowed
850            assert!(config.validate_path("/tmp/test").is_ok());
851        }
852
853        #[test]
854        fn test_sandbox_enabled_with_no_paths_rejects() {
855            let config = FileSandboxConfig {
856                enabled: true,
857                allowed_paths: vec![],
858            };
859            let result = config.validate_path("/tmp/test");
860            assert!(result.is_err());
861            assert!(result.unwrap_err().contains("no allowed paths configured"));
862        }
863
864        #[test]
865        fn test_sandbox_rejects_outside_path() {
866            let config = FileSandboxConfig {
867                enabled: true,
868                allowed_paths: vec![PathBuf::from("/var/lib/uni")],
869            };
870            let result = config.validate_path("/etc/passwd");
871            assert!(result.is_err());
872            assert!(result.unwrap_err().contains("outside allowed sandbox"));
873        }
874
875        #[test]
876        fn test_is_potentially_insecure() {
877            // Disabled is insecure
878            let disabled = FileSandboxConfig::default();
879            assert!(disabled.is_potentially_insecure());
880
881            // Enabled with no paths is insecure
882            let no_paths = FileSandboxConfig {
883                enabled: true,
884                allowed_paths: vec![],
885            };
886            assert!(no_paths.is_potentially_insecure());
887
888            // Enabled with paths is secure
889            let secure = FileSandboxConfig::sandboxed(vec![PathBuf::from("/data")]);
890            assert!(!secure.is_potentially_insecure());
891        }
892
893        #[test]
894        fn test_security_warning_when_disabled() {
895            let disabled = FileSandboxConfig::default();
896            assert!(disabled.security_warning().is_some());
897
898            let enabled = FileSandboxConfig::sandboxed(vec![PathBuf::from("/data")]);
899            assert!(enabled.security_warning().is_none());
900        }
901
902        #[test]
903        fn test_deployment_mode_defaults() {
904            let embedded = FileSandboxConfig::default_for_mode(DeploymentMode::Embedded);
905            assert!(!embedded.enabled);
906
907            let server = FileSandboxConfig::default_for_mode(DeploymentMode::Server);
908            assert!(server.enabled);
909            assert!(!server.allowed_paths.is_empty());
910        }
911    }
912}