Skip to content

Keep the functions that run code out of the schema - #13

Merged
marcobambini merged 2 commits into
sqliteai:mainfrom
davidschachterADFA:keep-code-functions-out-of-schema
Nov 12, 2026
Merged

marcobambini merged 2 commits into
sqliteai:mainfrom
davidschachterADFA:keep-code-functions-out-of-schema

Conversation

@davidschachterADFA

@davidschachterADFA davidschachterADFA commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

js_eval(), js_create_*(), js_load_text(), js_load_blob(), js_init_table() and js_set_max_stack_size() are registered without SQLITE_DIRECTONLY, so a view, trigger, CHECK constraint or generated column can call them. With PRAGMA trusted_schema on, which is the SQLite library's default (the sqlite3 shell turns it off), a database from an untrusted source runs JavaScript or reads files as soon as it is queried:

CREATE VIEW v AS SELECT js_load_text('/path/to/a/private/file');

This registers every function but js_version() with SQLITE_DIRECTONLY, so only top-level SQL can call them. The flag is computed from the function's name rather than from a table that parallels the function list, so a function added later gets it without a matching edit. The functions made with js_create_* are unchanged and still work in views, triggers and indexes. The README explains this in a new "Functions in the Schema" section, with a note that js_init_table(1) runs the JavaScript a database stores.

Test: a new Direct only test checks that every function but js_version() has the flag in function_list and, with trusted_schema on, that a view, a trigger and a generated column cannot call js_eval() or js_load_text(), that a top-level statement can, and that a view can still use js_version() and a function made with js_create_scalar(). make test passes on x86_64 Linux, also under ASan and UBSan.

These are six separate pull requests, #11 to #16, so each can be taken or declined on its own; only #15 depends on another (#14). They add tests at the same places in test/main.c, and #11 and #12 both add code just above globaljs_init(), so those merged later will need a small rebase, which we will do.

This pull request originated with AI. Claude, Anthropic's AI model, wrote the code, the tests and this description while packaging sqlite-js for Code on the Go, App Dev For All's Android IDE, which ships sqlite-js with this change. Contact: [email protected]

🤖 Generated with Claude Code

https://claude.ai/code/session_01Vh2DAXGyHbMzjSsi2zbrkw

Update, 2026-11-12. The README now says that TEMP views and triggers can still call these functions, since SQLite trusts the TEMP schema (72db0d2).

davidschachterADFA and others added 2 commits November 10, 2026 22:30
js_eval(), js_create_*(), js_load_text(), js_load_blob(), js_init_table()
and the js_set_* functions were registered without SQLITE_DIRECTONLY, so
a view, trigger, CHECK constraint or generated column could call them.
With PRAGMA trusted_schema ON, the default of the SQLite library (the
sqlite3 shell turns it off), a database from an untrusted source could
run JavaScript or read files just by being queried:
`CREATE VIEW v AS SELECT js_eval('...')`.

Register every function but js_version() with SQLITE_DIRECTONLY, so only
top-level SQL can call them. The functions created with js_create_* are
unchanged, so they still work in views, triggers and indexes. The flag
is computed from the function's name rather than from a table that
parallels the function list, so a function added later gets it without
a matching edit.

Add a Direct only test: every function but js_version() has the flag in
function_list; with trusted_schema ON, a view, a trigger and a
generated column cannot call js_eval() or js_load_text(), a top-level
statement can, and a view can still use js_version() and a function made
with js_create_scalar().

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Vh2DAXGyHbMzjSsi2zbrkw
SQLite trusts the TEMP schema, since only the connection itself can make
it, so SQLITE_DIRECTONLY does not keep js_eval() and the others out of
TEMP views and triggers. Say so.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Vh2DAXGyHbMzjSsi2zbrkw
marcobambini added a commit that referenced this pull request Nov 12, 2026
…ntents

PR #13 added the section but not its table-of-contents entry.
@marcobambini
marcobambini merged commit cefb82d into sqliteai:main Nov 12, 2026
@marcobambini

Copy link
Copy Markdown
Member

Merged into main as part of a review of #11–#19 together.

Verified locally on macOS/arm64: universal build with no new warnings, full suite green, clean under ASan + UBSan.

The new Direct only test is load-bearing — with flags forced to 0 the pragma_function_list assertion fails immediately. I separately confirmed the part that matters most for compatibility: js_create_common still registers user functions without SQLITE_DIRECTONLY, so functions made with js_create_* keep working in views, triggers and indexes. Only the extension's own entry points are restricted.

Computing the flag from the function name rather than a parallel table is the right call: a function added later gets it without a matching edit.

One small thing I fixed on merge (1c6f1c8): the new top-level Functions in the Schema section was missing from the README's table of contents, so I added the entry.

Worth stating plainly for anyone reading later: this is a deliberate breaking change for a database that already calls js_eval() from a view or trigger. The security argument carries it — with PRAGMA trusted_schema on, which is the library default, a hostile database could run JavaScript or read files just by being queried — and TEMP views and triggers still work, as your README update notes.

Thanks, good catch and a thorough test.

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.

2 participants