// safetyvibe-connect - pure helpers (no network, no writes). Tested in connect.test.mjs. // Egress = buildReport() body: v, name?, url?, platform?, environment?, lockfile, // versions_source, source, packages, truncated?, supabase (project_ref, host, // key_kind + key_fingerprint OR secret_key_exposed_client_side; never keys). // Authentication headers x-safetyvibe-site / x-safetyvibe-key / // x-safetyvibe-claim-token, only to the default API or SAFETYVIBE_API // (https, or http on localhost); redirects refused. Never source code or any Supabase keys. import { createHash } from 'node:crypto'; import { readFileSync, existsSync, statSync } from 'node:fs'; import { join, dirname, sep, relative } from 'node:path'; const MAX_PACKAGES = 5000; const read = (f) => { try { return readFileSync(f, 'utf8'); } catch { return null; } }; const readJson = (f) => { const t = read(f); if (t == null) return null; try { return JSON.parse(t); } catch { return null; } }; const semver = v => typeof v === 'string' && !!screenString(v, 'package version') && /^\d+\.\d+\.\d+(?:-[\w.-]+)?(?:\+[\w.-]+)?$/.test(v); function registryTarball(value) { try { const u = new URL(value); return /^https?:$/.test(u.protocol) && /\/(?:@[^/]+\/)?[^/]+\/-\/[^/]+-\d+\.\d+\.\d+(?:-[\w.-]+)?(?:\+[\w.-]+)?\.tgz$/.test(decodeURIComponent(u.pathname)); } catch { return false; } } const registrySpec = v => typeof v === 'string' && (/^npm:(?:@[^/]+\/)?[^@]+@/.test(v) ? registrySpec(v.slice(v.lastIndexOf('@') + 1)) : /^(?:[~^<>=]*\s*v?(?:\d+|[xX*])(?:\.(?:\d+|[xX*])){0,2}(?:-[\w.-]+)?(?:\+[\w.-]+)?)(?:(?:\s+(?:-\s+)?|\s*\|\|\s*)[~^<>=]*\s*v?(?:\d+|[xX*])(?:\.(?:\d+|[xX*])){0,2}(?:-[\w.-]+)?(?:\+[\w.-]+)?)*$/.test(v.trim())); const dependencySpecs = p => ({ ...p?.dependencies, ...p?.devDependencies, ...p?.optionalDependencies, ...p?.requires }); const nonRegistryNames = p => Object.entries(dependencySpecs(p)).filter(([, spec]) => typeof spec === 'string' && !registrySpec(spec)).map(([name]) => name); const registryEntry = v => v && !v.link && semver(v.version) && (v.resolved == null || registryTarball(v.resolved)); // ---- lockfiles ------------------------------------------------------------- // Each parser returns [{ name, version }] for every installed package (deduped later). export function fromPackageLock(json, { platform = process.platform, arch = process.arch } = {}) { const out = []; const excluded = new Set(); const remember = p => { for (const name of nonRegistryNames(p)) excluded.add(name); }; if (json && json.packages && typeof json.packages === 'object') { for (const v of Object.values(json.packages)) remember(v); for (const [path, v] of Object.entries(json.packages)) { if (!path || !registryEntry(v)) continue; const matches = (list, value) => !Array.isArray(list) || (!list.includes('!' + value) && (!list.some(x => !x.startsWith('!')) || list.includes(value) || list.includes('any'))); if (v.optional && (!matches(v.os, platform) || !matches(v.cpu, arch))) continue; const i = path.lastIndexOf('node_modules/'); if (i === -1 || excluded.has(path.slice(i + 'node_modules/'.length))) continue; out.push({ name: v.name || path.slice(i + 'node_modules/'.length), version: v.version, ...(v.dev === true ? { dev: true } : {}) }); } } else if (json && json.dependencies) { // lockfileVersion 1 const walk = (deps) => { for (const [n, v] of Object.entries(deps || {})) { remember(v); if (registryEntry(v)) out.push({ name: n, version: v.version, ...(v.dev === true ? { dev: true } : {}) }); walk(v && v.dependencies); } }; walk(json.dependencies); } return out.filter(p => !excluded.has(p.name)); } // bun.lock (text, JSONC): "packages": { "": ["name@version", registry, {...}, integrity], ... } export function fromBunLock(text) { const clean = String(text).replace(/^\s*\/\/.*$/gm, '').replace(/,(\s*[}\]])/g, '$1'); const json = JSON.parse(clean); const out = []; for (const v of Object.values((json && json.packages) || {})) { const spec = Array.isArray(v) ? v[0] : null; if (typeof spec !== 'string') continue; const at = spec.lastIndexOf('@'); if (at <= 0) continue; const version = spec.slice(at + 1); if (semver(version) && (v[1] === '' || (typeof v[1] === 'string' && /^https?:\/\//.test(v[1]) && !/\.tgz|\.git|github/i.test(v[1])))) out.push({ name: spec.slice(0, at), version }); } return out; } // yarn.lock v1 ("name@range":\n version "x") and berry ("name@npm:range":\n version: x) export function fromYarnLock(text) { const out = []; const seen = new Set(); const berry = /^__metadata:/m.test(text); for (const block of String(text).split(/(?=^\S)/m)) { const lines = block.split(/\r?\n/); const head = lines.shift(); if (!head?.endsWith(':') || head.startsWith('#') || head.startsWith('__metadata')) continue; const specs = head.slice(0, -1).split(/,\s*/).map(s => s.replace(/^"|"$/g, '')); const version = /^\s+version:?\s+"?([^"\s]+)/m.exec(block)?.[1]; const resolved = /^\s+resolved\s+["']?([^"'\s]+)/m.exec(block)?.[1]; const resolution = /^\s+resolution:\s+"?([^"\s]+)/m.exec(block)?.[1]; if (!semver(version) || (berry ? !/^(?:@[^/]+\/)?[^@:/]+@npm:/.test(resolution || '') : resolved != null && !registryTarball(resolved))) continue; for (const spec of new Set(specs)) { const at = spec.indexOf('@', spec.startsWith('@') ? 1 : 0); if (at < 1 || !(berry ? spec.slice(at + 1).startsWith('npm:') : registrySpec(spec.slice(at + 1)))) continue; const name = spec.slice(0, at); const id = name + '@' + version; if (!seen.has(id)) { seen.add(id); out.push({ name, version }); } } } return out; } export function fromPnpmLock(text) { const out = []; const section = /^packages:\s*\r?\n([\s\S]*?)(?=^\S|(?![\s\S]))/m.exec(text)?.[1] || ''; for (const block of section.split(/(?=^ {2}\S)/m)) { const head = block.split(/\r?\n/)[0]; if (/https?:|git[+:]|github|file:|link:|workspace:/.test(head) || /\b(?:tarball|repo|directory|type):/.test(block) || !/\bintegrity:/.test(block)) continue; const m = /^ {2}['"]?\/?((?:@[^/\s]+\/)?[^@/\s]+)(?:@|\/)(\d+\.\d+\.\d+(?:-[\w.-]+)?)/.exec(head); if (m) out.push({ name: m[1], version: m[2], ...(/^ {4}dev: true\s*$/m.test(block) ? { dev: true } : {}) }); } return out; } export function readPackages(root) { let list = []; let source = 'none'; const tries = [ ['package-lock.json', (t) => fromPackageLock(JSON.parse(t))], ['npm-shrinkwrap.json', (t) => fromPackageLock(JSON.parse(t))], ['bun.lock', fromBunLock], ['pnpm-lock.yaml', fromPnpmLock], ['yarn.lock', fromYarnLock], ]; let lockRoot = root; let current = root; for (let depth = 0; depth <= 6; depth++) { const ws = readJson(join(current, 'package.json'))?.workspaces; const yaml = read(join(current, 'pnpm-workspace.yaml')) || ''; const block = /^packages:\s*\r?\n((?:[ \t]+.*\r?\n?)*)/m.exec(yaml)?.[1] || ''; const globs = [...(Array.isArray(ws) ? ws : Array.isArray(ws?.packages) ? ws.packages : []), ...[...block.matchAll(/^\s+-\s+['"]?([^'"\r\n]+?)['"]?\s*$/gm)].map(m => m[1])]; const segments = relative(current, root).split(sep); const matchesGlob = g => { const parts = g.replace(/^\.\//, '').replace(/\/$/, '').split('/'); const segmentMatches = (pattern, value) => new RegExp('^' + pattern.split('*').map(s => s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')).join('[^/]*') + '$').test(value); const match = (i, j) => i === parts.length ? j === segments.length : parts[i] === '**' ? match(i + 1, j) || (j < segments.length && match(i, j + 1)) : j < segments.length && segmentMatches(parts[i], segments[j]) && match(i + 1, j + 1); return match(0, 0); }; const workspace = globs.some(g => typeof g === 'string' && !g.startsWith('!') && matchesGlob(g)) && !globs.some(g => typeof g === 'string' && g.startsWith('!') && matchesGlob(g.slice(1))); if (current === root || workspace) { lockRoot = current; if (workspace || tries.some(([f]) => existsSync(join(current, f))) || existsSync(join(current, 'bun.lockb'))) break; } if (existsSync(join(current, '.git')) || dirname(current) === current || depth === 6) break; current = dirname(current); } const manager = (readJson(join(root, 'package.json'))?.packageManager || readJson(join(lockRoot, 'package.json'))?.packageManager || '').split('@')[0]; const preferred = { npm: ['package-lock.json', 'npm-shrinkwrap.json'], pnpm: ['pnpm-lock.yaml'], yarn: ['yarn.lock'], bun: ['bun.lock'] }[manager] || []; const candidates = tries.filter(([f]) => existsSync(join(lockRoot, f))).sort(([a], [b]) => Number(preferred.includes(b)) - Number(preferred.includes(a)) || statSync(join(lockRoot, b)).mtimeMs - statSync(join(lockRoot, a)).mtimeMs); for (const [file, parse] of candidates) { const t = read(join(lockRoot, file)); if (t == null) continue; let ok = true; try { list = parse(t); } catch { list = []; ok = false; } if (list.length) { source = file; break; } // Recognized locks remain authoritative when every entry was excluded; falling back // to installed packages could reintroduce names from non-registry sources. if (ok && (/\.json$|^bun\.lock$/.test(file) || (file === 'pnpm-lock.yaml' && /^packages:/m.test(t)) || (file === 'yarn.lock' && /^(?:__metadata:|# yarn lockfile v1|\S.*:\s*$)/m.test(t)))) { source = file; break; } } // No node_modules fallback: installed folders do not say where a package came from, so a git/tarball/ // file dependency could be reported under its name. Without a readable lockfile no packages are sent and // lockfile stays 'none', so the dashboard keeps the last known inventory. const missing = source === 'none' ? { noLockfile: true, ...(existsSync(join(lockRoot, 'bun.lockb')) || existsSync(join(root, 'bun.lockb')) ? { binaryBun: true } : {}) } : {}; const seen = new Map(); const pkgs = []; for (const p of list) { const k = p.name + '@' + p.version; // Registry versions only. git/file/link/http specs can carry tokens or local paths: never sent. const safeName = screenString(p.name, 'package name', undefined, true); const safeVersion = screenString(p.version, 'package version'); if (!safeName || !safeVersion) continue; if (!/^(@[\w.-]+\/)?[\w.-]+$/.test(p.name) || !/^\d+\.\d+\.\d+[\w.+-]{0,40}$/.test(String(p.version))) continue; if (seen.has(k)) { if (p.dev !== true) delete seen.get(k).dev; continue; } const row = { ecosystem: 'npm', name: p.name, version: p.version, ...(p.dev === true ? { dev: true } : {}) }; seen.set(k, row); pkgs.push(row); } return { packages: pkgs.slice(0, MAX_PACKAGES), source, ...missing, ...(pkgs.length > MAX_PACKAGES ? { total: pkgs.length, truncated: true } : {}) }; } // ---- Supabase (project URL + PUBLIC key only) ------------------------------ function jwtPayload(value) { if (typeof value !== 'string' || value.length > 2000 || !/^[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+$/.test(value)) return null; try { const raw = Buffer.from(value.split('.')[1], 'base64url').toString('utf8'); // Tokenize complete strings so quoted text inside a value cannot become a key. const keys = [...raw.matchAll(/"(?:[^"\\]|\\.)*"/g)] .filter(m => /^\s*:/.test(raw.slice(m.index + m[0].length))).map(m => JSON.parse(m[0])); if (new Set(keys).size !== keys.length) return { role: 'rejected' }; const p = JSON.parse(raw); return p && typeof p === 'object' && !Array.isArray(p) ? p : null; } catch { return null; } } export function publicKeyKind(value) { if (typeof value !== 'string' || /sb_secret_|[\s#'"`]/i.test(value)) return null; if (/^sb_publishable_[A-Za-z0-9_-]{8,200}$/.test(value)) return 'publishable'; const payload = jwtPayload(value); const claims = new Set(['iss', 'ref', 'role', 'iat', 'exp', 'nbf', 'aud', 'sub']); return payload?.role === 'anon' && (!Object.hasOwn(payload, 'iss') || ['supabase', 'supabase-demo'].includes(payload.iss)) && (!Object.hasOwn(payload, 'ref') || (typeof payload.ref === 'string' && /^[a-z0-9]{10,40}$/.test(payload.ref))) && Object.entries(payload).every(([key, value]) => claims.has(key) && (typeof value === 'number' || (typeof value === 'string' && value.length <= 100 && !/sb_secret_|svk_|eyJ/i.test(value)))) ? 'anon' : null; } export function keyKind(value) { const kind = publicKeyKind(value); if (kind) return kind; const payload = jwtPayload(value); return /sb_secret_/i.test(String(value || '')) || (payload && typeof payload.role === 'string' && payload.role !== 'anon') ? 'secret' : 'unknown'; } export function parseEnv(text) { const env = {}; for (const line of String(text || '').split(/\r?\n/)) { const m = line.match(/^\s*(?:export\s+)?([\w.-]+)\s*=\s*(.*)$/); if (!m) continue; const value = m[2]; const quote = value[0]; if (["'", '"', '`'].includes(quote)) { let end = 1; for (; end < value.length; end++) { if (quote === '"' && value[end] === '\\') { end++; continue; } if (value[end] === quote) break; } if (end === value.length) continue; env[m[1]] = value.slice(1, end); } else env[m[1]] = value.split('#')[0].trim(); } return env; } // Only client-exposed variable names (bundled into the browser build by these prefixes); server-only // names such as SUPABASE_URL / SUPABASE_ANON_KEY are never read. const CLIENT_PREFIXES = ['VITE_', 'NEXT_PUBLIC_', 'EXPO_PUBLIC_', 'PUBLIC_', 'REACT_APP_', 'NUXT_PUBLIC_']; const URL_VARS = CLIENT_PREFIXES.map(p => p + 'SUPABASE_URL'); const KEY_VARS = CLIENT_PREFIXES.flatMap(p => [p + 'SUPABASE_PUBLISHABLE_KEY', p + 'SUPABASE_ANON_KEY']); const exposedSupabaseSecret = value => typeof value === 'string' && (/^sb_secret_[A-Za-z0-9_-]{20,}$/.test(value) || (/^[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+$/.test(value) && jwtPayload(value)?.role === 'service_role')); // A value assigned to a publishable/anon key name must be public: any other JWT role there is a secret. const secretInKeySlot = value => exposedSupabaseSecret(value) || (typeof value === 'string' && /^[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+$/.test(value) && !publicKeyKind(value) && typeof jwtPayload(value)?.role === 'string' && jwtPayload(value).role !== 'anon'); // Only clearly client-side source files: what the browser bundle already ships to every visitor. const SERVER_PATH = /(^|\/)(server|api|functions)\/|(^|\/)app\/api\/|(^|\/)pages\/api\/|(^|\/)supabase\/functions\/|(^|\/)netlify\/functions\/|(^|\/)middleware\.[cm]?[jt]sx?$|\.server\.[cm]?[jt]sx?$/i; export function clientSource(path, text) { if (SERVER_PATH.test(String(path).split('\\').join('/'))) return false; // Any quoted 'server-only' or "use server" anywhere (comments, spacing or syntax tricks included) marks // the file as server code. Excluding a client file by mistake is the safe direction. return !/['"`]server-only['"`]/.test(text) && !/['"`]use server['"`]/.test(text); } export function findSupabase(root, procEnv = process.env) { const env = {}; const warnings = new Set(); const serviceVariables = new Set(); const collect = (values, file = false) => { for (const k of Object.keys(values)) { // Server-side env vars are normal; only names written into .env* files get the local reminder. if (file && /SERVICE_ROLE|(?:^|_)SUPABASE_SERVICE_KEY$/i.test(k)) serviceVariables.add(k); if (!CLIENT_PREFIXES.some(p => k.startsWith(p))) continue; const value = values[k]; if (KEY_VARS.includes(k) ? secretInKeySlot(value) : exposedSupabaseSecret(value)) warnings.add(k); if (value) env[k] = value; } }; for (const f of ['.env', '.env.local', '.env.production', '.env.production.local']) collect(parseEnv(read(join(root, f))), true); collect(procEnv); let url = URL_VARS.map(k => env[k]).find(value => /^https:\/\/[a-z0-9-]+\.supabase\.(co|in)\/?$/i.test(value || '')) || ''; let key = KEY_VARS.map(k => env[k]).find(value => publicKeyKind(value)) || ''; // Scan every allowed client file even when environment config is complete. for (const f of ['src/integrations/supabase/client.ts', 'src/integrations/supabase/client.js', 'src/lib/supabase.ts', 'src/lib/supabase.js', 'lib/supabase.ts', 'utils/supabase.ts']) { const t = read(join(root, f)); if (!t || !clientSource(f, t)) continue; url = url || ((t.match(/["'`](https:\/\/[a-z0-9-]+\.supabase\.co)["'`]/i) || [])[1] || ''); for (const match of t.matchAll(/(["'`])((?:\\.|(?!\1)[^\\])*?)\1/g)) { const before = t.slice(0, match.index); const after = t.slice(match.index + match[0].length); if (/\.(startsWith|endsWith|includes|indexOf)\(\s*$/.test(before) || /[!=]==?\s*$/.test(before) || /^\s*[!=]==?/.test(after)) continue; if (exposedSupabaseSecret(match[2])) warnings.add(f); } // Quote pairing above can drift (an apostrophe in a comment); also check each key-shaped run that is // directly quoted on both sides, with the same comparison/prefix-check skips. for (const match of t.matchAll(/(["'`])(sb_secret_[A-Za-z0-9_-]{20,}|eyJ[A-Za-z0-9_-]*\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+)\1/g)) { const before = t.slice(0, match.index); const after = t.slice(match.index + match[0].length); if (/\.(startsWith|endsWith|includes|indexOf)\(\s*$/.test(before) || /[!=]==?\s*$/.test(before) || /^\s*[!=]==?/.test(after)) continue; if (exposedSupabaseSecret(match[2])) warnings.add(f); } for (const match of t.matchAll(/([\w]*(?:PUBLISHABLE|ANON)_KEY)\s*=\s*["'`]([^"'`]+)["'`]/gi)) { if (secretInKeySlot(match[2])) warnings.add(f); if (!key && publicKeyKind(match[2])) key = match[2]; } } if (!/^https:\/\/[a-z0-9-]+\.supabase\.(co|in)\/?$/i.test(url)) url = ''; const kind = key ? keyKind(key) : 'none'; // Never send anything that isn't provably a public key. if (!publicKeyKind(key)) key = ''; return { url: url.replace(/\/$/, ''), key, kind, warnings: [...warnings], serviceVariables: [...serviceVariables] }; } // ---- app identity ------------------------------------------------------------ const HOSTS = { 'lovable.app': 'Lovable', 'replit.app': 'Replit', 'repl.co': 'Replit', 'bolt.host': 'Bolt', 'base44.app': 'Base44', 'emergent.host': 'Emergent', 'emergentagent.com': 'Emergent', 'softgen.ai': 'Softgen', 'softgen.dev': 'Softgen', 'lp.dev': 'Leap', 'vercel.app': 'Vercel', 'netlify.app': 'Netlify', 'run.app': 'Google Cloud Run' }; export function detectPlatform(root, procEnv = process.env, url = '') { const pj = readJson(join(root, 'package.json')) || {}; const deps = { ...pj.dependencies, ...pj.devDependencies }; if (deps['lovable-tagger'] || existsSync(join(root, '.lovable'))) return 'Lovable'; if (existsSync(join(root, '.replit'))) return 'Replit'; for (const [marker, label] of Object.entries({ '.bolt': 'Bolt', '.emergent': 'Emergent', '.softgen': 'Softgen', '.leap': 'Leap', '.rocket': 'Rocket', '.v0': 'v0' })) if (existsSync(join(root, marker))) return label; if (String(pj.name || '').toLowerCase().replace(/^@[^/]+\//, '') === 'softgen-starter') return 'Softgen'; for (const dir of [root, join(root, 'frontend')]) for (const file of ['.env', '.env.local', '.env.production']) { for (const value of Object.values(parseEnv(read(join(dir, file))))) { try { const u = new URL(value); if (/^https?:$/.test(u.protocol) && /\.(emergentagent\.com|emergent\.host)$/.test(u.hostname)) return 'Emergent'; } catch {} } } if (deps['@base44/sdk']) return 'Base44'; if (Object.hasOwn(readJson(join(root, 'metadata.json')) || {}, 'requestFramePermissions') || (pj.name === 'react-example' && deps['@google/genai']) || ['ts', 'js', 'mjs', 'mts'].some(ext => /process\.env\.GEMINI_API_KEY/.test(read(join(root, 'vite.config.' + ext)) || ''))) return 'Google AI Studio'; try { const host = new URL(url).hostname; for (const [suffix, label] of Object.entries(HOSTS)) if (host.endsWith('.' + suffix)) return label; } catch {} if (procEnv.REPL_ID) return 'Replit'; if (procEnv.VERCEL) return 'Vercel'; if (procEnv.NETLIFY) return 'Netlify'; return ''; } const TEMPLATES = new Set('softgen-starter my-react-app create-react-app starter template workspace frontend backend client web app project src home repl code dev-server react-example vite-project vite-react-shadcn-ts tanstack-start-ts vite-react-typescript-starter rest-express my-app react-app nextjs next-app nextjs-app my-v0-project'.split(' ')); TEMPLATES.add(''); // Real JWTs start with eyJ (base64 of {"); plain dotted names like notes.example.com stay allowed. export function screenNormalized(value) { let text = String(value || ''); for (let i = 0; i < 3; i++) { const decoded = text.replace(/(?:%[0-9a-f]{2})+/gi, run => { try { return decodeURIComponent(run); } catch { return run; } }); if (decoded === text) break; text = decoded; } const glyphs = { \u0430:'a', \u0435:'e', \u043e:'o', \u0440:'p', \u0441:'c', \u0445:'x', \u0443:'y', \u0456:'i', \u0455:'s', \u0458:'j', \u043a:'k', \u043c:'m', \u043d:'h', \u0442:'t', \u0432:'b', \u03bf:'o', \u03b1:'a', \u03b5:'e', \u03b9:'i', \u03ba:'k', \u03bd:'v', \u03c1:'p', \u03c4:'t', \u03c5:'y', \u03c7:'x' }; // Decompose first so accents composed onto letters (e + U+0301 -> U+00E9) are stripped too, then recompose. return text.normalize('NFKD').replace(/[\p{M}\p{Cf}\p{Default_Ignorable_Code_Point}]/gu, '').normalize('NFKC').replace(/[\u0430 \u0435 \u043e \u0440 \u0441 \u0445 \u0443 \u0456 \u0455 \u0458 \u043a \u043c \u043d \u0442 \u0432 \u03bf \u03b1 \u03b5 \u03b9 \u03ba \u03bd \u03c1 \u03c4 \u03c5 \u03c7]/giu, c => glyphs[c.toLowerCase()] || c); } export function secretLike(value, packageName = false, credentials = true) { if (typeof value !== 'string') return false; return [value, screenNormalized(value)].some(v => { // eyJ is case-sensitive (base64 of '{"'): lowercase "eyj" occurs in ordinary words (honeyjar, journeyjs). if (/sb_publishable_|sb_secret_|sk_live_|sk_test_|sk-ant-|sk-proj-|rk_live_|service_role|svk_/i.test(v) || /eyJ/.test(v)) return true; // Generic secret-key shapes (sk_, sk-) with no separators in the random part. if (/sk_[A-Za-z0-9]{16,}|sk-[A-Za-z0-9]{20,}/i.test(v)) return true; if (credentials && (/[a-z][a-z0-9+.-]*:\/\/[^\s/?#]*@/i.test(v) || /(?:^|[?&])(?:apikey|api_key|access_token|token|key|sig|signature|secret|password)=/i.test(v))) return true; // Within a run, a separator-free piece of >= 32 characters, or >= 16 mixing letters and digits, is key // material; hyphenated words and paths ("some-long-path/another-segment") are not. Real npm package // names are exempt from this long-run rule only, never from the key prefixes above. if (packageName && /^(@[\w.-]+\/)?[\w.-]{1,214}$/.test(value)) return false; return (v.match(/[A-Za-z0-9+/_=-]{40,}/g) || []).some(run => run.split(/[-/_+=]/) .some(piece => piece.length >= 32 || (piece.length >= 16 && /[a-z]/i.test(piece) && /\d/.test(piece)))); }); } // Screening warnings are collected (deduplicated) and printed by the CLI after the summary. const screenWarnings = new Set(); export function takeScreenWarnings() { const list = [...screenWarnings]; screenWarnings.clear(); return list; } export function screenString(value, field, warn = message => screenWarnings.add(message), packageName = false) { if (typeof value !== 'string') return ''; if (!secretLike(value, packageName, false)) return value; warn('Dropped ' + field + ': it looked like a key.'); return ''; } export function appName(root, explicit = '', url = '') { let host = ''; try { host = new URL(url).hostname; if (Object.keys(HOSTS).some(s => host.endsWith('.' + s))) host = host.split('.')[0]; } catch {} for (const candidate of [explicit, host, readJson(join(root, 'package.json'))?.name]) { const raw = String(candidate || '').trim(); if (!screenString(raw, 'name')) { if (raw) return ''; continue; } const n = raw.replace(/[^A-Za-z0-9 @/._-]/g, '').trim(); if (n && (!screenString(n, 'name') || !screenString(n.slice(0, 80), 'name'))) return ''; if (n && !TEMPLATES.has(n.toLowerCase().replace(/^@[^/]+\//, '').replace(/_/g, '-'))) return n.slice(0, 80); } return ''; } export function packageManager(root, env = process.env) { const field = readJson(join(root, 'package.json'))?.packageManager?.split('@')[0]; if (['npm', 'pnpm', 'yarn', 'bun'].includes(field)) return field; const source = readPackages(root).source; if (source.startsWith('pnpm')) return 'pnpm'; if (source.startsWith('yarn')) return 'yarn'; if (source.startsWith('bun') || existsSync(join(root, 'bun.lockb'))) return 'bun'; if (source.endsWith('.json')) return 'npm'; return /^(pnpm|yarn|bun)\//.exec(env.npm_config_user_agent || '')?.[1] || 'npm'; } export const HOOK = 'safetyvibe-connect scan --quiet || exit 0'; export function withHook(current = '') { const cur = String(current).trim(); const base = cur.replace(/\s*&&\s*\(?safetyvibe-connect scan(?: --quiet)?(?: \|\| exit 0)?\)?$/, '').replace(/^\(?safetyvibe-connect scan(?: --quiet)?(?: \|\| exit 0)?\)?$/, '').trim(); if (/^[&|;]|[&|;\\^]$|[\r\n`<>#;$%!]|\$\(|[()]/.test(base) || /(? segment.length <= 40 && !/sb_|svk_|eyJ/i.test(segment)); return { url: u.origin + (keep && u.pathname !== '/' ? u.pathname : ''), cleaning: Boolean(u.username || u.password || u.search || u.hash || !keep) }; } catch { return { url: '', cleaning: false }; } } export function normPublicUrl(given) { return publicUrl(given).url; } export function urlNeedsCleaning(given) { return publicUrl(given).cleaning; } export function buildReport(root, opts = {}, procEnv = process.env) { const { packages, source, truncated } = readPackages(root); const url = normPublicUrl(opts.url || procEnv.SAFETYVIBE_URL || ''); const sb = findSupabase(root, procEnv); const host = sb.url ? screenString(new URL(sb.url).host, 'supabase host') || null : null; const project_ref = host && /^([a-z0-9]{20})\.supabase\.co$/.exec(host)?.[1] || null; const supabase = sb.warnings.length ? { project_ref, host, secret_key_exposed_client_side: true } : sb.url && sb.key ? { project_ref, host, key_kind: sb.kind, key_fingerprint: createHash('sha256').update(sb.key, 'utf8').digest('hex').slice(0, 8) } : null; const rawEnvironment = String(opts.environment || procEnv.SAFETYVIBE_ENV || (procEnv.NODE_ENV === 'production' ? 'production' : '')); const environment = rawEnvironment.replace(/[^A-Za-z0-9_-]/g, ''); return Object.fromEntries(Object.entries({ v: 1, name: appName(root, opts.name, url), url, platform: screenString(detectPlatform(root, procEnv, url), 'platform'), environment: screenString(rawEnvironment, 'environment') && screenString(environment, 'environment') ? environment.slice(0, 20) : '', lockfile: source, versions_source: "lockfile", source: "connector", packages, ...(truncated ? { truncated: true } : {}), supabase, }).filter(([, value]) => value !== '')); }