diesel/sqlite/connection/limits.rs
1#[cfg(not(all(target_family = "wasm", target_os = "unknown")))]
2extern crate libsqlite3_sys as ffi;
3
4#[cfg(all(target_family = "wasm", target_os = "unknown"))]
5use sqlite_wasm_rs as ffi;
6
7/// SQLite resource limits that can be configured per-connection.
8///
9/// These control aspects of SQLite's behavior and can be used to prevent
10/// resource exhaustion or limit query complexity.
11///
12/// Each variant exposes two associated constants: `DEFAULT_*_LIMIT` (SQLite's
13/// documented default) and `SAFE_*_LIMIT` (the hardened value applied by
14/// [`SqliteConnection::set_recommended_security_limits`](crate::sqlite::SqliteConnection::set_recommended_security_limits)).
15/// A connection's actual runtime default can differ from `DEFAULT_*_LIMIT`
16/// because some builds raise the compile-time maximum (for example the bundled
17/// `libsqlite3-sys` raises `FunctionArg` and `VariableNumber`).
18///
19/// See the [SQLite documentation](https://www.sqlite.org/c3ref/limit.html) for details.
20#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
21#[non_exhaustive]
22pub enum SqliteLimit {
23 /// Maximum length of any string or BLOB or table row, in bytes.
24 ///
25 /// See [`DEFAULT_LENGTH_LIMIT`](Self::DEFAULT_LENGTH_LIMIT) and
26 /// [`SAFE_LENGTH_LIMIT`](Self::SAFE_LENGTH_LIMIT).
27 Length,
28
29 /// Maximum length of an SQL statement, in bytes.
30 ///
31 /// See [`DEFAULT_SQL_LENGTH_LIMIT`](Self::DEFAULT_SQL_LENGTH_LIMIT) and
32 /// [`SAFE_SQL_LENGTH_LIMIT`](Self::SAFE_SQL_LENGTH_LIMIT).
33 SqlLength,
34
35 /// Maximum number of columns in a table definition, result set, or index,
36 /// and also the maximum number of columns in the ORDER BY or GROUP BY
37 /// clauses.
38 ///
39 /// See [`DEFAULT_COLUMN_COUNT_LIMIT`](Self::DEFAULT_COLUMN_COUNT_LIMIT) and
40 /// [`SAFE_COLUMN_COUNT_LIMIT`](Self::SAFE_COLUMN_COUNT_LIMIT).
41 ColumnCount,
42
43 /// Maximum depth of the parse tree for any expression.
44 ///
45 /// This can help prevent stack overflow from deeply nested expressions.
46 ///
47 /// See [`DEFAULT_EXPR_DEPTH_LIMIT`](Self::DEFAULT_EXPR_DEPTH_LIMIT) and
48 /// [`SAFE_EXPR_DEPTH_LIMIT`](Self::SAFE_EXPR_DEPTH_LIMIT).
49 ExprDepth,
50
51 /// Maximum number of terms in a compound SELECT statement.
52 ///
53 /// See [`DEFAULT_COMPOUND_SELECT_LIMIT`](Self::DEFAULT_COMPOUND_SELECT_LIMIT)
54 /// and [`SAFE_COMPOUND_SELECT_LIMIT`](Self::SAFE_COMPOUND_SELECT_LIMIT).
55 CompoundSelect,
56
57 /// Maximum number of instructions in a virtual machine program used to
58 /// implement an SQL statement.
59 ///
60 /// If [`sqlite3_prepare_v2()`](https://www.sqlite.org/c3ref/prepare.html)
61 /// or the equivalent tries to allocate space for more than this many
62 /// opcodes in a single prepared statement, an `SQLITE_NOMEM` error is
63 /// returned.
64 ///
65 /// See [`DEFAULT_VDBE_OP_LIMIT`](Self::DEFAULT_VDBE_OP_LIMIT) and
66 /// [`SAFE_VDBE_OP_LIMIT`](Self::SAFE_VDBE_OP_LIMIT).
67 VdbeOp,
68
69 /// Maximum number of arguments on a function.
70 ///
71 /// See [`DEFAULT_FUNCTION_ARG_LIMIT`](Self::DEFAULT_FUNCTION_ARG_LIMIT) and
72 /// [`SAFE_FUNCTION_ARG_LIMIT`](Self::SAFE_FUNCTION_ARG_LIMIT).
73 FunctionArg,
74
75 /// Maximum number of attached databases.
76 ///
77 /// See [`DEFAULT_ATTACHED_LIMIT`](Self::DEFAULT_ATTACHED_LIMIT) and
78 /// [`SAFE_ATTACHED_LIMIT`](Self::SAFE_ATTACHED_LIMIT).
79 Attached,
80
81 /// Maximum length of the pattern argument to the
82 /// [`LIKE`](https://www.sqlite.org/lang_expr.html#like) or
83 /// [`GLOB`](https://www.sqlite.org/lang_expr.html#glob) operators.
84 ///
85 /// See [`DEFAULT_LIKE_PATTERN_LENGTH_LIMIT`](Self::DEFAULT_LIKE_PATTERN_LENGTH_LIMIT)
86 /// and [`SAFE_LIKE_PATTERN_LENGTH_LIMIT`](Self::SAFE_LIKE_PATTERN_LENGTH_LIMIT).
87 LikePatternLength,
88
89 /// Maximum index number of any parameter in an SQL statement.
90 ///
91 /// See [`DEFAULT_VARIABLE_NUMBER_LIMIT`](Self::DEFAULT_VARIABLE_NUMBER_LIMIT)
92 /// and [`SAFE_VARIABLE_NUMBER_LIMIT`](Self::SAFE_VARIABLE_NUMBER_LIMIT).
93 VariableNumber,
94
95 /// Maximum recursion depth of triggers.
96 ///
97 /// See [`DEFAULT_TRIGGER_DEPTH_LIMIT`](Self::DEFAULT_TRIGGER_DEPTH_LIMIT) and
98 /// [`SAFE_TRIGGER_DEPTH_LIMIT`](Self::SAFE_TRIGGER_DEPTH_LIMIT).
99 TriggerDepth,
100
101 /// Maximum number of auxiliary worker threads that a single prepared
102 /// statement may start.
103 ///
104 /// See [`DEFAULT_WORKER_THREADS_LIMIT`](Self::DEFAULT_WORKER_THREADS_LIMIT)
105 /// and [`SAFE_WORKER_THREADS_LIMIT`](Self::SAFE_WORKER_THREADS_LIMIT).
106 WorkerThreads,
107}
108
109impl SqliteLimit {
110 /// SQLite's default for [`Length`](Self::Length).
111 pub const DEFAULT_LENGTH_LIMIT: i32 = 1_000_000_000;
112 /// Hardened value for [`Length`](Self::Length).
113 pub const SAFE_LENGTH_LIMIT: i32 = 1_000_000;
114
115 /// SQLite's default for [`SqlLength`](Self::SqlLength).
116 pub const DEFAULT_SQL_LENGTH_LIMIT: i32 = 1_000_000_000;
117 /// Hardened value for [`SqlLength`](Self::SqlLength).
118 pub const SAFE_SQL_LENGTH_LIMIT: i32 = 100_000;
119
120 /// SQLite's default for [`ColumnCount`](Self::ColumnCount).
121 pub const DEFAULT_COLUMN_COUNT_LIMIT: i32 = 2_000;
122 /// Hardened value for [`ColumnCount`](Self::ColumnCount).
123 pub const SAFE_COLUMN_COUNT_LIMIT: i32 = 100;
124
125 /// SQLite's default for [`ExprDepth`](Self::ExprDepth).
126 pub const DEFAULT_EXPR_DEPTH_LIMIT: i32 = 1_000;
127 /// Hardened value for [`ExprDepth`](Self::ExprDepth).
128 pub const SAFE_EXPR_DEPTH_LIMIT: i32 = 10;
129
130 /// SQLite's default for [`CompoundSelect`](Self::CompoundSelect).
131 pub const DEFAULT_COMPOUND_SELECT_LIMIT: i32 = 500;
132 /// Hardened value for [`CompoundSelect`](Self::CompoundSelect).
133 pub const SAFE_COMPOUND_SELECT_LIMIT: i32 = 3;
134
135 /// SQLite's default for [`VdbeOp`](Self::VdbeOp).
136 pub const DEFAULT_VDBE_OP_LIMIT: i32 = 250_000_000;
137 /// Hardened value for [`VdbeOp`](Self::VdbeOp).
138 pub const SAFE_VDBE_OP_LIMIT: i32 = 25_000;
139
140 /// SQLite's default for [`FunctionArg`](Self::FunctionArg).
141 pub const DEFAULT_FUNCTION_ARG_LIMIT: i32 = 127;
142 /// Hardened value for [`FunctionArg`](Self::FunctionArg).
143 pub const SAFE_FUNCTION_ARG_LIMIT: i32 = 8;
144
145 /// SQLite's default for [`Attached`](Self::Attached).
146 pub const DEFAULT_ATTACHED_LIMIT: i32 = 10;
147 /// Hardened value for [`Attached`](Self::Attached).
148 pub const SAFE_ATTACHED_LIMIT: i32 = 0;
149
150 /// SQLite's default for [`LikePatternLength`](Self::LikePatternLength).
151 pub const DEFAULT_LIKE_PATTERN_LENGTH_LIMIT: i32 = 50_000;
152 /// Hardened value for [`LikePatternLength`](Self::LikePatternLength).
153 pub const SAFE_LIKE_PATTERN_LENGTH_LIMIT: i32 = 50;
154
155 /// SQLite's published default for [`VariableNumber`](Self::VariableNumber).
156 ///
157 /// A particular build may compile a different maximum. The default
158 /// `libsqlite3-sys` bundle sets `SQLITE_MAX_VARIABLE_NUMBER=250000`, so a
159 /// connection's runtime default can exceed this published value.
160 pub const DEFAULT_VARIABLE_NUMBER_LIMIT: i32 = 32_766;
161 /// Hardened value for [`VariableNumber`](Self::VariableNumber).
162 pub const SAFE_VARIABLE_NUMBER_LIMIT: i32 = 10;
163
164 /// SQLite's default for [`TriggerDepth`](Self::TriggerDepth).
165 pub const DEFAULT_TRIGGER_DEPTH_LIMIT: i32 = 1_000;
166 /// Hardened value for [`TriggerDepth`](Self::TriggerDepth).
167 pub const SAFE_TRIGGER_DEPTH_LIMIT: i32 = 10;
168
169 /// SQLite's default for [`WorkerThreads`](Self::WorkerThreads).
170 pub const DEFAULT_WORKER_THREADS_LIMIT: i32 = 0;
171 /// Hardened value for [`WorkerThreads`](Self::WorkerThreads), equal to its
172 /// default, so the recommended setter leaves it untouched.
173 pub const SAFE_WORKER_THREADS_LIMIT: i32 = 0;
174
175 /// Convert to the corresponding FFI constant value.
176 pub(super) fn to_ffi(self) -> i32 {
177 match self {
178 SqliteLimit::Length => ffi::SQLITE_LIMIT_LENGTH,
179 SqliteLimit::SqlLength => ffi::SQLITE_LIMIT_SQL_LENGTH,
180 SqliteLimit::ColumnCount => ffi::SQLITE_LIMIT_COLUMN,
181 SqliteLimit::ExprDepth => ffi::SQLITE_LIMIT_EXPR_DEPTH,
182 SqliteLimit::CompoundSelect => ffi::SQLITE_LIMIT_COMPOUND_SELECT,
183 SqliteLimit::VdbeOp => ffi::SQLITE_LIMIT_VDBE_OP,
184 SqliteLimit::FunctionArg => ffi::SQLITE_LIMIT_FUNCTION_ARG,
185 SqliteLimit::Attached => ffi::SQLITE_LIMIT_ATTACHED,
186 SqliteLimit::LikePatternLength => ffi::SQLITE_LIMIT_LIKE_PATTERN_LENGTH,
187 SqliteLimit::VariableNumber => ffi::SQLITE_LIMIT_VARIABLE_NUMBER,
188 SqliteLimit::TriggerDepth => ffi::SQLITE_LIMIT_TRIGGER_DEPTH,
189 SqliteLimit::WorkerThreads => ffi::SQLITE_LIMIT_WORKER_THREADS,
190 }
191 }
192}