rlsproof 0.0.0-stage → 1.0.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/CALIBRATION.md +63 -0
- package/LICENSE +21 -0
- package/README.md +151 -2
- package/SECURITY.md +23 -0
- package/action.yml +113 -0
- package/bin/rlsproof.js +230 -0
- package/package.json +50 -4
- package/src/core/findings.js +145 -0
- package/src/core/ignore.js +197 -0
- package/src/core/rules.js +155 -0
- package/src/index.js +186 -0
- package/src/reporters/json.js +25 -0
- package/src/reporters/markdown.js +95 -0
- package/src/reporters/sarif.js +101 -0
- package/src/sql/expr-analyzer.js +289 -0
- package/src/sql/parser.js +486 -0
- package/src/sql/sql-rules.js +360 -0
- package/src/sql/state-graph.js +694 -0
- package/src/surface/ai-api-scanner.js +337 -0
- package/src/surface/config-toml.js +137 -0
- package/src/surface/file-walker.js +105 -0
- package/src/surface/secret-scanner.js +247 -0
|
@@ -0,0 +1,360 @@
|
|
|
1
|
+
import { createFinding } from "../core/findings.js";
|
|
2
|
+
import {
|
|
3
|
+
isTautologyAst,
|
|
4
|
+
referencesUserMetadata,
|
|
5
|
+
isStorageBucketOnlyCheck,
|
|
6
|
+
} from "./expr-analyzer.js";
|
|
7
|
+
|
|
8
|
+
const CLIENT_ROLES = new Set(["public", "anon", "authenticated"]);
|
|
9
|
+
const WRITE_COMMANDS = new Set(["ALL", "INSERT", "UPDATE", "DELETE"]);
|
|
10
|
+
|
|
11
|
+
const SENSITIVE_TABLE_PATTERNS =
|
|
12
|
+
/(?:invoice|payment|order|billing|subscription|secret|api_key|token|message|chat|conversation|document|file|lead|customer|client|audit_log|webhook|credential|session|private_|org|organization|team|workspace|account|payout|transaction)/i;
|
|
13
|
+
|
|
14
|
+
const TENANT_COLUMN_NAMES = new Set([
|
|
15
|
+
"org_id",
|
|
16
|
+
"organization_id",
|
|
17
|
+
"tenant_id",
|
|
18
|
+
"workspace_id",
|
|
19
|
+
"team_id",
|
|
20
|
+
"account_id",
|
|
21
|
+
]);
|
|
22
|
+
|
|
23
|
+
const SENSITIVE_BUCKET_NAMES =
|
|
24
|
+
/(?:private|invoice|kyc|document|receipt|backup|secret|export|contract|medical|tax|evidence|upload|user-files|client-files)/i;
|
|
25
|
+
|
|
26
|
+
const PUBLIC_READ_BUCKET_NAMES = /^(?:avatars?|public|assets|images|thumbnails|static|media)$/i;
|
|
27
|
+
|
|
28
|
+
function isClientRoleList(roles = []) {
|
|
29
|
+
return roles.some((r) => CLIENT_ROLES.has(r));
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function rolesOverlap(policyRolesA = [], policyRolesB = []) {
|
|
33
|
+
if (policyRolesA.includes("public") || policyRolesB.includes("public")) return true;
|
|
34
|
+
return policyRolesA.some((r) => policyRolesB.includes(r));
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function commandCoveredByRestrictive(permCmd, restCmd) {
|
|
38
|
+
if (restCmd === "ALL") return true;
|
|
39
|
+
return permCmd === restCmd;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function hasCoveringRestrictivePolicy(policiesList, permissivePol) {
|
|
43
|
+
return policiesList.some((other) => {
|
|
44
|
+
if (other.permissive) return false;
|
|
45
|
+
if (other.tableName !== permissivePol.tableName) return false;
|
|
46
|
+
if (!commandCoveredByRestrictive(permissivePol.command, other.command)) return false;
|
|
47
|
+
if (!rolesOverlap(permissivePol.roles, other.roles)) return false;
|
|
48
|
+
// A restrictive policy only meaningfully narrows access if it is NOT itself a tautology
|
|
49
|
+
const qualOk = !other.qualAst || !isTautologyAst(other.qualAst);
|
|
50
|
+
const checkOk = !other.withCheckAst || !isTautologyAst(other.withCheckAst);
|
|
51
|
+
return qualOk && checkOk;
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function extractBucketHintFromPolicy(pol) {
|
|
56
|
+
const txt = (pol.text || "").toLowerCase();
|
|
57
|
+
const m = txt.match(/\bbucket_id\s*=\s*'([^']+)'/);
|
|
58
|
+
return m ? m[1] : null;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function isSensitiveTableForSelect(tbl) {
|
|
62
|
+
if (!tbl) return false;
|
|
63
|
+
if (SENSITIVE_TABLE_PATTERNS.test(tbl.name.split(".")[1] || tbl.name)) return true;
|
|
64
|
+
return tbl.columns.some((c) => TENANT_COLUMN_NAMES.has(c));
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function hasAnyClientTableGrant(tbl) {
|
|
68
|
+
for (const role of ["anon", "authenticated"]) {
|
|
69
|
+
const privs = tbl.grants.get(role);
|
|
70
|
+
if (privs && privs.size > 0) return true;
|
|
71
|
+
}
|
|
72
|
+
return false;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Evaluates Rules 1–7 and the SQL component of Rule 9 over the cumulative state graph.
|
|
77
|
+
*/
|
|
78
|
+
export function evaluateSqlRules(state, { rootDir, anonSurfaceActive = false } = {}) {
|
|
79
|
+
const findings = [];
|
|
80
|
+
const allPolicies = [...state.policies.values()];
|
|
81
|
+
|
|
82
|
+
// Rule 1: RLS_DISABLED
|
|
83
|
+
for (const tbl of state.tables.values()) {
|
|
84
|
+
if (tbl.isTemp || tbl.isExternal) continue;
|
|
85
|
+
if (!state.exposedSchemas.has(tbl.schema)) continue;
|
|
86
|
+
if (tbl.rlsEnabled) continue;
|
|
87
|
+
if (!hasAnyClientTableGrant(tbl)) continue;
|
|
88
|
+
|
|
89
|
+
findings.push(
|
|
90
|
+
createFinding({
|
|
91
|
+
ruleId: "RLS_DISABLED",
|
|
92
|
+
severity: "CRITICAL",
|
|
93
|
+
file: tbl.file,
|
|
94
|
+
line: tbl.line,
|
|
95
|
+
object: tbl.name,
|
|
96
|
+
title: `RLS disabled on exposed table ${tbl.name}`,
|
|
97
|
+
detail: `Table ${tbl.name} is in PostgREST-exposed schema "${tbl.schema}" with client grants active and ROW LEVEL SECURITY disabled in the final migration state.`,
|
|
98
|
+
remediation: `Add \`ALTER TABLE ${tbl.name} ENABLE ROW LEVEL SECURITY;\` and explicit policies, or revoke anon/authenticated grants.`,
|
|
99
|
+
snippet: `CREATE TABLE ${tbl.name} (...); -- RLS disabled`,
|
|
100
|
+
rootDir,
|
|
101
|
+
})
|
|
102
|
+
);
|
|
103
|
+
|
|
104
|
+
// Rule 9 (SQL grant part): explicit anon write grants on RLS-disabled table
|
|
105
|
+
const anonPrivs = tbl.grants.get("anon");
|
|
106
|
+
const hasExplicitAnonWrite =
|
|
107
|
+
tbl.explicitAnonGrants &&
|
|
108
|
+
(tbl.explicitAnonGrants.has("insert") ||
|
|
109
|
+
tbl.explicitAnonGrants.has("update") ||
|
|
110
|
+
tbl.explicitAnonGrants.has("delete"));
|
|
111
|
+
if (
|
|
112
|
+
anonPrivs &&
|
|
113
|
+
(anonPrivs.has("insert") || anonPrivs.has("update") || anonPrivs.has("delete")) &&
|
|
114
|
+
(hasExplicitAnonWrite || anonSurfaceActive)
|
|
115
|
+
) {
|
|
116
|
+
findings.push(
|
|
117
|
+
createFinding({
|
|
118
|
+
ruleId: "ANON_OPEN_EXPOSURE",
|
|
119
|
+
severity: "HIGH",
|
|
120
|
+
file: tbl.file,
|
|
121
|
+
line: tbl.line,
|
|
122
|
+
object: `${tbl.name} (anon)`,
|
|
123
|
+
title: `Unauthenticated anon role has direct access to RLS-disabled table ${tbl.name}`,
|
|
124
|
+
detail: `Active anon client access combined with missing RLS on ${tbl.name} allows anonymous callers to read or mutate table rows directly.`,
|
|
125
|
+
rootDir,
|
|
126
|
+
})
|
|
127
|
+
);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// Rules 2, 3, 4, 7 (policies), 9 (anon write policies)
|
|
132
|
+
for (const pol of allPolicies) {
|
|
133
|
+
if (!isClientRoleList(pol.roles)) continue;
|
|
134
|
+
const tbl = state.tables.get(pol.tableName);
|
|
135
|
+
// If table was dropped, DropStmt already deleted policies; if external non-storage table, still evaluate.
|
|
136
|
+
|
|
137
|
+
const restrictiveCover = pol.permissive && hasCoveringRestrictivePolicy(allPolicies, pol);
|
|
138
|
+
|
|
139
|
+
// Rule 4: RLS_USER_METADATA_TRUST
|
|
140
|
+
if (
|
|
141
|
+
referencesUserMetadata(pol.qualAst, pol.text) ||
|
|
142
|
+
referencesUserMetadata(pol.withCheckAst, pol.text)
|
|
143
|
+
) {
|
|
144
|
+
findings.push(
|
|
145
|
+
createFinding({
|
|
146
|
+
ruleId: "RLS_USER_METADATA_TRUST",
|
|
147
|
+
severity: "HIGH",
|
|
148
|
+
file: pol.file,
|
|
149
|
+
line: pol.line,
|
|
150
|
+
object: `${pol.tableName} (${pol.policyName})`,
|
|
151
|
+
title: `Policy "${pol.policyName}" on ${pol.tableName} trusts user_metadata`,
|
|
152
|
+
detail: `Policy "${pol.policyName}" authorizes access using \`user_metadata\` / \`raw_user_meta_data\`, which authenticated users can overwrite via \`supabase.auth.updateUser()\`.`,
|
|
153
|
+
snippet: pol.text,
|
|
154
|
+
rootDir,
|
|
155
|
+
})
|
|
156
|
+
);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// Rule 7A: Storage objects policy
|
|
160
|
+
if (pol.tableName === "storage.objects") {
|
|
161
|
+
if (pol.permissive && !restrictiveCover) {
|
|
162
|
+
const qualBucketOnly = isStorageBucketOnlyCheck(pol.qualAst, pol.text);
|
|
163
|
+
const checkBucketOnly = isStorageBucketOnlyCheck(pol.withCheckAst, pol.text);
|
|
164
|
+
if (qualBucketOnly || checkBucketOnly) {
|
|
165
|
+
const bucketHint = extractBucketHintFromPolicy(pol);
|
|
166
|
+
const bucketMeta = bucketHint ? state.buckets.get(bucketHint) : null;
|
|
167
|
+
const isExplicitlyPublicRead =
|
|
168
|
+
pol.command === "SELECT" &&
|
|
169
|
+
((bucketMeta && bucketMeta.isPublic === true && !SENSITIVE_BUCKET_NAMES.test(bucketHint || "")) ||
|
|
170
|
+
(bucketHint && PUBLIC_READ_BUCKET_NAMES.test(bucketHint) && bucketMeta?.isPublic !== false));
|
|
171
|
+
|
|
172
|
+
if (!isExplicitlyPublicRead) {
|
|
173
|
+
findings.push(
|
|
174
|
+
createFinding({
|
|
175
|
+
ruleId: "STORAGE_BUCKET_OVEREXPOSED",
|
|
176
|
+
severity: "CRITICAL",
|
|
177
|
+
file: pol.file,
|
|
178
|
+
line: pol.line,
|
|
179
|
+
object: `storage.objects (${pol.policyName})`,
|
|
180
|
+
title: `Storage policy "${pol.policyName}" scopes by bucket_id without owner/folder check`,
|
|
181
|
+
detail: `Permissive ${pol.command} policy "${pol.policyName}" on storage.objects checks only bucket_id (or true) for roles [${pol.roles.join(", ")}] without owner or folder scoping.`,
|
|
182
|
+
snippet: pol.text,
|
|
183
|
+
rootDir,
|
|
184
|
+
})
|
|
185
|
+
);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
continue;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
// Rule 2: RLS_PERMISSIVE_TAUTOLOGY (skip storage.objects handled above)
|
|
193
|
+
if (pol.permissive && !restrictiveCover) {
|
|
194
|
+
const qualTrue = isTautologyAst(pol.qualAst);
|
|
195
|
+
const checkTrue = isTautologyAst(pol.withCheckAst);
|
|
196
|
+
|
|
197
|
+
if (qualTrue || checkTrue) {
|
|
198
|
+
const isWrite = WRITE_COMMANDS.has(pol.command);
|
|
199
|
+
if (isWrite || isSensitiveTableForSelect(tbl)) {
|
|
200
|
+
findings.push(
|
|
201
|
+
createFinding({
|
|
202
|
+
ruleId: "RLS_PERMISSIVE_TAUTOLOGY",
|
|
203
|
+
severity: isWrite ? "CRITICAL" : "HIGH",
|
|
204
|
+
file: pol.file,
|
|
205
|
+
line: pol.line,
|
|
206
|
+
object: `${pol.tableName} (${pol.policyName})`,
|
|
207
|
+
title: `Permissive tautology in "${pol.policyName}" (${pol.command}) on ${pol.tableName}`,
|
|
208
|
+
detail: `Active permissive policy "${pol.policyName}" for [${pol.roles.join(", ")}] evaluates to TRUE without a covering AS RESTRICTIVE policy.`,
|
|
209
|
+
snippet: pol.text,
|
|
210
|
+
rootDir,
|
|
211
|
+
})
|
|
212
|
+
);
|
|
213
|
+
}
|
|
214
|
+
} else if (pol.command === "INSERT" && !pol.withCheckAst) {
|
|
215
|
+
// Rule 3 (@all Lock #1): ONLY INSERT policies missing WITH CHECK.
|
|
216
|
+
// UPDATE and ALL inherit USING as WITH CHECK in PostgreSQL and MUST NOT fire here.
|
|
217
|
+
findings.push(
|
|
218
|
+
createFinding({
|
|
219
|
+
ruleId: "RLS_INSERT_MISSING_WITH_CHECK",
|
|
220
|
+
severity: "HIGH",
|
|
221
|
+
file: pol.file,
|
|
222
|
+
line: pol.line,
|
|
223
|
+
object: `${pol.tableName} (${pol.policyName})`,
|
|
224
|
+
title: `INSERT policy "${pol.policyName}" on ${pol.tableName} omits WITH CHECK`,
|
|
225
|
+
detail: `PostgreSQL INSERT policies require WITH CHECK to validate inserted rows; omitting it lets [${pol.roles.join(", ")}] insert rows for arbitrary owners or tenants.`,
|
|
226
|
+
snippet: pol.text,
|
|
227
|
+
rootDir,
|
|
228
|
+
})
|
|
229
|
+
);
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
// Rule 9 (@all Lock #2): Explicit `TO anon` write policy with no ownership/membership check
|
|
234
|
+
if (
|
|
235
|
+
pol.permissive &&
|
|
236
|
+
!restrictiveCover &&
|
|
237
|
+
pol.roles.includes("anon") &&
|
|
238
|
+
WRITE_COMMANDS.has(pol.command)
|
|
239
|
+
) {
|
|
240
|
+
const qualTrue = isTautologyAst(pol.qualAst);
|
|
241
|
+
const checkTrue = isTautologyAst(pol.withCheckAst);
|
|
242
|
+
const missingExpr = !pol.qualAst && !pol.withCheckAst;
|
|
243
|
+
if (qualTrue || checkTrue || missingExpr) {
|
|
244
|
+
findings.push(
|
|
245
|
+
createFinding({
|
|
246
|
+
ruleId: "ANON_OPEN_EXPOSURE",
|
|
247
|
+
severity: "HIGH",
|
|
248
|
+
file: pol.file,
|
|
249
|
+
line: pol.line,
|
|
250
|
+
object: `${pol.tableName} (${pol.policyName} · anon)`,
|
|
251
|
+
title: `Unauthenticated anon role granted open ${pol.command} policy on ${pol.tableName}`,
|
|
252
|
+
detail: `Policy "${pol.policyName}" explicitly grants ${pol.command} access to \`anon\` without an ownership or tenant check.`,
|
|
253
|
+
snippet: pol.text,
|
|
254
|
+
rootDir,
|
|
255
|
+
})
|
|
256
|
+
);
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
// Rule 7B: Sensitive storage buckets marked public = true
|
|
262
|
+
for (const bucket of state.buckets.values()) {
|
|
263
|
+
if (bucket.isPublic && SENSITIVE_BUCKET_NAMES.test(bucket.id)) {
|
|
264
|
+
findings.push(
|
|
265
|
+
createFinding({
|
|
266
|
+
ruleId: "STORAGE_BUCKET_OVEREXPOSED",
|
|
267
|
+
severity: "CRITICAL",
|
|
268
|
+
file: bucket.file,
|
|
269
|
+
line: bucket.line,
|
|
270
|
+
object: `storage.buckets (${bucket.id})`,
|
|
271
|
+
title: `Sensitive storage bucket "${bucket.id}" is configured as public = true`,
|
|
272
|
+
detail: `Bucket "${bucket.id}" matches a sensitive naming pattern yet is inserted/updated with \`public = true\`, allowing unauthenticated public object URL downloads.`,
|
|
273
|
+
snippet: `storage.buckets id='${bucket.id}', public=true`,
|
|
274
|
+
rootDir,
|
|
275
|
+
})
|
|
276
|
+
);
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
// Rule 5 (@all Lock #3): SECURITY_DEFINER_UNSAFE_RPC
|
|
281
|
+
for (const fn of state.functions.values()) {
|
|
282
|
+
if (!state.exposedSchemas.has(fn.schema)) continue;
|
|
283
|
+
if (fn.isTrigger) continue;
|
|
284
|
+
if (!fn.securityDefiner) continue;
|
|
285
|
+
|
|
286
|
+
const callableByClient =
|
|
287
|
+
fn.executeRoles.has("public") ||
|
|
288
|
+
fn.executeRoles.has("anon") ||
|
|
289
|
+
fn.executeRoles.has("authenticated");
|
|
290
|
+
if (!callableByClient) continue;
|
|
291
|
+
|
|
292
|
+
const isSafeCapabilityTokenLookup =
|
|
293
|
+
fn.searchPathSet &&
|
|
294
|
+
!fn.executeRoles.has("public") &&
|
|
295
|
+
!fn.executeRoles.has("anon") &&
|
|
296
|
+
/\bwhere\b[\s\S]{0,160}\b(?:token|invite_token|invitation_token)\s*=/i.test(fn.bodyText || "");
|
|
297
|
+
|
|
298
|
+
const effectiveHasAuthGuard = fn.hasAuthGuard || isSafeCapabilityTokenLookup;
|
|
299
|
+
|
|
300
|
+
if (!fn.searchPathSet || !effectiveHasAuthGuard) {
|
|
301
|
+
const gaps = [];
|
|
302
|
+
if (!fn.searchPathSet) gaps.push("SET search_path");
|
|
303
|
+
if (!effectiveHasAuthGuard) gaps.push("caller/tenant authorization check");
|
|
304
|
+
findings.push(
|
|
305
|
+
createFinding({
|
|
306
|
+
ruleId: "SECURITY_DEFINER_UNSAFE_RPC",
|
|
307
|
+
severity: effectiveHasAuthGuard ? "HIGH" : "CRITICAL",
|
|
308
|
+
file: fn.file,
|
|
309
|
+
line: fn.line,
|
|
310
|
+
object: fn.sigKey,
|
|
311
|
+
title: `Callable SECURITY DEFINER function ${fn.name} lacks ${gaps.join(" and ")}`,
|
|
312
|
+
detail: `Function ${fn.sigKey} is SECURITY DEFINER, callable by [${[...fn.executeRoles].join(", ")}], and lacks ${gaps.join(" + ")}.`,
|
|
313
|
+
snippet: `CREATE FUNCTION ${fn.name}(...) SECURITY DEFINER`,
|
|
314
|
+
rootDir,
|
|
315
|
+
})
|
|
316
|
+
);
|
|
317
|
+
|
|
318
|
+
// Rule 9: explicit anon EXECUTE on unsafe SECURITY DEFINER function
|
|
319
|
+
if (fn.explicitExecuteRoles?.has("anon")) {
|
|
320
|
+
findings.push(
|
|
321
|
+
createFinding({
|
|
322
|
+
ruleId: "ANON_OPEN_EXPOSURE",
|
|
323
|
+
severity: "HIGH",
|
|
324
|
+
file: fn.file,
|
|
325
|
+
line: fn.line,
|
|
326
|
+
object: `${fn.sigKey} (anon EXECUTE)`,
|
|
327
|
+
title: `Unauthenticated anon role can EXECUTE unsafe SECURITY DEFINER RPC ${fn.name}`,
|
|
328
|
+
detail: `Explicit GRANT EXECUTE ON FUNCTION ${fn.name} TO anon exposes an unguarded SECURITY DEFINER RPC to anonymous callers.`,
|
|
329
|
+
rootDir,
|
|
330
|
+
})
|
|
331
|
+
);
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
// Rule 6: VIEW_MISSING_SECURITY_INVOKER
|
|
337
|
+
for (const vw of state.views.values()) {
|
|
338
|
+
if (!state.exposedSchemas.has(vw.schema)) continue;
|
|
339
|
+
if (vw.securityInvoker) continue;
|
|
340
|
+
if (vw.referencedTables.length === 0) continue;
|
|
341
|
+
const callableByClient = vw.selectRoles.has("anon") || vw.selectRoles.has("authenticated");
|
|
342
|
+
if (!callableByClient) continue;
|
|
343
|
+
|
|
344
|
+
findings.push(
|
|
345
|
+
createFinding({
|
|
346
|
+
ruleId: "VIEW_MISSING_SECURITY_INVOKER",
|
|
347
|
+
severity: "HIGH",
|
|
348
|
+
file: vw.file,
|
|
349
|
+
line: vw.line,
|
|
350
|
+
object: vw.name,
|
|
351
|
+
title: `Exposed view ${vw.name} lacks security_invoker = true`,
|
|
352
|
+
detail: `View ${vw.name} references [${vw.referencedTables.join(", ")}] without \`WITH (security_invoker = true)\`, executing with the view creator's privileges and bypassing table RLS.`,
|
|
353
|
+
snippet: `CREATE VIEW ${vw.name} ...`,
|
|
354
|
+
rootDir,
|
|
355
|
+
})
|
|
356
|
+
);
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
return findings;
|
|
360
|
+
}
|