Skip to content

feat(rules): security/supabase-rls counts rows the anon key can see with a HEAD request (repo#1959) - #1883

Merged
nc9 merged 7 commits into
mainfrom
boat/supabase-rls-1959
Nov 17, 2026
Merged

nc9 merged 7 commits into
mainfrom
boat/supabase-rls-1959

Conversation

@nc9

@nc9 nc9 commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

Adds security/supabase-rls for squirrelscan/repo#1959 (part of repo#1952, policy per repo#1931). HOLD-LIST (security): planning reviews this at Opus depth before merge.

Revised per the planning decision: no row is ever read.

What it does

The rule reads the scripts and inline scripts the crawl already fetched, looking for a Supabase project URL (<ref>.supabase.co), an anon key, and the table names the site's own code queries (.from("x").select(...) chains, /rest/v1/x URLs). For each such table it sends one request:

HEAD https://<ref>.supabase.co/rest/v1/<table>
Prefer: count=exact
apikey: <anon key>
Authorization: Bearer <anon key>   (JWT keys only)

No query string, no select=, never a GET. The response body is never read, even if a server sends one to the HEAD.

Exposure is judged only from the status and the Content-Range total:

  • 2xx with total > 0: exposed (fail). The finding names the table and the row count, nothing else.
  • 429, a bot challenge (cf-mitigated: challenge) or a 403 with WAF/challenge markers (isThrottledOrChallenged from probe-http): pushback. Never protected; the run stops, no further request.
  • 401 / a plain 403 (PostgREST answers 42501 with these): protected.
  • 2xx with total 0 (*/0): not flagged. RLS hiding every row and an empty table look the same; the docs say so.
  • 404: missing table.
  • Anything else, a 2xx without a numeric total, or a network error: not checked.

A pass clears only what got a definite answer. The rule passes only when every table the code names, in every project found, answered 401, a plain 403, a zero total or 404. Pushback, an error, an unusable answer, a budget stop, or a table or project left out by the caps makes the run skipped probe-incomplete with value: "not-checked" (or the narrower probe-budget / probe-errors / probe-inconclusive when only that applies), never a pass. keySource drops userinfo as well as query and fragment.

Intentionally public tables: [[ignore]] (pub#1262) waives one by item id, e.g. items = ["glob:*/rest/v1/posts"]; the rule page documents it. A bare table name (items = ["posts"]) does not match, because the item id is <host>/rest/v1/<table> and the label carries the row count.

No table names are guessed (users, profiles, orders are gone). A project whose code names no table is skipped no-tables-in-code and nothing is sent. No column names are reported at all.

Safety (unchanged):

  • Loud probe: needs --probe aggressive. Cloud audits of unverified websites stay passive and never send.
  • Skipped when site.externalLinks is undefined (--offline, or external checks off).
  • Requests go only to <ref>.supabase.co|in (strict 20-char ref), through the egress guard (no private, link-local or metadata addresses, pinned to the vetted IP). No cookies, no custom audit headers, redirect: manual, 10 s per-request timeout, shared probe budget.
  • Keys: only unexpired JWTs with iss: supabase, role: anon and a valid ref, sent only to that project; a sb_publishable_ key only when its script names exactly one project. Service-role and sb_secret_ keys are never used.
  • Caps: 2 projects, 8 tables per project, 10 requests per audit.
  • graphql-probe.ts is no longer touched (no body reader needed).

Acceptance criteria

  • Uses the site's anon key against /rest/v1 tables and flags tables the anon key can see rows in. Tests: a table reporting rows is flagged from its count; no body is read and no value leaks, the probe is a HEAD with a row count on the project host, with the anon key.
  • A table protected by RLS (401, 403, zero total) is not flagged. Tests: tables behind RLS (401, 403, a zero total) or missing are not flagged, response classification uses only the status and the Content-Range total, answers without a usable total are not checked, never a pass.
  • The finding lists exposed tables with values redacted: none are fetched. The stub serves a JSON body with an email, a Stripe-style key, a bcrypt hash, a phone number and a card number on every route; the tests assert every request is HEAD with no query string, the stub body is never pulled (a counting ReadableStream), and none of the values (or their field names) appear in the serialized rule result (checks, items, details) or any console line.
  • Probes are capped, gated per fix(rules): content/reading-level skips code blocks (repo#1639) #1931, and go to the Supabase host only. Tests: table probes are capped across the run, and a capped run is not a pass, a 401 then 429s: the run stops at the first 429 and is not a pass, a bot challenge 403 (cf-mitigated) is not protected and not a pass, a network error on one table makes the run not checked, not a pass, a planted sb_secret_ key sends nothing and never appears in the result, passive, active or no probe budget: skipped, nothing sent, offline or external requests off: skipped, nothing sent, no guessed tables: a project whose code names no table sends nothing, a project host that resolves to a private address is refused, nothing sent, a zero probe budget sends nothing and is not a pass.

Docs and counts

Rebased onto main after pub#2056; counts recomputed from loadAllRules(): 341 rules, 32 in Security. Updated docs/rules/security/supabase-rls.mdx (HEAD-only probe, verdict table incl. pushback, results incl. probe-incomplete, waiving a public table with [[ignore]]), the "Rules that probe" table in docs/configuration/security.mdx, docs.json, the regenerated security hub, Security counts in README.md, docs/index.mdx, docs/rules/index.mdx, and CHANGELOG [Unreleased] / Added. Golden: one line "security/supabase-rls": 1 in canonical-rule-totals.json; the finding-messages snapshot gains this rule's one skipped entry.

Gates

  • packages/rules: supabase-rls, rule-check-declarations, rule-verdict-scope, docs-index-rule-counts, docs-rule-pages (--timeout 30000): 452 pass, 0 fail. supabase-rls alone: 24 pass; 5 of the new/rewritten tests fail on the pre-fix rule (86f648c).
  • packages/audit-engine: streaming-rules-canonical-golden: 1 pass; golden-finding-messages-229: 3 pass.
  • Typecheck packages/rules (src + tests) and packages/audit-engine: pass.
  • make validate in docs/: all checks passed, links good, 62 pass.

Agent skill

The squirrelscan agent skill does not need a change: it lists rule categories, not individual probing rules.

Round 5 (a14215c): HTML error pages are not a definite answer

  • HTML 401, 403 or 404 is not checked (packages/rules/src/security/supabase-rls.ts:279-284, isHtml at :294): after the pushback check, a 401, 403 or 404 whose Content-Type media type is text/html (any case, any parameters) is other: not protected, not missing. A JSON content type, or none at all, keeps today's meaning. An all-HTML run lands in the existing all-unanswered branch (probe-inconclusive, skipped); a mix with definite answers is probe-incomplete. Neither is a pass.
  • Tests (packages/rules/tests/supabase-rls.test.ts): an HTML 401, 403 or 404 is not a definite answer; JSON or no content type keeps its meaning (:326; covers classifyTableResponse(403, null, text/html) not protected, JSON 403 still protected, JSON 404 still missing, no content type unchanged), a run where every table answers an HTML 403 / 404 / 401 block page is not a pass (:657), an HTML 403 among JSON answers leaves the run probe-incomplete (:677), JSON 403 (PostgREST 42501) and JSON 404 answers still pass, and the zero-budget test now asserts the cap reason and 0 of 1 (:764). On 353940c: 25 pass, 6 fail (the 6 new/extended assertions); after: 31 pass, 0 fail.
  • What Content-Type PostgREST sends on HEAD for 401, 403 and 404: not established. No PostgREST source, docs copy or container exists in the sandbox (only the postgrest-js client), and no live capture was made. The no-content-type branch is kept exactly as specified, so a real PostgREST answer without the header stays a definite answer.
  • Budget message (supabase-rls.ts:572-580, 612, 629): names the cap and how far it got, e.g. No Supabase table was probed: the shared probe time budget of 60 s for all probing rules ran out first (0 of 3 table probe(s) sent) and ... ran out after 2 of 5 table probe(s) ...; the probe-incomplete reason list uses the same wording.
  • Docs and CHANGELOG: the rule page said only the status and row total are used and that every 401/403/404 is protected or missing; it now names the content type, adds an HTML 401/403/404 row (Not checked) and adds HTML error pages to the probe-incomplete list (docs/rules/security/supabase-rls.mdx:51,57-60,74). The CHANGELOG bullet's "judges only the status and the row total" now includes the content type and lists an HTML 401, 403 or 404 page among the not-checked answers (CHANGELOG.md:36).
  • Gates round 5: supabase-rls, rule-verdict-scope, streaming-rules-canonical-golden, golden-finding-messages-229 (--timeout 30000): 406 pass, 0 fail. Typecheck packages/rules (src + tests): pass. make validate in docs/: pass, 62 pass.

Round 6: rule order vs the link check (head db33672)

Rebased onto main (ecd2703, no conflicts).

What site.externalLinks is in each case

Case site.externalLinks Evidence
--offline undefined apps/cli/src/controllers/audit.ts:3389-3390 sets external_links.enabled = false; the engine maps that to undefined at packages/audit-engine/src/adapter.ts:2069 (v1), :3017 (streaming first pass), :3701 (late pass)
External link checking off (--no-external-links, [external_links] enabled = false, targeted run) undefined same three adapter lines; targeted run: controllers/audit.ts:3389
On, zero external links [] adapter.ts:2742-2787 (readExternalLinkVerdicts always returns an array), bound at :3698-3701
On, with links array of verdicts adapter.ts:2742-2787, :3698-3701
On, link check timed out array of the verdicts that arrived (unanswered links get no row, counted as outOfTime) late-link-verdicts.ts:17-18, adapter.ts:2770
On, late binding, rule in the FIRST pass (the a14215c case) the rows written so far ([] before any verdict), read before the check is bound adapter.ts:2833, :3679-3691

So undefined means "link checking off" in every engine path. In practice the a14215c first-pass read gets [], not undefined. Today the "skipped as external-requests-off" symptom can't happen in the engine, but the rule reads a field the link check fills before that field is bound. That breaks the repo#2144 contract and is why link-verdict-rules-2144.test.ts is red.

Fix

RuleContext has no offline or external-requests flag (packages/rules/src/types.ts:760-840; ctx.probe carries only level and budget). reliability/broken-forms reads the same field the same way and is listed. So security/supabase-rls joins LINK_VERDICT_RULE_IDS (packages/rules/src/link-verdict-rules.ts) and runs in the late pass, after the link check binds the field. A comment at the read (supabase-rls.ts:443-448) records why. With checking on and zero links the field is [], which reads as on.

Tests

  • packages/audit-engine/tests/late-link-verdicts-2144.test.ts "security/supabase-rls runs after the link check binds site.externalLinks, not before it": an online late-binding run. It fails with the list change reverted (rule in the first pass: Expected: > 1, Received: 0) and passes after.
  • packages/rules/tests/supabase-rls.test.ts "external link checking on with zero external links is not off: the table is probed" (new). "offline or external requests off: skipped, nothing sent" still passes.
  • packages/rules/tests/link-verdict-rules-2144.test.ts: green.

Gates

  • CI "Rules tests" step, exactly bun run scripts/test-suites.ts --run rules: 11296 pass, 25 todo, 0 fail (378 files)
  • golden-baseline, golden-finding-messages-229, late-link-verdicts-2144: 17 pass, 0 fail
  • packages/rules typecheck (src + tests) and packages/audit-engine typecheck: clean
  • CHANGELOG and the rule page already say "not --offline, external link checking on", so they are unchanged. No docs change, so no make validate.

Residual risk

The rule now starts after the link verdict deadline. That deadline holds back LINK_RULES_RESERVE_MS (30 s) before a run deadline, but the rule can probe for up to 10 requests × 10 s, inside a probe budget whose clock other probes may already have started. On a deadline-bound aggressive run the rule may hit the budget cap more often (reported as not checked, never as a pass) or run past the reserve. Hosted runs are locked passive unless ownership is verified, which limits this to verified hosted aggressive runs and local runs (which have no run deadline). A dedicated "external requests allowed" SiteData flag would remove the dependency; I left it as a follow-up.

The squirrelscan agent skill needs no change.

@nc9

nc9 commented Nov 14, 2026

Copy link
Copy Markdown
Contributor Author

Review requirement before merge: the probe should prove exposure without downloading anyone's data. Today GET /rest/v1/<table>?select=*&limit=1 pulls a full real row (for example from users, profiles or orders) and then discards the values. Please switch to HEAD /rest/v1/<table> with Prefer: count=exact and judge from the status and the Content-Range total. A count above 0 with the anon key means RLS is off; 401, 403 or 42501 means protected. If column names are still wanted, take them from what the site's own code queries, not from a fetched row. Add a test that no request ever has a body read beyond the headers, and that no row value can reach a finding, detail or log.

@nc9

nc9 commented Nov 14, 2026

Copy link
Copy Markdown
Contributor Author

Decision: only the HEAD + Prefer: count=exact version can merge. Acceptance criteria for the fix round:

  1. Only HEAD requests to /rest/v1/*, never GET with select.
  2. Exposure is judged from the status and the Content-Range total only.
  3. A stub server serves a body full of real-looking secret and email values; the test asserts the body is never read and no value reaches a finding, detail or log line.
  4. The guessed users, profiles and orders tables are dropped unless the site's own code references them.

@nc9
nc9 force-pushed the boat/supabase-rls-1959 branch from 71304b8 to 41a3482 Compare November 14, 2026 23:34
@nc9 nc9 changed the title feat(rules): security/supabase-rls flags Supabase tables readable with the site's anon key (repo#1959) feat(rules): security/supabase-rls counts rows the anon key can see with a HEAD request (repo#1959) Nov 14, 2026
@nc9
nc9 marked this pull request as ready for review November 14, 2026 23:43
@nc9
nc9 force-pushed the boat/supabase-rls-1959 branch 3 times, most recently from cb4b228 to dbfaf9e Compare November 15, 2026 04:00
@nc9

nc9 commented Nov 15, 2026

Copy link
Copy Markdown
Contributor Author

Review requirement (2026-11-15, from #1920): a probe answered with 429, a 401/403 bot challenge, an HTML challenge page (cf-mitigated, Cloudflare or WAF challenge markers), 5xx or a timeout must make the target UNVERIFIED. The rule then reports not checked or incomplete, never passed. Only a genuine 404, or a non-challenge answer, counts as not exposed. Please add a stub test for a 429 and for a 403 HTML challenge.

@nc9
nc9 force-pushed the boat/supabase-rls-1959 branch 2 times, most recently from 86f648c to 69c4b6c Compare November 17, 2026 15:49
nc9 added a commit that referenced this pull request Nov 17, 2026
…answered #1883

planned now uses every deduped table the code names, so a ninth table never probed keeps the run not checked instead of a pass. tablesCapped includes the per-project cap drops, and the cap reason names the 8-tables-per-project cap.

Signed-off-by: Nik Cubrilovic <[email protected]>
nc9 added a commit that referenced this pull request Nov 17, 2026
…answered #1883

planned now uses every deduped table the code names, so a ninth table never probed keeps the run not checked instead of a pass. tablesCapped includes the per-project cap drops, and the cap reason names the 8-tables-per-project cap.

Signed-off-by: Nik Cubrilovic <[email protected]>
@nc9
nc9 force-pushed the boat/supabase-rls-1959 branch from a14215c to db33672 Compare November 17, 2026 17:45
nc9 added a commit that referenced this pull request Nov 17, 2026
…e.externalLinks #1883

The rule reads site.externalLinks === undefined as "this audit may not
request other hosts". The rule context has no other signal for that, so
it joins LINK_VERDICT_RULE_IDS and runs in the late pass, after the link
check binds the field (repo#2144). Adds an engine test that fails when
the rule runs in the first pass, and a rule test that external link
checking on with zero links still probes.

Signed-off-by: Nik Cubrilovic <[email protected]>
nc9 added 7 commits November 18, 2026 05:19
… can count with a HEAD request (repo#1959)

Sends one HEAD /rest/v1/<table> with Prefer: count=exact per table the
site's own code names, with the site's anon key, and judges exposure only
from the status and the Content-Range total. Never a GET, never a query
string, never a response body read, no guessed table names. Aggressive
probing only, external requests on, at most ten requests per audit.

Signed-off-by: Nik Cubrilovic <[email protected]>
… guard passes (repo#1959)

Signed-off-by: Nik Cubrilovic <[email protected]>
… its query string or fragment (repo#1959)

Signed-off-by: Nik Cubrilovic <[email protected]>
…definite answer (repo#1959)

A 429, a bot challenge or a 403 with WAF markers stops the run and is
never read as protected. Pushback, a network error, an answer without a
usable total, or a table or project left out by the caps makes the run
probe-incomplete (not checked) instead of a pass that clears the rule.
The key's source URL drops userinfo as well as query and fragment. The
rule page documents waiving a public table with [[ignore]], and the
canonical golden gains its one per-rule line.

Signed-off-by: Nik Cubrilovic <[email protected]>
…answered #1883

planned now uses every deduped table the code names, so a ninth table never probed keeps the run not checked instead of a pass. tablesCapped includes the per-project cap drops, and the cap reason names the 8-tables-per-project cap.

Signed-off-by: Nik Cubrilovic <[email protected]>
…d, names the budget cap (repo#1959)

Signed-off-by: Nik Cubrilovic <[email protected]>
…e.externalLinks #1883

The rule reads site.externalLinks === undefined as "this audit may not
request other hosts". The rule context has no other signal for that, so
it joins LINK_VERDICT_RULE_IDS and runs in the late pass, after the link
check binds the field (repo#2144). Adds an engine test that fails when
the rule runs in the first pass, and a rule test that external link
checking on with zero links still probes.

Signed-off-by: Nik Cubrilovic <[email protected]>
@nc9
nc9 force-pushed the boat/supabase-rls-1959 branch from db33672 to 6f5df51 Compare November 17, 2026 18:19
@nc9
nc9 merged commit 0add374 into main Nov 17, 2026
16 checks passed
nc9 added a commit that referenced this pull request Nov 17, 2026
…answered #1883

planned now uses every deduped table the code names, so a ninth table never probed keeps the run not checked instead of a pass. tablesCapped includes the per-project cap drops, and the cap reason names the 8-tables-per-project cap.

Signed-off-by: Nik Cubrilovic <[email protected]>
@nc9
nc9 deleted the boat/supabase-rls-1959 branch November 17, 2026 18:37
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.

1 participant