Skip to content

Commit 35d3bc8

Browse files
authored
sqlite: add limits property to DatabaseSync
PR-URL: #61298 Fixes: #61268 Reviewed-By: René <contact.9a5d6388@renegade334.me.uk> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
1 parent cc6c188 commit 35d3bc8

File tree

5 files changed

+707
-0
lines changed

5 files changed

+707
-0
lines changed

doc/api/sqlite.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,23 @@ changes:
163163
language features that allow ordinary SQL to deliberately corrupt the database file are disabled.
164164
The defensive flag can also be set using `enableDefensive()`.
165165
**Default:** `true`.
166+
* `limits` {Object} Configuration for various SQLite limits. These limits
167+
can be used to prevent excessive resource consumption when handling
168+
potentially malicious input. See [Run-Time Limits][] and [Limit Constants][]
169+
in the SQLite documentation for details. Default values are determined by
170+
SQLite's compile-time defaults and may vary depending on how SQLite was
171+
built. The following properties are supported:
172+
* `length` {number} Maximum length of a string or BLOB.
173+
* `sqlLength` {number} Maximum length of an SQL statement.
174+
* `column` {number} Maximum number of columns.
175+
* `exprDepth` {number} Maximum depth of an expression tree.
176+
* `compoundSelect` {number} Maximum number of terms in a compound SELECT.
177+
* `vdbeOp` {number} Maximum number of VDBE instructions.
178+
* `functionArg` {number} Maximum number of function arguments.
179+
* `attach` {number} Maximum number of attached databases.
180+
* `likePatternLength` {number} Maximum length of a LIKE pattern.
181+
* `variableNumber` {number} Maximum number of SQL variables.
182+
* `triggerDepth` {number} Maximum trigger recursion depth.
166183

167184
Constructs a new `DatabaseSync` instance.
168185

@@ -451,6 +468,36 @@ added:
451468
* Type: {boolean} Whether the database is currently within a transaction. This method
452469
is a wrapper around [`sqlite3_get_autocommit()`][].
453470

471+
### `database.limits`
472+
473+
<!-- YAML
474+
added: REPLACEME
475+
-->
476+
477+
* Type: {Object}
478+
479+
An object for getting and setting SQLite database limits at runtime.
480+
Each property corresponds to an SQLite limit and can be read or written.
481+
482+
```js
483+
const db = new DatabaseSync(':memory:');
484+
485+
// Read current limit
486+
console.log(db.limits.length);
487+
488+
// Set a new limit
489+
db.limits.sqlLength = 100000;
490+
491+
// Reset a limit to its compile-time maximum
492+
db.limits.sqlLength = Infinity;
493+
```
494+
495+
Available properties: `length`, `sqlLength`, `column`, `exprDepth`,
496+
`compoundSelect`, `vdbeOp`, `functionArg`, `attach`, `likePatternLength`,
497+
`variableNumber`, `triggerDepth`.
498+
499+
Setting a property to `Infinity` resets the limit to its compile-time maximum value.
500+
454501
### `database.open()`
455502

456503
<!-- YAML
@@ -1478,6 +1525,8 @@ callback function to indicate what type of operation is being authorized.
14781525
[Changesets and Patchsets]: https://www.sqlite.org/sessionintro.html#changesets_and_patchsets
14791526
[Constants Passed To The Conflict Handler]: https://www.sqlite.org/session/c_changeset_conflict.html
14801527
[Constants Returned From The Conflict Handler]: https://www.sqlite.org/session/c_changeset_abort.html
1528+
[Limit Constants]: https://www.sqlite.org/c3ref/c_limit_attached.html
1529+
[Run-Time Limits]: https://www.sqlite.org/c3ref/limit.html
14811530
[SQL injection]: https://en.wikipedia.org/wiki/SQL_injection
14821531
[Type conversion between JavaScript and SQLite]: #type-conversion-between-javascript-and-sqlite
14831532
[`ATTACH DATABASE`]: https://www.sqlite.org/lang_attach.html

src/env_properties.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@
237237
V(kind_string, "kind") \
238238
V(last_insert_rowid_string, "lastInsertRowid") \
239239
V(length_string, "length") \
240+
V(limits_string, "limits") \
240241
V(library_string, "library") \
241242
V(loop_count, "loopCount") \
242243
V(max_buffer_string, "maxBuffer") \
@@ -435,6 +436,7 @@
435436
V(socketaddress_constructor_template, v8::FunctionTemplate) \
436437
V(space_stats_template, v8::DictionaryTemplate) \
437438
V(sqlite_column_template, v8::DictionaryTemplate) \
439+
V(sqlite_limits_template, v8::ObjectTemplate) \
438440
V(sqlite_run_result_template, v8::DictionaryTemplate) \
439441
V(sqlite_statement_sync_constructor_template, v8::FunctionTemplate) \
440442
V(sqlite_statement_sync_iterator_constructor_template, v8::FunctionTemplate) \

0 commit comments

Comments
 (0)