| index ac42d83..7a4b7f0 100644 | ||
| --- a/README.md | ||
| +++ b/README.md | ||
| @@ -17,8 +17,11 @@ binary, so nothing needs a `sqlite3` CLI, `libsqlite3.so` or `sqlite3.dll` on th | ||
| 17 | 17 | ## What it does |
| 18 | 18 | |
| 19 | 19 | * Discovers `.db` / `.db3` / `.sqlite` / `.sqlite3` files under `/workspace`, `/codebases`, |
| 20 | - `$HOME` and `$SQLITE_ALLOWED_ROOT` (depth 4, 60 000 entries, symlinked directories never | |
| 21 | - followed). | |
| 20 | + `$HOME` and `$SQLITE_ALLOWED_ROOT` (depth 4, 60 000 entries **per root**, symlinked | |
| 21 | + directories never followed). Each root gets its own budget, so one enormous tree cannot | |
| 22 | + starve the others, and `action=dbs` reports per-root `dbs` / `scanned_entries` / | |
| 23 | + `truncated` so a short list caused by the cap is visible rather than looking like an empty | |
| 24 | + node. `$SQLITE_SCAN_BUDGET` lowers the allowance (tests use it). | |
| 22 | 25 | * Shows tables and views with row counts, columns (type / NOT NULL / primary key) and indexes |
| 23 | 26 | (unique or not). |
| 24 | 27 | * Runs `SELECT` / `WITH` / read-only `PRAGMA` / `EXPLAIN` statements and streams typed rows as |
| @@ -72,7 +75,7 @@ requires the node password (`X-Bot-Password`, `Authorization: Bearer`, or `?pwd= | ||
| 72 | 75 | |
| 73 | 76 | | Route | Answer | |
| 74 | 77 | |---|---| |
| 75 | -| `?action=dbs` | `{ok, roots:[{label,path,exists}], count, dbs:[{path,root,bytes,mtime}]}` | | |
| 78 | +| `?action=dbs` | `{ok, roots:[{label,path,exists,dbs,scanned_entries,truncated}], count, max_scan_entries, dbs:[{path,root,bytes,mtime}]}` | | |
| 76 | 79 | | `?action=schema&db=<path>` | `{ok, db, tables:[{name,type,rows,columns:[{name,type,notnull,pk}],indexes:[{name,unique}]}], table_count}` | |
| 77 | 80 | | `?action=query&db=<path>&sql=<SELECT ...>` | `{ok, db, columns:[{name,decl}], rows:[[...]], row_count, truncated, limit, ms, aborted, types}` | |
| 78 | 81 | | `?action=info` | plugin id, version, bundled SQLite version, platform, limits | |
| @@ -105,12 +108,14 @@ box). Bumping the engine to `-O2` changes nothing measurable for an I/O-bound 50 | ||
| 105 | 108 | ## Tests |
| 106 | 109 | |
| 107 | 110 | `tests/run.sh` builds a throwaway database, points the plugin at it with `SQLITE_ALLOWED_ROOT`, |
| 108 | -and runs 120+ assertions: | |
| 111 | +and runs 134 assertions: | |
| 109 | 112 | |
| 110 | 113 | * `--meta` shape (single-word name, at most 15-char description, compact, valid JSON) |
| 111 | 114 | * node auth: 401 without credentials, `X-Bot-Password` / `Bearer` / `?pwd=` accepted, wrong |
| 112 | 115 | password refused, HTML route gated too |
| 113 | -* discovery, schema (tables, views, indexes, row counts, column detail) and queries | |
| 116 | +* discovery, schema (tables, views, indexes, row counts, column detail) and queries, | |
| 117 | + including that every allowlisted root is scanned with its own budget (a truncated root is | |
| 118 | + reported as `truncated: true` and does not starve the roots after it) | |
| 114 | 119 | * typed values: integers, reals, text, JSON `null`, BLOB hex preview |
| 115 | 120 | * read-only: 15 rejected mutations (INSERT/UPDATE/DELETE/DROP/ALTER/CREATE TABLE/CREATE VIEW/ |
| 116 | 121 | ATTACH/DETACH/REINDEX/VACUUM/BEGIN/settable PRAGMA/stacked statement/WITH-DELETE) plus proof |
| @@ -146,7 +151,7 @@ compile it yourself — it is one C file plus the vendored amalgamation and libc | ||
| 146 | 151 | ``` |
| 147 | 152 | src/plugin_sqlite.c the whole plugin (3 modes, path gate, authorizer, HTML UI) |
| 148 | 153 | vendor/sqlite3.{c,h} public-domain SQLite 3.53.4 amalgamation |
| 149 | -tests/run.sh 120+ offline assertions | |
| 154 | +tests/run.sh 134 offline assertions | |
| 150 | 155 | Makefile native / musl / arm64 / win64 / macos / test / clean |
| 151 | 156 | build.ps1 Windows MinGW static build |
| 152 | 157 | release-dist.sh build all targets, package, publish, mirror |
| index ce389d0..fdecf99 100644 | ||
| --- a/plugin.json | ||
| +++ b/plugin.json | ||
| @@ -1,9 +1,12 @@ | ||
| 1 | 1 | { |
| 2 | 2 | "slug": "sqlite", |
| 3 | 3 | "id": "plugin_sqlite", |
| 4 | + "name": "sqlite", | |
| 5 | + "desc": "SQLite viewer", | |
| 4 | 6 | "title": "SQLite Explorer", |
| 5 | 7 | "summary": "Zero-dependency, strictly read-only SQLite browser: discovers .db/.sqlite files under your workspace and home, shows tables, columns, indexes and row counts, and runs capped SELECT queries with a sortable result grid - the SQLite engine is compiled into the binary so no sqlite3 CLI, libsqlite3.so or sqlite3.dll is needed.", |
| 6 | 8 | "category": "devtools", |
| 9 | + "version": "1.0.1", | |
| 7 | 10 | "license": "MIT", |
| 8 | 11 | "capabilities": ["local_filesystem_read", "sqlite_readonly_open", "bounded_directory_walk"], |
| 9 | 12 | "trust_notice": "Executes native C code on your node. Opens databases with SQLITE_OPEN_READONLY and installs an authorizer that permits only SELECT, READ, FUNCTION and read-only PRAGMA statements, so INSERT/UPDATE/DELETE/DROP/ALTER/ATTACH/CREATE are refused by the engine. Only .db/.sqlite/.sqlite3 files under /workspace, /codebases, $HOME or $SQLITE_ALLOWED_ROOT are browsable; results are capped at 500 rows and about 2 seconds per statement. Makes zero outbound network requests and opens no sockets." |
| index 11ef894..d6ecc71 100755 | ||
| --- a/release-dist.sh | ||
| +++ b/release-dist.sh | ||
| @@ -28,7 +28,7 @@ set -euo pipefail | ||
| 28 | 28 | |
| 29 | 29 | SLUG="sqlite" |
| 30 | 30 | ID="plugin_sqlite" |
| 31 | -VERSION="1.0.0" | |
| 31 | +VERSION="1.0.1" | |
| 32 | 32 | DIST_BASE_URL="https://dist.scorpiox.net/bot-plugins" |
| 33 | 33 | |
| 34 | 34 | # Compile flags for the vendored SQLite amalgamation. |
| index 624c641..343ffc5 100644 | ||
| --- a/src/plugin_sqlite.c | ||
| +++ b/src/plugin_sqlite.c | ||
| @@ -71,7 +71,7 @@ | ||
| 71 | 71 | |
| 72 | 72 | #define PLUGIN_SLUG "sqlite" |
| 73 | 73 | #define PLUGIN_ID "plugin_sqlite" |
| 74 | -#define PLUGIN_VER "1.0.0" | |
| 74 | +#define PLUGIN_VER "1.0.1" | |
| 75 | 75 | |
| 76 | 76 | #define MAX_NAME 256 |
| 77 | 77 | /* glibc's fortified realpath() demands a PATH_MAX-sized buffer. */ |
| @@ -85,6 +85,22 @@ | ||
| 85 | 85 | #define MAX_SCAN_DEPTH 4 |
| 86 | 86 | #define STMT_BUDGET_MS 2000 |
| 87 | 87 | |
| 88 | +/* | |
| 89 | + * Per-root entry allowance for the discovery walk. $SQLITE_SCAN_BUDGET lowers | |
| 90 | + * it so tests can prove the truncation path on a tiny fixture; a value that is | |
| 91 | + * not a positive integer is ignored rather than trusted. | |
| 92 | + */ | |
| 93 | +static int scan_budget(void) | |
| 94 | +{ | |
| 95 | + static int cached = -1; | |
| 96 | + if (cached < 0) { | |
| 97 | + const char *e = getenv("SQLITE_SCAN_BUDGET"); | |
| 98 | + long v = (e && *e) ? strtol(e, NULL, 10) : 0; | |
| 99 | + cached = (v > 0 && v <= MAX_SCAN_ENTRIES) ? (int)v : MAX_SCAN_ENTRIES; | |
| 100 | + } | |
| 101 | + return cached; | |
| 102 | +} | |
| 103 | + | |
| 88 | 104 | /* ------------------------------------------------------------------ */ |
| 89 | 105 | /* Small utilities */ |
| 90 | 106 | /* ------------------------------------------------------------------ */ |
| @@ -687,7 +703,7 @@ static int skip_name(const char *n) | ||
| 687 | 703 | static int walk_dbs(const char *dir, const char *rootlabel, int depth, |
| 688 | 704 | sx_db_t *out, int max, int *count, int *visited) |
| 689 | 705 | { |
| 690 | - if (depth > MAX_SCAN_DEPTH || *count >= max || *visited > MAX_SCAN_ENTRIES) | |
| 706 | + if (depth > MAX_SCAN_DEPTH || *count >= max || *visited > scan_budget()) | |
| 691 | 707 | return 0; |
| 692 | 708 | |
| 693 | 709 | #if defined(_WIN32) |
| @@ -703,7 +719,7 @@ static int walk_dbs(const char *dir, const char *rootlabel, int depth, | ||
| 703 | 719 | SX_STAT st; |
| 704 | 720 | if (skip_name(fd.cFileName)) continue; |
| 705 | 721 | (*visited)++; |
| 706 | - if (*visited > MAX_SCAN_ENTRIES || *count >= max) break; | |
| 722 | + if (*visited > scan_budget() || *count >= max) break; | |
| 707 | 723 | snprintf(full, sizeof(full), "%s\\%s", dir, fd.cFileName); |
| 708 | 724 | if ((fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0) { |
| 709 | 725 | walk_dbs(full, rootlabel, depth + 1, out, max, count, visited); |
| @@ -728,7 +744,7 @@ static int walk_dbs(const char *dir, const char *rootlabel, int depth, | ||
| 728 | 744 | SX_STAT st; |
| 729 | 745 | if (skip_name(e->d_name)) continue; |
| 730 | 746 | (*visited)++; |
| 731 | - if (*visited > MAX_SCAN_ENTRIES || *count >= max) break; | |
| 747 | + if (*visited > scan_budget() || *count >= max) break; | |
| 732 | 748 | snprintf(full, sizeof(full), "%s/%s", dir, e->d_name); |
| 733 | 749 | if (lstat(full, &st) != 0) continue; |
| 734 | 750 | if (S_ISDIR(st.st_mode)) { |
| @@ -758,10 +774,34 @@ static void action_dbs(void) | ||
| 758 | 774 | { |
| 759 | 775 | sx_root_t roots[8]; |
| 760 | 776 | sx_db_t dbs[MAX_DBS]; |
| 761 | - int nroots, i, count = 0, visited = 0; | |
| 777 | + int nroots, i, count = 0; | |
| 778 | + int total_visited = 0; | |
| 779 | + int root_visited[8]; | |
| 780 | + int root_count[8]; | |
| 781 | + int root_truncated[8]; | |
| 762 | 782 | |
| 763 | 783 | nroots = collect_roots(roots, 8); |
| 764 | 784 | memset(dbs, 0, sizeof(dbs)); |
| 785 | + memset(root_visited, 0, sizeof(root_visited)); | |
| 786 | + memset(root_count, 0, sizeof(root_count)); | |
| 787 | + memset(root_truncated, 0, sizeof(root_truncated)); | |
| 788 | + | |
| 789 | + /* Each root gets its own entry budget. A single global counter lets one big | |
| 790 | + * tree (/codebases, a monorepo) exhaust the allowance before the walk ever | |
| 791 | + * reaches $HOME, so databases sitting in the user's home directory were | |
| 792 | + * undiscoverable - the UI reported "1 database" on a node that had three. | |
| 793 | + * The per-root counts also let the UI name the root that was truncated | |
| 794 | + * instead of silently showing a short list. */ | |
| 795 | + for (i = 0; i < nroots && count < MAX_DBS; i++) { | |
| 796 | + int before = count; | |
| 797 | + int visited = 0; | |
| 798 | + if (!roots[i].exists) continue; | |
| 799 | + walk_dbs(roots[i].path, roots[i].label, 0, dbs, MAX_DBS, &count, &visited); | |
| 800 | + root_visited[i] = visited; | |
| 801 | + root_count[i] = count - before; | |
| 802 | + root_truncated[i] = (visited > scan_budget() || count >= MAX_DBS); | |
| 803 | + total_visited += visited; | |
| 804 | + } | |
| 765 | 805 | |
| 766 | 806 | json_header(); |
| 767 | 807 | fputs("{\"ok\":true,\"action\":\"dbs\",\"plugin\":\"" PLUGIN_ID "\"", stdout); |
| @@ -773,17 +813,16 @@ static void action_dbs(void) | ||
| 773 | 813 | json_str(roots[i].label); |
| 774 | 814 | fputs(",\"path\":", stdout); |
| 775 | 815 | json_str(roots[i].path); |
| 776 | - printf(",\"exists\":%s}", roots[i].exists ? "true" : "false"); | |
| 816 | + printf(",\"exists\":%s,\"dbs\":%d,\"scanned_entries\":%d,\"truncated\":%s}", | |
| 817 | + roots[i].exists ? "true" : "false", | |
| 818 | + root_count[i], root_visited[i], | |
| 819 | + root_truncated[i] ? "true" : "false"); | |
| 777 | 820 | } |
| 778 | 821 | fputs("]", stdout); |
| 779 | 822 | |
| 780 | - for (i = 0; i < nroots && count < MAX_DBS; i++) { | |
| 781 | - if (!roots[i].exists) continue; | |
| 782 | - walk_dbs(roots[i].path, roots[i].label, 0, dbs, MAX_DBS, &count, &visited); | |
| 783 | - } | |
| 784 | - | |
| 785 | - printf(",\"count\":%d,\"scanned_entries\":%d,\"max_depth\":%d,\"dbs\":[", | |
| 786 | - count, visited, MAX_SCAN_DEPTH); | |
| 823 | + printf(",\"count\":%d,\"scanned_entries\":%d,\"max_depth\":%d,\"max_scan_entries\":%d," | |
| 824 | + "\"dbs\":[", | |
| 825 | + count, total_visited, MAX_SCAN_DEPTH, scan_budget()); | |
| 787 | 826 | for (i = 0; i < count; i++) { |
| 788 | 827 | char utc[40]; |
| 789 | 828 | time_t t = (time_t)dbs[i].mtime; |
| @@ -1204,7 +1243,8 @@ static int mode_html(int embed) | ||
| 1204 | 1243 | "<select id='dbsel'><option value=''>Loading databases...</option></select></div>" |
| 1205 | 1244 | "<button class='ghost' id='reloadbtn' type='button'>Rescan</button></div>" |
| 1206 | 1245 | "<div class='hint' id='dbhint'>Only .db / .sqlite / .sqlite3 files under the " |
| 1207 | - "allowlisted roots are listed.</div></div>", stdout); | |
| 1246 | + "allowlisted roots are listed.</div>" | |
| 1247 | + "<div class='hint' id='rootline'></div></div>", stdout); | |
| 1208 | 1248 | |
| 1209 | 1249 | fputs("<div class='layout'><div class='side'><h3>Tables</h3>" |
| 1210 | 1250 | "<div id='tables'><div class='empty'>Select a database.</div></div></div>", stdout); |
| @@ -1288,6 +1328,17 @@ static int mode_html(int embed) | ||
| 1288 | 1328 | " if (!d.ok) { err(d.error || 'failed to list databases'); return; }\n" |
| 1289 | 1329 | " var list = d.dbs || [];\n" |
| 1290 | 1330 | " txt('dbcount', list.length + (list.length === 1 ? ' database' : ' databases'));\n" |
| 1331 | +" /* Name the roots, and say so out loud when one was cut short - a short\n" | |
| 1332 | +" list because of the scan cap looks exactly like a node with no files. */\n" | |
| 1333 | +" var roots = d.roots || [], walked = [], cut = [];\n" | |
| 1334 | +" roots.forEach(function(r){\n" | |
| 1335 | +" if (!r.exists) return;\n" | |
| 1336 | +" walked.push(r.label);\n" | |
| 1337 | +" if (r.truncated) cut.push(r.label);\n" | |
| 1338 | +" });\n" | |
| 1339 | +" txt('rootline', walked.length ? ('roots: ' + walked.join(', ') +\n" | |
| 1340 | +" (cut.length ? ' \\u00b7 scan cap reached in ' + cut.join(', ') : '')) :\n" | |
| 1341 | +" 'no allowlisted root exists on this node');\n" | |
| 1291 | 1342 | " if (!list.length) {\n" |
| 1292 | 1343 | " var o = el('option', null, 'No .db / .sqlite files found');\n" |
| 1293 | 1344 | " o.value = ''; sel.appendChild(o);\n" |
| index 13c8788..acc711b 100755 | ||
| --- a/tests/run.sh | ||
| +++ b/tests/run.sh | ||
| @@ -108,7 +108,7 @@ DESC=$(printf '%s' "$META" | sed 's/.*"desc":"\([^"]*\)".*/\1/') | ||
| 108 | 108 | [ ${#DESC} -le 15 ] && pass "desc length ${#DESC} <= 15 chars" || fail "desc too long (${#DESC})" |
| 109 | 109 | NAME=$(printf '%s' "$META" | sed 's/.*"name":"\([^"]*\)".*/\1/') |
| 110 | 110 | [ "$(printf '%s' "$NAME" | wc -w | tr -d ' ')" -le 1 ] && pass "name is a single word" || fail "name has spaces" |
| 111 | -check "--version prints version" '1.0.0' "$("$BIN" --version)" | |
| 111 | +check "--version prints version" '1.0.1' "$("$BIN" --version)" | |
| 112 | 112 | check "--version names the sqlite lib" 'sqlite 3.' "$("$BIN" --version)" |
| 113 | 113 | check "--help is available" 'usage:' "$("$BIN" --help)" |
| 114 | 114 | META_AUTH=$(BOT_PASSWORD=secret "$BIN" --meta) |
| @@ -140,6 +140,8 @@ check "dbs declares read-only" '"readonly":true' "$DBS" | ||
| 140 | 140 | check "dbs carries the allowed root" "$TMP" "$DBS" |
| 141 | 141 | check "dbs finds our database" 'shop.sqlite' "$DBS" |
| 142 | 142 | check "dbs reports a count" '"count":' "$DBS" |
| 143 | +check "dbs reports per-root fields" '"scanned_entries":' "$DBS" | |
| 144 | +check "dbs declares the scan cap" '"max_scan_entries":' "$DBS" | |
| 143 | 145 | printf '%s' "$DBS" | python3 -c 'import json,sys; json.load(sys.stdin)' 2>/dev/null \ |
| 144 | 146 | && pass "dbs body is valid JSON" || fail "dbs body JSON invalid" |
| 145 | 147 | python3 - "$TMP/dbs.json" <<'PYEOF' && pass "dbs rows are well formed" || fail "dbs rows malformed" |
| @@ -155,9 +157,42 @@ for x in d["dbs"]: | ||
| 155 | 157 | assert x["bytes"] > 0 |
| 156 | 158 | assert x["root"] |
| 157 | 159 | assert x["mtime"].endswith("Z") |
| 160 | +for r in d["roots"]: | |
| 161 | + for k in ("label", "path", "exists", "dbs", "scanned_entries", "truncated"): | |
| 162 | + assert k in r, (k, r) | |
| 163 | + assert isinstance(r["dbs"], int) and r["dbs"] >= 0 | |
| 164 | + assert isinstance(r["scanned_entries"], int) and r["scanned_entries"] >= 0 | |
| 165 | + assert isinstance(r["truncated"], bool) | |
| 166 | +assert d["max_scan_entries"] > 0 | |
| 158 | 167 | PYEOF |
| 159 | 168 | check_not "dbs ignores non-db files" 'notes.txt' "$DBS" |
| 160 | 169 | |
| 170 | +# Every allowlisted root must get its own scan budget. One global counter let a | |
| 171 | +# large tree exhaust the allowance before the walk reached $HOME, so a database | |
| 172 | +# in the home directory was simply invisible. SQLITE_SCAN_BUDGET shrinks the | |
| 173 | +# allowance so a five-directory fixture can prove it. | |
| 174 | +BIGDIR="$TMP/bigtree" | |
| 175 | +mkdir -p "$BIGDIR" | |
| 176 | +for i in 1 2 3 4 5 6 7 8 9; do mkdir -p "$BIGDIR/f$i"; done | |
| 177 | +ROOTRUN=$(SQLITE_ALLOWED_ROOT="$BIGDIR" SQLITE_SCAN_BUDGET=4 QUERY_STRING='action=dbs' "$BIN" | sed '1,/^\r$/d') | |
| 178 | +check "dbs reports per-root scan counts" '"scanned_entries":' "$ROOTRUN" | |
| 179 | +check "dbs flags the truncated root" '"truncated":true' "$ROOTRUN" | |
| 180 | +HOMEHIT=$(printf '%s' "$ROOTRUN" | python3 -c ' | |
| 181 | +import json, sys | |
| 182 | +d = json.load(sys.stdin) | |
| 183 | +labels = {r["label"]: r for r in d["roots"]} | |
| 184 | +assert labels["allowed"]["truncated"] is True, labels["allowed"] | |
| 185 | +home = labels.get("home") | |
| 186 | +assert home is not None, labels | |
| 187 | +if home["exists"]: | |
| 188 | + assert home["scanned_entries"] > 0 or home["dbs"] > 0, home | |
| 189 | + print("home_walked", home["scanned_entries"]) | |
| 190 | +' 2>/dev/null) | |
| 191 | +case "$HOMEHIT" in | |
| 192 | +home_walked*) pass "home is still walked after a truncated root" ;; | |
| 193 | +*) fail "home was skipped after a truncated root (got: $HOMEHIT)" ;; | |
| 194 | +esac | |
| 195 | + | |
| 161 | 196 | # ---------------------------------------------------------------- schema |
| 162 | 197 | section "mode 2 action=schema" |
| 163 | 198 | SCHEMA=$(body "$(QUERY_STRING="action=schema&db=$DB" "$BIN")") |
| @@ -329,6 +364,7 @@ check "html has a doctype" '<!DOCTYPE html>' "$HTML" | ||
| 329 | 364 | check "html titles the tool" '<title>sqlite</title>' "$HTML" |
| 330 | 365 | check "html ships the sql editor" "id='sql'" "$HTML" |
| 331 | 366 | check "html ships the db picker" "id='dbsel'" "$HTML" |
| 367 | +check "html shows which roots were walked" "id='rootline'" "$HTML" | |
| 332 | 368 | check "html ships the result grid" "id='reswrap'" "$HTML" |
| 333 | 369 | check "html supports ctrl+enter" 'Ctrl' "$HTML" |
| 334 | 370 | check "html offers CSV copy" 'Copy CSV' "$HTML" |
| 335 | 371 |
| index ac42d83..7a4b7f0 100644 | index ac42d83..7a4b7f0 100644 | ||
| --- a/README.md | --- a/README.md | ||
| +++ b/README.md | +++ b/README.md | ||
| @@ -17,8 +17,11 @@ binary, so nothing needs a `sqlite3` CLI, `libsqlite3.so` or `sqlite3.dll` on th | @@ -17,8 +17,11 @@ binary, so nothing needs a `sqlite3` CLI, `libsqlite3.so` or `sqlite3.dll` on th | ||
| 17 | ## What it does | 17 | ## What it does |
| 18 | 18 | ||
| 19 | * Discovers `.db` / `.db3` / `.sqlite` / `.sqlite3` files under `/workspace`, `/codebases`, | 19 | * Discovers `.db` / `.db3` / `.sqlite` / `.sqlite3` files under `/workspace`, `/codebases`, |
| 20 | - `$HOME` and `$SQLITE_ALLOWED_ROOT` (depth 4, 60 000 entries, symlinked directories never | 20 | + `$HOME` and `$SQLITE_ALLOWED_ROOT` (depth 4, 60 000 entries **per root**, symlinked |
| 21 | - followed). | 21 | + directories never followed). Each root gets its own budget, so one enormous tree cannot |
| 22 | + starve the others, and `action=dbs` reports per-root `dbs` / `scanned_entries` / | ||
| 23 | + `truncated` so a short list caused by the cap is visible rather than looking like an empty | ||
| 24 | + node. `$SQLITE_SCAN_BUDGET` lowers the allowance (tests use it). | ||
| 22 | * Shows tables and views with row counts, columns (type / NOT NULL / primary key) and indexes | 25 | * Shows tables and views with row counts, columns (type / NOT NULL / primary key) and indexes |
| 23 | (unique or not). | 26 | (unique or not). |
| 24 | * Runs `SELECT` / `WITH` / read-only `PRAGMA` / `EXPLAIN` statements and streams typed rows as | 27 | * Runs `SELECT` / `WITH` / read-only `PRAGMA` / `EXPLAIN` statements and streams typed rows as |
| @@ -72,7 +75,7 @@ requires the node password (`X-Bot-Password`, `Authorization: Bearer`, or `?pwd= | @@ -72,7 +75,7 @@ requires the node password (`X-Bot-Password`, `Authorization: Bearer`, or `?pwd= | ||
| 72 | 75 | ||
| 73 | | Route | Answer | | 76 | | Route | Answer | |
| 74 | |---|---| | 77 | |---|---| |
| 75 | -| `?action=dbs` | `{ok, roots:[{label,path,exists}], count, dbs:[{path,root,bytes,mtime}]}` | | 78 | +| `?action=dbs` | `{ok, roots:[{label,path,exists,dbs,scanned_entries,truncated}], count, max_scan_entries, dbs:[{path,root,bytes,mtime}]}` | |
| 76 | | `?action=schema&db=<path>` | `{ok, db, tables:[{name,type,rows,columns:[{name,type,notnull,pk}],indexes:[{name,unique}]}], table_count}` | | 79 | | `?action=schema&db=<path>` | `{ok, db, tables:[{name,type,rows,columns:[{name,type,notnull,pk}],indexes:[{name,unique}]}], table_count}` | |
| 77 | | `?action=query&db=<path>&sql=<SELECT ...>` | `{ok, db, columns:[{name,decl}], rows:[[...]], row_count, truncated, limit, ms, aborted, types}` | | 80 | | `?action=query&db=<path>&sql=<SELECT ...>` | `{ok, db, columns:[{name,decl}], rows:[[...]], row_count, truncated, limit, ms, aborted, types}` | |
| 78 | | `?action=info` | plugin id, version, bundled SQLite version, platform, limits | | 81 | | `?action=info` | plugin id, version, bundled SQLite version, platform, limits | |
| @@ -105,12 +108,14 @@ box). Bumping the engine to `-O2` changes nothing measurable for an I/O-bound 50 | @@ -105,12 +108,14 @@ box). Bumping the engine to `-O2` changes nothing measurable for an I/O-bound 50 | ||
| 105 | ## Tests | 108 | ## Tests |
| 106 | 109 | ||
| 107 | `tests/run.sh` builds a throwaway database, points the plugin at it with `SQLITE_ALLOWED_ROOT`, | 110 | `tests/run.sh` builds a throwaway database, points the plugin at it with `SQLITE_ALLOWED_ROOT`, |
| 108 | -and runs 120+ assertions: | 111 | +and runs 134 assertions: |
| 109 | 112 | ||
| 110 | * `--meta` shape (single-word name, at most 15-char description, compact, valid JSON) | 113 | * `--meta` shape (single-word name, at most 15-char description, compact, valid JSON) |
| 111 | * node auth: 401 without credentials, `X-Bot-Password` / `Bearer` / `?pwd=` accepted, wrong | 114 | * node auth: 401 without credentials, `X-Bot-Password` / `Bearer` / `?pwd=` accepted, wrong |
| 112 | password refused, HTML route gated too | 115 | password refused, HTML route gated too |
| 113 | -* discovery, schema (tables, views, indexes, row counts, column detail) and queries | 116 | +* discovery, schema (tables, views, indexes, row counts, column detail) and queries, |
| 117 | + including that every allowlisted root is scanned with its own budget (a truncated root is | ||
| 118 | + reported as `truncated: true` and does not starve the roots after it) | ||
| 114 | * typed values: integers, reals, text, JSON `null`, BLOB hex preview | 119 | * typed values: integers, reals, text, JSON `null`, BLOB hex preview |
| 115 | * read-only: 15 rejected mutations (INSERT/UPDATE/DELETE/DROP/ALTER/CREATE TABLE/CREATE VIEW/ | 120 | * read-only: 15 rejected mutations (INSERT/UPDATE/DELETE/DROP/ALTER/CREATE TABLE/CREATE VIEW/ |
| 116 | ATTACH/DETACH/REINDEX/VACUUM/BEGIN/settable PRAGMA/stacked statement/WITH-DELETE) plus proof | 121 | ATTACH/DETACH/REINDEX/VACUUM/BEGIN/settable PRAGMA/stacked statement/WITH-DELETE) plus proof |
| @@ -146,7 +151,7 @@ compile it yourself — it is one C file plus the vendored amalgamation and libc | @@ -146,7 +151,7 @@ compile it yourself — it is one C file plus the vendored amalgamation and libc | ||
| 146 | ``` | 151 | ``` |
| 147 | src/plugin_sqlite.c the whole plugin (3 modes, path gate, authorizer, HTML UI) | 152 | src/plugin_sqlite.c the whole plugin (3 modes, path gate, authorizer, HTML UI) |
| 148 | vendor/sqlite3.{c,h} public-domain SQLite 3.53.4 amalgamation | 153 | vendor/sqlite3.{c,h} public-domain SQLite 3.53.4 amalgamation |
| 149 | -tests/run.sh 120+ offline assertions | 154 | +tests/run.sh 134 offline assertions |
| 150 | Makefile native / musl / arm64 / win64 / macos / test / clean | 155 | Makefile native / musl / arm64 / win64 / macos / test / clean |
| 151 | build.ps1 Windows MinGW static build | 156 | build.ps1 Windows MinGW static build |
| 152 | release-dist.sh build all targets, package, publish, mirror | 157 | release-dist.sh build all targets, package, publish, mirror |
| index ce389d0..fdecf99 100644 | index ce389d0..fdecf99 100644 | ||
| --- a/plugin.json | --- a/plugin.json | ||
| +++ b/plugin.json | +++ b/plugin.json | ||
| @@ -1,9 +1,12 @@ | @@ -1,9 +1,12 @@ | ||
| 1 | { | 1 | { |
| 2 | "slug": "sqlite", | 2 | "slug": "sqlite", |
| 3 | "id": "plugin_sqlite", | 3 | "id": "plugin_sqlite", |
| 4 | + "name": "sqlite", | ||
| 5 | + "desc": "SQLite viewer", | ||
| 4 | "title": "SQLite Explorer", | 6 | "title": "SQLite Explorer", |
| 5 | "summary": "Zero-dependency, strictly read-only SQLite browser: discovers .db/.sqlite files under your workspace and home, shows tables, columns, indexes and row counts, and runs capped SELECT queries with a sortable result grid - the SQLite engine is compiled into the binary so no sqlite3 CLI, libsqlite3.so or sqlite3.dll is needed.", | 7 | "summary": "Zero-dependency, strictly read-only SQLite browser: discovers .db/.sqlite files under your workspace and home, shows tables, columns, indexes and row counts, and runs capped SELECT queries with a sortable result grid - the SQLite engine is compiled into the binary so no sqlite3 CLI, libsqlite3.so or sqlite3.dll is needed.", |
| 6 | "category": "devtools", | 8 | "category": "devtools", |
| 9 | + "version": "1.0.1", | ||
| 7 | "license": "MIT", | 10 | "license": "MIT", |
| 8 | "capabilities": ["local_filesystem_read", "sqlite_readonly_open", "bounded_directory_walk"], | 11 | "capabilities": ["local_filesystem_read", "sqlite_readonly_open", "bounded_directory_walk"], |
| 9 | "trust_notice": "Executes native C code on your node. Opens databases with SQLITE_OPEN_READONLY and installs an authorizer that permits only SELECT, READ, FUNCTION and read-only PRAGMA statements, so INSERT/UPDATE/DELETE/DROP/ALTER/ATTACH/CREATE are refused by the engine. Only .db/.sqlite/.sqlite3 files under /workspace, /codebases, $HOME or $SQLITE_ALLOWED_ROOT are browsable; results are capped at 500 rows and about 2 seconds per statement. Makes zero outbound network requests and opens no sockets." | 12 | "trust_notice": "Executes native C code on your node. Opens databases with SQLITE_OPEN_READONLY and installs an authorizer that permits only SELECT, READ, FUNCTION and read-only PRAGMA statements, so INSERT/UPDATE/DELETE/DROP/ALTER/ATTACH/CREATE are refused by the engine. Only .db/.sqlite/.sqlite3 files under /workspace, /codebases, $HOME or $SQLITE_ALLOWED_ROOT are browsable; results are capped at 500 rows and about 2 seconds per statement. Makes zero outbound network requests and opens no sockets." |
| index 11ef894..d6ecc71 100755 | index 11ef894..d6ecc71 100755 | ||
| --- a/release-dist.sh | --- a/release-dist.sh | ||
| +++ b/release-dist.sh | +++ b/release-dist.sh | ||
| @@ -28,7 +28,7 @@ set -euo pipefail | @@ -28,7 +28,7 @@ set -euo pipefail | ||
| 28 | 28 | ||
| 29 | SLUG="sqlite" | 29 | SLUG="sqlite" |
| 30 | ID="plugin_sqlite" | 30 | ID="plugin_sqlite" |
| 31 | -VERSION="1.0.0" | 31 | +VERSION="1.0.1" |
| 32 | DIST_BASE_URL="https://dist.scorpiox.net/bot-plugins" | 32 | DIST_BASE_URL="https://dist.scorpiox.net/bot-plugins" |
| 33 | 33 | ||
| 34 | # Compile flags for the vendored SQLite amalgamation. | 34 | # Compile flags for the vendored SQLite amalgamation. |
| index 624c641..343ffc5 100644 | index 624c641..343ffc5 100644 | ||
| --- a/src/plugin_sqlite.c | --- a/src/plugin_sqlite.c | ||
| +++ b/src/plugin_sqlite.c | +++ b/src/plugin_sqlite.c | ||
| @@ -71,7 +71,7 @@ | @@ -71,7 +71,7 @@ | ||
| 71 | 71 | ||
| 72 | #define PLUGIN_SLUG "sqlite" | 72 | #define PLUGIN_SLUG "sqlite" |
| 73 | #define PLUGIN_ID "plugin_sqlite" | 73 | #define PLUGIN_ID "plugin_sqlite" |
| 74 | -#define PLUGIN_VER "1.0.0" | 74 | +#define PLUGIN_VER "1.0.1" |
| 75 | 75 | ||
| 76 | #define MAX_NAME 256 | 76 | #define MAX_NAME 256 |
| 77 | /* glibc's fortified realpath() demands a PATH_MAX-sized buffer. */ | 77 | /* glibc's fortified realpath() demands a PATH_MAX-sized buffer. */ |
| @@ -85,6 +85,22 @@ | @@ -85,6 +85,22 @@ | ||
| 85 | #define MAX_SCAN_DEPTH 4 | 85 | #define MAX_SCAN_DEPTH 4 |
| 86 | #define STMT_BUDGET_MS 2000 | 86 | #define STMT_BUDGET_MS 2000 |
| 87 | 87 | ||
| 88 | +/* | ||
| 89 | + * Per-root entry allowance for the discovery walk. $SQLITE_SCAN_BUDGET lowers | ||
| 90 | + * it so tests can prove the truncation path on a tiny fixture; a value that is | ||
| 91 | + * not a positive integer is ignored rather than trusted. | ||
| 92 | + */ | ||
| 93 | +static int scan_budget(void) | ||
| 94 | +{ | ||
| 95 | + static int cached = -1; | ||
| 96 | + if (cached < 0) { | ||
| 97 | + const char *e = getenv("SQLITE_SCAN_BUDGET"); | ||
| 98 | + long v = (e && *e) ? strtol(e, NULL, 10) : 0; | ||
| 99 | + cached = (v > 0 && v <= MAX_SCAN_ENTRIES) ? (int)v : MAX_SCAN_ENTRIES; | ||
| 100 | + } | ||
| 101 | + return cached; | ||
| 102 | +} | ||
| 103 | + | ||
| 88 | /* ------------------------------------------------------------------ */ | 104 | /* ------------------------------------------------------------------ */ |
| 89 | /* Small utilities */ | 105 | /* Small utilities */ |
| 90 | /* ------------------------------------------------------------------ */ | 106 | /* ------------------------------------------------------------------ */ |
| @@ -687,7 +703,7 @@ static int skip_name(const char *n) | @@ -687,7 +703,7 @@ static int skip_name(const char *n) | ||
| 687 | static int walk_dbs(const char *dir, const char *rootlabel, int depth, | 703 | static int walk_dbs(const char *dir, const char *rootlabel, int depth, |
| 688 | sx_db_t *out, int max, int *count, int *visited) | 704 | sx_db_t *out, int max, int *count, int *visited) |
| 689 | { | 705 | { |
| 690 | - if (depth > MAX_SCAN_DEPTH || *count >= max || *visited > MAX_SCAN_ENTRIES) | 706 | + if (depth > MAX_SCAN_DEPTH || *count >= max || *visited > scan_budget()) |
| 691 | return 0; | 707 | return 0; |
| 692 | 708 | ||
| 693 | #if defined(_WIN32) | 709 | #if defined(_WIN32) |
| @@ -703,7 +719,7 @@ static int walk_dbs(const char *dir, const char *rootlabel, int depth, | @@ -703,7 +719,7 @@ static int walk_dbs(const char *dir, const char *rootlabel, int depth, | ||
| 703 | SX_STAT st; | 719 | SX_STAT st; |
| 704 | if (skip_name(fd.cFileName)) continue; | 720 | if (skip_name(fd.cFileName)) continue; |
| 705 | (*visited)++; | 721 | (*visited)++; |
| 706 | - if (*visited > MAX_SCAN_ENTRIES || *count >= max) break; | 722 | + if (*visited > scan_budget() || *count >= max) break; |
| 707 | snprintf(full, sizeof(full), "%s\\%s", dir, fd.cFileName); | 723 | snprintf(full, sizeof(full), "%s\\%s", dir, fd.cFileName); |
| 708 | if ((fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0) { | 724 | if ((fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0) { |
| 709 | walk_dbs(full, rootlabel, depth + 1, out, max, count, visited); | 725 | walk_dbs(full, rootlabel, depth + 1, out, max, count, visited); |
| @@ -728,7 +744,7 @@ static int walk_dbs(const char *dir, const char *rootlabel, int depth, | @@ -728,7 +744,7 @@ static int walk_dbs(const char *dir, const char *rootlabel, int depth, | ||
| 728 | SX_STAT st; | 744 | SX_STAT st; |
| 729 | if (skip_name(e->d_name)) continue; | 745 | if (skip_name(e->d_name)) continue; |
| 730 | (*visited)++; | 746 | (*visited)++; |
| 731 | - if (*visited > MAX_SCAN_ENTRIES || *count >= max) break; | 747 | + if (*visited > scan_budget() || *count >= max) break; |
| 732 | snprintf(full, sizeof(full), "%s/%s", dir, e->d_name); | 748 | snprintf(full, sizeof(full), "%s/%s", dir, e->d_name); |
| 733 | if (lstat(full, &st) != 0) continue; | 749 | if (lstat(full, &st) != 0) continue; |
| 734 | if (S_ISDIR(st.st_mode)) { | 750 | if (S_ISDIR(st.st_mode)) { |
| @@ -758,10 +774,34 @@ static void action_dbs(void) | @@ -758,10 +774,34 @@ static void action_dbs(void) | ||
| 758 | { | 774 | { |
| 759 | sx_root_t roots[8]; | 775 | sx_root_t roots[8]; |
| 760 | sx_db_t dbs[MAX_DBS]; | 776 | sx_db_t dbs[MAX_DBS]; |
| 761 | - int nroots, i, count = 0, visited = 0; | 777 | + int nroots, i, count = 0; |
| 778 | + int total_visited = 0; | ||
| 779 | + int root_visited[8]; | ||
| 780 | + int root_count[8]; | ||
| 781 | + int root_truncated[8]; | ||
| 762 | 782 | ||
| 763 | nroots = collect_roots(roots, 8); | 783 | nroots = collect_roots(roots, 8); |
| 764 | memset(dbs, 0, sizeof(dbs)); | 784 | memset(dbs, 0, sizeof(dbs)); |
| 785 | + memset(root_visited, 0, sizeof(root_visited)); | ||
| 786 | + memset(root_count, 0, sizeof(root_count)); | ||
| 787 | + memset(root_truncated, 0, sizeof(root_truncated)); | ||
| 788 | + | ||
| 789 | + /* Each root gets its own entry budget. A single global counter lets one big | ||
| 790 | + * tree (/codebases, a monorepo) exhaust the allowance before the walk ever | ||
| 791 | + * reaches $HOME, so databases sitting in the user's home directory were | ||
| 792 | + * undiscoverable - the UI reported "1 database" on a node that had three. | ||
| 793 | + * The per-root counts also let the UI name the root that was truncated | ||
| 794 | + * instead of silently showing a short list. */ | ||
| 795 | + for (i = 0; i < nroots && count < MAX_DBS; i++) { | ||
| 796 | + int before = count; | ||
| 797 | + int visited = 0; | ||
| 798 | + if (!roots[i].exists) continue; | ||
| 799 | + walk_dbs(roots[i].path, roots[i].label, 0, dbs, MAX_DBS, &count, &visited); | ||
| 800 | + root_visited[i] = visited; | ||
| 801 | + root_count[i] = count - before; | ||
| 802 | + root_truncated[i] = (visited > scan_budget() || count >= MAX_DBS); | ||
| 803 | + total_visited += visited; | ||
| 804 | + } | ||
| 765 | 805 | ||
| 766 | json_header(); | 806 | json_header(); |
| 767 | fputs("{\"ok\":true,\"action\":\"dbs\",\"plugin\":\"" PLUGIN_ID "\"", stdout); | 807 | fputs("{\"ok\":true,\"action\":\"dbs\",\"plugin\":\"" PLUGIN_ID "\"", stdout); |
| @@ -773,17 +813,16 @@ static void action_dbs(void) | @@ -773,17 +813,16 @@ static void action_dbs(void) | ||
| 773 | json_str(roots[i].label); | 813 | json_str(roots[i].label); |
| 774 | fputs(",\"path\":", stdout); | 814 | fputs(",\"path\":", stdout); |
| 775 | json_str(roots[i].path); | 815 | json_str(roots[i].path); |
| 776 | - printf(",\"exists\":%s}", roots[i].exists ? "true" : "false"); | 816 | + printf(",\"exists\":%s,\"dbs\":%d,\"scanned_entries\":%d,\"truncated\":%s}", |
| 817 | + roots[i].exists ? "true" : "false", | ||
| 818 | + root_count[i], root_visited[i], | ||
| 819 | + root_truncated[i] ? "true" : "false"); | ||
| 777 | } | 820 | } |
| 778 | fputs("]", stdout); | 821 | fputs("]", stdout); |
| 779 | 822 | ||
| 780 | - for (i = 0; i < nroots && count < MAX_DBS; i++) { | 823 | + printf(",\"count\":%d,\"scanned_entries\":%d,\"max_depth\":%d,\"max_scan_entries\":%d," |
| 781 | - if (!roots[i].exists) continue; | 824 | + "\"dbs\":[", |
| 782 | - walk_dbs(roots[i].path, roots[i].label, 0, dbs, MAX_DBS, &count, &visited); | 825 | + count, total_visited, MAX_SCAN_DEPTH, scan_budget()); |
| 783 | - } | ||
| 784 | - | ||
| 785 | - printf(",\"count\":%d,\"scanned_entries\":%d,\"max_depth\":%d,\"dbs\":[", | ||
| 786 | - count, visited, MAX_SCAN_DEPTH); | ||
| 787 | for (i = 0; i < count; i++) { | 826 | for (i = 0; i < count; i++) { |
| 788 | char utc[40]; | 827 | char utc[40]; |
| 789 | time_t t = (time_t)dbs[i].mtime; | 828 | time_t t = (time_t)dbs[i].mtime; |
| @@ -1204,7 +1243,8 @@ static int mode_html(int embed) | @@ -1204,7 +1243,8 @@ static int mode_html(int embed) | ||
| 1204 | "<select id='dbsel'><option value=''>Loading databases...</option></select></div>" | 1243 | "<select id='dbsel'><option value=''>Loading databases...</option></select></div>" |
| 1205 | "<button class='ghost' id='reloadbtn' type='button'>Rescan</button></div>" | 1244 | "<button class='ghost' id='reloadbtn' type='button'>Rescan</button></div>" |
| 1206 | "<div class='hint' id='dbhint'>Only .db / .sqlite / .sqlite3 files under the " | 1245 | "<div class='hint' id='dbhint'>Only .db / .sqlite / .sqlite3 files under the " |
| 1207 | - "allowlisted roots are listed.</div></div>", stdout); | 1246 | + "allowlisted roots are listed.</div>" |
| 1247 | + "<div class='hint' id='rootline'></div></div>", stdout); | ||
| 1208 | 1248 | ||
| 1209 | fputs("<div class='layout'><div class='side'><h3>Tables</h3>" | 1249 | fputs("<div class='layout'><div class='side'><h3>Tables</h3>" |
| 1210 | "<div id='tables'><div class='empty'>Select a database.</div></div></div>", stdout); | 1250 | "<div id='tables'><div class='empty'>Select a database.</div></div></div>", stdout); |
| @@ -1288,6 +1328,17 @@ static int mode_html(int embed) | @@ -1288,6 +1328,17 @@ static int mode_html(int embed) | ||
| 1288 | " if (!d.ok) { err(d.error || 'failed to list databases'); return; }\n" | 1328 | " if (!d.ok) { err(d.error || 'failed to list databases'); return; }\n" |
| 1289 | " var list = d.dbs || [];\n" | 1329 | " var list = d.dbs || [];\n" |
| 1290 | " txt('dbcount', list.length + (list.length === 1 ? ' database' : ' databases'));\n" | 1330 | " txt('dbcount', list.length + (list.length === 1 ? ' database' : ' databases'));\n" |
| 1331 | +" /* Name the roots, and say so out loud when one was cut short - a short\n" | ||
| 1332 | +" list because of the scan cap looks exactly like a node with no files. */\n" | ||
| 1333 | +" var roots = d.roots || [], walked = [], cut = [];\n" | ||
| 1334 | +" roots.forEach(function(r){\n" | ||
| 1335 | +" if (!r.exists) return;\n" | ||
| 1336 | +" walked.push(r.label);\n" | ||
| 1337 | +" if (r.truncated) cut.push(r.label);\n" | ||
| 1338 | +" });\n" | ||
| 1339 | +" txt('rootline', walked.length ? ('roots: ' + walked.join(', ') +\n" | ||
| 1340 | +" (cut.length ? ' \\u00b7 scan cap reached in ' + cut.join(', ') : '')) :\n" | ||
| 1341 | +" 'no allowlisted root exists on this node');\n" | ||
| 1291 | " if (!list.length) {\n" | 1342 | " if (!list.length) {\n" |
| 1292 | " var o = el('option', null, 'No .db / .sqlite files found');\n" | 1343 | " var o = el('option', null, 'No .db / .sqlite files found');\n" |
| 1293 | " o.value = ''; sel.appendChild(o);\n" | 1344 | " o.value = ''; sel.appendChild(o);\n" |
| index 13c8788..acc711b 100755 | index 13c8788..acc711b 100755 | ||
| --- a/tests/run.sh | --- a/tests/run.sh | ||
| +++ b/tests/run.sh | +++ b/tests/run.sh | ||
| @@ -108,7 +108,7 @@ DESC=$(printf '%s' "$META" | sed 's/.*"desc":"\([^"]*\)".*/\1/') | @@ -108,7 +108,7 @@ DESC=$(printf '%s' "$META" | sed 's/.*"desc":"\([^"]*\)".*/\1/') | ||
| 108 | [ ${#DESC} -le 15 ] && pass "desc length ${#DESC} <= 15 chars" || fail "desc too long (${#DESC})" | 108 | [ ${#DESC} -le 15 ] && pass "desc length ${#DESC} <= 15 chars" || fail "desc too long (${#DESC})" |
| 109 | NAME=$(printf '%s' "$META" | sed 's/.*"name":"\([^"]*\)".*/\1/') | 109 | NAME=$(printf '%s' "$META" | sed 's/.*"name":"\([^"]*\)".*/\1/') |
| 110 | [ "$(printf '%s' "$NAME" | wc -w | tr -d ' ')" -le 1 ] && pass "name is a single word" || fail "name has spaces" | 110 | [ "$(printf '%s' "$NAME" | wc -w | tr -d ' ')" -le 1 ] && pass "name is a single word" || fail "name has spaces" |
| 111 | -check "--version prints version" '1.0.0' "$("$BIN" --version)" | 111 | +check "--version prints version" '1.0.1' "$("$BIN" --version)" |
| 112 | check "--version names the sqlite lib" 'sqlite 3.' "$("$BIN" --version)" | 112 | check "--version names the sqlite lib" 'sqlite 3.' "$("$BIN" --version)" |
| 113 | check "--help is available" 'usage:' "$("$BIN" --help)" | 113 | check "--help is available" 'usage:' "$("$BIN" --help)" |
| 114 | META_AUTH=$(BOT_PASSWORD=secret "$BIN" --meta) | 114 | META_AUTH=$(BOT_PASSWORD=secret "$BIN" --meta) |
| @@ -140,6 +140,8 @@ check "dbs declares read-only" '"readonly":true' "$DBS" | @@ -140,6 +140,8 @@ check "dbs declares read-only" '"readonly":true' "$DBS" | ||
| 140 | check "dbs carries the allowed root" "$TMP" "$DBS" | 140 | check "dbs carries the allowed root" "$TMP" "$DBS" |
| 141 | check "dbs finds our database" 'shop.sqlite' "$DBS" | 141 | check "dbs finds our database" 'shop.sqlite' "$DBS" |
| 142 | check "dbs reports a count" '"count":' "$DBS" | 142 | check "dbs reports a count" '"count":' "$DBS" |
| 143 | +check "dbs reports per-root fields" '"scanned_entries":' "$DBS" | ||
| 144 | +check "dbs declares the scan cap" '"max_scan_entries":' "$DBS" | ||
| 143 | printf '%s' "$DBS" | python3 -c 'import json,sys; json.load(sys.stdin)' 2>/dev/null \ | 145 | printf '%s' "$DBS" | python3 -c 'import json,sys; json.load(sys.stdin)' 2>/dev/null \ |
| 144 | && pass "dbs body is valid JSON" || fail "dbs body JSON invalid" | 146 | && pass "dbs body is valid JSON" || fail "dbs body JSON invalid" |
| 145 | python3 - "$TMP/dbs.json" <<'PYEOF' && pass "dbs rows are well formed" || fail "dbs rows malformed" | 147 | python3 - "$TMP/dbs.json" <<'PYEOF' && pass "dbs rows are well formed" || fail "dbs rows malformed" |
| @@ -155,9 +157,42 @@ for x in d["dbs"]: | @@ -155,9 +157,42 @@ for x in d["dbs"]: | ||
| 155 | assert x["bytes"] > 0 | 157 | assert x["bytes"] > 0 |
| 156 | assert x["root"] | 158 | assert x["root"] |
| 157 | assert x["mtime"].endswith("Z") | 159 | assert x["mtime"].endswith("Z") |
| 160 | +for r in d["roots"]: | ||
| 161 | + for k in ("label", "path", "exists", "dbs", "scanned_entries", "truncated"): | ||
| 162 | + assert k in r, (k, r) | ||
| 163 | + assert isinstance(r["dbs"], int) and r["dbs"] >= 0 | ||
| 164 | + assert isinstance(r["scanned_entries"], int) and r["scanned_entries"] >= 0 | ||
| 165 | + assert isinstance(r["truncated"], bool) | ||
| 166 | +assert d["max_scan_entries"] > 0 | ||
| 158 | PYEOF | 167 | PYEOF |
| 159 | check_not "dbs ignores non-db files" 'notes.txt' "$DBS" | 168 | check_not "dbs ignores non-db files" 'notes.txt' "$DBS" |
| 160 | 169 | ||
| 170 | +# Every allowlisted root must get its own scan budget. One global counter let a | ||
| 171 | +# large tree exhaust the allowance before the walk reached $HOME, so a database | ||
| 172 | +# in the home directory was simply invisible. SQLITE_SCAN_BUDGET shrinks the | ||
| 173 | +# allowance so a five-directory fixture can prove it. | ||
| 174 | +BIGDIR="$TMP/bigtree" | ||
| 175 | +mkdir -p "$BIGDIR" | ||
| 176 | +for i in 1 2 3 4 5 6 7 8 9; do mkdir -p "$BIGDIR/f$i"; done | ||
| 177 | +ROOTRUN=$(SQLITE_ALLOWED_ROOT="$BIGDIR" SQLITE_SCAN_BUDGET=4 QUERY_STRING='action=dbs' "$BIN" | sed '1,/^\r$/d') | ||
| 178 | +check "dbs reports per-root scan counts" '"scanned_entries":' "$ROOTRUN" | ||
| 179 | +check "dbs flags the truncated root" '"truncated":true' "$ROOTRUN" | ||
| 180 | +HOMEHIT=$(printf '%s' "$ROOTRUN" | python3 -c ' | ||
| 181 | +import json, sys | ||
| 182 | +d = json.load(sys.stdin) | ||
| 183 | +labels = {r["label"]: r for r in d["roots"]} | ||
| 184 | +assert labels["allowed"]["truncated"] is True, labels["allowed"] | ||
| 185 | +home = labels.get("home") | ||
| 186 | +assert home is not None, labels | ||
| 187 | +if home["exists"]: | ||
| 188 | + assert home["scanned_entries"] > 0 or home["dbs"] > 0, home | ||
| 189 | + print("home_walked", home["scanned_entries"]) | ||
| 190 | +' 2>/dev/null) | ||
| 191 | +case "$HOMEHIT" in | ||
| 192 | +home_walked*) pass "home is still walked after a truncated root" ;; | ||
| 193 | +*) fail "home was skipped after a truncated root (got: $HOMEHIT)" ;; | ||
| 194 | +esac | ||
| 195 | + | ||
| 161 | # ---------------------------------------------------------------- schema | 196 | # ---------------------------------------------------------------- schema |
| 162 | section "mode 2 action=schema" | 197 | section "mode 2 action=schema" |
| 163 | SCHEMA=$(body "$(QUERY_STRING="action=schema&db=$DB" "$BIN")") | 198 | SCHEMA=$(body "$(QUERY_STRING="action=schema&db=$DB" "$BIN")") |
| @@ -329,6 +364,7 @@ check "html has a doctype" '<!DOCTYPE html>' "$HTML" | @@ -329,6 +364,7 @@ check "html has a doctype" '<!DOCTYPE html>' "$HTML" | ||
| 329 | check "html titles the tool" '<title>sqlite</title>' "$HTML" | 364 | check "html titles the tool" '<title>sqlite</title>' "$HTML" |
| 330 | check "html ships the sql editor" "id='sql'" "$HTML" | 365 | check "html ships the sql editor" "id='sql'" "$HTML" |
| 331 | check "html ships the db picker" "id='dbsel'" "$HTML" | 366 | check "html ships the db picker" "id='dbsel'" "$HTML" |
| 367 | +check "html shows which roots were walked" "id='rootline'" "$HTML" | ||
| 332 | check "html ships the result grid" "id='reswrap'" "$HTML" | 368 | check "html ships the result grid" "id='reswrap'" "$HTML" |
| 333 | check "html supports ctrl+enter" 'Ctrl' "$HTML" | 369 | check "html supports ctrl+enter" 'Ctrl' "$HTML" |
| 334 | check "html offers CSV copy" 'Copy CSV' "$HTML" | 370 | check "html offers CSV copy" 'Copy CSV' "$HTML" |
| 335 | 371 |