# plugin_sqlite — read-only SQLite explorer for SCORPIOX BOT A clean-room, **strictly read-only** local SQLite schema and query browser, written in pure C99. One binary, three modes: discovery metadata, a JSON node API, and a responsive HTML tool UI. The public-domain SQLite amalgamation (`vendor/sqlite3.c`, 3.53.4) is compiled **into** the binary, so nothing needs a `sqlite3` CLI, `libsqlite3.so` or `sqlite3.dll` on the node. | | | |---|---| | slug | `sqlite` | | executable | `plugin_sqlite` (`plugin_sqlite.exe` on Windows) | | metadata | `{"name":"sqlite","desc":"SQLite viewer"}` | | category | `devtools` | | license | MIT (SQLite itself is public domain) | ## What it does * Discovers `.db` / `.db3` / `.sqlite` / `.sqlite3` files under `/workspace`, `/codebases`, `$HOME` and `$SQLITE_ALLOWED_ROOT` (depth 4, 60 000 entries **per root**, symlinked directories never followed). Each root gets its own budget, so one enormous tree cannot starve the others, and `action=dbs` reports per-root `dbs` / `scanned_entries` / `truncated` so a short list caused by the cap is visible rather than looking like an empty node. `$SQLITE_SCAN_BUDGET` lowers the allowance (tests use it). * Shows tables and views with row counts, columns (type / NOT NULL / primary key) and indexes (unique or not). * Runs `SELECT` / `WITH` / read-only `PRAGMA` / `EXPLAIN` statements and streams typed rows as JSON: integers and reals as JSON numbers, text as strings, `NULL` as `null`, and BLOBs as a bounded `{"blob_bytes":N,"hex":"..."}` preview. * Renders an HTML explorer: database picker, sidebar table list, SQL editor (`Ctrl`+`Enter` to run), sortable result grid, CSV and JSON copy, dark and light themes, `?embed=1` aware. ## Read-only by construction Two independent mechanisms, because a single one is a promise and two are a design: 1. **`SQLITE_OPEN_READONLY`** on every `sqlite3_open_v2()`, plus `SQLITE_LIMIT_ATTACHED = 0`. 2. **`sqlite3_set_authorizer()`** that answers `SQLITE_OK` only for `SQLITE_SELECT`, `SQLITE_READ`, `SQLITE_RECURSIVE`, `SQLITE_FUNCTION`, and a whitelist of read-only PRAGMAs (`table_info`, `index_list`, `foreign_key_list`, `page_count`, `integrity_check`, ...). Everything else — `INSERT`, `UPDATE`, `DELETE`, `CREATE`, `DROP`, `ALTER`, `ATTACH`, `DETACH`, `TRANSACTION`, `REINDEX`, `VACUUM`, and settable pragmas such as `journal_mode=...` — is `SQLITE_DENY`, which the engine enforces at prepare time. On top of that: * The statement must start with `select`, `with`, `pragma`, `explain` or `values` (friendly errors rather than a bare "not authorized"), and stacked statements are rejected. * Result rows are capped at **500** (`truncated: true` when the cap bites). * A `sqlite3_progress_handler` aborts any statement that runs past **~2 s** (`aborted: true`, partial rows kept). ## Path discipline Only regular files with a database suffix, resolved through `realpath()`, inside one of the allowlisted roots are opened. Rejected outright: any `..` segment, `file:`-prefixed SQLite URI filenames, `://` schemes, query strings and fragments, non-database suffixes, symlinks pointing out of a root, and system paths such as `/etc/passwd`. An allowlisted-but-absent file answers `not_found`, which is a different error from `forbidden_path` on purpose — the UI can tell "you looked in the wrong place" from "you are not allowed there". Test the policy yourself: ``` curl -s -G 'http://127.0.0.1:8150/plugin_sqlite' -H 'X-Bot-Password: ' \ --data-urlencode 'action=query' --data-urlencode 'db=/etc/passwd' --data-urlencode 'sql=select 1' # {"ok":false,"error":"forbidden_path",...} ``` ## API All routes answer `application/json; charset=utf-8` unless noted. Everything except `--meta` requires the node password (`X-Bot-Password`, `Authorization: Bearer`, or `?pwd=`; unset `BOT_PASSWORD` disables the check, which is how you run it from a shell). | Route | Answer | |---|---| | `?action=dbs` | `{ok, roots:[{label,path,exists,dbs,scanned_entries,truncated}], count, max_scan_entries, dbs:[{path,root,bytes,mtime}]}` | | `?action=schema&db=` | `{ok, db, tables:[{name,type,rows,columns:[{name,type,notnull,pk}],indexes:[{name,unique}]}], table_count}` | | `?action=query&db=&sql=