Skip to content

See how your app actually fits together. An interactive, plain-English map of your codebase, drawn by Claude or Codex.

Health: the classic mistakes in AI-built apps (Supabase, Firebase, Stripe, public keys, CORS)

Hayden Smith54f2645e5bbf
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 83544dd..eadfd7e 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,5 +1,15 @@
# Changelog
+## 0.6.0 (2026-10-31)
+
+- **Health catches the classic mistakes in AI-built apps.** Five new checks, each in plain words with a fix to paste:
+ - **Secret keys sent to the browser:** an OpenAI, Stripe or Supabase service-role key (and others) in a `NEXT_PUBLIC_`, `VITE_`, `REACT_APP_` or similar variable, which every visitor can read. Keys that are public by design, like Supabase's anon key or Stripe's publishable key, are left alone.
+ - **Supabase tables anyone can read and change:** tables whose row-level security is never switched on (or is switched off), worked out by replaying the migrations in order, so a table locked later or dropped doesn't count. Policies that let anyone add, change or delete rows are flagged too, as worth a look.
+ - **Firebase rules that let anyone in:** Firestore, Storage and Realtime Database rules open to anyone, including "test mode" rules, with the date they stop working (or stopped).
+ - **Stripe webhooks anyone could fake:** code that acts on events like `checkout.session.completed` without checking Stripe's signature, so anyone could send a fake "payment succeeded".
+ - **Servers that let any website call them (CORS):** a small note on its own, and worth fixing when login cookies are allowed along with it.
+- **No more false alarm on Supabase's `config.toml`:** values like `"env(S3_SECRET_KEY)"` read a setting from the environment, so they're no longer reported as a key written into the code.
+
## 0.5.0 (2026-10-31)
- **Fits phones and tablets:** the top bar wraps instead of running off the screen, the details open as a sheet at the bottom of a phone (tap a box to see them), the long description under the title folds to a line with More / Less, and every control is at least 44 px to tap. Nothing changes with a mouse at 1000 px and wider. Requested in #18 and built by @menachem-kurkus in #19.
diff --git a/README.md b/README.md
index 79bea15..5ea54c8 100644
--- a/README.md
+++ b/README.md
@@ -14,7 +14,7 @@ Claude or Codex reads your codebase and draws an interactive map of it. Every pa
[![Claude Code plugin](https://img.shields.io/badge/Claude%20Code-plugin-D97757?style=flat-square)](https://code.claude.com/docs/en/overview)
[![Codex plugin](https://img.shields.io/badge/Codex-plugin-111?style=flat-square)](docs/codex.md)
[![Tests](https://img.shields.io/github/actions/workflow/status/haydenschmitty/floorplan/test.yml?branch=main&style=flat-square&label=tests)](https://github.com/haydenschmitty/floorplan/actions/workflows/test.yml)
-[![Version](https://img.shields.io/badge/version-0.5.0-111?style=flat-square)](CHANGELOG.md)
+[![Version](https://img.shields.io/badge/version-0.6.0-111?style=flat-square)](CHANGELOG.md)
[![License: MIT](https://img.shields.io/badge/license-MIT-111?style=flat-square)](LICENSE)
[![Node 18+](https://img.shields.io/badge/node-%E2%89%A518-111?style=flat-square)](https://nodejs.org)
[![Zero dependencies](https://img.shields.io/badge/dependencies-0-111?style=flat-square)](#how-it-works)
@@ -48,7 +48,7 @@ All three turn code into something you can see, and all three are worth knowing.
| **The question it answers** | How does the app I built actually work, and what should I worry about? | How do I show this clearly? | How does this big codebase work, down to the functions? |
| **Made for** | People who build with AI and don't read the code | Anyone explaining a system, a process or an idea | Developers getting to know a large codebase |
| **What you get** | A zoomable map of your whole app: parts, files and endpoints, the screens, the requests between frontend and backend (even across repos), and outside services | A polished diagram in one of 10 types (architecture, sequence, ERD…) or a one-page explainer, from a repo or just a description | A knowledge graph of every file, function and class, with layers, a business-domain view and guided tours |
-| **Only here** | **Health check** (keys in code, endpoints anyone can use), **Recent work** (what your AI just changed, and why), blueprints from a PRD | 10 diagram types, image and video exports, diagrams of things that aren't code | Diff impact, a dashboard that adapts to your role, knowledge-base graphs |
+| **Only here** | **Health check** (keys in code, open Supabase tables and Firebase rules, fakeable Stripe webhooks, endpoints anyone can use), **Recent work** (what your AI just changed, and why), blueprints from a PRD | 10 diagram types, image and video exports, diagrams of things that aren't code | Diff impact, a dashboard that adapts to your role, knowledge-base graphs |
| **Runs in** | Claude Code, Codex | Claude Code, Codex, Cursor, OpenCode and more | Claude Code, Codex, Cursor, Copilot, Gemini CLI and more |
**Pick Floorplan** to understand and check an app you own. **Pick Archify** to draw a diagram for a doc, a talk or a decision. **Pick Understand Anything** to study a large codebase function by function.
@@ -96,7 +96,7 @@ Pick a page you know and see everything behind it: its parts, the requests it ma
<td valign="top">
**🩺 Health check**<br>
-Finds passwords in code, endpoints anyone can use without signing in, unused files and files too big to edit safely. Each comes with a ready-to-paste fix.
+Catches the classic mistakes in AI-built apps: keys in the code or sent to the browser, Supabase tables anyone can read, open Firebase rules, Stripe webhooks anyone could fake, and endpoints with no sign-in. Each comes with a ready-to-paste fix. A check, not an audit.
</td>
</tr>
diff --git a/docs/development.md b/docs/development.md
index 4cea1a3..9293632 100644
--- a/docs/development.md
+++ b/docs/development.md
@@ -81,6 +81,7 @@ The tests use Node's built-in test runner, so there's nothing to install (`npm t
| `helpers-lib.test.mjs` | globs, file types, command-line flags, fallback names |
| `secrets.test.mjs` | Health finds secrets, masks them, and ignores placeholders |
| `health.test.mjs` | which endpoints Health calls open to anyone (FastAPI sample in `fixtures/`) |
+| `stack-health.test.mjs` | Health's checks for Supabase row-level security, Firebase rules, Stripe webhook signatures, secret keys in public variables and CORS, with look-alikes each must leave alone, and a whole sample app done wrong and done right |
| `firebase.test.mjs` | Firebase Cloud Functions: callables and onRequest functions as endpoints, the website's calls to them, Screens, Health, background functions (React + Firebase sample in `fixtures/`) |
| `blueprint.test.mjs` | a map planned from a PRD before any code exists: plan.json errors, endpoints and calls, Screens, the design review, refreshing and switching to the real code (sample PRD in `fixtures/`) |
| `blueprint-change.test.mjs` | changing a blueprint from the map: the agent's updated plan is checked, saved and highlighted, retried once when it fails the checks, and undone (stand-in CLI in `fixtures/fake-cli/`) |
diff --git a/docs/using-the-map.md b/docs/using-the-map.md
index 0d52357..c1f56b7 100644
--- a/docs/using-the-map.md
+++ b/docs/using-the-map.md
@@ -54,6 +54,11 @@ Drag the panel's left edge to resize it.
### Health
A count of important issues, and a plain-language list of problems the scan found:
- passwords and keys written into the code, or into a committed `.env` file;
+- secret keys in settings the browser gets, like an OpenAI key in a `VITE_` or `NEXT_PUBLIC_` variable;
+- Supabase tables with row-level security switched off, and database rules that let anyone change a table;
+- Firebase rules that let anyone read or change your data or files, including "test mode" rules and when they run out;
+- Stripe webhooks that act on payments without checking Stripe's signature, so anyone could fake one;
+- servers that let any website call them, flagged as worth fixing when login cookies go along too;
- endpoints that let anyone change or read data without signing in;
- files nothing uses;
- files over about 1,000 lines, which AI struggles to edit safely.
diff --git a/skills/floorplan/SKILL.md b/skills/floorplan/SKILL.md
index 46f0acc..c4b8e7b 100644
--- a/skills/floorplan/SKILL.md
+++ b/skills/floorplan/SKILL.md
@@ -157,6 +157,11 @@ It writes `floorplan/<project>-map.html` unless given a file. `--include` defaul
- **Health.** A header button, with a count of important issues, opens a plain-language list of problems the scan found:
- passwords and API keys written into the code or a committed `.env`;
+ - secret keys in variables the browser gets (`NEXT_PUBLIC_`, `VITE_` and the like);
+ - Supabase tables without row-level security, and policies that let anyone change a table (from the SQL migrations);
+ - Firebase rules (Firestore, Storage, Realtime Database) that let anyone in, including test mode and its expiry;
+ - Stripe webhooks that act on events without checking Stripe's signature;
+ - CORS that allows every website (worth fixing when credentials go along);
- endpoints that let anyone change or read data without signing in (including a sign-in check placed above the route line);
- files nothing uses;
- files over ~1,000 lines, which AI struggles to edit safely.
diff --git a/skills/floorplan/assets/ui/20-health.js b/skills/floorplan/assets/ui/20-health.js
index fdb2ecf..58fadb9 100644
--- a/skills/floorplan/assets/ui/20-health.js
+++ b/skills/floorplan/assets/ui/20-health.js
@@ -101,7 +101,7 @@
const top = `<div class="sec">
<div class="ph"><span class="ico" style="--c:${urgent ? SEV[count('high') ? 'high' : 'medium'].color : 'var(--ok)'}">${SC.icon('health')}</span><span class="kindlabel">${PLAN ? 'Design review' : 'Health check'}</span></div>
<h2 class="pt">${h(headline())}</h2>
- <p class="pd">${PLAN ? 'We checked the plan for parts anyone on the internet could use, decisions the PRD leaves open, and pieces nothing connects to. Settling these now is cheaper than after it’s built.' : 'We looked for passwords and keys left in the code, parts of your app anyone on the internet can use, leftover files and files too big to edit safely.'}</p>
+ <p class="pd">${PLAN ? 'We checked the plan for parts anyone on the internet could use, decisions the PRD leaves open, and pieces nothing connects to. Settling these now is cheaper than after it’s built.' : 'We looked for keys left in the code or sent to the browser, data and parts of your app anyone on the internet can reach (Supabase tables, Firebase rules, Stripe webhooks, endpoints), leftover files and files too big to edit safely. It’s a quick check, not a security audit.'}</p>
${issues.length ? breakdown() : `<div class="hc-calm">Nothing stood out. This is a quick automatic check, not a full ${PLAN ? 'design' : 'security'} review.</div>`}
<div class="actions"><button class="btn soft" data-hc-back>${SC.iconI('left')}Back</button></div>
</div>`;
diff --git a/skills/floorplan/scripts/lib/health.mjs b/skills/floorplan/scripts/lib/health.mjs
index 56370fd..027cd0e 100644
--- a/skills/floorplan/scripts/lib/health.mjs
+++ b/skills/floorplan/scripts/lib/health.mjs
@@ -1,6 +1,7 @@
// Health check: plain-language warnings about the codebase (secrets, endpoints anyone can call,
-// unused files, oversized files), written for people who built an app with AI and don't know its
-// insides. Precision beats recall: a false alarm costs this audience more than a missed one.
+// the usual Supabase / Firebase / Stripe / CORS mistakes, unused files, oversized files), written for
+// people who built an app with AI and don't know its insides. It's a check, not an audit. Precision
+// beats recall: a false alarm costs this audience more than a missed one.
//
// findIssues(src, file) — called by scan.mjs for every code/config/docs file with its source.
// Returns raw findings for that file, stored in scan.json files[].issues.
@@ -10,7 +11,7 @@
import fs from 'node:fs';
import path from 'node:path';
-import { git } from './common.mjs';
+import { git, compileGlob } from './common.mjs';
const lineIndex = (src) => {
const starts = [0];
@@ -95,6 +96,7 @@ function valueLooksReal(v, name) {
if (/\s/.test(v) || PLACEHOLDER.test(v)) return false;
if (/^(none|null|nil|undefined|true|false|string|number|text|required|optional|bearer|basic|secret|token|password|test|dev|development|local|admin)$/i.test(v)) return false;
if (/^[A-Z][A-Z0-9_]+$/.test(v)) return false; // the NAME of an env var
+ if (/^env\([A-Za-z_][\w]*\)$/.test(v)) return false; // Supabase config.toml reading one: "env(S3_SECRET_KEY)"
if (/^[a-z_]+(\.[a-z_]+)+$/i.test(v)) return false; // dotted identifier / key path
if (/^(\.{0,2}\/|~\/|https?:\/\/|[a-z]:\\)/i.test(v)) return false; // paths and URLs
if (/\.(json|pem|key|txt|ya?ml|env|crt|p12)$/i.test(v)) return false; // a file name, not the key
@@ -368,12 +370,145 @@ function pyAliasGuards(files, findings) {
return out;
}
+// ---------- 3. the usual mistakes in apps built on Supabase, Firebase, Stripe, Next.js and Vite ----------
+
+// Frontend build tools copy variables with these prefixes into the code every visitor downloads.
+// (A bare PUBLIC_ prefix only means that to SvelteKit and Astro, so it counts only where they read it.)
+const PUBLIC_PREFIX = /^(NEXT_PUBLIC|VITE|REACT_APP|EXPO_PUBLIC|NUXT_PUBLIC|GATSBY|PUBLIC)_/;
+const CLIENT_ONLY_PREFIX = /^(VITE|REACT_APP|EXPO_PUBLIC|GATSBY|PUBLIC)_/; // these exist only in browser code
+// After the prefix: names that hold something private…
+const PRIVATE_NAME = /(^|_)(SECRETS?|SERVICE_ROLE|SERVICE_KEY|PRIVATE|PASSWORD|PASSWD|PWD|DATABASE_URL|DB_URL|CONNECTION_STRING|ADMIN_KEY|ADMIN_TOKEN|AUTH_TOKEN|JWT_SECRET)(_|$)/;
+// …or a key or token for a service that charges you or acts as you (AI, email, messaging, code hosting)
+const PRIVATE_SERVICE = /(^|_)(OPENAI|ANTHROPIC|CLAUDE|GEMINI|GROQ|GROK|XAI|MISTRAL|REPLICATE|ELEVEN_?LABS|SENDGRID|RESEND|POSTMARK|MAILGUN|BREVO|TWILIO|OPENROUTER|DEEPSEEK|PERPLEXITY|COHERE|TOGETHER|FIREWORKS|HUGGING_?FACE|HF|STABILITY|FAL|ASSEMBLY_?AI|DEEPGRAM|PINECONE|SERPAPI|TAVILY|FIRECRAWL|AIRTABLE|NOTION|GITHUB|SLACK|DISCORD|TELEGRAM|STRIPE)(_|$)/;
+const KEYISH = /(^|_)(KEY|API_?KEY|TOKEN|SECRET)$/;
+// Keys that are public by design: Supabase's anon key, Stripe's publishable key, reCAPTCHA site keys, IDs
+const PUBLIC_BY_DESIGN = /(^|_)(PUBLISHABLE|ANON|PK|PUBLIC|SITE|CLIENT_ID|APP_ID|PROJECT_ID|MEASUREMENT_ID)(_|$)/;
+const SERVICE_LABEL = { OPENAI: 'OpenAI', ANTHROPIC: 'Anthropic', CLAUDE: 'Anthropic', GEMINI: 'Google Gemini', GROQ: 'Groq', GROK: 'xAI', XAI: 'xAI', ELEVENLABS: 'ElevenLabs', ELEVEN_LABS: 'ElevenLabs', SENDGRID: 'SendGrid', OPENROUTER: 'OpenRouter', DEEPSEEK: 'DeepSeek', HF: 'Hugging Face', HUGGINGFACE: 'Hugging Face', HUGGING_FACE: 'Hugging Face', ASSEMBLYAI: 'AssemblyAI', ASSEMBLY_AI: 'AssemblyAI', SERPAPI: 'SerpApi', GITHUB: 'GitHub' };
+
+/** For a variable like VITE_OPENAI_API_KEY: the service it unlocks ('OpenAI', or '' for a generic secret), or null when it's fine to be public. */
+export function leakyPublicVar(name) {
+ const m = PUBLIC_PREFIX.exec(name);
+ if (!m) return null;
+ const rest = name.slice(m[0].length);
+ if (PUBLIC_BY_DESIGN.test(rest)) return null;
+ const svc = PRIVATE_SERVICE.exec(rest)?.[2];
+ if (svc && KEYISH.test(rest)) return SERVICE_LABEL[svc] || svc.charAt(0) + svc.slice(1).toLowerCase();
+ if (PRIVATE_NAME.test(rest)) return /SERVICE_ROLE|SERVICE_KEY/.test(rest) ? 'Supabase' : '';
+ return null;
+}
+
+function findPublicEnv(src, f, out) {
+ const at = lineIndex(src);
+ const seen = new Set();
+ const add = (name, idx) => { if (leakyPublicVar(name) === null || seen.has(name)) return; seen.add(name); out.push({ k: 'public-env', name, line: at(idx) }); };
+ for (const m of src.matchAll(/\b(process\.env\.|process\.env\[\s*['"]|import\.meta\.env\.)([A-Z][A-Z0-9_]*)/g)) {
+ if (/^PUBLIC_/.test(m[2]) && m[1].startsWith('process')) continue; // PUBLIC_ means nothing to Node
+ add(m[2], m.index);
+ }
+ for (const m of src.matchAll(/import\s*\{([^}]*)\}\s*from\s*['"]\$env\/(?:static|dynamic)\/public['"]/g)) for (const n of m[1].match(/[A-Z][A-Z0-9_]*/g) || []) add(n, m.index);
+}
+
+// Stripe: code that acts on payment events. Without checking Stripe's signature first, anyone can send
+// a fake "payment succeeded" event to the webhook address.
+const STRIPE_EVENT = /(?:\bcase\s*|[!=]==?\s*)["'](checkout\.session\.(?:completed|async_payment_succeeded)|payment_intent\.succeeded|invoice\.(?:paid|payment_succeeded|payment_failed)|customer\.subscription\.(?:created|updated|deleted)|charge\.(?:succeeded|refunded))["']/g;
+const STRIPE_VERIFIED = /constructEvent(?:Async)?\s*\(|construct_event\s*\(|verifyHeader|verify_header|events\.retrieve\s*\(|Event\.retrieve\s*\(/;
+
+function findStripeWebhook(src, f, out) {
+ if (!/stripe/i.test(src) || STRIPE_VERIFIED.test(src)) return;
+ const hits = [...src.matchAll(STRIPE_EVENT)];
+ if (!hits.length) return;
+ out.push({ k: 'stripe-unsigned', line: lineIndex(src)(hits[0].index), events: [...new Set(hits.map((m) => m[1]))].slice(0, 4) });
+}
+
+// CORS: a server that tells browsers any website may call it. Harmless for a public API without login
+// cookies; with them, any site a signed-in visitor opens could act as them.
+function findCors(src, f, out) {
+ const at = lineIndex(src);
+ const add = (idx, how, credentials) => out.push({ k: 'cors-any', line: at(idx), how, credentials });
+ if (['typescript', 'javascript'].includes(f.lang)) {
+ if (/require\(\s*['"]cors['"]\s*\)|from\s+['"]cors['"]/.test(src)) {
+ for (const m of src.matchAll(/\bcors\(\s*\)/g)) add(m.index, 'cors()', false);
+ for (const m of src.matchAll(/\bcors\(\s*\{([^}]*)\}/g)) {
+ const origin = /\borigin\s*:\s*(?:(['"`])\*\1|(true)\b)/.exec(m[1]);
+ // '*' with credentials is refused by browsers; origin: true echoes whichever site asks
+ if (origin) add(m.index, origin[2] ? 'cors({ origin: true })' : "cors({ origin: '*' })", !!origin[2] && /\bcredentials\s*:\s*true/.test(m[1]));
+ }
+ }
+ const credHeader = /['"]Access-Control-Allow-Credentials['"]\s*[,:]\s*['"`]?true/i.test(src);
+ for (const m of src.matchAll(/['"]Access-Control-Allow-Origin['"]\s*[,:]\s*(?:(['"`])\*\1|(req(?:uest)?\.headers(?:\.origin\b|\[\s*['"]origin['"]\s*\]|\.get\(\s*['"]origin['"]\s*\))))/gi)) {
+ add(m.index, m[2] ? 'Access-Control-Allow-Origin set to the caller’s origin' : 'Access-Control-Allow-Origin: *', !!m[2] && credHeader);
+ }
+ } else if (f.lang === 'python') {
+ for (const m of src.matchAll(/CORSMiddleware\s*,([\s\S]{0,800}?)\)\s*$/gm)) {
+ if (/allow_origins\s*=\s*\[\s*(['"])\*\1\s*\]|allow_origin_regex\s*=\s*r?(['"])\.\*\2/.test(m[1])) add(m.index, 'CORSMiddleware with allow_origins=["*"]', /allow_credentials\s*=\s*True/.test(m[1]));
+ }
+ for (const m of src.matchAll(/\bCORS\(\s*\w+\s*(?:\)|,([^)]*)\))/g)) {
+ const opts = m[1] || '';
+ if (/\borigins["']?\s*[:=]\s*(?!(['"])\*\1)/.test(opts)) continue; // a list of allowed sites (origins=… or "origins": …)
+ add(m.index, 'CORS(app) with every origin allowed', /supports_credentials\s*=\s*True/.test(opts));
+ }
+ const all = /^\s*CORS_(?:ALLOW_ALL_ORIGINS|ORIGIN_ALLOW_ALL)\s*=\s*True\b/m.exec(src);
+ if (all) add(all.index, 'CORS_ALLOW_ALL_ORIGINS = True', /^\s*CORS_ALLOW_CREDENTIALS\s*=\s*True\b/m.test(src));
+ }
+}
+
+// Supabase / Postgres migrations: tables, row-level security switched on or off, tables dropped or
+// renamed, and security rules that let anyone change a table. build.mjs replays them in file order.
+const SQL_ID = String.raw`(?:"[^"]+"|[A-Za-z_][\w$]*)`;
+const SQL_NAME = String.raw`(?:${SQL_ID}\s*\.\s*)?${SQL_ID}`;
+const sqlName = (raw) => {
+ const parts = raw.split(/\s*\.\s*(?=(?:"[^"]*"|[^"])*$)/).map((p) => p.trim().replace(/^"|"$/g, '').toLowerCase());
+ return parts.length > 1 ? `${parts[0]}.${parts[1]}` : `public.${parts[0]}`;
+};
+
+function findSql(src, f, out) {
+ // blank out comments, keeping line breaks so line numbers stay right
+ const s = src.replace(/--[^\n]*/g, (m) => ' '.repeat(m.length)).replace(/\/\*[\s\S]*?\*\//g, (m) => m.replace(/[^\n]/g, ' '));
+ const at = lineIndex(s);
+ const found = [];
+ for (const m of s.matchAll(new RegExp(String.raw`\bcreate\s+(?:(temp|temporary)\s+|unlogged\s+)?table\s+(?:if\s+not\s+exists\s+)?(${SQL_NAME})`, 'gi'))) {
+ if (!m[1]) found.push({ i: m.index, k: 'sql-table', table: sqlName(m[2]) });
+ }
+ for (const m of s.matchAll(new RegExp(String.raw`\balter\s+table\s+(?:if\s+exists\s+)?(?:only\s+)?(${SQL_NAME})\s+(enable|disable|force|no\s+force)\s+row\s+level\s+security`, 'gi'))) {
+ found.push({ i: m.index, k: 'sql-rls', table: sqlName(m[1]), on: !/^(disable|no)/i.test(m[2]) });
+ }
+ for (const m of s.matchAll(new RegExp(String.raw`\balter\s+table\s+(?:if\s+exists\s+)?(?:only\s+)?(${SQL_NAME})\s+rename\s+to\s+(${SQL_ID})`, 'gi'))) {
+ const from = sqlName(m[1]);
+ found.push({ i: m.index, k: 'sql-rename', table: from, to: `${from.split('.')[0]}.${m[2].replace(/^"|"$/g, '').toLowerCase()}` });
+ }
+ for (const m of s.matchAll(new RegExp(String.raw`\bdrop\s+table\s+(?:if\s+exists\s+)?(${SQL_NAME}(?:\s*,\s*${SQL_NAME})*)`, 'gi'))) {
+ for (const t of m[1].split(/\s*,\s*/)) found.push({ i: m.index, k: 'sql-drop', table: sqlName(t) });
+ }
+ // a permissive rule that lets signed-out visitors add, change or delete rows: `using (true)` / `with check (true)`
+ for (const m of s.matchAll(new RegExp(String.raw`\bcreate\s+policy\s+(${SQL_ID})\s+on\s+(${SQL_NAME})([^;]*)`, 'gi'))) {
+ const body = m[3];
+ if (/\bas\s+restrictive\b/i.test(body)) continue;
+ const cmd = (/\bfor\s+(all|select|insert|update|delete)\b/i.exec(body)?.[1] || 'all').toLowerCase();
+ if (cmd === 'select') continue;
+ const to = /\bto\s+([\w\s,"]+?)(?=\s+(?:using|with)\b|\s*$)/i.exec(body)?.[1];
+ const roles = to ? to.split(/\s*,\s*/).map((r) => r.replace(/"/g, '').trim().toLowerCase()) : ['public'];
+ if (!roles.some((r) => r === 'anon' || r === 'public')) continue;
+ const yes = (clause) => new RegExp(String.raw`\b${clause}\s*\(\s*\(?\s*true\s*\)?\s*\)`, 'i').test(body);
+ const open = cmd === 'insert' ? yes('with\\s+check') : cmd === 'all' ? yes('using') || yes('with\\s+check') : yes('using');
+ if (open) found.push({ i: m.index, k: 'sql-open-policy', table: sqlName(m[2]), policy: m[1].replace(/^"|"$/g, ''), cmd, signedOut: roles.includes('anon') || !to });
+ }
+ for (const x of found.sort((a, b) => a.i - b.i)) { const { i, ...rest } = x; out.push({ ...rest, line: at(i) }); }
+}
+
+const USES_SUPABASE = /@supabase\/(?:supabase-js|ssr|auth-helpers[\w-]*)|from\s+supabase\s+import|^\s*import\s+supabase\b/m;
+
// ---------- per-file entry point ----------
export function findIssues(src, f) {
const out = [];
findSecrets(src, f, out);
if (f.category !== 'code') return out;
+ if (f.lang === 'sql') findSql(src, f, out);
+ if (!TEST_FILE.test(f.path) && !EXAMPLE_FILE.test(f.path)) {
+ if (['typescript', 'javascript', 'vue', 'svelte', 'astro'].includes(f.lang)) findPublicEnv(src, f, out);
+ if (['typescript', 'javascript', 'python'].includes(f.lang)) { findStripeWebhook(src, f, out); findCors(src, f, out); }
+ }
+ if (['typescript', 'javascript', 'python'].includes(f.lang) && USES_SUPABASE.test(src)) out.push({ k: 'uses-supabase' });
if (f.lang === 'python') pyRoutes(src, f, out);
else if (['typescript', 'javascript'].includes(f.lang)) jsRoutes(src, f, out);
// run directly or registered by name (so "nothing imports it" doesn't mean unused)
@@ -410,7 +545,10 @@ export function buildHealth({ graph, scan, arch, root }) {
const isFrontend = (p) => { const f = fileByPath.get(p); return !!f && kinds(f.n).includes('ui'); };
const nameOf = (p) => fileByPath.get(p)?.name || p.split('/').pop();
const label = (id) => nodeById.get(id)?.label || id;
- // Files outside the map (config, .env) point at the module that owns their folder instead.
+ // Files outside the map (config, .env, Firebase rules) point at the module that owns their folder, or
+ // failing that the most specific part of the map whose paths name them.
+ const archGlobs = (arch?.nodes || []).filter((n) => n?.id && nodeById.has(n.id) && Array.isArray(n.paths)).flatMap((n) => n.paths.map((p) => ({ id: n.id, g: compileGlob(p) })));
+ const globOwner = (p) => archGlobs.filter((x) => x.g.test(p)).sort((a, b) => b.g.specificity - a.g.specificity)[0]?.id;
const dirOwner = new Map();
for (const f of graph.files) { let d = f.p; while (d.includes('/')) { d = d.slice(0, d.lastIndexOf('/')); if (!dirOwner.has(d)) dirOwner.set(d, new Map()); const m = dirOwner.get(d); m.set(f.n, (m.get(f.n) || 0) + 1); } }
const itemFor = (p) => {
@@ -422,7 +560,8 @@ export function buildHealth({ graph, scan, arch, root }) {
const top = m && [...m.entries()].filter(([n]) => n !== 'unassigned').sort((a, b) => b[1] - a[1])[0];
if (top) return 'n:' + top[0];
}
- return null;
+ const owner = globOwner(p);
+ return owner ? 'n:' + owner : null;
};
const findings = (p) => scanByPath.get(p)?.issues || [];
@@ -526,6 +665,256 @@ export function buildHealth({ graph, scan, arch, root }) {
});
}
+ // ---------- secret keys in variables the browser gets (NEXT_PUBLIC_, VITE_ …) ----------
+ const live = (p) => !TEST_FILE.test(p) && !EXAMPLE_FILE.test(p);
+ const publicVars = new Map(); // name -> { svc, refs: [{file, line}], defs: [{file, line}] }
+ const publicVar = (name) => { if (!publicVars.has(name)) publicVars.set(name, { name, svc: leakyPublicVar(name), refs: [], defs: [] }); return publicVars.get(name); };
+ for (const f of scan.files) if (live(f.path)) for (const x of findings(f.path)) if (x.k === 'public-env') publicVar(x.name).refs.push({ file: f.path, line: x.line });
+ for (const f of scan.files) {
+ const base = f.path.split('/').pop();
+ if (!/^\.env(\.[\w.-]+)?$/i.test(base) || /example|sample|template|dist|schema/i.test(base) || !live(f.path)) continue;
+ let src = '';
+ try { src = readHead(path.join(root, f.path), 262144); } catch { continue; }
+ src.split('\n').forEach((t, i) => {
+ const m = /^\s*(?:export\s+)?([A-Z][A-Z0-9_]*)\s*=\s*["']?([^"'#\s]*)/.exec(t);
+ if (m && m[2] && !PLACEHOLDER.test(m[2]) && !/^\$/.test(m[2]) && leakyPublicVar(m[1]) !== null) publicVar(m[1]).defs.push({ file: f.path, line: i + 1 });
+ });
+ }
+ if (publicVars.size) {
+ const vars = [...publicVars.values()];
+ const browserSide = (p) => isFrontend(p) || /\.(jsx|tsx|vue|svelte|astro)$/.test(p);
+ const sev = vars.some((v) => CLIENT_ONLY_PREFIX.test(v.name) || v.refs.some((r) => browserSide(r.file))) ? 'high' : 'medium';
+ const svcs = [...new Set(vars.map((v) => v.svc).filter(Boolean))];
+ const locations = vars.flatMap((v) => [...v.defs, ...v.refs]);
+ const items = [], notes = {};
+ for (const v of vars) for (const loc of [...v.refs, ...v.defs]) {
+ const it = itemFor(loc.file);
+ if (!it) continue;
+ if (!items.includes(it)) items.push(it);
+ const note = `line ${loc.line}: ${v.name}`;
+ if (!(notes[it] || '').includes(note)) notes[it] = (notes[it] ? notes[it] + ' · ' : '') + note;
+ }
+ const names = vars.map((v) => v.name);
+ issues.push({
+ id: 'public-env', kind: 'public-env-secret', severity: sev,
+ title: vars.length === 1 ? `${names[0]} is sent to every visitor’s browser` : `${vars.length} secret keys are sent to every visitor’s browser`,
+ detail: `Settings whose names start with NEXT_PUBLIC_, VITE_ and the like are copied into the code your site sends to every visitor, where anyone can read them in their browser’s developer tools. ${vars.length === 1 ? `${names[0]} looks like a secret key` : `${names.slice(0, 4).join(', ')}${vars.length > 4 ? '…' : ''} look like secret keys`}${svcs.length ? ` (for ${svcs.slice(0, 4).join(', ')})` : ''}, so anyone could use ${vars.length === 1 ? 'it' : 'them'} as you: run up your bill, send messages as you, or reach your data.${sev === 'medium' ? ' Right now only server code seems to read it, so it may not have reached a browser yet, but the name invites that.' : ''}`,
+ items, notes, locations,
+ fix: [
+ `Security fix: secret keys in public environment variables (the browser gets every variable with these prefixes). Do not print, echo or repeat any key value.`,
+ ``,
+ ...vars.map((v) => `- ${v.name}${v.svc ? ` (${v.svc})` : ''}: ${[...v.defs.map((d) => `set in ${d.file}:${d.line}`), ...v.refs.map((r) => `used in ${r.file}:${r.line}`)].slice(0, 6).join(', ')}`),
+ ``,
+ `For each one:`,
+ `1. Rename it without the public prefix (for example OPENAI_API_KEY instead of VITE_OPENAI_API_KEY) in the .env files, .env.example and the hosting settings.`,
+ `2. Move every place that uses it into server-side code (a Next.js route handler or server action, a Supabase Edge Function, a serverless function or the existing backend) and have the frontend call that instead, so the key never reaches the browser.`,
+ vars.some((v) => v.svc === 'Supabase') ? `3. For the Supabase service-role key: browser code should use the anon key and rely on row-level security; only server code may use the service-role key.` : `3. Keep the frontend working the same way, and run the app to check.`,
+ ``,
+ `Then give me a checklist (names only, no values) of the keys to rotate in each service's dashboard: anyone who loaded the site may have copied them.`,
+ ].join('\n'),
+ });
+ }
+
+ // ---------- Supabase tables anyone can read and change ----------
+ const usesSupabase = scan.files.some((f) => /(^|\/)supabase\/(migrations\/|config\.toml$|functions\/)/.test(f.path) || findings(f.path).some((x) => x.k === 'uses-supabase'));
+ if (usesSupabase) {
+ const byRepo = new Map(); // repo -> [file, …] in path order (migrations are named by timestamp)
+ for (const f of scan.files) if (f.lang === 'sql' && live(f.path) && findings(f.path).some((x) => x.k.startsWith('sql-'))) { const k = f.repo ?? ''; if (!byRepo.has(k)) byRepo.set(k, []); byRepo.get(k).push(f.path); }
+ for (const [repo, sqlFiles] of byRepo) {
+ const tables = new Map(); // table -> { file, line, rls, off }
+ const policies = [];
+ for (const p of sqlFiles.sort()) for (const x of findings(p)) {
+ const t = tables.get(x.table);
+ if (x.k === 'sql-table' && !t) tables.set(x.table, { file: p, line: x.line, rls: null });
+ else if (x.k === 'sql-rls' && t) { t.rls = x.on; t.off = x.on ? null : { file: p, line: x.line }; }
+ else if (x.k === 'sql-rename' && t) { tables.delete(x.table); tables.set(x.to, t); for (const pol of policies) if (pol.table === x.table) pol.table = x.to; }
+ else if (x.k === 'sql-drop') tables.delete(x.table);
+ else if (x.k === 'sql-open-policy') policies.push({ ...x, file: p });
+ }
+ const short = (t) => t.replace(/^public\./, '');
+ const open = [...tables.entries()].filter(([name, t]) => name.startsWith('public.') && t.rls !== true);
+ if (open.length) {
+ const items = [], notes = {};
+ for (const [name, t] of open) {
+ const it = itemFor(t.file);
+ if (!it) continue;
+ if (!items.includes(it)) items.push(it);
+ notes[it] = (notes[it] ? notes[it] + ' · ' : '') + `line ${t.line}: ${short(name)} (row-level security ${t.off ? 'switched off' : 'never switched on'})`;
+ }
+ const list = open.map(([name]) => short(name));
+ issues.push({
+ id: 'rls:' + repo, kind: 'database-rules', severity: 'high',
+ title: open.length === 1 ? `Anyone can read and change your ${list[0]} table` : `Anyone can read and change ${open.length} of your database tables`,
+ detail: `Your app talks to Supabase with a public key that every visitor’s browser has. Row-level security is what stops one person from reading or changing someone else’s rows, and it’s off for ${open.length === 1 ? `the ${list[0]} table` : `${list.slice(0, 6).join(', ')}${list.length > 6 ? ` and ${list.length - 6} more` : ''}`}. Without it, anyone can use that key to read, change or delete every row${open.length === 1 ? '' : ' in them'}. If your app only reaches the database from its own server, the risk is lower, but switching it on is still the safe default.`,
+ items, notes,
+ locations: open.map(([, t]) => t.off || { file: t.file, line: t.line }),
+ fix: [
+ `Security fix: Supabase tables without row-level security (RLS). Do not print any keys.`,
+ ``,
+ `Anyone with the app's public (anon) key can read and change these tables:`,
+ ...open.map(([name, t]) => `- ${name}: created in ${t.file}:${t.line}${t.off ? `, row-level security switched off in ${t.off.file}:${t.off.line}` : ''}`),
+ ``,
+ `1. Read how the app uses each table: which pages read or write it, and whether rows belong to a user (for example a user_id column that should match auth.uid()).`,
+ `2. Write one new migration (don't edit the old ones) that runs \`alter table <table> enable row level security;\` for each table, plus policies so the app keeps working: usually signed-in users can read and change only their own rows (\`using (auth.uid() = user_id)\`), and tables meant to be public get a read-only policy. Server code that uses the service-role key isn't affected by these rules.`,
+ `3. Explain the policies to me in plain words before applying anything, and don't loosen any existing policy.`,
+ `4. Tell me how to apply the migration (for example \`supabase db push\`) and how to check, while signed out, that I can't read other people's rows.`,
+ ].join('\n'),
+ });
+ }
+ const loose = policies.filter((pol) => tables.get(pol.table)?.rls === true);
+ if (loose.length) {
+ const verb = (cmd) => ({ insert: 'add', update: 'change', delete: 'delete' }[cmd] || 'add, change and delete');
+ const rowsOf = (cmd, t) => `${verb(cmd)} rows ${{ insert: 'to', delete: 'from' }[cmd] || 'in'} ${t}`;
+ const items = [], notes = {};
+ for (const pol of loose) {
+ const it = itemFor(pol.file);
+ if (!it) continue;
+ if (!items.includes(it)) items.push(it);
+ notes[it] = (notes[it] ? notes[it] + ' · ' : '') + `line ${pol.line}: “${pol.policy}” lets anyone ${rowsOf(pol.cmd, short(pol.table))}`;
+ }
+ issues.push({
+ id: 'open-policy:' + repo, kind: 'database-rules', severity: 'medium',
+ title: loose.length === 1 ? `A database rule lets anyone change your ${short(loose[0].table)} table` : `${loose.length} database rules let anyone change your data`,
+ detail: `${loose.length === 1 ? `The rule “${loose[0].policy}” lets anyone` : `These rules let anyone`}${loose.some((pol) => pol.signedOut) ? ', even people who aren’t signed in,' : ''} ${loose.length === 1 ? rowsOf(loose[0].cmd, short(loose[0].table)) : `add, change or delete rows in ${[...new Set(loose.map((pol) => short(pol.table)))].slice(0, 5).join(', ')}`}. If a table is a public form (like a waitlist or contact form), letting anyone add rows is fine; otherwise limit it to signed-in users and their own rows.`,
+ items, notes,
+ locations: loose.map((pol) => ({ file: pol.file, line: pol.line })),
+ fix: [
+ `Security review: Supabase row-level security policies that allow anyone to change data (\`using (true)\` / \`with check (true)\` for anon or public):`,
+ ...loose.map((pol) => `- “${pol.policy}” on ${pol.table} (for ${pol.cmd}) — ${pol.file}:${pol.line}`),
+ ``,
+ `For each one, tell me in plain words what it allows and whether the app needs that. If the table isn't meant to be public, replace \`true\` with a check such as \`auth.uid() = user_id\`, limited to authenticated users, in a new migration. If it's a public form, keep adding rows open but make sure nobody can read, change or delete other people's rows. Show me the change before applying it.`,
+ ].join('\n'),
+ });
+ }
+ }
+ }
+
+ // ---------- Firebase security rules that let anyone in ----------
+ const today = new Date();
+ const longDate = (d) => d.toLocaleDateString('en-US', { timeZone: 'UTC', month: 'long', day: 'numeric', year: 'numeric' });
+ for (const f of scan.files) {
+ const base = f.path.split('/').pop();
+ if (!live(f.path) || !(/\.rules$/i.test(base) || /^database(\.[\w-]+)?\.rules\.json$/i.test(base))) continue;
+ let src = '';
+ try { src = readHead(path.join(root, f.path), 262144); } catch { continue; }
+ const rtdb = /\.json$/i.test(base);
+ const service = rtdb ? 'Realtime Database' : /service\s+firebase\.storage/.test(src) ? 'Storage' : /service\s+cloud\.firestore/.test(src) ? 'Firestore' : null;
+ if (!service) continue;
+ const rules = rtdb ? firebaseRtdbRules(src, today) : firebaseRules(src, today);
+ // the whole database, or (Firestore and Storage) any change beyond adding new records to one place
+ const severe = (r) => r.wide || (!rtdb && !r.createOnly);
+ const writes = rules.filter((r) => r.write && r.open !== 'expired' && severe(r));
+ const deepWrites = rules.filter((r) => r.write && r.open === 'always' && !severe(r));
+ const reads = rules.filter((r) => r.read && !r.write && r.open !== 'expired' && r.wide);
+ const until = rules.find((r) => r.open === 'until');
+ const expired = rules.find((r) => r.open === 'expired');
+ if (!writes.length && !deepWrites.length && !reads.length && !expired) continue;
+ const files = service === 'Storage';
+ const what = files ? 'files' : 'data';
+ const deploy = { Firestore: 'firestore:rules', Storage: 'storage', 'Realtime Database': 'database' }[service];
+ const shown = [...writes, ...deepWrites, ...reads, ...(expired ? [expired] : [])];
+ let title, detail, severity;
+ if (until && writes.length) {
+ severity = 'high';
+ title = `Your Firebase ${service} is open to anyone until ${longDate(until.until)}`;
+ detail = `${base} still has Firebase’s “test mode” rules: anyone can read and change ${files ? 'every file' : 'everything'} until ${longDate(until.until)}, without signing in or using your app. After that date every request is refused and the app stops working, so replace them with real rules rather than moving the date.`;
+ } else if (writes.length) {
+ severity = 'high';
+ title = files ? 'Anyone can upload, replace or delete your Firebase files' : `Anyone can change or delete your Firebase ${service} data`;
+ detail = `A rule in ${base} (line ${writes[0].line}, at ${writes[0].where}) lets anyone, signed in or not, ${files ? 'upload, replace or delete files' : 'add, change or delete data'}. Your app’s Firebase settings are in every visitor’s browser, so anyone can use them to reach your ${service} directly, without going through your app.`;
+ } else if (deepWrites.length || reads.length) {
+ severity = 'medium';
+ title = reads.length && !deepWrites.length ? `Anyone can read everything in your Firebase ${service}` : `Anyone can change part of your Firebase ${service}`;
+ detail = [
+ reads.length ? `A rule in ${base} (line ${reads[0].line}) lets anyone read every ${files ? 'file' : 'record'}, including data that may belong to your users. If everything there is meant to be public, that’s fine; otherwise it needs a sign-in check.` : '',
+ deepWrites.length ? `A rule in ${base} (line ${deepWrites[0].line}) lets anyone ${deepWrites[0].createOnly ? 'add to' : 'change'} ${deepWrites[0].where} without signing in. If that’s a public form (like a waitlist or contact form), that can be fine; otherwise it needs a sign-in check.` : '',
+ ].filter(Boolean).join(' ');
+ } else {
+ severity = 'medium';
+ title = `Your Firebase ${service} test-mode rules ran out on ${longDate(expired.until)}`;
+ detail = `${base} has Firebase’s “test mode” rules, which stopped allowing access on ${longDate(expired.until)}. Every read and write is now refused, so the parts of the app that use ${service} won’t work. Replace them with real rules instead of moving the date, which would open everything to anyone again.`;
+ }
+ const it = itemFor(f.path);
+ issues.push({
+ id: 'firebase-rules:' + f.path, kind: 'database-rules', severity, title, detail,
+ items: it ? [it] : [], notes: it ? { [it]: shown.slice(0, 4).map((r) => `line ${r.line}: ${r.text}`).join(' · ') } : {},
+ locations: shown.map((r) => ({ file: f.path, line: r.line })),
+ fix: [
+ `Security fix: Firebase ${service} security rules in ${f.path}.`,
+ ``,
+ `These rules let anyone in (or have expired):`,
+ ...shown.map((r) => `- line ${r.line}: ${r.text}`),
+ ``,
+ `1. Read how the app uses each ${rtdb ? 'path' : files ? 'storage folder' : 'collection'}: what each screen reads and writes, and which data belongs to a user (for example ${files ? 'files under a folder named after the user’s uid' : 'documents keyed by the user’s uid, or with an ownerId field'}).`,
+ `2. Rewrite the rules so signed-in users can only read and write their own ${what} (${rtdb ? 'auth != null && auth.uid === $uid' : files ? 'request.auth != null && request.auth.uid == userId' : 'request.auth != null && request.auth.uid == resource.data.ownerId'}), public content is read-only, and everything else is denied. Don't use test-mode dates.`,
+ `3. Explain the new rules to me in plain words and wait for my OK.`,
+ `4. Then deploy them with \`firebase deploy --only ${deploy}\` and check them in the Firebase console's Rules Playground (or the emulator): a signed-out user shouldn't be able to read or change anything private.`,
+ ].join('\n'),
+ });
+ }
+
+ // ---------- Stripe webhooks that don't check Stripe's signature ----------
+ const stripeFiles = new Map(); // file -> finding
+ for (const f of scan.files) if (live(f.path)) { const x = findings(f.path).find((y) => y.k === 'stripe-unsigned'); if (x) stripeFiles.set(f.path, x); }
+ for (const [file, x] of stripeFiles) {
+ const routesHere = graph.routes.map((r, i) => ({ r, i })).filter(({ r }) => r.f === file && r.m !== 'GET');
+ const it = itemFor(file);
+ const items = [...(it ? [it] : []), ...routesHere.map(({ i }) => 'r:' + i)];
+ const py = /\.py$/.test(file);
+ const next = /(^|\/)app\/.*route\.[jt]s$|(^|\/)pages\/api\//.test(file);
+ issues.push({
+ id: 'stripe-webhook:' + file, kind: 'stripe-webhook', severity: 'high',
+ title: `Anyone could fake a Stripe payment in ${nameOf(file)}`,
+ detail: `${file.split('/').pop()} acts on Stripe events (like “${x.events[0]}”) without checking Stripe’s signature, so anyone who finds the address can send a fake “payment succeeded” message and get orders, credits or a subscription for free. Stripe signs every real event; checking that signature takes a few lines.`,
+ items, notes: Object.fromEntries(items.map((id) => [id, id.startsWith('r:') ? 'Receives Stripe events without checking they’re real' : `line ${x.line}: handles ${x.events.join(', ')} without checking Stripe’s signature`])),
+ locations: [{ file, line: x.line }],
+ fix: [
+ `Security fix in ${file}: verify Stripe webhook signatures before acting on any event. Do not print any secret values.`,
+ ``,
+ `It handles ${x.events.join(', ')} (line ${x.line}) without calling ${py ? 'stripe.Webhook.construct_event' : 'stripe.webhooks.constructEvent'}.`,
+ ``,
+ py
+ ? `1. Read the raw request body as bytes (FastAPI: \`await request.body()\`; Flask: \`request.get_data()\`), not the parsed JSON.`
+ : next ? `1. Read the raw request body with \`await req.text()\` (App Router) or turn off the body parser for this route (Pages Router), not the parsed JSON.` : `1. Give this route the raw body: \`express.raw({ type: 'application/json' })\` on it, registered before \`express.json()\` runs for it.`,
+ py
+ ? `2. Call \`stripe.Webhook.construct_event(payload, request.headers["stripe-signature"], os.environ["STRIPE_WEBHOOK_SECRET"])\` and return 400 if it raises.`
+ : `2. Call \`stripe.webhooks.constructEvent(rawBody, signatureHeader, process.env.STRIPE_WEBHOOK_SECRET)\` (the \`stripe-signature\` header) and return 400 if it throws.`,
+ `3. Use only the verified event from then on, and handle each event once even if Stripe sends it again.`,
+ `4. Add STRIPE_WEBHOOK_SECRET (empty) to .env.example and tell me where to get it: the Stripe dashboard (Developers → Webhooks → the endpoint → Signing secret), or \`stripe listen\` when testing locally.`,
+ ].join('\n'),
+ });
+ }
+
+ // ---------- servers that let any website call them (CORS) ----------
+ const cors = [];
+ for (const f of scan.files) if (live(f.path)) for (const x of findings(f.path)) if (x.k === 'cors-any') cors.push({ file: f.path, ...x });
+ if (cors.length) {
+ const cred = cors.filter((c) => c.credentials);
+ const filesC = [...new Set(cors.map((c) => c.file))];
+ const items = [], notes = {};
+ for (const c of cors) {
+ const it = itemFor(c.file);
+ if (!it) continue;
+ if (!items.includes(it)) items.push(it);
+ notes[it] = (notes[it] ? notes[it] + ' · ' : '') + `line ${c.line}: ${c.how}${c.credentials ? ', with login cookies' : ''}`;
+ }
+ issues.push({
+ id: 'cors', kind: 'cors', severity: cred.length ? 'medium' : 'low',
+ title: cred.length ? 'Any website could act as your signed-in users' : 'Any website can call your server from a visitor’s browser',
+ detail: cred.length
+ ? `${cred[0].file.split('/').pop()} lets any website call this server from a visitor’s browser and send along their login cookies. So a site a signed-in user happens to visit could quietly read their data or act as them. Allow only your own app’s address instead.`
+ : `${filesC.length === 1 ? filesC[0].split('/').pop() : `${filesC.length} files`} tell browsers that any website may call this server. That’s fine for a public API that doesn’t use login cookies (for example one that checks a token in the Authorization header); otherwise, allow only your own app’s address.`,
+ items, notes,
+ locations: cors.map((c) => ({ file: c.file, line: c.line })),
+ fix: [
+ `Security review: CORS settings that allow every website:`,
+ ...cors.map((c) => `- ${c.file}:${c.line}: ${c.how}${c.credentials ? ' (with credentials)' : ''}`),
+ ``,
+ `First tell me in plain words whether this server uses login cookies or sessions, and which websites legitimately call it. Then replace the allow-everything setting with an explicit list of allowed origins read from an environment variable (for example ALLOWED_ORIGINS=https://myapp.com,http://localhost:3000). Keep credentials only if the app logs in with cookies, and never combine them with "*" or with echoing back whatever origin asks. Show me the change before making it, and change nothing else.`,
+ ].join('\n'),
+ });
+ }
+
// ---------- endpoints anyone can call ----------
const routeFinding = new Map(); // `${file}|${line}|${method}` -> finding
const globalGuards = new Map(); // repo -> [{framework, name, file}]
@@ -574,6 +963,8 @@ export function buildHealth({ graph, scan, arch, root }) {
// webhook receivers: by URL, or a webhook-ish handler in a webhook file (not that file's admin endpoints)
const webhook = (WEBHOOK_PATH.test(r.p) && !WEBHOOK_ADMIN.test(r.p)) || (/webhook|hooks?\//i.test(r.f) && /webhook|hook|notify|event|ingress|pubsub|callback|receive|inbound/i.test(r.h || '') && !/create|list|manage|register|delete|test|setup|sync/i.test((r.h || '') + r.p));
if (webhook && fnd.signed) return;
+ // a Stripe webhook that doesn't check Stripe's signature has its own, more specific issue
+ if (stripeFiles.has(r.f) && (webhook || /stripe|payment|hook/i.test(r.p) || graph.routes.filter((x) => x.f === r.f && x.m !== 'GET').length === 1)) return;
if (!webhook && PUBLIC_PATH.test(r.p.replace(/<[^>]*>|\{[^}]*\}|:\w+|\[[^\]]*\]/g, 'x'))) return;
if (r.p === '/' && r.m === 'GET') return;
const entry = { i, r, fnd, write: !['GET', 'ANY'].includes(r.m), webhook };
@@ -735,7 +1126,7 @@ export function buildHealth({ graph, scan, arch, root }) {
});
}
- const KIND_ORDER = ['secret', 'env-file', 'unprotected-endpoint', 'large-file', 'hardcoded-url', 'unused-file'];
+ const KIND_ORDER = ['secret', 'public-env-secret', 'database-rules', 'stripe-webhook', 'env-file', 'unprotected-endpoint', 'cors', 'large-file', 'hardcoded-url', 'unused-file'];
issues.sort((a, b) => SEV_ORDER[a.severity] - SEV_ORDER[b.severity] || KIND_ORDER.indexOf(a.kind) - KIND_ORDER.indexOf(b.kind) || (b.locations?.length || 0) - (a.locations?.length || 0));
const kept = issues.slice(0, 40);
const summary = { high: 0, medium: 0, low: 0 };
@@ -743,6 +1134,60 @@ export function buildHealth({ graph, scan, arch, root }) {
return { summary, issues: kept };
}
+/** Test mode, in Firestore/Storage rules (request.time < timestamp.date(Y, M, D)) or Realtime Database rules (now < ms). */
+const testModeEnd = (cond) => {
+ const d = /^request\.time\s*<\s*timestamp\.date\(\s*(\d{4})\s*,\s*(\d{1,2})\s*,\s*(\d{1,2})\s*\)$/.exec(cond);
+ if (d) return new Date(Date.UTC(+d[1], +d[2] - 1, +d[3]));
+ const ms = /^now\s*<\s*(\d{12,14})$/.exec(cond);
+ return ms ? new Date(+ms[1]) : null;
+};
+
+/** Firestore / Storage rules that let anyone in: `allow …: if true`, `allow …;` (no condition) and test mode. */
+function firebaseRules(src, now) {
+ const s = src.replace(/\/\/[^\n]*/g, (m) => ' '.repeat(m.length));
+ const at = lineIndex(s);
+ const matches = [...s.matchAll(/\bmatch\s+(\/\S*)\s*\{/g)].map((m) => ({ i: m.index, path: m[1] }));
+ const out = [];
+ for (const m of s.matchAll(/\ballow\s+([a-z][a-z,\s]*?)\s*(?::\s*if\s+([^;]+?))?\s*;/g)) {
+ const ops = m[1].split(/[\s,]+/).filter(Boolean);
+ const cond = (m[2] || 'true').trim().replace(/\s+/g, ' ');
+ const end = cond === 'true' ? null : testModeEnd(cond);
+ if (cond !== 'true' && !end) continue;
+ const where = matches.filter((x) => x.i < m.index).pop()?.path || '/';
+ const writeOps = ops.filter((o) => /^(write|create|update|delete)$/.test(o));
+ out.push({
+ line: at(m.index), text: `allow ${ops.join(', ')}${m[2] ? `: if ${cond}` : ' (no condition)'} at ${where}`,
+ read: ops.some((o) => /^(read|get|list)$/.test(o)), write: writeOps.length > 0, createOnly: writeOps.length > 0 && writeOps.every((o) => o === 'create'),
+ open: !end ? 'always' : end > now ? 'until' : 'expired', until: end, where,
+ wide: /=\*\*\}/.test(where) || /\/documents$/.test(where) || where === '/',
+ });
+ }
+ return out;
+}
+
+/** Realtime Database rules (database.rules.json) with ".read" / ".write" set to true or test mode. */
+function firebaseRtdbRules(src, now) {
+ const s = src.replace(/(^|[^:"])\/\/[^\n]*/g, (m, pre) => pre + ' '.repeat(m.length - pre.length));
+ const at = lineIndex(s);
+ const out = [];
+ for (const m of s.matchAll(/"\.(read|write)"\s*:\s*(true\b|"([^"]*)")/g)) {
+ const cond = m[2] === 'true' ? 'true' : (m[3] || '').trim();
+ const end = cond === 'true' ? null : testModeEnd(cond);
+ if (cond !== 'true' && !end) continue;
+ // how deep it sits: { "rules": { ".read": … } } is the whole database
+ let depth = 0;
+ for (let i = 0; i < m.index; i++) { if (s[i] === '{') depth++; else if (s[i] === '}') depth--; }
+ const keys = [...s.slice(0, m.index).matchAll(/"([^".][^"]*)"\s*:\s*\{/g)].map((k) => k[1]);
+ const where = depth <= 2 ? 'the whole database' : '/' + keys.slice(-(depth - 2)).join('/');
+ out.push({
+ line: at(m.index), text: `"${m[1] === 'read' ? '.read' : '.write'}": ${m[2]} for ${where}`,
+ read: m[1] === 'read', write: m[1] === 'write', createOnly: false,
+ open: !end ? 'always' : end > now ? 'until' : 'expired', until: end, where, wide: depth <= 2, root: depth <= 2,
+ });
+ }
+ return out;
+}
+
function readHead(file, max) {
const fd = fs.openSync(file, 'r');
try { const buf = Buffer.alloc(max); const n = fs.readSync(fd, buf, 0, max, 0); return buf.subarray(0, n).toString('utf8'); } finally { fs.closeSync(fd); }
diff --git a/tests/secrets.test.mjs b/tests/secrets.test.mjs
index 964a4a7..b2bd811 100644
--- a/tests/secrets.test.mjs
+++ b/tests/secrets.test.mjs
@@ -35,6 +35,8 @@ test('placeholders, values read from the environment and public settings are not
assert.deepEqual(secrets('DB_PASSWORD = "changeme"\nAPI_KEY = "your-api-key-here"\nSECRET = "xxx"\n'), []);
assert.deepEqual(secrets('DB_PASSWORD = os.environ["DB_PASSWORD"]\nAPI_KEY = os.getenv("API_KEY")\n'), []);
assert.deepEqual(secrets('API_URL = "https://api.example.com/v1/orders"\n'), []);
+ // Supabase's config.toml reads secrets from the environment with env(NAME)
+ assert.deepEqual(secrets('s3_secret_key = "env(S3_SECRET_KEY)"\n', 'supabase/config.toml', 'toml'), []);
});
test('tokens that are public by design are not flagged (#23)', () => {
diff --git a/tests/stack-health.test.mjs b/tests/stack-health.test.mjs
new file mode 100644
index 0000000..f75476a
--- /dev/null
+++ b/tests/stack-health.test.mjs
@@ -0,0 +1,233 @@
+// Health's checks for the usual mistakes in apps built on Supabase, Firebase, Stripe, Next.js and Vite:
+// tables without row-level security, Firebase rules that let anyone in, Stripe webhooks that don't
+// check the signature, secret keys in variables the browser gets, and CORS that allows every website.
+// Each check has cases it must find and look-alikes it must leave alone (a false alarm costs more than
+// a miss). Secret-looking values are assembled at runtime so none is ever committed to this repo.
+import { test } from 'node:test';
+import assert from 'node:assert/strict';
+import fs from 'node:fs';
+import path from 'node:path';
+import { lib, makeProject, scanAndBuild, readGraph, cleanup, openEndpoints } from './helpers.mjs';
+
+const { findIssues, leakyPublicVar } = await lib('health.mjs');
+const find = (src, file, lang, k) => findIssues(src, { path: file, category: 'code', lang }).filter((x) => x.k === k);
+const FAKE_OPENAI_KEY = ['sk', 'proj', 'Zq8x', 'T3Bl', 'bkFJ', 'k2m9', 'Wv4r', 'Lp7d', 'Hs1n'].join('');
+const NEXT_YEAR = new Date().getUTCFullYear() + 1;
+
+// ---------- secret keys in variables the browser gets ----------
+
+test('public variables named like secret keys are recognised, and keys that are public by design are not', () => {
+ assert.equal(leakyPublicVar('VITE_OPENAI_API_KEY'), 'OpenAI');
+ assert.equal(leakyPublicVar('NEXT_PUBLIC_SUPABASE_SERVICE_ROLE_KEY'), 'Supabase');
+ assert.equal(leakyPublicVar('NEXT_PUBLIC_STRIPE_SECRET_KEY'), 'Stripe');
+ assert.equal(leakyPublicVar('REACT_APP_ANTHROPIC_KEY'), 'Anthropic');
+ assert.equal(leakyPublicVar('NEXT_PUBLIC_DATABASE_URL'), '');
+ for (const ok of ['NEXT_PUBLIC_SUPABASE_ANON_KEY', 'NEXT_PUBLIC_SUPABASE_URL', 'NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY', 'VITE_FIREBASE_API_KEY',
+ 'VITE_GOOGLE_MAPS_API_KEY', 'NEXT_PUBLIC_GITHUB_CLIENT_ID', 'NEXT_PUBLIC_RECAPTCHA_SITE_KEY', 'NEXT_PUBLIC_API_KEY', 'OPENAI_API_KEY', 'STRIPE_SECRET_KEY']) {
+ assert.equal(leakyPublicVar(ok), null, ok);
+ }
+});
+
+test('code that reads a secret from a public variable is found, wherever the framework reads them', () => {
+ assert.deepEqual(find(`const key = import.meta.env.VITE_OPENAI_API_KEY;\n`, 'src/chat.ts', 'typescript', 'public-env').map((x) => [x.name, x.line]), [['VITE_OPENAI_API_KEY', 1]]);
+ assert.equal(find(`fetch(u, { headers: { Authorization: process.env['NEXT_PUBLIC_STRIPE_SECRET_KEY'] } })`, 'app/page.tsx', 'typescript', 'public-env').length, 1);
+ assert.equal(find(`import { PUBLIC_OPENAI_KEY } from '$env/static/public';`, 'src/routes/+page.svelte', 'svelte', 'public-env').length, 1);
+ // PUBLIC_ means nothing to Node, the anon key is public by design, and tests are left alone
+ assert.deepEqual(find(`const k = process.env.PUBLIC_OPENAI_KEY;`, 'server.js', 'javascript', 'public-env'), []);
+ assert.deepEqual(find(`createClient(url, import.meta.env.VITE_SUPABASE_ANON_KEY)`, 'src/db.ts', 'typescript', 'public-env'), []);
+ assert.deepEqual(find(`const key = import.meta.env.VITE_OPENAI_API_KEY;`, 'src/chat.test.ts', 'typescript', 'public-env'), []);
+});
+
+// ---------- Stripe webhooks ----------
+
+test('code that acts on Stripe payment events without checking the signature is found', () => {
+ const js = [
+ `import Stripe from 'stripe';`,
+ `export async function POST(req) {`,
+ ` const event = await req.json();`,
+ ` switch (event.type) {`,
+ ` case 'checkout.session.completed': await markPaid(event.data.object); break;`,
+ ` case 'customer.subscription.deleted': await cancel(event.data.object); break;`,
+ ` }`,
+ `}`,
+ ].join('\n');
+ const [hit] = find(js, 'app/api/webhooks/stripe/route.ts', 'typescript', 'stripe-unsigned');
+ assert.equal(hit.line, 5);
+ assert.deepEqual(hit.events, ['checkout.session.completed', 'customer.subscription.deleted']);
+ const py = `import stripe\[email protected]("/stripe")\nasync def hook(request):\n event = await request.json()\n if event["type"] == "invoice.paid":\n grant_credits(event)\n`;
+ assert.equal(find(py, 'api/billing.py', 'python', 'stripe-unsigned').length, 1);
+});
+
+test('verified Stripe webhooks, and code that only starts a checkout, are left alone', () => {
+ const verified = `const event = stripe.webhooks.constructEvent(body, req.headers['stripe-signature'], process.env.STRIPE_WEBHOOK_SECRET);\nif (event.type === 'checkout.session.completed') fulfil(event);`;
+ assert.deepEqual(find(verified, 'server/stripe.js', 'javascript', 'stripe-unsigned'), []);
+ const py = `event = stripe.Webhook.construct_event(payload, sig, secret)\nif event["type"] == "invoice.paid":\n pay()`;
+ assert.deepEqual(find(py, 'billing.py', 'python', 'stripe-unsigned'), []);
+ // listing the events to subscribe to isn't handling them
+ const setup = `await stripe.webhookEndpoints.create({ url, enabled_events: ['checkout.session.completed'] });`;
+ assert.deepEqual(find(setup, 'scripts/setup-stripe.js', 'javascript', 'stripe-unsigned'), []);
+});
+
+// ---------- CORS ----------
+
+test('CORS that allows every website is found, and marked serious only when login cookies go along', () => {
+ const cors = (src, file = 'server.js', lang = 'javascript') => find(src, file, lang, 'cors-any').map((x) => x.credentials);
+ assert.deepEqual(cors(`import cors from 'cors';\napp.use(cors());`), [false]);
+ assert.deepEqual(cors(`const cors = require('cors');\napp.use(cors({ origin: true, credentials: true }));`), [true]);
+ // browsers refuse "*" together with cookies, so that pair isn't the dangerous one
+ assert.deepEqual(cors(`import cors from 'cors';\napp.use(cors({ origin: '*', credentials: true }));`), [false]);
+ assert.deepEqual(cors(`res.setHeader('Access-Control-Allow-Origin', req.headers.origin);\nres.setHeader('Access-Control-Allow-Credentials', 'true');`), [true]);
+ assert.deepEqual(cors(`app.add_middleware(\n CORSMiddleware,\n allow_origins=["*"],\n allow_credentials=True,\n)`, 'main.py', 'python'), [true]);
+ assert.deepEqual(cors(`CORS(app)`, 'app.py', 'python'), [false]);
+ assert.deepEqual(cors(`CORS_ALLOW_ALL_ORIGINS = True\nCORS_ALLOW_CREDENTIALS = True`, 'mysite/settings.py', 'python'), [true]);
+});
+
+test('CORS limited to named websites is left alone', () => {
+ const none = (src, file = 'server.js', lang = 'javascript') => assert.deepEqual(find(src, file, lang, 'cors-any'), [], src);
+ none(`import cors from 'cors';\napp.use(cors({ origin: ['https://myapp.com'], credentials: true }));`);
+ none(`import cors from 'cors';\napp.use(cors({ origin: process.env.ALLOWED_ORIGINS.split(',') }));`);
+ none(`app.add_middleware(CORSMiddleware, allow_origins=["https://myapp.com"], allow_credentials=True)`, 'main.py', 'python');
+ none(`CORS(app, origins=["https://myapp.com"], supports_credentials=True)`, 'app.py', 'python');
+ none(`CORS(app, resources={r"/api/*": {"origins": "https://myapp.com"}})`, 'app.py', 'python');
+});
+
+// ---------- Supabase migrations ----------
+
+test('migrations: tables, row-level security on and off, renames, drops and open rules are read in order', () => {
+ const sql = [
+ `create table public.profiles (id uuid primary key);`,
+ `-- alter table public.profiles disable row level security;`,
+ `alter table public.profiles enable row level security;`,
+ `create table if not exists "notes" (id bigint);`,
+ `create temp table scratch (id int);`,
+ `alter table notes rename to posts;`,
+ `drop table if exists old_stuff;`,
+ `create policy "Anyone can join" on public.waitlist for insert to anon with check (true);`,
+ `create policy "Owners read" on public.profiles for select using (auth.uid() = id);`,
+ `create policy "Anyone reads" on public.posts for select using (true);`,
+ ].join('\n');
+ const got = findIssues(sql, { path: 'supabase/migrations/1_init.sql', category: 'code', lang: 'sql' }).map(({ k, table, on, to, line, cmd }) => [k, table, on ?? to ?? cmd ?? null, line]);
+ assert.deepEqual(got, [
+ ['sql-table', 'public.profiles', null, 1],
+ ['sql-rls', 'public.profiles', true, 3],
+ ['sql-table', 'public.notes', null, 4],
+ ['sql-rename', 'public.notes', 'public.posts', 6],
+ ['sql-drop', 'public.old_stuff', null, 7],
+ ['sql-open-policy', 'public.waitlist', 'insert', 8],
+ ]);
+});
+
+// ---------- a whole app ----------
+
+const futureRules = `rules_version = '2';\nservice cloud.firestore {\n match /databases/{database}/documents {\n match /{document=**} {\n allow read, write: if request.time < timestamp.date(${NEXT_YEAR}, 3, 1);\n }\n }\n}\n`;
+
+/** A Vite + Supabase app with one of every mistake, beside a Next.js-style Stripe webhook and an Express server. */
+function riskyApp() {
+ return makeProject({
+ 'package.json': JSON.stringify({ name: 'vibe', dependencies: { '@supabase/supabase-js': '^2.45.0', stripe: '^16.0.0', express: '^4.19.0', cors: '^2.8.5' } }),
+ 'src/db.ts': `import { createClient } from '@supabase/supabase-js';\nexport const supabase = createClient(import.meta.env.VITE_SUPABASE_URL, import.meta.env.VITE_SUPABASE_ANON_KEY);\n`,
+ 'src/chat.ts': `export async function ask(q: string) {\n return fetch('https://api.openai.com/v1/chat/completions', { headers: { Authorization: 'Bearer ' + import.meta.env.VITE_OPENAI_API_KEY } });\n}\n`,
+ '.env.local': `VITE_SUPABASE_URL=https://abcd.supabase.co\nVITE_OPENAI_API_KEY=${FAKE_OPENAI_KEY}\n`,
+ 'supabase/migrations/20250101000000_init.sql': [
+ `create table public.profiles (id uuid primary key references auth.users, name text);`,
+ `create table public.notes (id bigint generated always as identity primary key, user_id uuid, body text);`,
+ `create table public.old_stuff (id int);`,
+ `create table public.waitlist (email text primary key);`,
+ `alter table public.waitlist enable row level security;`,
+ `create policy "Anyone can join" on public.waitlist for insert to anon with check (true);`,
+ ].join('\n'),
+ 'supabase/migrations/20250201000000_lock_profiles.sql': `alter table public.profiles enable row level security;\ndrop table public.old_stuff;\n`,
+ 'firestore.rules': futureRules,
+ 'storage.rules': `rules_version = '2';\nservice firebase.storage {\n match /b/{bucket}/o {\n match /{allPaths=**} {\n allow read, write;\n }\n }\n}\n`,
+ 'database.rules.json': `{\n "rules": {\n ".read": true,\n ".write": "auth != null"\n }\n}\n`,
+ 'app/api/webhooks/stripe/route.ts': `import Stripe from 'stripe';\nexport async function POST(req: Request) {\n const event = await req.json();\n if (event.type === 'checkout.session.completed') await fulfil(event.data.object);\n return new Response('ok');\n}\nasync function fulfil(s: unknown) {}\n`,
+ 'server/index.js': `import express from 'express';\nimport cors from 'cors';\nconst app = express();\napp.use(cors({ origin: true, credentials: true }));\napp.listen(3001);\n`,
+ }, { arch: { project: { name: 'Vibe', summary: 'A sample app.' }, nodes: [{ id: 'app', label: 'App', kind: 'ui', summary: 'The app.', paths: ['**'] }] } });
+}
+
+test('a whole app: each mistake becomes one plain-English issue with a fix to paste', () => {
+ const root = riskyApp();
+ try {
+ scanAndBuild(root);
+ const graph = readGraph(root);
+ const issues = graph.health.issues;
+ const byId = (prefix) => issues.filter((i) => i.id.startsWith(prefix));
+
+ // Supabase: only notes is open (profiles was locked later, old_stuff was dropped, waitlist has RLS)
+ const [rls] = byId('rls:');
+ assert.equal(rls.severity, 'high');
+ assert.equal(rls.title, 'Anyone can read and change your notes table');
+ assert.deepEqual(rls.locations, [{ file: 'supabase/migrations/20250101000000_init.sql', line: 2 }]);
+ assert.match(rls.fix, /alter table <table> enable row level security/);
+ // the waitlist's open insert rule is worth a look, not an alarm
+ const [policy] = byId('open-policy:');
+ assert.equal(policy.severity, 'medium');
+ assert.match(policy.detail, /public form/);
+
+ // Firebase: test mode with its date, open storage, and a database anyone can read
+ const fb = Object.fromEntries(byId('firebase-rules:').map((i) => [i.id.split(':')[1], i]));
+ assert.equal(fb['firestore.rules'].severity, 'high');
+ assert.match(fb['firestore.rules'].title, new RegExp(`open to anyone until March 1, ${NEXT_YEAR}`));
+ assert.match(fb['firestore.rules'].fix, /firebase deploy --only firestore:rules/);
+ assert.equal(fb['storage.rules'].severity, 'high');
+ assert.equal(fb['storage.rules'].title, 'Anyone can upload, replace or delete your Firebase files');
+ assert.equal(fb['database.rules.json'].severity, 'medium');
+ assert.equal(fb['database.rules.json'].title, 'Anyone can read everything in your Firebase Realtime Database');
+
+ // Stripe: one specific issue, and the same webhook isn't also reported as an open endpoint
+ const [stripe] = byId('stripe-webhook:');
+ assert.equal(stripe.severity, 'high');
+ assert.match(stripe.title, /fake a Stripe payment/);
+ assert.match(stripe.fix, /await req\.text\(\)/);
+ assert.ok(!openEndpoints(graph).includes('POST /api/webhooks/stripe'), openEndpoints(graph).join(', '));
+
+ // the OpenAI key in a VITE_ variable: set in .env.local and read by browser code
+ const [pub] = byId('public-env');
+ assert.equal(pub.severity, 'high');
+ assert.equal(pub.title, 'VITE_OPENAI_API_KEY is sent to every visitor’s browser');
+ assert.deepEqual(pub.locations.map((l) => l.file).sort(), ['.env.local', 'src/chat.ts']);
+
+ // CORS that echoes any origin with cookies
+ const [cors] = byId('cors');
+ assert.equal(cors.severity, 'medium');
+ assert.equal(cors.title, 'Any website could act as your signed-in users');
+
+ // important issues come first, and no key value reaches anything Floorplan writes
+ assert.equal(issues[0].severity, 'high');
+ for (const f of fs.readdirSync(path.join(root, 'floorplan'), { recursive: true })) {
+ const p = path.join(root, 'floorplan', f);
+ if (fs.statSync(p).isFile()) assert.ok(!fs.readFileSync(p, 'utf8').includes(FAKE_OPENAI_KEY.slice(4)), `${f} contains the key`);
+ }
+ } finally { cleanup(root); }
+});
+
+test('the same app done right raises none of these issues', () => {
+ const root = makeProject({
+ 'package.json': JSON.stringify({ name: 'tidy', dependencies: { '@supabase/supabase-js': '^2.45.0', stripe: '^16.0.0', cors: '^2.8.5' } }),
+ 'src/db.ts': `import { createClient } from '@supabase/supabase-js';\nexport const supabase = createClient(import.meta.env.VITE_SUPABASE_URL, import.meta.env.VITE_SUPABASE_ANON_KEY);\n`,
+ 'supabase/migrations/20250101000000_init.sql': `create table public.notes (id bigint primary key, user_id uuid);\nalter table public.notes enable row level security;\ncreate policy "Own notes" on public.notes for all to authenticated using (auth.uid() = user_id) with check (auth.uid() = user_id);\n`,
+ 'firestore.rules': `rules_version = '2';\nservice cloud.firestore {\n match /databases/{database}/documents {\n match /users/{userId} {\n allow read, write: if request.auth != null && request.auth.uid == userId;\n }\n match /products/{id} {\n allow read: if true;\n }\n }\n}\n`,
+ 'app/api/webhooks/stripe/route.ts': `import Stripe from 'stripe';\nconst stripe = new Stripe(process.env.STRIPE_SECRET_KEY!);\nexport async function POST(req: Request) {\n const event = stripe.webhooks.constructEvent(await req.text(), req.headers.get('stripe-signature')!, process.env.STRIPE_WEBHOOK_SECRET!);\n if (event.type === 'checkout.session.completed') await fulfil(event.data.object);\n return new Response('ok');\n}\nasync function fulfil(s: unknown) {}\n`,
+ 'server/index.js': `import express from 'express';\nimport cors from 'cors';\nconst app = express();\napp.use(cors({ origin: ['https://tidy.app'], credentials: true }));\napp.listen(3001);\n`,
+ }, { arch: { project: { name: 'Tidy', summary: 'A sample app.' }, nodes: [{ id: 'app', label: 'App', kind: 'ui', summary: 'The app.', paths: ['**'] }] } });
+ try {
+ scanAndBuild(root);
+ const kinds = readGraph(root).health.issues.map((i) => i.kind);
+ for (const k of ['database-rules', 'stripe-webhook', 'public-env-secret', 'cors']) assert.ok(!kinds.includes(k), `unexpected ${k}: ${kinds.join(', ')}`);
+ } finally { cleanup(root); }
+});
+
+test('plain Postgres migrations without Supabase are not held to Supabase’s rules, and expired test mode is called out', () => {
+ const root = makeProject({
+ 'db/schema.sql': `create table users (id serial primary key, email text);\n`,
+ 'firestore.rules': `rules_version = '2';\nservice cloud.firestore {\n match /databases/{database}/documents {\n match /{document=**} {\n allow read, write: if request.time < timestamp.date(2020, 1, 1);\n }\n }\n}\n`,
+ }, { arch: { project: { name: 'Plain', summary: 'A sample app.' }, nodes: [{ id: 'app', label: 'App', kind: 'service', summary: 'The app.', paths: ['**'] }] } });
+ try {
+ scanAndBuild(root);
+ const issues = readGraph(root).health.issues;
+ assert.ok(!issues.some((i) => i.id.startsWith('rls:')), 'no row-level security issue without Supabase');
+ const [fb] = issues.filter((i) => i.id === 'firebase-rules:firestore.rules');
+ assert.equal(fb.severity, 'medium');
+ assert.equal(fb.title, 'Your Firebase Firestore test-mode rules ran out on January 1, 2020');
+ } finally { cleanup(root); }
+});