skylite beta
Documentation menu

0007: SQL policy

Client SQL runs one statement at a time under a default-deny SQLite authorizer, defensive configuration and limits that lower, but do not remove, the chance of overflowing the ncruces build's 64 KiB C stack, with a cooperative time limit, panic containment that turns an overflow into a broken connection, and a reset between principals, all applied by internal/sqlpolicy; the engine and the server carry the requirements the package cannot meet alone.

Status: Accepted

Amended by the engine’s handling of broken connections, on 2026-11-16: requirement 1 is met by releasing the broken connection’s SQLite instance, not by keeping lock state in the engine’s VFS, and the server reports a broken connection to the engine by an error that wraps engine.ErrBroken (0004 decision 6).

Date: 2026-11-15

Context

skylite’s requirements restrict the SQL that arrives from clients: no extension loading, no attaching arbitrary files, bounded query time and result size, and nothing a client sends may read or write outside its own database or change an engine setting the server depends on. The client protocol (0005) leaves this SQL policy to this record and fixes how it is consulted: before any step, on every path (0005 decision 5). This record decides the part that sits next to the SQLite connection, and the pull request that adds it also adds internal/sqlpolicy, which applies it to a connection of the pure-Go SQLite binding ncruces/go-sqlite3 v0.37.0 (SQLite 3.54.1, MIT licence). The engine, the server and connection pooling come later; what they must do for this policy to hold is stated in Requirements on the engine and the server.

Threat model. A client is authenticated but its SQL text is untrusted, and services of many tenants send SQL to the same server. Each connection serves one database, so the policy has to keep a statement inside that database and away from the server’s own settings, its files and its process.

Facts the policy has to respect:

  • sqlite3_stmt_readonly is true for BEGIN, COMMIT, ROLLBACK, SAVEPOINT, RELEASE, ATTACH and DETACH, so “read-only” is not a safety check by itself.
  • Some pragmas take effect when the statement is compiled, not when it runs, so the policy must decide before or during compilation. The authorizer is consulted during compilation, which is where it decides. It sees each function by name only, not with its number of arguments.
  • Because the authorizer runs at compile time, a compiled statement must never be reused under a different policy.

What the WebAssembly build means. ncruces compiles SQLite to WebAssembly and translates that into Go, so the engine is ordinary Go code with its own linear memory per connection:

  • Extension loading does not exist. The build defines SQLITE_OMIT_LOAD_EXTENSION (sqlite_opt.h#L38), so neither load_extension() nor sqlite3_load_extension is compiled in.
  • Each connection’s memory is capped at 256 MiB by default (wrap.go#L28); WithMaxMemory can change it when the connection is opened. When SQLite reports SQLITE_NOMEM, ncruces panics instead of returning an error (conn.go#L493-L494). SQLite reports SQLITE_NOMEM for a statement over the VDBE-operations limit as well (vdbeaux.c#L187-L188).
  • The C stack is a fixed 64 KiB region at the start of linear memory (build.sh#L23, sqlite3.go#L33). A statement that recurses too deeply drives the stack pointer below zero; the translated code then panics with an index out of range, and every later call on that connection panics too. Neither SQLite’s default limits nor the policy’s prevent this on this build; The C stack gives the measurements.
  • The configuration the build compiles in: foreign keys are enforced by default (sqlite_opt.h#L22) and trusted_schema is off by default (sqlite_opt.h#L23). SQLite’s own default leaves foreign keys off (foreignkeys.html, section 2).
  • Conn.Config rejects database-configuration options above SQLITE_DBCONFIG_REVERSE_SCANORDER (config.go#L19-L20), so SQLITE_DBCONFIG_ENABLE_ATTACH_CREATE and _ATTACH_WRITE cannot be set through it. The policy does not need them, because it refuses ATTACH.
  • The authorizer callback receives a NULL argument as an empty string (config.go#L221-L222), so it cannot tell PRAGMA foreign_keys from PRAGMA foreign_keys = '', which turns enforcement off. A pragma is therefore allowed or refused by name, whatever its argument.
  • ncruces installs a progress handler on every connection that interrupts the running statement, every 1,000 virtual-machine instructions, once the context given to SetInterrupt is done (conn.go#L120, conn.go#L351-L361); measured by the first review of this record, it also fires while a statement is compiled. Work inside one instruction, such as one call of an SQL function or one page read, is never interrupted.

Decision

Where the policy applies

The engine opens each connection, does its own set-up (its VFS, journal_mode, synchronous and the rest that 0004 requires), then calls Policy.Apply once, with a context that carries its deadline. From then on:

  • The server’s own statements, BEGIN, COMMIT, ROLLBACK and its pragmas, go through Conn.Privileged, which runs a function with the authorizer suspended and restores it when the function returns or panics. The server qualifies its own SQL with main., because a client’s temporary objects can shadow names in main. A statement under Privileged also runs the client’s triggers and views it reaches, with no authorizer and no time limit; requirement 7 keeps the server’s statements away from them.
  • Every client statement goes through Conn.Run, one statement per call. Before compiling, Run refuses SQL longer than MaxSQLLength (reason sql_length) and SQL holding a NUL byte (nul), whose text after the NUL SQLite would otherwise ignore. After compiling the first statement and before any step, it refuses text after it other than white space, comments and the statement’s own semicolon, and SQL that holds no statement (statement_count), as 0005 decision 6 requires. The server answers these three with invalid_request, a refusal by the authorizer with statement_forbidden, time_limit with timeout, and memory or a broken connection with internal.

Run compiles the statement, runs it and closes it, so no compiled statement outlives the request it was compiled for, and there is no statement cache. sqlite3_set_authorizer also expires every statement already prepared on the connection (auth.c#L81), so a statement prepared before Apply or inside Privileged is compiled again, under the policy, if it is stepped later.

Mechanisms

MechanismWhat it is for
Authorizer, default-deny (docs)Decides, while each statement is compiled, which actions, pragmas and functions it may use. Any action code the policy does not list is refused, including ones a later SQLite adds.
One statement per Run, no NUL, bounded textThe protocol’s statement rules, enforced before any step.
SQLITE_DBCONFIG_DEFENSIVE on (docs)Disables the language features that can corrupt the file, such as PRAGMA writable_schema = ON and PRAGMA journal_mode = OFF, as a second line behind the pragma allowlist.
SQLITE_DBCONFIG_WRITABLE_SCHEMA offClears a writable schema set before Apply.
trusted_schema off (docs)Views, triggers, CHECK constraints, defaults and generated columns may call only functions marked innocuous, so a schema object cannot run a function the client could not call directly.
SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION off (docs)Kept off although the build omits extension loading, so the setting is right if a later build includes it.
temp_store = MEMORYTemporary tables and indices, statement journals and sorts stay in the connection’s memory; see Temporary tables.
Run-time limits (docs)Bound the size of one statement and one value, and lower the chance that nesting which recurses in C overflows the 64 KiB stack, which they do not prevent; see Limits and The C stack. The number of attached databases is fixed at zero.
Page count, optionalLimits.MaxPageCount sets max_page_count through Privileged, so the server can bound a database’s size. Unset by default.
Two-argument trim, ltrim, rtrimReplaced by the policy’s own, with a bounded character set; see Functions.
Time limitRun gives the connection a context that ends at the policy’s timeout, and the progress handler interrupts the statement then (docs). It is cooperative; see Time.
Panic containmentApply, Run, Privileged and Reset recover a panic raised inside ncruces, for SQLITE_NOMEM or a stack overflow, and mark the connection broken. A panic raised in the caller’s own code is passed on.
Conn.ResetReturns everything a client can change on the connection to the policy’s defaults; see Session state.

Authorizer actions allowed: SELECT, reading a column, recursive common table expressions, INSERT, UPDATE, DELETE, creating and dropping tables, indices, views and triggers (temporary or not), ALTER TABLE except the forms that check existing rows (Functions), REINDEX, ANALYZE, savepoints inside a transaction, and the pragmas and functions listed below. Refused: transaction control, ATTACH, DETACH, CREATE VIRTUAL TABLE, DROP VIRTUAL TABLE, and any other action code. Writes to sqlite_schema cannot be refused by the authorizer, because SQLite authorizes its own write to sqlite_schema with the same code when it creates a table (build.c#L1320); defensive mode and writable_schema off refuse them instead.

Transaction control. Transactions are the protocol’s operations, not SQL (0005 decision 5). The policy refuses BEGIN, COMMIT, END and ROLLBACK in every client statement, with reason transaction. SAVEPOINT, RELEASE and ROLLBACK TO are allowed only when a transaction is open as Run starts, which on the protocol’s paths means a transaction the server opened on a channel; outside one, SAVEPOINT would begin a transaction, and the policy refuses all three. The server opens and ends transactions through Privileged. The authorizer cannot see, and so the policy cannot refuse, a rollback that a conflict action (INSERT OR ROLLBACK, UPDATE OR ROLLBACK) or RAISE(ROLLBACK, …) in a trigger performs, which ends the server’s transaction; and inside a transaction a client’s RELEASE or ROLLBACK TO can end a savepoint the server holds. Both are tested. So the server reads autocommit after every Run, as 0005 decision 6 already does, and never holds a savepoint of its own while client SQL runs (requirement 8).

Schema in the file. SQLite does not consult the authorizer for schema text it loads from the database file. A view, trigger, CHECK constraint or default written through a path without the policy, for example with a function the policy refuses, runs when a client’s statement uses it. The schema of a skylite database must therefore only ever be written through the policy.

Limits

Each limit is a field of sqlpolicy.Limits. The defaults below are the package’s; the server sets every field from its own configuration, and each sits under the protocol limit named in the last column (0005 decision 9). New refuses values outside SQLite’s compiled range, with MaxLength from SQLite’s minimum of 30, and combinations of the nesting limits whose modelled stack cost exceeds a budget, which is a floor and not a guarantee (The C stack); Apply reads each limit back. MaxSQLLength and MaxVDBEOps also bound the policy’s own statements and the SQL SQLite compiles itself, so only use checks their lower end: measured by the second review of this record, Apply fails below 26 bytes and 51 instructions, and CREATE TABLE fails below 121 bytes of SQL.

LimitDefaultSQLite’s default hereWhyUnder
SQLITE_LIMIT_LENGTH1 MiB1,000,000,000One string or BLOB, one row’s record, and the result of group_concat and the JSON aggregates (measured: one value of 1,048,571 bytes is stored, three values of 400,000 bytes in one row are not). Bounds the work of a function the time limit cannot interrupt; see Time.Request body (16 MiB), held result (1 MiB), result size (64 MiB)
SQLITE_LIMIT_SQL_LENGTH1 MiB1,000,000,000One statement’s text; longer SQL is refused with sql_length before compiling.Request body
SQLITE_LIMIT_EXPR_DEPTH181,000Depth of an expression, against which tables in a join, the pairs of a composite-key OR, nested IN, EXISTS and scalar subqueries, and CTEs joined in one SELECT also count; and nesting of views, CTEs and subqueries. See The C stack.—
SQLITE_LIMIT_PARSER_DEPTH1002,500Nesting in parentheses; 100 allows 15 nested subqueries in FROM. The parser’s first 50 entries are static, so values below 50 act as 50 (parse.y#L25, lempar.c#L306-L311), and New refuses them.—
SQLITE_LIMIT_COMPOUND_SELECT4500Terms of one compound SELECT; see The C stack.—
SQLITE_LIMIT_TRIGGER_DEPTH81,000Nesting of triggers and foreign key actions, at compile time and at run time; see The C stack.—
SQLITE_LIMIT_LIKE_PATTERN_LENGTH4,096; at most 4,09650,000Matching recurses once per wildcard and overflowed the stack from about 9,000 bytes; a constant LIKE that the planner evaluates while it compiles adds to the nesting around it (The C stack); its time grows with pattern length times text length (Time).—
SQLITE_LIMIT_VDBE_OP250,000,000250,000,000Not lowered: the time limit, which also acts during compilation, bounds time, and the memory cap bounds memory.—
SQLITE_LIMIT_FUNCTION_ARG1,0001,000Kept. json_extract, json_remove and json_set repeat their work on the whole document for each path argument; see Time.—
SQLITE_LIMIT_VARIABLE_NUMBER32,76632,766Kept; ORMs bind one parameter per element of an IN list.Request body
SQLITE_LIMIT_ATTACHED010Fixed, not configurable; a second refusal of ATTACH and VACUUM.—
max_page_countunset4,294,967,294Optional bound on a database’s size; Apply fails if the database is already larger.—
Time limit5 snonePer Run; the server sets it to what remains of the request’s statement time.Statement time (30 s)

A limit that SQLite enforces fails the statement with SQLite’s own error, which names the limit; the policy passes it through as an engine error and does not parse messages.

The C stack

Measurements, on this build: SQLite 3.54.1 in ncruces v0.37.0 and go-sqlite3-wasm v6.5.35401, Go 1.26.11, linux/amd64, a new in-memory connection for each statement, SQLite’s own limits so that only the stack could stop a statement. The figures are what broke the connection, not properties SQLite documents, and they must be measured again on any upgrade of the binding.

  • Alone: a chain of 332 operators (SELECT 1 + 1 + …); 220 UNION ALL terms; 263 nested subqueries in FROM; a chain of 197 views; a chain of 70 INSERT triggers (137 UPDATE, 147 DELETE, 132 ON DELETE CASCADE); a LIKE pattern of about 9,000 bytes alternating % and a character.
  • Combined, the limits multiply. With T terms in a compound SELECT at each of N levels, the nested part in the first term, the connection broke when N × T reached about 180 to 220: at 19 levels of 10-term INTERSECT or EXCEPT compounds, 22 of UNION ALL, 44 levels of 5 terms and 110 of 2. The figure was the same for subqueries in FROM, chains of views and chains of CTEs, each nested alone; nested inside one another they multiply, below. An expression of depth 50 at the innermost level moved the break by at most one level. Scalar and IN subqueries never broke: SQLite’s expression-height check stopped them first.
  • Trigger nesting adds to the same stack: with a nested statement in the deepest trigger of an INSERT chain, N × T at the break fell by about 2 to 2.5 per level of the chain (to 168 at 8 levels, 148 at 16, 100 at 32), and by less for UPDATE, DELETE and cascade chains.

Which limit bounds which nesting: views, CTEs and subqueries in FROM count against SQLITE_LIMIT_EXPR_DEPTH (select.c#L2497-L2500), which allowed E − 2 levels of views or CTEs; subqueries in FROM are also bounded by the parser depth, which allowed 6 levels at 50, 15 at 100 and 31 at 200; triggers and foreign key actions by SQLITE_LIMIT_TRIGGER_DEPTH, which SQLite checks while it compiles a chain (trigger.c#L1259-L1261) and again when it runs one (vdbe.c#L7683-L7686). No limit bounds their combinations: the body of each view is parsed on its own, with a fresh parser depth, so subqueries in FROM inside each view of a chain multiply the two; and when SQLite flattens a subquery or view into the query around it, it copies the inner expressions into the outer one without checking their height (select.c#L3934), so the depth of each level’s expression adds up across the levels.

The model. In the unit of one compound term at one level, New charges a configuration T × max(E − 2, P / 6) + 2.5 × D, for compound terms T, expression depth E, parser depth P and trigger depth D, and refuses one that costs more than 90, half the 180 at which the connection broke. The defaults cost 84. The model counts one kind of nesting at a time, so it is a floor, not a guarantee: the limits lower the chance that ordinary SQL overflows the stack, and SQL within them still does. Measured margins at the defaults, for each kind alone: chains of views or CTEs reach 16 levels and broke at 47 (4-term INTERSECT), 2.9 times; subqueries in FROM reach 15 levels and broke at 48, 3.2 times; a trigger chain of 8 with the same nesting in its last trigger broke at 42; an expression reaches depth 17 against 332; a LIKE pattern 4,096 bytes against about 9,000. TestNestingStopsAtTheLimits runs each of these shapes alone at the deepest the limits allow, at the defaults and at three corners of what New accepts (11 compound terms; expression depth 90 with one term; trigger depth 33), and checks that each fails with SQLite’s error and leaves the connection usable.

What still overflows the stack, measured by the second review of this record, through Run with every limit at its default:

  • Views over subqueries in FROM: four views, each 13 levels of subqueries in FROM with a 4-term INTERSECT at every level and the innermost level reading the previous view, then SELECT * FROM v4: 6 statements, 3,920 bytes. With UNION ALL the fifth view broke. With the final SELECT as the body of the last trigger in a chain of 8, three views of 14 levels broke (three of 13 ran), and seven with 2-term compounds; outside a chain, eight.
  • Flattened expressions: five views, each SELECT a + 1 + … AS a with 16 operators over the previous one, read by one SELECT of 16 levels nested through 15 subqueries in FROM, with the same expression at every level: 8 statements, 1,933 bytes, the last 1,360. Flattening makes the expression about (levels + 1) × operators deep; with four views (320) it ran; with five (336) it broke.
  • A constant LIKE while planning: on an indexed table of 2,000 rows after ANALYZE, the planner evaluates a > -like(…), with a pattern of 4,096 bytes, against the sqlite_stat4 samples while it compiles 17 chained CTEs of 4-term INTERSECT in the last trigger of a chain of 8; the trigger’s statement is about 7.6 KB. Without the unary minus it held at the defaults. With only the expression depth raised, the pattern cost the 4-term chain about 24 levels, about 96 units.
  • At corners that New accepts: 11 compound terms with expression depth 10, parser depth 50 and trigger depth 1 broke at three views of 6 levels, and with the bare LIKE at 8 CTEs; one term with expression depth 90 at 19 views of 14 levels; two terms with expression depth 37 at 35 views of a 35-operator expression.

Measured by the verification of the stack spike below, two more shapes, with limits given as expression depth, parser depth, compound terms and trigger depth:

  • Chained generated columns: one CREATE TABLE whose generated columns each read the next, each column a nest of BETWEENs. SQLite compiles a generated column that reads another recursively, and CREATE TABLE compiles SELECT * on the new table itself, so the one statement is enough; only SQLITE_LIMIT_COLUMN (2,000, which the policy does not set) bounds the chain. On this build at the default limits, five columns in 1,436 bytes, through Run, broke the connection and four ran. On the rebuilt engines it broke at every size and limit set tried: at 500 columns on 4 MiB with the defaults, at 233 on 4 MiB with 40/50/16/64, at 210 on 4 MiB with 28/100/8/32, and at 72 on 1 MiB with 22/50/8/32.
  • Views whose column expressions nest compound scalar subqueries: each view 6 or 14 levels of subqueries in FROM, with the column expression at each level nesting compound scalar subqueries such as (VALUES ((VALUES (a) UNION ALL SELECT 1 …)) UNION ALL SELECT 1 …). A compound scalar subquery counts as one level of expression depth but takes stack for each of its terms, the parser depth bounds how deep one nest goes but not how many nests a view body holds, and flattening puts them all on one path. It broke on 4 MiB at 34 views with 40/50/16/64 (38 statements, 245,522 bytes) and at 24 with 28/100/8/32, and on 1 MiB at 19 with 22/50/8/32. With the default limits it stopped at SQLite’s nesting error on the 2 MiB and 4 MiB engines.

A stack overflow is therefore one more way a connection breaks, of the same class as memory exhaustion (Memory and a broken connection): Run returns an error wrapping ErrBroken, the connection is never used again, and the requirements on the engine for a broken connection apply. TestStackOverflowBreaksTheConnection runs the second review’s three shapes and the five chained generated columns at the defaults and checks that the Run and the next one return ErrBroken, that neither hangs, and that another connection still works.

Decided: containment, not a larger stack. A spike rebuilt the engine with a larger C stack, and a separate verification repeated it. Measured by that spike, on go-sqlite3-wasm v6.5.35401 (SQLite 3.54.1) and ncruces v0.37.0, Go 1.26.11: upstream’s build reproduces byte for byte in a container; an engine with a 1 MiB or 4 MiB stack needs one link option in the engine module’s build (-Wl,-z,stack-size=N) and one change to the memory size that go-sqlite3’s wrap.go hard-codes (mem.Grow(5)); go-sqlite3’s own suite passes on it (808 passed, none failed, at 1 MiB, and at 4 MiB in the verification); and the break points grow with the stack, 3.7 to 4 times for each fourfold. The larger stack is not adopted, because no setting of SQLite’s limits bounds the stack at any size: the verification broke every limit set the spike proposed, 40/50/16/64 and 28/100/8/32 on 4 MiB and 22/50/8/32 on 1 MiB, with the views over compound scalar subqueries above, and the chained generated columns overflow at every size and limit set. A larger stack would loosen the limits for ORMs without changing the safety model, at the cost of owning a patched build of two modules and measuring again on every upgrade.

The safety model is therefore containment: a stack overflow breaks one connection, which returns ErrBroken, and nothing else, under the requirements on a broken connection below. The limits exist so that ordinary SQL fails with an SQLite error instead of breaking its connection, not to stop a client that sets out to overflow the stack.

Still open. The default limits are provisional. They will be set again, on the containment model, for what GORM and Hibernate actually send, when skylite’s Go driver and Hibernate dialect are built. ncruces may be asked for a configurable stack size.

What the defaults cost clients, measured by the second review, through Run at the defaults: the largest size accepted, and SQLite’s error at the next. A chain of 17 AND terms is accepted and 18 are refused; an earlier version of this record said 17 were refused.

ShapeLargest acceptedError at the next size
AND or OR chain in WHERE17 terms; 16 with a soft-delete IS NULL term addedExpression tree is too large (maximum depth 18)
|| or + chain18 operandsthe same
(a = ? AND b = ?) OR …, composite keys16 pairsthe same
(a, b) IN (VALUES (?, ?), …)16,000 pairs, the probe’s ceilingnone
JOIN … ON, inner or left17 tables; 18 with USINGExpression tree is too large
OR of IN lists17 liststhe same
nested function calls; nested CASE17 levels; 16the same
nested IN or EXISTS subqueries4 levelsthe same
nested scalar subqueries3 levelsthe same
CTEs joined in one SELECT9; 5 if each joins 3 tablesVIEWs and/or subqueries nested too deep
a chain of views or CTEs16the same
subqueries in FROM, nested15Recursion limit
compound SELECT, UNION ALL or UNION4 termstoo many terms in compound SELECT
INSERT … VALUES of many rows16,383 rows of 2 parameterstoo many SQL variables
ON DELETE or ON UPDATE CASCADE chain9 tablestriggers nested too deep
self-referencing ON DELETE CASCADE tree7 levels below the deleted rowthe same
one value in a rowabout 1,048,570 bytes, by row shapestring or blob too big
SQL text1,048,576 bytesrefusal sql_length

TestORMShapesAtTheDefaultLimits pins the first figure of the WHERE, composite-key, join and joined-CTE rows, 17 AND terms, 16 pairs, 17 tables joined with ON and 9 CTEs, each with SQLite’s error at the next size, so that a change of the limits shows up as a change of that test. Through the policy at the defaults, GORM v1.31.2 refused only 18 or more chained Where conditions (17 on a soft-delete model) and CurrentDatabase, and all 58 distinct statements of the Hibernate acceptance suite ran. The server may trade one limit for another within the budget.

Time

The time limit is cooperative. The progress handler stops a statement between virtual-machine instructions and during compilation, but not inside one instruction. Measurements, on this build, by the first review of this record, with a 300 ms limit, none interrupted: regexp on 10,000 characters with a{1000}b took 4.4 s, about 0.5 ms per character, and ((a{10}){10}){10}b on 1 MiB ran more than 8 minutes before it was killed; decimal_pow2(20000) took 5.2 s; decimal_mul took 1.7 s at 20,000 digits and 6.1 s at 40,000; on a 1.1 GiB table, DELETE FROM big took 868 ms and succeeded, and PRAGMA quick_check 947 ms.

So the policy refuses or replaces every function whose time is not close to linear in its input, and bounds the rest through MaxLength. Sweep, on this build: every function the build registers, called with every assignment of up to three arguments from a set of inputs, timed at two sizes. A first pass at 64 KiB and 128 KiB used nine inputs: a run of a; a run of a of half the length ending in b; the same with the letters swapped; runs of g and of b ending in g; a JSON object of distinct keys; a JSON array; a long JSON path; nested JSON arrays. The full pass at 512 KiB and 1 MiB used seven, without the two g inputs and the nested arrays and with a set of 64 characters, after the changes below: 120 functions and 9,843 calls, six processes at once. The calls whose time grew about four times when the input doubled:

FunctionTime at 128 KiBDecision
trim, ltrim, rtrim with two arguments, a set of 64 Ki characters matching only at its end35 to 91 sReplaced: the policy’s own, the same comparisons as SQLite’s trimFunc (func.c#L1578-L1657), refused with reason function when the set holds more than 64 characters. Linear: 0.67 s at 1 MiB with 64 characters.
unhex with two arguments, a long set of characters to skip15 sRefused; neither ORM uses it. The authorizer cannot refuse only the two-argument form.
json_patch, jsonb_patch1.0 to 1.6 sRefused; neither ORM uses them.
replace, instr with a needle of half the text0.11 to 0.15 sKept, bounded by MaxLength; see the table below.

Of every other call, those that grew more than 2.8 times or took over 20 ms at 1 MiB in the full pass, 187 calls, were run again one at a time, and grew at most 2.6 times. regexp, regexpi and the decimal functions are refused on the review’s measurements above and were not swept.

Worst uninterruptible times, on this build, one statement each, on 8 cores: at 1 MiB one at a time on an otherwise idle machine, with a time limit of 100 ms; at 4 MiB and 16 MiB with a limit of 2 s, so that binding the arguments fitted in it, while other work loaded the machine (load average up to about 40), so those two columns overstate what the statement costs alone: at 4 MiB by up to about 40% against the 1 MiB figures scaled by the size, or by its square for replace and instr. The time limit had passed in every case before the statement ended:

Statement1 MiB4 MiB16 MiB
replace(x, y, 'c'), x a run of a, y half of it ending in b7.5 s2 min 44 s1 h 16 min
instr(x, y), the same8.1 s2 min 44 s1 h 15 min
x LIKE '%' || 'a'×4094 || 'b'15.4 s1 min 28 s7 min 29 s
x GLOB '*' || 'a'×4094 || 'b'15.8 s1 min 37 s7 min 4 s
x GLOB '*' || '[a]'×1364 || 'b'22.9 s1 min 59 s9 min 7 s
trim(x, y), y 64 characters, x’s only at the end0.67 s2.8 s13 s

At the default MaxLength of 1 MiB one statement can therefore hold its connection for about 23 s past the time limit; replace and instr grow with the square of MaxLength, and LIKE and GLOB with MaxLength times MaxLikePatternLength. The server may raise MaxLength, accepting the times in the table, or lower MaxLikePatternLength.

Measured by the second review, under load, two more costs that no instruction boundary interrupts. A sort in memory: SELECT r FROM (SELECT random() AS r FROM generate_series(1, 3000000)) ORDER BY r returned its first row after 5.5 s against a 2 s limit, and 1,000,000 rows took 1.5 s; with temp_store = MEMORY it reads no page, so requirement 3 does not bound it, and only the connection’s memory cap does. A JSON function with many path arguments repeats its work on the whole document for each one: on a 1 MiB array, json_extract with 999 paths took 5.3 s, json_remove 5.2 s and json_set with 499 paths 2.5 s, against 21 ms for one path and 528 ms for 100, so the time grows with MaxFunctionArgs times MaxLength. Both stay under the 23 s above.

PRAGMA quick_check is allowed only in a Run whose statement the authorizer has already seen as ALTER TABLE: SQLite runs it itself, through pragma_quick_check, when ALTER TABLE … ADD COLUMN changes a STRICT table or one with CHECK constraints (alter.c#L472-L487). Its time grows with the table, like DELETE FROM a large table or building an index, which no limit here bounds; requirement 3 below does.

When the time limit interrupts a write inside a transaction, SQLite rolls back the whole transaction; an interrupted read leaves the transaction open. Both are tested. If the caller’s own context ends first, Run returns the engine’s SQLITE_INTERRUPT error rather than a policy refusal.

Memory and a broken connection

The connection’s memory cap is the backstop for memory: a statement that exhausts it is refused with reason memory and breaks the connection. Deep view expansion ends that way: measured by the first review, about 700 bytes of SQL, 30 chained views with small compounds in a WHERE … IN, reached the 256 MiB cap in about 0.5 s with a VDBE-operations limit of 1, 4 or 250 million alike. The requirements below make a broken connection safe.

After Run recovers a panic from the engine, it returns *Error with reason memory for SQLITE_NOMEM or an error wrapping ErrBroken for anything else, such as a stack overflow (The C stack), and every later Run, Privileged and Reset on that connection returns ErrBroken. Conn.Close tries to close it; after a stack overflow SQLite cannot.

A broken connection can keep its locks. Measured by the first review, on this build: on a database file in WAL mode, connection A ran BEGIN IMMEDIATE and an INSERT, then a statement that overflowed the stack; Run and Close returned ErrBroken. A second connection’s INSERT then failed with “database is locked”, and still did after 47 s and about 400 forced garbage collections. Go’s finalizers had closed A’s file descriptors, but /proc/locks still showed A’s OFD locks on the -shm file (a write lock at byte 120, read locks at 123 and 128): SQLite never reached xShmUnmap (shm_ofd.go#L158-L176), and the file’s mapping keeps the open file description, and its locks, alive. An overflow in autocommit or in a read transaction did not block a writer. The package cannot tell whether a write transaction was open when the engine broke, because the failing statement may have begun one and the engine’s state is then unknown; the engine must assume that one was. The engine does not close a broken connection through SQLite: it releases it, which frees those locks (requirement 1).

Session state

A client can change its connection in three ways: PRAGMA foreign_keys, objects in the temp schema, and the last inserted row id. Conn.Reset rolls back an open transaction, drops every table, view and trigger in the temp schema, and their indices with them, with foreign key enforcement off so that a temporary child table cannot block the drop of its parent, turns enforcement back on, deletes the rows of the temp schema’s sqlite_sequence, which SQLite does not let be dropped and a client can write to directly, and sets the last inserted row id to zero, then checks the result; if anything fails the connection is broken. Reset is interrupted when the policy’s timeout or the caller’s context ends, which also breaks the connection: its time grows with the square of the temp objects left behind (measured by the second review: 742 ms for 2,500 temp views, 7.9 s for 10,000). changes() and total_changes() still count the earlier principal’s rows; they reveal counts, not data, within the same database.

PRAGMA foreign_keys is a session setting that clients may read and change. GORM’s table rebuild turns it off and on again around a transaction (below), and refusing it silently deleted referencing rows. Because the authorizer cannot tell a read of a pragma from a write, it is allowed by name.

Pragmas

Every pragma is refused unless it is on this list. The policy decides by the pragma’s name, in any letter case, for PRAGMA x, PRAGMA x = y, PRAGMA x(y) and schema-qualified forms. A pragma’s table-valued function, such as pragma_journal_mode, compiles the pragma when the statement scans the function (pragma.c#L3011-L3021), and the authorizer refuses it then; a statement whose plan never scans it succeeds without running the pragma.

Allowed pragmaWhy
table_infoRead-only column list. JDBC getPrimaryKeys in the reference driver, sqlite-jdbc (JDBC3DatabaseMetaData.java#L2020).
table_xinfoRead-only column list including hidden columns. sqlite-jdbc getColumns (#L980).
index_listRead-only. sqlite-jdbc getIndexInfo (#L1543); GORM GetIndexes as PRAGMA_index_list(?) (migrator.go#L342).
index_infoRead-only. sqlite-jdbc (#L1567); GORM (migrator.go#L350).
foreign_key_listRead-only. sqlite-jdbc getImportedKeys (#L1430).
foreign_keysA session setting; see Session state. GORM (migrator.go#L18-L27).
quick_check, inside ALTER TABLE onlySee Time.

Settings the server owns, which clients may not read or change through a pragma: journal_mode, synchronous, locking_mode, page_size, wal_checkpoint, wal_autocheckpoint, journal_size_limit, fullfsync, checkpoint_fullfsync, auto_vacuum, incremental_vacuum, encoding, secure_delete, max_page_count, schema_version, user_version, application_id, writable_schema, trusted_schema, cell_size_check, ignore_check_constraints, legacy_alter_table, defer_foreign_keys, recursive_triggers, query_only, read_uncommitted, temp_store, temp_store_directory, cache_size, cache_spill, mmap_size, threads, soft_heap_limit, hard_heap_limit, busy_timeout, analysis_limit and every other pragma of the build. Refused introspection includes database_list, which shows the database file’s path on the server, and integrity_check.

ATTACH, DETACH and VACUUM

ATTACH and DETACH are refused by the authorizer in every form: a plain or URI filename, :memory:, an empty name, or a filename computed by an expression. SQLite does not accept ATTACH inside a trigger body or a view, so those are syntax errors. VACUUM consults no authorizer action of its own (vacuum.c#L105-L138); while it runs, it attaches its output with ATTACH (vacuum.c#L226), the authorizer refuses that, and the attached-database limit of zero would refuse it again (attach.c#L170). So VACUUM, VACUUM main and VACUUM INTO are refused, with reason attach; compacting the file belongs to the engine. VACUUM temp is accepted and does nothing, because SQLite generates no vacuum for the temp schema (vacuum.c#L126-L132).

Temporary tables

Clients may create and drop temporary tables, indices, views and triggers, including temporary triggers on their own main tables: they are private to the connection and hold only the client’s own data. (Hibernate’s SQLite dialect does not use them; it keeps the default of ordinary tables for its bulk-operation tables, Dialect.java#L4018-L4020.) With temp_store = MEMORY, SQLite keeps temporary databases in memory (main.c#L2725-L2741) and opens statement journals in memory (btree.c#L3747, pager.c#L4581-L4584); measured, sorts that spill SQLite’s page cache opened no file either. Otherwise ncruces creates unnamed files in the process’s temporary directory (os_linux.go#L14-L41). Temporary objects count against the connection’s memory, and Reset drops them, so they live no longer than the client’s hold on a connection. A temporary object can shadow a main object of the same name, which is why the server qualifies its own SQL with main..

Functions

Every SQL function is refused unless it is on the allowlist in internal/sqlpolicy: the core scalar, aggregate, window, date and time, math and JSON functions, base64 and the ieee754 functions. A test pins the allowlist and the refused list, and fails if ncruces v0.37.0 registers a function, according to pragma_function_list, that is on neither. Refused: sqlite_log, which writes to the server’s log; sqlite_compileoption_get, sqlite_compileoption_used and sqlite_source_id, which describe the server’s build; match, which only full-text search uses and this build does not include; and, for their time (Time), regexp, regexpi, unhex, json_patch, jsonb_patch and the decimal functions. Functions that read files or load code (load_extension, readfile, writefile, edit, fts3_tokenizer) are not in this build; the authorizer refuses them if a later build or the server registers one, and requirement 5 covers the server’s. sqlite_version stays, because JDBC DatabaseMetaData reports the engine’s version.

The authorizer does not see the functions in the CHECK constraint or generated column of ALTER TABLE … ADD COLUMN (measured by the second review: regexp ran there for 4.6 s under a 200 ms limit). So Apply replaces every refused function the connection has, at every number of arguments pragma_function_list reports, with one that is not innocuous and fails with reason function. With trusted_schema off, SQLite then refuses it in those expressions, and in a view of main, with its own error, “unsafe use of f()”, before it asks the authorizer; that error reaches the client as an engine error. On a temp table SQLite’s check does not apply: ADD COLUMN with such a CHECK is accepted on an empty table, a generated column fails as non-deterministic, and every call reaches the stand-in, which fails with reason function. A test runs every refused function through both forms of ADD COLUMN, on a table of main and on a temp table.

Apply registers the policy’s own two-argument trim, ltrim and rtrim, which take the place of SQLite’s (create_function.html: “Built-in functions may be overloaded by new application-defined functions”; SQLite’s registrations at func.c#L3409-L3414). They keep SQLite’s results, which a test compares on text, BLOB, numeric, NULL and malformed UTF-8 arguments, and refuse a set of more than 64 characters. Hibernate’s SQLite dialect uses the two-argument forms for a trim of anything other than white space (SQLiteDialect.java#L339-L352); the one-argument forms stay SQLite’s. SQL that SQLite compiles itself prefers its built-in functions, so the forms of ALTER TABLE that check the existing rows with such SQL, ADD CONSTRAINT … CHECK, ADD CHECK and ALTER [COLUMN] … SET NOT NULL, would run SQLite’s trim on them (measured by the second review: 2.1 s at 16 KiB under a 200 ms limit, four times longer per doubling). Only those forms compile SQLite’s internal sqlite_fail, and when the authorizer has seen it, Run refuses the statement with reason statement before any step. ADD COLUMN checks a new constraint through quick_check, which runs the policy’s trim.

SQLite’s internal functions for ALTER TABLE are allowed: SQLite calls them from SQL it compiles itself, the authorizer sees them first (resolve.c#L1204), and SQLite resolves them only in that SQL (resolve.c#L1251-L1263), so a client calling one gets “no such function”. All nine are pinned, and the tests run every form of ALTER TABLE that SQLite 3.54.1 parses (parse.y#L1892-L1933): RENAME TO, RENAME [COLUMN], ADD [COLUMN] (including the forms that run quick_check), DROP [COLUMN], DROP CONSTRAINT and ALTER [COLUMN] … DROP NOT NULL succeed, and ADD CONSTRAINT … CHECK, ADD CHECK and ALTER [COLUMN] … SET NOT NULL are refused. Refusing one of them inside SQLite’s own ALTER SQL panicked the connection in the first review.

With trusted_schema off, a function that is not marked innocuous, such as base64, works in a query but not in a view, trigger or CHECK constraint.

Errors

A refusal is an *sqlpolicy.Error with a stable reason code and no SQL text, which can hold secrets. Everything else is the engine’s error, unchanged, including SQLite’s “unsafe use of f()” for a refused function in an ADD COLUMN expression on a table of main or in a view of main (Functions).

ReasonRule
attachATTACH, and VACUUM, which attaches its output
detachDETACH
transactionBEGIN, COMMIT, END, ROLLBACK; SAVEPOINT, RELEASE and ROLLBACK TO outside a transaction
pragmaA pragma not on the allowlist, in any form
functionA function not on the allowlist, or a two-argument trim with more than 64 characters to trim
statementAny other action the authorizer refuses, such as CREATE VIRTUAL TABLE, and the forms of ALTER TABLE that check existing rows
statement_countNo statement, or text after the first one
nulA NUL byte in the SQL
sql_lengthSQL longer than MaxSQLLength
time_limitThe statement ran past the time limit
memoryThe connection’s memory; the connection is then broken

What the two ORMs need

Hibernate ORM 7.4.13 with the community SQLite dialect issues no pragmas (SQLiteDialect.java). For the JPA acceptance suite in clients/java/acceptance, its schema creation emitted drop table if exists and create table with inline unique, then select last_insert_rowid() after each insert of a generated identifier. Its schema validation reads JDBC metadata; skylite’s driver does not exist yet, so the test uses the queries the reference driver, sqlite-jdbc 3.53.4.0, runs for those calls. sqlite-jdbc builds getColumns as one compound SELECT with one term per column of every table that matches (JDBC3DatabaseMetaData.java#L998); under this policy that fails past 4 columns. skylite’s JDBC driver must not build metadata results that way. skylite’s Hibernate dialect, when it exists, must render composite-key predicates as row values, (a, b) IN ((?, ?), …), which this build runs at 16,000 pairs, rather than as an OR of AND pairs, which the expression depth stops at 16 pairs.

GORM v1.31.2 with gorm.io/driver/sqlite v1.6.0 runs, for AutoMigrate, queries against sqlite_master (migrator.go#L29-L76, #L109-L155, #L208-L225, #L281-L298), CREATE TABLE, CREATE INDEX (migrator.go#L252-L279) and ALTER TABLE ? ADD ? ? (gorm migrator.go#L122-L203, #L386-L387); GetIndexes uses PRAGMA_index_list and PRAGMA_index_info. CurrentDatabase reads PRAGMA database_list (migrator.go#L227-L231), which is refused; the SQLite migrator does not call it for AutoMigrate.

GORM changes a column, a constraint or drops a column by rebuilding the table: in a transaction, it creates <table>__temp, copies the rows, drops the table and renames the copy (migrator.go#L376-L430). Dropping a table that other tables reference with enforcement on first deletes its rows, which fires ON DELETE CASCADE and SET NULL actions or, for other actions, fails the DROP (foreignkeys.html, section 5). Only DropTable and AlterColumn turn enforcement off around the work, with RunWithoutForeignKey, which reads PRAGMA foreign_keys and sets it off and on again outside the transaction, ignoring errors (migrator.go#L18-L27, #L37-L38, #L78-L80). Under this policy, which allows the pragma, those two keep the referencing rows, as a test shows. DropColumn, CreateConstraint and DropConstraint rebuild without it (#L157-L158, #L168-L172, #L193-L200): on a table others reference, they delete or null the referencing rows, or fail, under skylite and on any SQLite connection where the application has turned enforcement on. On SQLite’s default, enforcement off, nothing is lost. The setting holds on the connection that ran it, so the pragma and GORM’s transaction must reach the same server connection for the rows to survive; the Go driver’s design has to ensure that.

The test suite runs each of these statements on a database in memory and in a file, and all pass.

Testing

The package’s tests pin every default, the allowed and refused functions and pragmas, and the internal ALTER functions as literals. FuzzRun runs arbitrary SQL on a database file that holds the server’s settings (WAL, synchronous = FULL, no automatic checkpoint, and values other than SQLite’s defaults for the rest) and checks after each input that every setting the server owns, every database configuration flag and limit, the attached databases, the database file’s size and integrity_check are unchanged, and that SQLite opened no file but the database’s own. Run it with go test ./internal/sqlpolicy -run '^$' -fuzz FuzzRun -fuzztime 3m -fuzzminimizetime 0; without -fuzzminimizetime 0, Go’s minimizer spends up to a minute on each new input of a target that costs milliseconds, and in the first review half the intervals reported no executions.

Requirements on the engine and the server

The package cannot meet these alone. SQL within the limits can still overflow the C stack (The C stack) or exhaust the connection’s memory, so these requirements are what make a broken connection safe.

  1. A broken connection’s locks are released without SQLite. When a connection is reported broken (ErrBroken, which includes a stack overflow, or a memory refusal), the engine releases everything that connection held and discards its uncommitted transaction, without running SQLite on it again. Amended: the engine meets this by releasing the connection’s SQLite instance, closing every file it holds through the engine’s VFS and unmapping its memory, which holds the shared-memory index’s mapping and so its locks, rather than by keeping lock state in its own VFS (0004 decision 6). It does so for a panic that reaches its Run and for a Run whose function returns an error wrapping engine.ErrBroken; the engine’s Run then fails with engine.ErrBroken, as not committed or, when the Run committed before the connection broke, with that transaction’s number. So the server’s function must return an error wrapping engine.ErrBroken when this package reports ErrBroken or a memory refusal. If the broken connection had reached the commit path, the fail-stop rule of 0004 decision 6 applies. Because the package cannot tell whether a write transaction was open, the engine assumes it was.
  2. A broken connection is never reused, and is replaced.
  3. The deadline reaches page reads. The engine’s VFS checks the statement’s deadline on every page read and fails the read once it has passed, so that a single operation that touches many pages, such as deleting every row of a large table or an integrity check, ends with an error.
  4. Reset between holders. The server calls Conn.Reset before a connection is used by a different channel or principal, and discards a connection for which it fails. That includes the pooled connections of the stateless read path: PRAGMA foreign_keys = OFF passes SQLite’s read-only test (measured, Stmt.ReadOnly is true for it), so it reaches that path as a pure read, and 0005 decision 6 shares those connections on the ground that a pure read leaves no state behind. The server resets a pooled connection before every reuse, or refuses the pragma on that path.
  5. Order of set-up. The engine calls Apply after its own set-up of a connection and issues every later statement of its own through Privileged, qualified with main.. The first statement on a connection that reads the schema loads it, and clients wrote it. The engine’s set-up statements, such as journal_mode, synchronous and cache_size, load it when they run before Apply, outside Apply’s deadline and panic containment (measured: with 400 tables of 1,000 columns and a 4 MiB memory cap, each of those three and SELECT 1 panicked with out of memory). So the engine runs its set-up under its deadline, with SetInterrupt, recovers an engine panic there and discards the connection; or it runs no statement that reads the schema before Apply. If the schema is not yet loaded, Apply’s first statement loads it; Apply is interrupted when the engine’s context ends, a panic in the engine there is returned as an error, and the engine discards a connection for which Apply fails. It registers no SQL function after Apply: Apply replaces only the refused functions that exist when it runs, and an innocuous function registered later is reachable through ALTER TABLE … ADD COLUMN.
  6. Schema only through the policy. Nothing writes a skylite database’s schema except client statements under this policy and the server’s own statements.
  7. Server statements reach no client SQL. A statement under Privileged runs a client’s triggers and views with no authorizer and no time limit (measured by the second review: a temp trigger’s work ran 1.4 s to completion under the server’s INSERT, where the client’s own INSERT was stopped at 200 ms). The server’s statements never write a table a client can attach a trigger to and never read a view a client defined; if the engine needs such a statement, it runs it under the function rules and a deadline.
  8. The server checks its transaction after client SQL. The server reads autocommit after every Run and treats a transaction that has gone as ended, as 0005 decision 6 does. It never begins a transaction with SAVEPOINT and holds no savepoint of its own while client SQL runs.

Not in the first version

  • A policy per principal, for example read-only clients, and row-level rules.
  • Resource accounting per tenant. The per-connection memory cap is the engine’s choice when it opens a connection.
  • Result size and row count, which belong to the server.

Alternatives considered

  • Parse SQL with a separate parser and decide on its syntax tree. Rejected: the policy would have to track SQLite’s grammar exactly, and any statement it parsed differently from SQLite would bypass it. The authorizer sees what SQLite actually compiles, including statements SQLite compiles itself, views, triggers and the ATTACH inside VACUUM.
  • A denylist of dangerous actions, pragmas and functions. Rejected: SQLite and ncruces add pragmas, functions and authorizer codes between releases, and a denylist would allow each new one until someone noticed. A default-deny allowlist refuses them, and the registry test reports them.
  • sqlite3_stmt_readonly or PRAGMA query_only as the guard. Rejected: neither refuses ATTACH, pragmas or functions, and sqlite3_stmt_readonly is true for ATTACH.
  • Bounding each nesting limit alone. Rejected: the first review broke the connection at the earlier defaults with three levels of 99-term compounds, 5,642 bytes of SQL. New bounds a modelled product instead, which is a floor (The C stack).
  • Refusing trim, ltrim and rtrim. Rejected: Hibernate uses them, and the authorizer cannot refuse only the two-argument forms.
  • Server-owned foreign_keys. Rejected: GORM’s table rebuild ignores the refusal and deletes referencing rows.

Consequences

  • The engine must apply the policy to every connection that runs client SQL, run that SQL only through Conn.Run and its own through Conn.Privileged, and meet the requirements above.
  • skylite’s JDBC driver must answer DatabaseMetaData without a compound SELECT per column.
  • Clients meet the nesting limits of The C stack, including cascade trees of at most 8 levels at the defaults, and SQL within those limits can still break its connection by overflowing the stack, which breaks only that connection.
  • skylite’s Hibernate dialect must render composite-key predicates as row values.
  • One statement can hold its connection for tens of seconds past the time limit at the default MaxLength; see Time.
  • GORM’s DropColumn, CreateConstraint and DropConstraint on tables that other tables reference delete or null the referencing rows, as on any SQLite connection with enforcement on.
  • Upgrading ncruces means rerunning the package’s tests, which check the function and pragma registries and the stack shapes at the limits, and measuring the stack and the function sweep again.