Skip to content

fix(sof): cap SQLite value size for SQL Queries at 128 MiB - #3296

Open
angela-helios wants to merge 2 commits into
mainfrom
fix/3186-sqlite-value-length-limit
Open

angela-helios wants to merge 2 commits into
mainfrom
fix/3186-sqlite-value-length-limit

Conversation

@angela-helios

Copy link
Copy Markdown
Contributor

Closes #3186

Behaviour change to review

  • A SQL Query ($sql-run, SQL Views) on the SQLite, MongoDB and S3 backends that builds or reads a single string, BLOB or row larger than 128 MiB now fails with 422 SQLite error: string or blob too big. Before, SQLite's default of 1,000,000,000 bytes applied.
  • The limit is a fixed constant (helios_sof::sqlquery::MAX_SQL_VALUE_BYTES), not a setting. 128 MiB matches the default request body limit, so a resource the server accepts by default can still be read by a SQL Query. A deployment that raised the body limit and stored a resource larger than 128 MiB will see SQL Queries over that row fail on SQLite.
  • This reduces the largest value one expression can allocate by about 8x. It does not make it small: a lower or configurable limit is left for a follow-up.

Summary

The SQLite connections that run caller SQL kept SQLite's default SQLITE_LIMIT_LENGTH, so zeroblob, randomblob or printf could build a value of close to 1 GB from a small request.

  • The in-memory engine used for MongoDB and S3 sets SQLITE_LIMIT_LENGTH to 128 MiB when it opens its per-request database.
  • The primary SQLite path lowers the limit on the pooled connection for the statement and restores the connection's previous value in the same drop guard that removes the authorizer and the progress handler, so the write path that shares the pool is unchanged.
  • rusqlite's limits feature is enabled in helios-sof and helios-persistence.
  • The error reaches the client through the existing SqlQueryError::Sqlite mapping (422).

Not in this change: SQLITE_LIMIT_SQL_LENGTH, SQLITE_LIMIT_EXPR_DEPTH, SQLITE_LIMIT_LIKE_PATTERN_LENGTH, and PostgreSQL.

Tests

  • crates/sof/src/sqlquery/engine.rs: zeroblob(900000000), randomblob(200000000), printf('%.*c', 900000000, 'x') and values one byte past the limit fail with SQLite's TooBig; 1 MB values are still returned.
  • crates/persistence/src/sof/sqlite_native.rs: the same on a pooled connection, and the connection has its original limit back after a successful statement, a too-big statement and a refused statement.

Results:

  • cargo test -p helios-sof --lib: 420 passed.
  • cargo test -p helios-persistence --lib: 2081 passed.
  • cargo test -p helios-persistence --test sof_sqlite_runner native_sql_query: 1 passed.
  • I did not run clippy locally; CI runs it.

These ran on a branch that also carried fourteen other pending fixes, so the suites were verified in combination, not on this branch alone.

The SQLite connections that run caller SQL kept the default
SQLITE_LIMIT_LENGTH of 1,000,000,000 bytes, so zeroblob, randomblob or
printf could build a value of close to 1 GB from a small $sql-run
request.

The in-memory engine now sets the limit when it opens its database. The
primary SQLite path lowers it for the statement and restores the pooled
connection's previous value with the other per-statement hooks. A value
past the limit fails with "string or blob too big", reported as 422.

Closes #3186
@claude

claude Bot commented Nov 30, 2026

Copy link
Copy Markdown

Code review

No issues found. Checked for bugs and CLAUDE.md compliance.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SQL-on-FHIR on SQLite: caller SQL can build values near 1 GB (zeroblob, randomblob, printf); no SQLITE_LIMIT_LENGTH

1 participant