Skip to content

fix: correctness and hardening fixes, release 0.2.0 - #1

Merged
marcobambini merged 1 commit into
mainfrom
fix/correctness-and-hardening-0.2.0
Nov 1, 2026
Merged

marcobambini merged 1 commit into
mainfrom
fix/correctness-and-hardening-0.2.0

Conversation

@marcobambini

Copy link
Copy Markdown
Member

Fixes found while auditing the sources. Bumps SQLITE_AGENT_VERSION to 0.2.0
and adds a CHANGELOG.md.

Merging this releases 0.2.0. The release job runs on main whenever the
version differs from the latest GitHub release, and publishes to npm, Maven
Central and pub.dev. This PR also rewrites parts of that release workflow, which
could not be executed before merging — the CI run on this branch covers the
build matrix but, by design, not the signing or publishing steps.

Breaking changes

  • agent_run() is registered SQLITE_DIRECTONLY: it can no longer be called
    from a VIEW, TRIGGER, CHECK constraint, generated column or index expression.
    It makes network calls and writes rows, so querying a database file prepared
    elsewhere could otherwise trigger those side effects on the caller's behalf.
  • max_iterations must be in 1..100. Non-positive values divided by zero; an
    unbounded value let one statement spend unbounded time and money.
  • A tool is invoked only if the MCP server advertised it.

Memory safety and resource handling

  • Stack buffer overflow at roughly 22 or more columns: the INSERT was
    assembled with unbounded strcat() into two fixed 512-byte buffers. Now built
    once per call with sqlite3_str. AddressSanitizer reports
    stack-buffer-overflow on the previous code for the same input.
  • The caller's transaction was destroyed. BEGIN/COMMIT/ROLLBACK ran
    with return codes ignored while one of the caller's statements was already
    executing, so the inner ROLLBACK discarded the caller's uncommitted work.
    Replaced with a SAVEPOINT, which nests safely.
  • agent_run()'s stack frame goes from ~155 KB to ~6 KB; the old frame exceeded
    musl's 128 KB default thread stack.
  • Four leaks of the 64 KB tool list on table-mode error paths.

Parsing and data handling

  • JSON objects are delimited by matching braces, honouring nesting and string
    literals; escape sequences including \uXXXX are decoded. The first } used
    to end the object, losing every field after a nested one and mis-splitting
    subsequent rows.
  • Values are bound by SQLite type affinity. Columns declared INT, BIGINT,
    FLOAT, DOUBLE, NUMERIC or VARCHAR(n) took the string path, where an
    unquoted JSON number was never bound and the column became NULL.
  • Tool results are fed back to the model in text mode; the system prompt was
    re-sent unchanged, so the model never saw what a tool returned.
  • The truncation budget is capped by the buffer that holds the message.
  • Tool arguments survive a } inside a string value.

Prompt-injection controls

MCP-supplied text is fenced with a per-call random delimiter and the model is
told it is data, so injected instructions cannot escape the data region.
Identifiers are quoted with %w, string literals with %q. Neither control
protects against a malicious MCP server; see "Security model" in API.md.

Supply chain and build

  • Apple signing secrets restricted to main; secrets passed through env
    rather than interpolated into shell bodies; keychain cleaned up on failure.
  • Actions pinned to commit SHAs; workflow permissions default to
    contents: read; the SQLite amalgamation is verified against a pinned
    SHA-256 before being compiled.
  • Stack protector and _FORTIFY_SOURCE everywhere, RELRO and non-executable
    stack on Linux/Android, NX and ASLR on Windows.
  • package-lock.json tracked; package generation fails when a platform binary
    is missing instead of publishing an empty package.

Node package

The ESM entry point always threw ExtensionNotFoundError, claiming the platform
package was not installed, because the bundler rewrites a bare require() into
a throwing stub. Verified against the real tsup output in both formats.

Verification

Checked locally on macOS arm64 (universal build, 0 warnings, -std=c99 -pedantic clean):

  • Each fix verified by difference — the tests fail on the previous code and pass
    on this branch.
  • AddressSanitizer clean over repeated runs with the extension statically
    linked; leaks reports 0 leaks.
  • The 10 agent_run() forms in README.md, USAGE.md and API.md all work,
    including the subquery and CTE forms, and the 4 documented error cases behave
    as described.
  • The airbnb and github examples produce byte-identical output with the
    previous extension; both stop at Failed to load AI extension because
    sqlite-ai is not installed on that machine, so they never reach
    agent_run().

Not verifiable locally: the Linux, musl, Android and Windows hardening flags
(no cross sysroot), and the release workflow itself.

🤖 Generated with Claude Code

Fixes found while auditing the sources, with three behaviour changes.

Breaking:
- agent_run() is registered SQLITE_DIRECTONLY, so it can no longer be
  reached from a VIEW, TRIGGER, CHECK constraint, generated column or
  index expression. It makes network calls and writes rows, so querying
  a database file prepared elsewhere could otherwise trigger those side
  effects on the caller's behalf.
- max_iterations must be in 1..100. Non-positive values divided by zero;
  an unbounded value let one statement spend unbounded time and money.
- A tool is invoked only if the MCP server advertised it.

Memory safety and resource handling:
- Fix a stack buffer overflow at ~22 columns: the INSERT was built with
  unbounded strcat() into two 512-byte buffers. It is now assembled once
  per call with sqlite3_str. Same fix for the embedding statements.
- Stop destroying the caller's transaction. BEGIN/COMMIT/ROLLBACK ran
  with return codes ignored while one of the caller's statements was
  already executing, so the inner ROLLBACK discarded the caller's
  uncommitted work. Use a SAVEPOINT, which nests safely.
- Cut the agent_run() stack frame from ~155KB to ~6KB by moving scratch
  buffers to the heap; the old frame exceeded musl's 128KB thread stack.
- Fix four leaks of the 64KB tool list on table-mode error paths.

Parsing and data handling:
- Delimit JSON objects by matching braces, honouring nesting and string
  literals, and decode escape sequences. The first '}' used to end the
  object, losing every field after a nested one and mis-splitting rows.
- Bind values by SQLite type affinity. Columns declared INT, BIGINT,
  FLOAT, DOUBLE, NUMERIC or VARCHAR(n) took the string path, where an
  unquoted JSON number was never bound and the column became NULL.
- Feed tool results back to the model in text mode; the system prompt
  was re-sent unchanged, so the model never saw what a tool returned.
- Cap the truncation budget by the buffer that holds the message.

Prompt-injection controls:
- Fence MCP-supplied text with a per-call random delimiter and tell the
  model it is data, so injected instructions cannot escape the data
  region. Quote identifiers with %w and string literals with %q.

Supply chain and build:
- Restrict Apple signing secrets to main, pass secrets through env, and
  clean up the keychain even after a failure.
- Pin actions to commit SHAs, default workflow permissions to
  contents: read, and verify the SQLite amalgamation against a pinned
  SHA-256 before compiling it.
- Enable stack protector and _FORTIFY_SOURCE everywhere, RELRO and a
  non-executable stack on Linux/Android, NX and ASLR on Windows.
- Track package-lock.json to pin the toolchain that builds the bundle.
- Fail package generation when a platform binary is missing.

Node package:
- Fix the ESM entry point, which always threw ExtensionNotFoundError
  because the bundler rewrites a bare require() into a throwing stub.

Add CHANGELOG.md and a "Security model" section to API.md.

Co-Authored-By: Claude Opus 5 <[email protected]>
@marcobambini
marcobambini merged commit ae02599 into main Nov 1, 2026
16 checks passed
@marcobambini
marcobambini deleted the fix/correctness-and-hardening-0.2.0 branch November 1, 2026 13:59
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.

1 participant