Repository navigation
fix: correctness and hardening fixes, release 0.2.0 - #1
Merged
Merged
Conversation
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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes found while auditing the sources. Bumps
SQLITE_AGENT_VERSIONto0.2.0and adds a
CHANGELOG.md.Merging this releases 0.2.0. The release job runs on
mainwhenever theversion 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 registeredSQLITE_DIRECTONLY: it can no longer be calledfrom 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_iterationsmust be in1..100. Non-positive values divided by zero; anunbounded value let one statement spend unbounded time and money.
Memory safety and resource handling
INSERTwasassembled with unbounded
strcat()into two fixed 512-byte buffers. Now builtonce per call with
sqlite3_str. AddressSanitizer reportsstack-buffer-overflowon the previous code for the same input.BEGIN/COMMIT/ROLLBACKranwith return codes ignored while one of the caller's statements was already
executing, so the inner
ROLLBACKdiscarded 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 exceededmusl's 128 KB default thread stack.
Parsing and data handling
literals; escape sequences including
\uXXXXare decoded. The first}usedto end the object, losing every field after a nested one and mis-splitting
subsequent rows.
INT,BIGINT,FLOAT,DOUBLE,NUMERICorVARCHAR(n)took the string path, where anunquoted JSON number was never bound and the column became
NULL.re-sent unchanged, so the model never saw what a tool returned.
}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 controlprotects against a malicious MCP server; see "Security model" in
API.md.Supply chain and build
main; secrets passed throughenvrather than interpolated into shell bodies; keychain cleaned up on failure.
contents: read; the SQLite amalgamation is verified against a pinnedSHA-256 before being compiled.
_FORTIFY_SOURCEeverywhere, RELRO and non-executablestack on Linux/Android, NX and ASLR on Windows.
package-lock.jsontracked; package generation fails when a platform binaryis missing instead of publishing an empty package.
Node package
The ESM entry point always threw
ExtensionNotFoundError, claiming the platformpackage was not installed, because the bundler rewrites a bare
require()intoa throwing stub. Verified against the real
tsupoutput in both formats.Verification
Checked locally on macOS arm64 (universal build, 0 warnings,
-std=c99 -pedanticclean):on this branch.
linked;
leaksreports 0 leaks.agent_run()forms inREADME.md,USAGE.mdandAPI.mdall work,including the subquery and CTE forms, and the 4 documented error cases behave
as described.
airbnbandgithubexamples produce byte-identical output with theprevious extension; both stop at
Failed to load AI extensionbecausesqlite-aiis not installed on that machine, so they never reachagent_run().Not verifiable locally: the Linux, musl, Android and Windows hardening flags
(no cross sysroot), and the release workflow itself.
🤖 Generated with Claude Code