scorpiox-bot-plugin-sqlite

scorpiox-bot-plugin-sqlite

main1 branch · 0 tags
VS Code
HTTPS
ForkDelete
Recent Commits
README

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: <pwd>' \
     --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=<path> {ok, db, tables:[{name,type,rows,columns:[{name,type,notnull,pk}],indexes:[{name,unique}]}], table_count}
?action=query&db=<path>&sql=<SELECT ...> {ok, db, columns:[{name,decl}], rows:[[...]], row_count, truncated, limit, ms, aborted, types}
?action=info plugin id, version, bundled SQLite version, platform, limits
?format=json falls back to action=dbs
GET (no action) HTML explorer, honours ?embed=1

query also accepts POST with a sql=... body: the caller is expected to hand the urlencoded

body through the HTTP_BODY_FILE environment variable (the form scorpiox-server uses). A bare

body is treated as the statement itself.

Rows: text is a JSON string, integer/real are JSON numbers, NULL is null, BLOB is

{"blob_bytes":N,"hex":"..."} with the hex preview truncated after 48 bytes.

Build

make            # native (glibc)          -> ./plugin_sqlite
make musl       # static musl x86_64      -> ./plugin_sqlite.musl
make arm64      # static aarch64          -> ./plugin_sqlite.arm64
make win64      # MinGW-w64 static PE     -> ./plugin_sqlite.exe
make macos      # Apple clang (on macOS)  -> ./plugin_sqlite
make test       # run tests/run.sh

Windows without a cross toolchain: powershell -ExecutionPolicy Bypass -File build.ps1.

The SQLite amalgamation compiles at -O1 (roughly 10 s on a laptop, about a minute on a Windows

box). Bumping the engine to -O2 changes nothing measurable for an I/O-bound 500-row browser.

Tests

tests/run.sh builds a throwaway database, points the plugin at it with SQLITE_ALLOWED_ROOT,

and runs 134 assertions:

  • --meta shape (single-word name, at most 15-char description, compact, valid JSON)
  • node auth: 401 without credentials, X-Bot-Password / Bearer / ?pwd= accepted, wrong

password refused, HTML route gated too

  • discovery, schema (tables, views, indexes, row counts, column detail) and queries,

including that every allowlisted root is scanned with its own budget (a truncated root is

reported as truncated: true and does not starve the roots after it)

  • typed values: integers, reals, text, JSON null, BLOB hex preview
  • read-only: 15 rejected mutations (INSERT/UPDATE/DELETE/DROP/ALTER/CREATE TABLE/CREATE VIEW/

ATTACH/DETACH/REINDEX/VACUUM/BEGIN/settable PRAGMA/stacked statement/WITH-DELETE) plus proof

the file on disk did not change and no ATTACH file appeared

  • path discipline: /etc/passwd, .. traversal, file: URIs, :// schemes, non-db suffixes
  • the 500-row cap and the ~2 s statement budget
  • the HTML UI (no CDN scripts, no Content-Length, embed behaviour)
  • artifact checks when the musl / arm64 / Win64 builds are present (ELF magic, e_machine,

static-link proof, MZ header)

  • a full git-history audit for LAN IPs, tokens and private keys

Install on a node

cp plugin_sqlite ~/.scorpiox/plugins/ && chmod +x ~/.scorpiox/plugins/plugin_sqlite
curl -s -H 'X-Bot-Password: <pwd>' 'http://127.0.0.1:8150/plugin_sqlite?action=info'

Discovery is automatic — no server restart. On Windows the plugin directory is

%LOCALAPPDATA%\scorpiox\plugins\.

Source-first

git clone https://plugingit.scorpiox.net/scorpiox-bot-plugin-sqlite.git

Audit src/plugin_sqlite.c, change the allowlisted roots or the row cap for your setup, and

compile it yourself — it is one C file plus the vendored amalgamation and libc.

Layout

src/plugin_sqlite.c   the whole plugin (3 modes, path gate, authorizer, HTML UI)
vendor/sqlite3.{c,h}  public-domain SQLite 3.53.4 amalgamation
tests/run.sh          134 offline assertions
Makefile              native / musl / arm64 / win64 / macos / test / clean
build.ps1             Windows MinGW static build
release-dist.sh       build all targets, package, publish, mirror
plugin.json           marketplace meta