Skip to content

Commit ceeea8d

Browse files
mertcanaltinaduh95
authored andcommitted
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 e5d3795 commit ceeea8d

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
@@ -156,6 +156,23 @@ changes:
156156
language features that allow ordinary SQL to deliberately corrupt the database file are disabled.
157157
The defensive flag can also be set using `enableDefensive()`.
158158
**Default:** `true`.
159+
* `limits` {Object} Configuration for various SQLite limits. These limits
160+
can be used to prevent excessive resource consumption when handling
161+
potentially malicious input. See [Run-Time Limits][] and [Limit Constants][]
162+
in the SQLite documentation for details. Default values are determined by
163+
SQLite's compile-time defaults and may vary depending on how SQLite was
164+
built. The following properties are supported:
165+
* `length` {number} Maximum length of a string or BLOB.
166+
* `sqlLength` {number} Maximum length of an SQL statement.
167+
* `column` {number} Maximum number of columns.
168+
* `exprDepth` {number} Maximum depth of an expression tree.
169+
* `compoundSelect` {number} Maximum number of terms in a compound SELECT.
170+
* `vdbeOp` {number} Maximum number of VDBE instructions.
171+
* `functionArg` {number} Maximum number of function arguments.
172+
* `attach` {number} Maximum number of attached databases.
173+
* `likePatternLength` {number} Maximum length of a LIKE pattern.
174+
* `variableNumber` {number} Maximum number of SQL variables.
175+
* `triggerDepth` {number} Maximum trigger recursion depth.
159176

160177
Constructs a new `DatabaseSync` instance.
161178

@@ -437,6 +454,36 @@ added: v24.0.0
437454
* Type: {boolean} Whether the database is currently within a transaction. This method
438455
is a wrapper around [`sqlite3_get_autocommit()`][].
439456

457+
### `database.limits`
458+
459+
<!-- YAML
460+
added: REPLACEME
461+
-->
462+
463+
* Type: {Object}
464+
465+
An object for getting and setting SQLite database limits at runtime.
466+
Each property corresponds to an SQLite limit and can be read or written.
467+
468+
```js
469+
const db = new DatabaseSync(':memory:');
470+
471+
// Read current limit
472+
console.log(db.limits.length);
473+
474+
// Set a new limit
475+
db.limits.sqlLength = 100000;
476+
477+
// Reset a limit to its compile-time maximum
478+
db.limits.sqlLength = Infinity;
479+
```
480+
481+
Available properties: `length`, `sqlLength`, `column`, `exprDepth`,
482+
`compoundSelect`, `vdbeOp`, `functionArg`, `attach`, `likePatternLength`,
483+
`variableNumber`, `triggerDepth`.
484+
485+
Setting a property to `Infinity` resets the limit to its compile-time maximum value.
486+
440487
### `database.open()`
441488

442489
<!-- YAML
@@ -1456,6 +1503,8 @@ callback function to indicate what type of operation is being authorized.
14561503
[Changesets and Patchsets]: https://www.sqlite.org/sessionintro.html#changesets_and_patchsets
14571504
[Constants Passed To The Conflict Handler]: https://www.sqlite.org/session/c_changeset_conflict.html
14581505
[Constants Returned From The Conflict Handler]: https://www.sqlite.org/session/c_changeset_abort.html
1506+
[Limit Constants]: https://www.sqlite.org/c3ref/c_limit_attached.html
1507+
[Run-Time Limits]: https://www.sqlite.org/c3ref/limit.html
14591508
[SQL injection]: https://en.wikipedia.org/wiki/SQL_injection
14601509
[Type conversion between JavaScript and SQLite]: #type-conversion-between-javascript-and-sqlite
14611510
[`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
@@ -236,6 +236,7 @@
236236
V(kind_string, "kind") \
237237
V(last_insert_rowid_string, "lastInsertRowid") \
238238
V(length_string, "length") \
239+
V(limits_string, "limits") \
239240
V(library_string, "library") \
240241
V(loop_count, "loopCount") \
241242
V(max_buffer_string, "maxBuffer") \
@@ -433,6 +434,7 @@
433434
V(socketaddress_constructor_template, v8::FunctionTemplate) \
434435
V(space_stats_template, v8::DictionaryTemplate) \
435436
V(sqlite_column_template, v8::DictionaryTemplate) \
437+
V(sqlite_limits_template, v8::ObjectTemplate) \
436438
V(sqlite_run_result_template, v8::DictionaryTemplate) \
437439
V(sqlite_statement_sync_constructor_template, v8::FunctionTemplate) \
438440
V(sqlite_statement_sync_iterator_constructor_template, v8::FunctionTemplate) \

0 commit comments

Comments
 (0)