rattlecheck 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/NOTICE +4 -0
- package/README.md +173 -2
- package/bin/rattlecheck.js +12 -0
- package/docs/rules/RC001.md +40 -0
- package/docs/rules/RC002.md +35 -0
- package/docs/rules/RC003.md +25 -0
- package/docs/rules/RC004.md +23 -0
- package/docs/rules/RC005.md +32 -0
- package/docs/rules/RC101.md +31 -0
- package/integrations/AGENTS.md +11 -0
- package/integrations/claude-code/rattlecheck/SKILL.md +42 -0
- package/integrations/cursor/rattlecheck.mdc +13 -0
- package/package.json +45 -4
- package/src/cli.js +158 -0
- package/src/config.js +62 -0
- package/src/live/catalog.js +43 -0
- package/src/live/env.js +102 -0
- package/src/live/ownership.js +72 -0
- package/src/live/probe.js +80 -0
- package/src/live/rc101-anon-readable.js +38 -0
- package/src/mcp/server.js +163 -0
- package/src/model.js +723 -0
- package/src/report/badge-svg.js +26 -0
- package/src/report/card-svg.js +50 -0
- package/src/report/grade.js +26 -0
- package/src/report/sarif.js +44 -0
- package/src/report/terminal.js +105 -0
- package/src/rules/index.js +14 -0
- package/src/rules/supabase/rc001-table-without-rls.js +54 -0
- package/src/rules/supabase/rc002-definer-callable-by-anon.js +37 -0
- package/src/rules/supabase/rc003-definer-search-path.js +33 -0
- package/src/rules/supabase/rc004-invoker-trigger-locked-table.js +49 -0
- package/src/rules/supabase/rc005-permissive-true-policy.js +81 -0
- package/src/scan.js +167 -0
- package/src/sql/ident.js +25 -0
- package/src/sql/signature.js +143 -0
- package/src/sql/statements.js +267 -0
- package/src/version.js +6 -0
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
// README badge: "rattlecheck | A". Shows the grade only.
|
|
2
|
+
const W = (s) => Math.round(s.length * 6.6 + 12);
|
|
3
|
+
|
|
4
|
+
export function renderBadge(report) {
|
|
5
|
+
const left = 'rattlecheck';
|
|
6
|
+
const right = report.grade.letter;
|
|
7
|
+
const lw = W(left);
|
|
8
|
+
const rw = Math.max(22, W(right));
|
|
9
|
+
const total = lw + rw;
|
|
10
|
+
const color = report.grade.color;
|
|
11
|
+
return `<svg xmlns="http://www.w3.org/2000/svg" width="${total}" height="20" role="img" aria-label="${left}: ${right}">
|
|
12
|
+
<title>${left}: ${right} (${report.grade.label})</title>
|
|
13
|
+
<linearGradient id="s" x2="0" y2="100%"><stop offset="0" stop-color="#fff" stop-opacity=".1"/><stop offset="1" stop-opacity=".1"/></linearGradient>
|
|
14
|
+
<clipPath id="r"><rect width="${total}" height="20" rx="3" fill="#fff"/></clipPath>
|
|
15
|
+
<g clip-path="url(#r)">
|
|
16
|
+
<rect width="${lw}" height="20" fill="#1b1b1d"/>
|
|
17
|
+
<rect x="${lw}" width="${rw}" height="20" fill="${color}"/>
|
|
18
|
+
<rect width="${total}" height="20" fill="url(#s)"/>
|
|
19
|
+
</g>
|
|
20
|
+
<g fill="#fff" text-anchor="middle" font-family="Verdana,Geneva,DejaVu Sans,sans-serif" font-size="11">
|
|
21
|
+
<text x="${lw / 2}" y="14">${left}</text>
|
|
22
|
+
<text x="${lw + rw / 2}" y="14" font-weight="bold">${right}</text>
|
|
23
|
+
</g>
|
|
24
|
+
</svg>
|
|
25
|
+
`;
|
|
26
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
// The share card, 1200x630 (the size X, LinkedIn and Slack preview at).
|
|
2
|
+
//
|
|
3
|
+
// Grade and counts ONLY. No project URL, no table or function names. A failing
|
|
4
|
+
// card must never tell anyone where the hole is, so people can post it before
|
|
5
|
+
// they've fixed it without handing out a map.
|
|
6
|
+
const esc = (s) => String(s).replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
|
|
7
|
+
|
|
8
|
+
export function renderCard(report) {
|
|
9
|
+
const g = report.grade;
|
|
10
|
+
const { checked, open } = report.doors;
|
|
11
|
+
const headline = open === 0 ? 'Every door locked.' : open === 1 ? '1 door open.' : `${open} doors open.`;
|
|
12
|
+
const sub =
|
|
13
|
+
open === 0
|
|
14
|
+
? `${checked.toLocaleString('en-US')} doors checked. Nothing gave.`
|
|
15
|
+
: `${checked.toLocaleString('en-US')} doors checked. Fixing ${open === 1 ? 'it' : 'them'} now.`;
|
|
16
|
+
|
|
17
|
+
// A row of small doors: open ones drawn ajar in the grade color.
|
|
18
|
+
const doorCount = Math.min(24, Math.max(checked, 1));
|
|
19
|
+
const openCount = Math.min(open, doorCount);
|
|
20
|
+
const doors = [];
|
|
21
|
+
for (let i = 0; i < doorCount; i++) {
|
|
22
|
+
const x = 96 + i * 42;
|
|
23
|
+
const isOpen = i < openCount;
|
|
24
|
+
doors.push(
|
|
25
|
+
isOpen
|
|
26
|
+
? `<g><rect x="${x}" y="486" width="28" height="44" rx="3" fill="#0b0b0c" stroke="${g.color}" stroke-width="2"/><path d="M${x} 486 L${x + 16} 492 L${x + 16} 536 L${x} 530 Z" fill="${g.color}"/></g>`
|
|
27
|
+
: `<g><rect x="${x}" y="486" width="28" height="44" rx="3" fill="#232326"/><circle cx="${x + 21}" cy="509" r="2.2" fill="#6b6b70"/></g>`,
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return `<svg xmlns="http://www.w3.org/2000/svg" width="1200" height="630" viewBox="0 0 1200 630" role="img" aria-label="rattlecheck grade ${g.letter}: ${esc(headline)}">
|
|
32
|
+
<title>rattlecheck grade ${g.letter}: ${esc(headline)}</title>
|
|
33
|
+
<rect width="1200" height="630" fill="#0b0b0c"/>
|
|
34
|
+
<rect x="0" y="0" width="1200" height="8" fill="${g.color}"/>
|
|
35
|
+
<text x="96" y="122" fill="#f4f4f5" font-family="ui-monospace,SFMono-Regular,Menlo,Consolas,monospace" font-size="34" font-weight="700">rattlecheck</text>
|
|
36
|
+
<text x="96" y="160" fill="#8b8b91" font-family="ui-sans-serif,system-ui,-apple-system,Segoe UI,Helvetica,Arial,sans-serif" font-size="24">Rattle every door you own.</text>
|
|
37
|
+
|
|
38
|
+
<circle cx="1010" cy="300" r="118" fill="none" stroke="${g.color}" stroke-width="10"/>
|
|
39
|
+
<text x="1010" y="345" text-anchor="middle" fill="${g.color}" font-family="ui-sans-serif,system-ui,-apple-system,Segoe UI,Helvetica,Arial,sans-serif" font-size="132" font-weight="800">${g.letter}</text>
|
|
40
|
+
<text x="1010" y="458" text-anchor="middle" fill="#c9c9ce" font-family="ui-sans-serif,system-ui,-apple-system,Segoe UI,Helvetica,Arial,sans-serif" font-size="26" font-weight="600">${esc(g.label)}</text>
|
|
41
|
+
|
|
42
|
+
<text x="96" y="318" fill="#f4f4f5" font-family="ui-sans-serif,system-ui,-apple-system,Segoe UI,Helvetica,Arial,sans-serif" font-size="76" font-weight="800">${esc(headline)}</text>
|
|
43
|
+
<text x="96" y="378" fill="#a1a1a7" font-family="ui-sans-serif,system-ui,-apple-system,Segoe UI,Helvetica,Arial,sans-serif" font-size="32">${esc(sub)}</text>
|
|
44
|
+
|
|
45
|
+
${doors.join('\n ')}
|
|
46
|
+
|
|
47
|
+
<text x="1104" y="590" text-anchor="end" fill="#6b6b70" font-family="ui-monospace,SFMono-Regular,Menlo,Consolas,monospace" font-size="22">npx rattlecheck</text>
|
|
48
|
+
</svg>
|
|
49
|
+
`;
|
|
50
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
// The one place the grade is decided. Cards, badges, terminal output and the
|
|
2
|
+
// MCP server all read from here, so they cannot disagree.
|
|
3
|
+
export const GRADES = {
|
|
4
|
+
A: { label: 'Locked tight', color: '#1a9e55' },
|
|
5
|
+
B: { label: 'Mostly locked', color: '#6fa832' },
|
|
6
|
+
C: { label: 'Needs work', color: '#e0a100' },
|
|
7
|
+
D: { label: 'At risk', color: '#e8710a' },
|
|
8
|
+
F: { label: 'Wide open', color: '#e5322d' },
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export function countBySeverity(findings) {
|
|
12
|
+
const counts = { critical: 0, high: 0, warning: 0 };
|
|
13
|
+
for (const f of findings) counts[f.severity] = (counts[f.severity] ?? 0) + 1;
|
|
14
|
+
return counts;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function gradeFor(counts) {
|
|
18
|
+
if (counts.critical > 0) return 'F';
|
|
19
|
+
if (counts.high >= 2) return 'D';
|
|
20
|
+
if (counts.high === 1) return 'C';
|
|
21
|
+
if (counts.warning > 0) return 'B';
|
|
22
|
+
return 'A';
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** Severities that mean a door is open (or broken), as opposed to worth a look. */
|
|
26
|
+
export const OPEN_SEVERITIES = new Set(['critical', 'high']);
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
// SARIF 2.1.0, the format GitHub code scanning and most enterprise and
|
|
2
|
+
// government security tooling ingest.
|
|
3
|
+
import { ALL_RULES } from '../rules/index.js';
|
|
4
|
+
|
|
5
|
+
const LEVEL = { critical: 'error', high: 'error', warning: 'warning' };
|
|
6
|
+
const SECURITY_SEVERITY = { critical: '9.0', high: '7.5', warning: '4.0' };
|
|
7
|
+
|
|
8
|
+
export function renderSarif(report) {
|
|
9
|
+
const rules = ALL_RULES.map((r) => ({
|
|
10
|
+
id: r.id,
|
|
11
|
+
name: r.title.replace(/[^A-Za-z0-9]+/g, ' ').trim().replace(/ (\w)/g, (_, c) => c.toUpperCase()).replace(/^\w/, (c) => c.toUpperCase()),
|
|
12
|
+
shortDescription: { text: r.title },
|
|
13
|
+
fullDescription: { text: r.summary },
|
|
14
|
+
help: { text: r.summary },
|
|
15
|
+
defaultConfiguration: { level: LEVEL[r.severity] },
|
|
16
|
+
properties: {
|
|
17
|
+
tags: ['security', r.target, ...r.standards.cwe, ...r.standards.nist80053.map((c) => `NIST-800-53:${c}`), ...r.standards.cis.map((c) => `CIS:${c}`)],
|
|
18
|
+
'security-severity': SECURITY_SEVERITY[r.severity],
|
|
19
|
+
},
|
|
20
|
+
}));
|
|
21
|
+
|
|
22
|
+
const results = report.findings.map((f) => ({
|
|
23
|
+
ruleId: f.rule,
|
|
24
|
+
level: LEVEL[f.severity],
|
|
25
|
+
message: { text: `${f.plain} ${f.todo}` },
|
|
26
|
+
...(f.file
|
|
27
|
+
? { locations: [{ physicalLocation: { artifactLocation: { uri: f.file.split('\\').join('/') }, ...(f.line ? { region: { startLine: f.line } } : {}) } }] }
|
|
28
|
+
: {}),
|
|
29
|
+
partialFingerprints: { rattlecheckObject: `${f.rule}:${f.object}${f.detail ? `:${f.detail}` : ''}` },
|
|
30
|
+
properties: { severity: f.severity, object: f.name, technical: f.message, fix: f.fix, 'security-severity': SECURITY_SEVERITY[f.severity] },
|
|
31
|
+
}));
|
|
32
|
+
|
|
33
|
+
return {
|
|
34
|
+
$schema: 'https://json.schemastore.org/sarif-2.1.0.json',
|
|
35
|
+
version: '2.1.0',
|
|
36
|
+
runs: [
|
|
37
|
+
{
|
|
38
|
+
tool: { driver: { name: 'rattlecheck', version: report.tool.version, semanticVersion: report.tool.version, rules } },
|
|
39
|
+
results,
|
|
40
|
+
properties: { grade: report.grade.letter, doors: report.doors, coverage: report.coverage, live: report.live },
|
|
41
|
+
},
|
|
42
|
+
],
|
|
43
|
+
};
|
|
44
|
+
}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
// Terminal output in two voices.
|
|
2
|
+
// plain (default): what's wrong and what to do, for anyone.
|
|
3
|
+
// technical: adds file:line, the exact condition, standards and the fix SQL.
|
|
4
|
+
|
|
5
|
+
const SEV = {
|
|
6
|
+
critical: { label: 'CRITICAL', color: 31 },
|
|
7
|
+
high: { label: 'HIGH', color: 33 },
|
|
8
|
+
warning: { label: 'CHECK', color: 36 },
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
function painter(useColor) {
|
|
12
|
+
const c = (code) => (s) => (useColor ? `\x1b[${code}m${s}\x1b[0m` : String(s));
|
|
13
|
+
return { bold: c(1), dim: c(2), red: c(31), green: c(32), yellow: c(33), cyan: c(36), code: c };
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function gradeCode(letter) {
|
|
17
|
+
return { A: 32, B: 32, C: 33, D: 33, F: 31 }[letter];
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function box(lines, width) {
|
|
21
|
+
const top = `┌${'─'.repeat(width + 2)}┐`;
|
|
22
|
+
const bottom = `└${'─'.repeat(width + 2)}┘`;
|
|
23
|
+
// eslint-disable-next-line no-control-regex
|
|
24
|
+
const visible = (s) => s.replace(/\x1b\[[0-9;]*m/g, '').length;
|
|
25
|
+
return [top, ...lines.map((l) => `│ ${l}${' '.repeat(Math.max(0, width - visible(l)))} │`), bottom];
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const plural = (n, one, many = `${one}s`) => `${n.toLocaleString('en-US')} ${n === 1 ? one : many}`;
|
|
29
|
+
|
|
30
|
+
export function renderTerminal(report, { technical = false, color = false } = {}) {
|
|
31
|
+
const p = painter(color);
|
|
32
|
+
const out = [];
|
|
33
|
+
const g = report.grade;
|
|
34
|
+
|
|
35
|
+
out.push('');
|
|
36
|
+
out.push(` ${p.bold('rattlecheck')} ${p.dim(report.tool.version)} ${p.dim('rattle every door you own')}`);
|
|
37
|
+
out.push('');
|
|
38
|
+
const card = box(
|
|
39
|
+
[
|
|
40
|
+
`${p.bold('GRADE')} ${p.code(`1;${gradeCode(g.letter)}`)(g.letter)} ${p.code(gradeCode(g.letter))(g.label)}`,
|
|
41
|
+
`${plural(report.doors.checked, 'door')} checked, ${report.doors.open === 0 ? p.green('0 open') : p.red(`${report.doors.open} open`)}`,
|
|
42
|
+
],
|
|
43
|
+
44,
|
|
44
|
+
);
|
|
45
|
+
out.push(...card.map((l) => ` ${l}`));
|
|
46
|
+
out.push('');
|
|
47
|
+
|
|
48
|
+
if (report.findings.length === 0) {
|
|
49
|
+
out.push(` ${p.green('✓')} Nothing open. Every door rattlecheck knows how to check is shut.`);
|
|
50
|
+
out.push('');
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
report.findings.forEach((f, i) => {
|
|
54
|
+
const sev = SEV[f.severity];
|
|
55
|
+
out.push(` ${p.code(`1;${sev.color}`)(sev.label.padEnd(8))} ${p.bold(f.plain)}`);
|
|
56
|
+
out.push(` ${' '.repeat(8)} ${p.dim('What to do:')} ${f.todo}`);
|
|
57
|
+
if (technical) {
|
|
58
|
+
out.push(` ${' '.repeat(8)} ${p.dim(`${f.rule} ${f.title}`)}`);
|
|
59
|
+
out.push(` ${' '.repeat(8)} ${p.dim(f.message)}`);
|
|
60
|
+
if (f.file) out.push(` ${' '.repeat(8)} ${p.dim(`${f.file}${f.line ? `:${f.line}` : ''}`)}`);
|
|
61
|
+
out.push(` ${' '.repeat(8)} ${p.dim(`${f.standards.cwe.join(', ')} · NIST 800-53 ${f.standards.nist80053.join(', ')} · CIS ${f.standards.cis.join(', ')}`)}`);
|
|
62
|
+
out.push(` ${' '.repeat(8)} ${p.dim('Fix:')}`);
|
|
63
|
+
for (const l of f.fix.split('\n')) out.push(` ${' '.repeat(10)} ${p.cyan(l)}`);
|
|
64
|
+
}
|
|
65
|
+
if (i < report.findings.length - 1) out.push('');
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
if (report.findings.length) out.push('');
|
|
69
|
+
if (!technical && report.findings.length) {
|
|
70
|
+
out.push(` ${p.dim('Run with --technical for file locations and the exact SQL fix for each one.')}`);
|
|
71
|
+
out.push('');
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
if (report.allowed.length) {
|
|
75
|
+
out.push(` ${p.dim(`${plural(report.allowed.length, 'finding')} allowed by rattlecheck.config.json:`)}`);
|
|
76
|
+
for (const a of report.allowed) out.push(` ${p.dim(` ${a.rule} ${a.name}: ${a.reason}`)}`);
|
|
77
|
+
out.push('');
|
|
78
|
+
}
|
|
79
|
+
for (const w of report.configWarnings) out.push(` ${p.yellow('!')} ${w}`);
|
|
80
|
+
if (report.configWarnings.length) out.push('');
|
|
81
|
+
|
|
82
|
+
const c = report.coverage;
|
|
83
|
+
out.push(
|
|
84
|
+
` ${p.dim(`Checked ${plural(c.files, 'migration file')}, ${plural(c.statements, 'statement')}: ${plural(c.tables, 'table')}, ${plural(c.functions, 'function')}. Exposed schemas: ${c.exposedSchemas.join(', ')}.`)}`,
|
|
85
|
+
);
|
|
86
|
+
if (report.live.ran) {
|
|
87
|
+
const l = report.live;
|
|
88
|
+
out.push(
|
|
89
|
+
` ${p.dim(`Live check (ownership proven by ${l.ownership}): ${plural(l.probed, "table or view", "tables and views")} asked, ${l.open} open, ${l.lockedOrEmpty} locked or empty, ${l.locked} locked, ${l.notExposed} not exposed${l.errors ? `, ${l.errors} errors` : ''}.`)}`,
|
|
90
|
+
);
|
|
91
|
+
if (l.lockedOrEmpty) out.push(` ${p.dim('"Locked or empty": from outside, an empty table and a locked one look the same.')}`);
|
|
92
|
+
if (l.coverage.complete) {
|
|
93
|
+
out.push(` ${p.dim(`Table list from the ${l.coverage.source}: ${l.coverage.fromMigrations} from your migrations, ${l.coverage.outsideMigrations} created outside them.`)}`);
|
|
94
|
+
} else {
|
|
95
|
+
out.push(
|
|
96
|
+
` ${p.yellow('!')} ${p.yellow(`Live check only covered the ${l.coverage.fromMigrations} tables in your migrations. The project wouldn't list its tables (${l.coverage.reason}), so anything created outside migrations, like a backup copy made by hand, was NOT checked. Set SUPABASE_ACCESS_TOKEN to cover everything.`)}`,
|
|
97
|
+
);
|
|
98
|
+
}
|
|
99
|
+
} else {
|
|
100
|
+
out.push(` ${p.dim('Live check not run. Add --live to test your running project too.')}`);
|
|
101
|
+
}
|
|
102
|
+
if (!c.ok) out.push(` ${p.red('Coverage failure: migration files were found but no statements could be parsed. The result above cannot be trusted.')}`);
|
|
103
|
+
out.push('');
|
|
104
|
+
return out.join('\n');
|
|
105
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import rc001 from './supabase/rc001-table-without-rls.js';
|
|
2
|
+
import rc002 from './supabase/rc002-definer-callable-by-anon.js';
|
|
3
|
+
import rc003 from './supabase/rc003-definer-search-path.js';
|
|
4
|
+
import rc004 from './supabase/rc004-invoker-trigger-locked-table.js';
|
|
5
|
+
import rc005 from './supabase/rc005-permissive-true-policy.js';
|
|
6
|
+
import rc101 from '../live/rc101-anon-readable.js';
|
|
7
|
+
|
|
8
|
+
export const STATIC_RULES = [rc001, rc002, rc003, rc004, rc005];
|
|
9
|
+
export const LIVE_RULES = [rc101];
|
|
10
|
+
export const ALL_RULES = [...STATIC_RULES, ...LIVE_RULES];
|
|
11
|
+
|
|
12
|
+
export function getRule(id) {
|
|
13
|
+
return ALL_RULES.find((r) => r.id.toLowerCase() === String(id).toLowerCase()) ?? null;
|
|
14
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { tableExposure } from '../../model.js';
|
|
2
|
+
import { qualified } from '../../sql/ident.js';
|
|
3
|
+
|
|
4
|
+
export default {
|
|
5
|
+
id: 'RC001',
|
|
6
|
+
target: 'supabase',
|
|
7
|
+
severity: 'critical',
|
|
8
|
+
title: 'Table reachable through the API without row-level security',
|
|
9
|
+
summary:
|
|
10
|
+
'Supabase serves every table in an exposed schema over HTTP. Without row-level security, the key that ships in your website lets anyone read and change every row.',
|
|
11
|
+
standards: { cwe: ['CWE-284', 'CWE-862'], nist80053: ['AC-3', 'AC-6'], cis: ['3.3'] },
|
|
12
|
+
|
|
13
|
+
check(model) {
|
|
14
|
+
const checked = [];
|
|
15
|
+
const findings = [];
|
|
16
|
+
for (const t of model.tables.values()) {
|
|
17
|
+
const exp = tableExposure(model, t);
|
|
18
|
+
if (!exp.anon && !exp.authenticated) continue;
|
|
19
|
+
checked.push(`table:${t.key}`);
|
|
20
|
+
if (t.rls) continue;
|
|
21
|
+
|
|
22
|
+
const privs = exp.anon ? t.privs.anon : t.privs.authenticated;
|
|
23
|
+
const canRead = privs.has('select');
|
|
24
|
+
const canWrite = ['insert', 'update', 'delete'].some((p) => privs.has(p));
|
|
25
|
+
const verbs = canRead && canWrite ? 'read and change' : canRead ? 'read' : 'change';
|
|
26
|
+
const who = exp.anon
|
|
27
|
+
? "Anyone on the internet, using only the key that ships in your website's code,"
|
|
28
|
+
: 'Anyone who signs up for your app';
|
|
29
|
+
const copyNote =
|
|
30
|
+
t.kind === 'ctas'
|
|
31
|
+
? ' It was created as a copy of another table, and copies inherit none of the original\'s security.'
|
|
32
|
+
: '';
|
|
33
|
+
|
|
34
|
+
const ref = qualified(t.schema, t.name);
|
|
35
|
+
const fix =
|
|
36
|
+
t.kind === 'ctas'
|
|
37
|
+
? `alter table ${ref} enable row level security;\nrevoke all on ${ref} from anon, authenticated;\n-- This table is a copy. If it was a one-off snapshot, drop it once you're done with it.`
|
|
38
|
+
: `alter table ${ref} enable row level security;\n-- With RLS on and no policies, only your server (service role) can use this table.\n-- Then add a policy for each thing users should be allowed to do, for example:\n-- create policy "read own rows" on ${ref} for select to authenticated using ((select auth.uid()) = user_id);`;
|
|
39
|
+
|
|
40
|
+
findings.push({
|
|
41
|
+
object: `table:${t.key}`,
|
|
42
|
+
name: t.key,
|
|
43
|
+
file: t.file,
|
|
44
|
+
line: t.line,
|
|
45
|
+
severity: 'critical',
|
|
46
|
+
message: `${t.key} has row-level security off and is reachable through the Data API by ${exp.anon ? 'anon' : 'authenticated'} (${[...privs].join(', ')}).${t.kind === 'ctas' ? ' Created as a copy (CREATE TABLE AS or SELECT INTO).' : ''}`,
|
|
47
|
+
plain: `${who} can ${verbs} every row in the "${t.name}" table.${copyNote}`,
|
|
48
|
+
todo: t.kind === 'ctas' ? 'Lock the copy, or drop it if you no longer need it.' : 'Turn on row-level security for this table, then add rules for who can see what.',
|
|
49
|
+
fix,
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
return { checked, findings };
|
|
53
|
+
},
|
|
54
|
+
};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { functionReach } from '../../model.js';
|
|
2
|
+
import { functionRef } from '../../sql/ident.js';
|
|
3
|
+
|
|
4
|
+
export default {
|
|
5
|
+
id: 'RC002',
|
|
6
|
+
target: 'supabase',
|
|
7
|
+
severity: 'critical',
|
|
8
|
+
title: 'SECURITY DEFINER function callable by anyone',
|
|
9
|
+
summary:
|
|
10
|
+
'A SECURITY DEFINER function runs with its owner\'s powers and skips row-level security. Postgres lets PUBLIC execute every new function by default, so unless that grant is revoked, anyone with your public key can call it over the API.',
|
|
11
|
+
standards: { cwe: ['CWE-269', 'CWE-862'], nist80053: ['AC-3', 'AC-6'], cis: ['3.3', '6.8'] },
|
|
12
|
+
|
|
13
|
+
check(model) {
|
|
14
|
+
const checked = [];
|
|
15
|
+
const findings = [];
|
|
16
|
+
for (const f of model.functions.values()) {
|
|
17
|
+
if (!f.isSecurityDefiner || f.isTrigger) continue;
|
|
18
|
+
if (!model.exposedSchemas.includes(f.schema)) continue;
|
|
19
|
+
checked.push(`function:${f.key}`);
|
|
20
|
+
const reach = functionReach(model, f);
|
|
21
|
+
if (!reach.public && !reach.anon) continue;
|
|
22
|
+
const ref = functionRef(f);
|
|
23
|
+
findings.push({
|
|
24
|
+
object: `function:${f.key}`,
|
|
25
|
+
name: f.key,
|
|
26
|
+
file: f.file,
|
|
27
|
+
line: f.line,
|
|
28
|
+
severity: 'critical',
|
|
29
|
+
message: `${f.key} is SECURITY DEFINER and executable by ${reach.public ? 'PUBLIC' : 'anon'}. It bypasses row-level security for every caller.`,
|
|
30
|
+
plain: `Anyone on the internet can run the "${f.name}" function. It runs with full database powers and skips your security rules, so whatever it can read or change, anyone can.`,
|
|
31
|
+
todo: 'Stop the public from calling it. If signed-in users need it, allow only them.',
|
|
32
|
+
fix: `revoke execute on function ${ref} from public, anon;\n-- Only if signed-in users should be able to call it:\ngrant execute on function ${ref} to authenticated;`,
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
return { checked, findings };
|
|
36
|
+
},
|
|
37
|
+
};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { functionRef } from '../../sql/ident.js';
|
|
2
|
+
|
|
3
|
+
export default {
|
|
4
|
+
id: 'RC003',
|
|
5
|
+
target: 'supabase',
|
|
6
|
+
severity: 'high',
|
|
7
|
+
title: 'SECURITY DEFINER function without a pinned search_path',
|
|
8
|
+
summary:
|
|
9
|
+
'Without SET search_path, a privileged function looks names up in whatever order the caller\'s session says, including the caller\'s temporary schema. A caller can shadow a table or function the definer uses and have their own code run with the owner\'s powers.',
|
|
10
|
+
standards: { cwe: ['CWE-426'], nist80053: ['CM-6', 'AC-6'], cis: ['4.1'] },
|
|
11
|
+
|
|
12
|
+
check(model) {
|
|
13
|
+
const checked = [];
|
|
14
|
+
const findings = [];
|
|
15
|
+
for (const f of model.functions.values()) {
|
|
16
|
+
if (!f.isSecurityDefiner) continue;
|
|
17
|
+
checked.push(`function:${f.key}`);
|
|
18
|
+
if (f.searchPath) continue;
|
|
19
|
+
findings.push({
|
|
20
|
+
object: `function:${f.key}`,
|
|
21
|
+
name: f.key,
|
|
22
|
+
file: f.file,
|
|
23
|
+
line: f.line,
|
|
24
|
+
severity: 'high',
|
|
25
|
+
message: `${f.key} is SECURITY DEFINER with no SET search_path.`,
|
|
26
|
+
plain: `The "${f.name}" function runs with full database powers but doesn't lock down where it looks things up. Someone could trick it into running their code with those powers.`,
|
|
27
|
+
todo: 'Pin the function\'s search path.',
|
|
28
|
+
fix: `alter function ${functionRef(f)} set search_path = '';\n-- With an empty search_path, write every table inside the function with its schema (public.orders, not orders).`,
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
return { checked, findings };
|
|
32
|
+
},
|
|
33
|
+
};
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { functionRef } from '../../sql/ident.js';
|
|
2
|
+
|
|
3
|
+
const esc = (s) => s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
4
|
+
|
|
5
|
+
export default {
|
|
6
|
+
id: 'RC004',
|
|
7
|
+
target: 'supabase',
|
|
8
|
+
severity: 'high',
|
|
9
|
+
title: 'Invoker-rights trigger writes to a table locked to the server',
|
|
10
|
+
summary:
|
|
11
|
+
'A table with RLS on and no policies is deny-all for everyone except the service role. A trigger that runs with the CALLER\'s rights and writes to it makes every user action that fires the trigger fail with 42501 and roll back. Server jobs bypass RLS, so pipelines stay green while users cannot save.',
|
|
12
|
+
standards: { cwe: ['CWE-279'], nist80053: ['AC-6', 'AU-9'], cis: ['8.2'] },
|
|
13
|
+
|
|
14
|
+
check(model) {
|
|
15
|
+
const checked = [];
|
|
16
|
+
const findings = [];
|
|
17
|
+
const denyAll = [...model.tables.values()].filter((t) => t.rls && t.policies.size === 0);
|
|
18
|
+
|
|
19
|
+
for (const f of model.functions.values()) {
|
|
20
|
+
if (!f.isTrigger) continue;
|
|
21
|
+
checked.push(`function:${f.key}`);
|
|
22
|
+
if (f.isSecurityDefiner) continue;
|
|
23
|
+
const fnName = `${f.schema}.${f.name}`;
|
|
24
|
+
const firesOn = model.triggers.filter((tr) => tr.fn === fnName).map((tr) => tr.table);
|
|
25
|
+
|
|
26
|
+
for (const t of denyAll) {
|
|
27
|
+
const target = `(?:"?${esc(t.schema)}"?\\.)?"?${esc(t.name)}"?`;
|
|
28
|
+
const writes = new RegExp(`(?:INSERT\\s+INTO|UPDATE|DELETE\\s+FROM)\\s+${target}(?![\\w$])`, 'i');
|
|
29
|
+
// Unqualified names only resolve to public through the default search_path.
|
|
30
|
+
if (!writes.test(f.body)) continue;
|
|
31
|
+
if (t.schema !== 'public' && !new RegExp(`"?${esc(t.schema)}"?\\.`, 'i').test(f.body)) continue;
|
|
32
|
+
const ref = functionRef(f);
|
|
33
|
+
const on = firesOn.length ? firesOn.map((x) => x.split('.').pop()).join(', ') : 'a table';
|
|
34
|
+
findings.push({
|
|
35
|
+
object: `function:${f.key}`,
|
|
36
|
+
name: f.key,
|
|
37
|
+
file: f.file,
|
|
38
|
+
line: f.line,
|
|
39
|
+
severity: 'high',
|
|
40
|
+
message: `Trigger function ${f.key} (SECURITY INVOKER) writes to ${t.key}, which has RLS enabled and no policies. User writes that fire it raise 42501.${firesOn.length ? ` Fires on: ${firesOn.join(', ')}.` : ''}`,
|
|
41
|
+
plain: `When a user changes ${on}, the "${f.name}" trigger tries to write to "${t.name}", which only your server is allowed to touch. The user's change fails and is undone. Your server jobs skip this lock, so everything can look healthy while users can't save.`,
|
|
42
|
+
todo: 'Let the trigger run with its owner\'s rights, so it works no matter who fired it.',
|
|
43
|
+
fix: `alter function ${ref} security definer;\nalter function ${ref} set search_path = '';\n-- Trigger functions can't be called directly, so this does not open a new door.`,
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return { checked, findings };
|
|
48
|
+
},
|
|
49
|
+
};
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { tableExposure } from '../../model.js';
|
|
2
|
+
import { qualified, quoteIdent } from '../../sql/ident.js';
|
|
3
|
+
|
|
4
|
+
// `true`, `(true)`, `1 = 1` and friends: an expression that admits every row.
|
|
5
|
+
function alwaysTrue(expr) {
|
|
6
|
+
if (expr == null) return false;
|
|
7
|
+
const e = expr.toLowerCase().replace(/[\s()]/g, '');
|
|
8
|
+
return e === 'true' || e === '1=1' || e === "'t'" || e === 'true::boolean';
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export default {
|
|
12
|
+
id: 'RC005',
|
|
13
|
+
target: 'supabase',
|
|
14
|
+
severity: 'high',
|
|
15
|
+
title: 'Policy lets anyone change every row',
|
|
16
|
+
summary:
|
|
17
|
+
'Row-level security only protects a table as well as its policies do. A permissive policy whose condition is simply true lets every matching role touch every row, which turns RLS back off for that action.',
|
|
18
|
+
standards: { cwe: ['CWE-732'], nist80053: ['AC-3', 'AC-6'], cis: ['3.3'] },
|
|
19
|
+
|
|
20
|
+
check(model) {
|
|
21
|
+
const checked = [];
|
|
22
|
+
const findings = [];
|
|
23
|
+
for (const t of model.tables.values()) {
|
|
24
|
+
if (!t.rls) continue;
|
|
25
|
+
const exp = tableExposure(model, t);
|
|
26
|
+
if (!exp.anon && !exp.authenticated) continue;
|
|
27
|
+
checked.push(`table:${t.key}`);
|
|
28
|
+
|
|
29
|
+
for (const p of t.policies.values()) {
|
|
30
|
+
if (!p.permissive) continue;
|
|
31
|
+
const forAnon = exp.anon && (p.roles.includes('public') || p.roles.includes('anon'));
|
|
32
|
+
const forUsers = exp.authenticated && (p.roles.includes('public') || p.roles.includes('authenticated'));
|
|
33
|
+
if (!forAnon && !forUsers) continue;
|
|
34
|
+
|
|
35
|
+
const usingTrue = p.cmd !== 'INSERT' && alwaysTrue(p.using);
|
|
36
|
+
const checkTrue = p.cmd !== 'SELECT' && p.cmd !== 'DELETE' && alwaysTrue(p.check);
|
|
37
|
+
if (!usingTrue && !checkTrue) continue;
|
|
38
|
+
// Three different risks, three severities:
|
|
39
|
+
// change (UPDATE/DELETE/ALL): anyone can alter other people's rows. High.
|
|
40
|
+
// add (INSERT): anyone can add rows. Often a signup or contact form by
|
|
41
|
+
// design, so a warning: make sure nobody can add rows as someone else.
|
|
42
|
+
// claim (UPDATE/ALL with a real USING but WITH CHECK (true)): people can
|
|
43
|
+
// only touch their own rows, but can save them as someone else's. Warning.
|
|
44
|
+
// read (SELECT): often deliberate (a menu, store hours). Warning, anon only.
|
|
45
|
+
const kind = p.cmd === 'SELECT' ? 'read' : p.cmd === 'INSERT' ? 'add' : usingTrue ? 'change' : 'claim';
|
|
46
|
+
if (kind === 'read' && !forAnon) continue;
|
|
47
|
+
|
|
48
|
+
const who = forAnon ? 'Anyone on the internet' : 'Anyone who signs up for your app';
|
|
49
|
+
const action = { ALL: 'read, add, change and delete', UPDATE: 'change', DELETE: 'delete' }[p.cmd];
|
|
50
|
+
const owner = '(select auth.uid()) = user_id';
|
|
51
|
+
const clause = { SELECT: `using (${owner})`, DELETE: `using (${owner})`, INSERT: `with check (${owner})`, UPDATE: `using (${owner}) with check (${owner})`, ALL: `using (${owner}) with check (${owner})` }[p.cmd];
|
|
52
|
+
const ref = qualified(t.schema, t.name);
|
|
53
|
+
const plain = {
|
|
54
|
+
change: `${who} can ${action} any row in the "${t.name}" table, including other people's. The rule "${p.name}" allows it for every row.`,
|
|
55
|
+
add: `${who} can add rows to the "${t.name}" table. That's fine for a signup or contact form. Make sure nobody can add a row that pretends to belong to someone else.`,
|
|
56
|
+
claim: `${who} can save rows in the "${t.name}" table that claim to belong to someone else. The rule "${p.name}" checks which rows they start from, but not what they save.`,
|
|
57
|
+
read: `${who} can read every row in the "${t.name}" table. That's fine for truly public data, like a menu. It is not fine for anything personal.`,
|
|
58
|
+
}[kind];
|
|
59
|
+
const todo = {
|
|
60
|
+
change: 'Replace this rule with one that only lets people touch their own rows.',
|
|
61
|
+
add: 'If this is a public form, mark it as allowed in rattlecheck.config.json with a reason. If rows belong to users, check that the new row is theirs.',
|
|
62
|
+
claim: 'Add a WITH CHECK that matches the USING, so a saved row still belongs to the person saving it.',
|
|
63
|
+
read: 'Confirm this table holds nothing private. If it\'s meant to be public, mark it as allowed in rattlecheck.config.json with a reason.',
|
|
64
|
+
}[kind];
|
|
65
|
+
findings.push({
|
|
66
|
+
object: `table:${t.key}`,
|
|
67
|
+
name: t.key,
|
|
68
|
+
detail: p.name,
|
|
69
|
+
file: p.file,
|
|
70
|
+
line: p.line,
|
|
71
|
+
severity: kind === 'change' ? 'high' : 'warning',
|
|
72
|
+
message: `Policy "${p.name}" on ${t.key} is permissive FOR ${p.cmd} TO ${p.roles.join(', ')} with an always-true condition.`,
|
|
73
|
+
plain,
|
|
74
|
+
todo,
|
|
75
|
+
fix: `drop policy ${quoteIdent(p.name)} on ${ref};\n-- Replace it with a rule scoped to the row's owner, for example:\n-- create policy ${quoteIdent(p.name)} on ${ref} for ${p.cmd.toLowerCase()} to authenticated ${clause};`,
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return { checked, findings };
|
|
80
|
+
},
|
|
81
|
+
};
|