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,694 @@
|
|
|
1
|
+
import { redactSecret, sanitizeSnippet } from "../core/findings.js";
|
|
2
|
+
import { extractReferencedRelations, hasAuthOrTenantCheckInSqlBody } from "./expr-analyzer.js";
|
|
3
|
+
|
|
4
|
+
function normalizeId(raw) {
|
|
5
|
+
return String(raw || "").replace(/^"|"$/g, "").trim().toLowerCase();
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
function normalizeTableRef(rangeVar, defaultSchema = "public") {
|
|
9
|
+
if (!rangeVar) return "";
|
|
10
|
+
const schema = normalizeId(rangeVar.schemaname || defaultSchema);
|
|
11
|
+
const rel = normalizeId(rangeVar.relname || "");
|
|
12
|
+
return rel ? `${schema}.${rel}` : "";
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function normalizeNameList(listNodes, defaultSchema = "public") {
|
|
16
|
+
const parts = (listNodes || [])
|
|
17
|
+
.map((n) => n?.String?.sval)
|
|
18
|
+
.filter(Boolean)
|
|
19
|
+
.map((s) => normalizeId(s));
|
|
20
|
+
if (parts.length === 0) return "";
|
|
21
|
+
if (parts.length === 1) return `${defaultSchema}.${parts[0]}`;
|
|
22
|
+
return `${parts[parts.length - 2]}.${parts[parts.length - 1]}`;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function normalizeRoles(roleNodes) {
|
|
26
|
+
if (!Array.isArray(roleNodes) || roleNodes.length === 0) {
|
|
27
|
+
return ["public"];
|
|
28
|
+
}
|
|
29
|
+
const roles = [];
|
|
30
|
+
for (const r of roleNodes) {
|
|
31
|
+
const spec = r?.RoleSpec;
|
|
32
|
+
if (!spec) continue;
|
|
33
|
+
if (spec.roletype === "ROLESPEC_PUBLIC") {
|
|
34
|
+
roles.push("public");
|
|
35
|
+
} else if (spec.roletype === "ROLESPEC_CURRENT_USER") {
|
|
36
|
+
roles.push("current_user");
|
|
37
|
+
} else if (spec.rolename) {
|
|
38
|
+
roles.push(normalizeId(spec.rolename));
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return roles.length > 0 ? roles : ["public"];
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function extractFunctionArgTypes(paramNodes = []) {
|
|
45
|
+
return paramNodes.map((p) => {
|
|
46
|
+
const tn = p?.FunctionParameter?.argType || p?.TypeName;
|
|
47
|
+
if (!tn?.names) return "any";
|
|
48
|
+
return tn.names
|
|
49
|
+
.map((n) => n?.String?.sval)
|
|
50
|
+
.filter(Boolean)
|
|
51
|
+
.join(".")
|
|
52
|
+
.toLowerCase();
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function extractLiteralString(node) {
|
|
57
|
+
if (!node) return null;
|
|
58
|
+
if (node.A_Const?.sval?.sval !== undefined) return String(node.A_Const.sval.sval);
|
|
59
|
+
if (node.String?.sval !== undefined) return String(node.String.sval);
|
|
60
|
+
return null;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function extractLiteralBool(node) {
|
|
64
|
+
if (!node) return null;
|
|
65
|
+
if (node.A_Const?.boolval !== undefined) return Boolean(node.A_Const.boolval.boolval);
|
|
66
|
+
if (node.Boolean?.boolval !== undefined) return Boolean(node.Boolean.boolval);
|
|
67
|
+
if (node.String?.sval !== undefined) {
|
|
68
|
+
const s = node.String.sval.toLowerCase();
|
|
69
|
+
if (s === "true" || s === "on" || s === "1") return true;
|
|
70
|
+
if (s === "false" || s === "off" || s === "0") return false;
|
|
71
|
+
}
|
|
72
|
+
if (node.A_Const?.sval?.sval !== undefined) {
|
|
73
|
+
const s = node.A_Const.sval.sval.toLowerCase();
|
|
74
|
+
if (s === "true" || s === "on" || s === "1") return true;
|
|
75
|
+
if (s === "false" || s === "off" || s === "0") return false;
|
|
76
|
+
}
|
|
77
|
+
if (node.A_Const?.ival !== undefined) {
|
|
78
|
+
return Number(node.A_Const.ival.ival) !== 0;
|
|
79
|
+
}
|
|
80
|
+
return null;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function policyKey(tableName, policyName) {
|
|
84
|
+
return `${tableName}::${normalizeId(policyName)}`;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function functionSignatureKey(schemaName, fnName, argTypes) {
|
|
88
|
+
return `${schemaName}.${fnName}(${argTypes.join(",")})`;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Replays parsed AST statements across ordered migration files and produces
|
|
93
|
+
* the final cumulative PostgreSQL/Supabase security state graph.
|
|
94
|
+
*/
|
|
95
|
+
export function buildCumulativeStateGraph(parsedStatements, initialExposedSchemas = ["public"]) {
|
|
96
|
+
const exposedSchemas = new Set(
|
|
97
|
+
initialExposedSchemas
|
|
98
|
+
.map((s) => normalizeId(s))
|
|
99
|
+
.filter((s) => s && s !== "extensions" && s !== "graphql_public")
|
|
100
|
+
);
|
|
101
|
+
if (exposedSchemas.size === 0) exposedSchemas.add("public");
|
|
102
|
+
|
|
103
|
+
const tables = new Map();
|
|
104
|
+
const policies = new Map();
|
|
105
|
+
const functions = new Map();
|
|
106
|
+
const views = new Map();
|
|
107
|
+
const buckets = new Map();
|
|
108
|
+
const unsupported = [];
|
|
109
|
+
const defaultRevokedFunctionSchemas = new Set();
|
|
110
|
+
|
|
111
|
+
function ensureSyntheticExternalTable(tableName, file, line) {
|
|
112
|
+
if (tables.has(tableName)) return tables.get(tableName);
|
|
113
|
+
const [schema] = tableName.split(".");
|
|
114
|
+
const entry = {
|
|
115
|
+
name: tableName,
|
|
116
|
+
schema,
|
|
117
|
+
isTemp: false,
|
|
118
|
+
isExternal: true,
|
|
119
|
+
columns: [],
|
|
120
|
+
rlsEnabled: tableName === "storage.objects",
|
|
121
|
+
rlsForced: false,
|
|
122
|
+
grants: new Map([
|
|
123
|
+
["anon", new Set(["select", "insert", "update", "delete"])],
|
|
124
|
+
["authenticated", new Set(["select", "insert", "update", "delete"])],
|
|
125
|
+
]),
|
|
126
|
+
file,
|
|
127
|
+
line,
|
|
128
|
+
};
|
|
129
|
+
tables.set(tableName, entry);
|
|
130
|
+
return entry;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
for (const stmt of parsedStatements) {
|
|
134
|
+
const { stmtType, node, file, line, text } = stmt;
|
|
135
|
+
|
|
136
|
+
if (stmtType === "CreateStmt") {
|
|
137
|
+
const tableName = normalizeTableRef(node.relation);
|
|
138
|
+
if (!tableName) continue;
|
|
139
|
+
if (node.if_not_exists && tables.has(tableName)) continue;
|
|
140
|
+
const isTemp = node.relation?.relpersistence === "t";
|
|
141
|
+
const [schema] = tableName.split(".");
|
|
142
|
+
const columns = [];
|
|
143
|
+
for (const el of node.tableElts || []) {
|
|
144
|
+
const colName = el?.ColumnDef?.colname;
|
|
145
|
+
if (colName) columns.push(normalizeId(colName));
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
const defaultGrants = new Map();
|
|
149
|
+
if (!isTemp && exposedSchemas.has(schema)) {
|
|
150
|
+
defaultGrants.set("anon", new Set(["select", "insert", "update", "delete"]));
|
|
151
|
+
defaultGrants.set("authenticated", new Set(["select", "insert", "update", "delete"]));
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
tables.set(tableName, {
|
|
155
|
+
name: tableName,
|
|
156
|
+
schema,
|
|
157
|
+
isTemp,
|
|
158
|
+
isExternal: false,
|
|
159
|
+
columns,
|
|
160
|
+
rlsEnabled: false,
|
|
161
|
+
rlsForced: false,
|
|
162
|
+
grants: defaultGrants,
|
|
163
|
+
explicitAnonGrants: new Set(),
|
|
164
|
+
file,
|
|
165
|
+
line,
|
|
166
|
+
});
|
|
167
|
+
continue;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
if (stmtType === "AlterTableStmt") {
|
|
171
|
+
const tableName = normalizeTableRef(node.relation);
|
|
172
|
+
if (!tableName) continue;
|
|
173
|
+
|
|
174
|
+
if (node.objtype === "OBJECT_VIEW") {
|
|
175
|
+
const view = views.get(tableName);
|
|
176
|
+
if (view) {
|
|
177
|
+
for (const cmdWrap of node.cmds || []) {
|
|
178
|
+
const cmd = cmdWrap?.AlterTableCmd;
|
|
179
|
+
if (cmd?.subtype === "AT_SetRelOptions" || cmd?.subtype === "AT_ResetRelOptions") {
|
|
180
|
+
const items = cmd?.def?.List?.items || [];
|
|
181
|
+
for (const item of items) {
|
|
182
|
+
const def = item?.DefElem;
|
|
183
|
+
if (normalizeId(def?.defname) === "security_invoker") {
|
|
184
|
+
if (cmd.subtype === "AT_ResetRelOptions") {
|
|
185
|
+
view.securityInvoker = false;
|
|
186
|
+
} else {
|
|
187
|
+
const b = extractLiteralBool(def.arg);
|
|
188
|
+
if (b !== null) view.securityInvoker = b;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
continue;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
const tbl = tables.get(tableName) || ensureSyntheticExternalTable(tableName, file, line);
|
|
199
|
+
for (const cmdWrap of node.cmds || []) {
|
|
200
|
+
const cmd = cmdWrap?.AlterTableCmd;
|
|
201
|
+
if (!cmd) continue;
|
|
202
|
+
if (cmd.subtype === "AT_EnableRowSecurity") {
|
|
203
|
+
tbl.rlsEnabled = true;
|
|
204
|
+
} else if (cmd.subtype === "AT_DisableRowSecurity") {
|
|
205
|
+
tbl.rlsEnabled = false;
|
|
206
|
+
} else if (cmd.subtype === "AT_ForceRowSecurity") {
|
|
207
|
+
tbl.rlsForced = true;
|
|
208
|
+
} else if (cmd.subtype === "AT_NoForceRowSecurity") {
|
|
209
|
+
tbl.rlsForced = false;
|
|
210
|
+
} else if (cmd.subtype === "AT_AddColumn") {
|
|
211
|
+
const colName = cmd.def?.ColumnDef?.colname;
|
|
212
|
+
if (colName && !tbl.columns.includes(normalizeId(colName))) {
|
|
213
|
+
tbl.columns.push(normalizeId(colName));
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
continue;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
if (stmtType === "CreatePolicyStmt") {
|
|
221
|
+
const tableName = normalizeTableRef(node.table);
|
|
222
|
+
const pName = normalizeId(node.policy_name);
|
|
223
|
+
if (!tableName || !pName) continue;
|
|
224
|
+
|
|
225
|
+
// Gate 0 verified: CreatePolicyStmt.permissive is `true` for permissive and omitted for AS RESTRICTIVE.
|
|
226
|
+
const permissive = node.permissive === true;
|
|
227
|
+
const command = String(node.cmd_name || "all").toUpperCase();
|
|
228
|
+
const roles = normalizeRoles(node.roles);
|
|
229
|
+
|
|
230
|
+
policies.set(policyKey(tableName, pName), {
|
|
231
|
+
tableName,
|
|
232
|
+
policyName: pName,
|
|
233
|
+
rawPolicyName: node.policy_name,
|
|
234
|
+
permissive,
|
|
235
|
+
command,
|
|
236
|
+
roles,
|
|
237
|
+
qualAst: node.qual || null,
|
|
238
|
+
withCheckAst: node.with_check || null,
|
|
239
|
+
file,
|
|
240
|
+
line,
|
|
241
|
+
text,
|
|
242
|
+
});
|
|
243
|
+
continue;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
if (stmtType === "AlterPolicyStmt") {
|
|
247
|
+
const tableName = normalizeTableRef(node.table);
|
|
248
|
+
const pName = normalizeId(node.policy_name);
|
|
249
|
+
const existing = policies.get(policyKey(tableName, pName));
|
|
250
|
+
if (existing) {
|
|
251
|
+
if (Array.isArray(node.roles) && node.roles.length > 0) {
|
|
252
|
+
existing.roles = normalizeRoles(node.roles);
|
|
253
|
+
}
|
|
254
|
+
if (node.qual) {
|
|
255
|
+
existing.qualAst = node.qual;
|
|
256
|
+
}
|
|
257
|
+
if (node.with_check) {
|
|
258
|
+
existing.withCheckAst = node.with_check;
|
|
259
|
+
}
|
|
260
|
+
existing.file = file;
|
|
261
|
+
existing.line = line;
|
|
262
|
+
existing.text = text;
|
|
263
|
+
}
|
|
264
|
+
continue;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
if (stmtType === "CreateFunctionStmt") {
|
|
268
|
+
const fullName = normalizeNameList(node.funcname);
|
|
269
|
+
if (!fullName) continue;
|
|
270
|
+
const [schema, fnShort] = fullName.split(".");
|
|
271
|
+
const argTypes = extractFunctionArgTypes(node.parameters);
|
|
272
|
+
const sigKey = functionSignatureKey(schema, fnShort, argTypes);
|
|
273
|
+
|
|
274
|
+
const retParts = (node.returnType?.names || [])
|
|
275
|
+
.map((n) => n?.String?.sval?.toLowerCase())
|
|
276
|
+
.filter(Boolean);
|
|
277
|
+
const returnType = retParts.join(".");
|
|
278
|
+
const isTrigger = returnType === "trigger" || returnType === "event_trigger";
|
|
279
|
+
|
|
280
|
+
let securityDefiner = false;
|
|
281
|
+
let searchPathSet = false;
|
|
282
|
+
let bodyText = "";
|
|
283
|
+
|
|
284
|
+
for (const optWrap of node.options || []) {
|
|
285
|
+
const def = optWrap?.DefElem;
|
|
286
|
+
if (!def) continue;
|
|
287
|
+
const dname = normalizeId(def.defname);
|
|
288
|
+
if (dname === "security") {
|
|
289
|
+
securityDefiner = Boolean(def.arg?.Boolean?.boolval);
|
|
290
|
+
} else if (dname === "set") {
|
|
291
|
+
const vs = def.arg?.VariableSetStmt;
|
|
292
|
+
if (vs && normalizeId(vs.name) === "search_path") {
|
|
293
|
+
searchPathSet = true;
|
|
294
|
+
}
|
|
295
|
+
} else if (dname === "as") {
|
|
296
|
+
const items = def.arg?.List?.items || [];
|
|
297
|
+
bodyText += items.map((i) => i?.String?.sval || "").join("\n");
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
const existing = functions.get(sigKey);
|
|
302
|
+
let executeRoles;
|
|
303
|
+
let explicitExecuteRoles;
|
|
304
|
+
if (existing) {
|
|
305
|
+
executeRoles = existing.executeRoles;
|
|
306
|
+
explicitExecuteRoles = existing.explicitExecuteRoles;
|
|
307
|
+
} else if (defaultRevokedFunctionSchemas.has(schema)) {
|
|
308
|
+
executeRoles = new Set(["postgres", "service_role"]);
|
|
309
|
+
explicitExecuteRoles = new Set();
|
|
310
|
+
} else {
|
|
311
|
+
executeRoles = new Set(["public"]);
|
|
312
|
+
explicitExecuteRoles = new Set();
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
functions.set(sigKey, {
|
|
316
|
+
sigKey,
|
|
317
|
+
name: fullName,
|
|
318
|
+
schema,
|
|
319
|
+
argTypes,
|
|
320
|
+
returnType,
|
|
321
|
+
isTrigger,
|
|
322
|
+
securityDefiner,
|
|
323
|
+
searchPathSet,
|
|
324
|
+
hasAuthGuard: hasAuthOrTenantCheckInSqlBody(bodyText),
|
|
325
|
+
bodyText,
|
|
326
|
+
executeRoles,
|
|
327
|
+
explicitExecuteRoles,
|
|
328
|
+
file,
|
|
329
|
+
line,
|
|
330
|
+
});
|
|
331
|
+
continue;
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
if (stmtType === "AlterFunctionStmt") {
|
|
335
|
+
const fullName = normalizeNameList(node.func?.objname);
|
|
336
|
+
if (!fullName) continue;
|
|
337
|
+
const [schema, fnShort] = fullName.split(".");
|
|
338
|
+
const argTypes = (node.func?.objargs || []).map((a) =>
|
|
339
|
+
(a?.TypeName?.names || []).map((n) => n?.String?.sval?.toLowerCase()).filter(Boolean).join(".")
|
|
340
|
+
);
|
|
341
|
+
const exactKey = functionSignatureKey(schema, fnShort, argTypes);
|
|
342
|
+
const matchingFns = functions.has(exactKey)
|
|
343
|
+
? [functions.get(exactKey)]
|
|
344
|
+
: [...functions.values()].filter((f) => f.name === fullName);
|
|
345
|
+
|
|
346
|
+
for (const fn of matchingFns) {
|
|
347
|
+
for (const actWrap of node.actions || []) {
|
|
348
|
+
const def = actWrap?.DefElem;
|
|
349
|
+
if (!def) continue;
|
|
350
|
+
const dname = normalizeId(def.defname);
|
|
351
|
+
if (dname === "security") {
|
|
352
|
+
fn.securityDefiner = Boolean(def.arg?.Boolean?.boolval);
|
|
353
|
+
} else if (dname === "set") {
|
|
354
|
+
const vs = def.arg?.VariableSetStmt;
|
|
355
|
+
if (vs && normalizeId(vs.name) === "search_path") {
|
|
356
|
+
fn.searchPathSet = vs.kind !== "VAR_RESET";
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
continue;
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
if (stmtType === "ViewStmt") {
|
|
365
|
+
const viewName = normalizeTableRef(node.view);
|
|
366
|
+
if (!viewName) continue;
|
|
367
|
+
const [schema] = viewName.split(".");
|
|
368
|
+
let securityInvoker = false;
|
|
369
|
+
|
|
370
|
+
for (const optWrap of node.options || []) {
|
|
371
|
+
const def = optWrap?.DefElem;
|
|
372
|
+
if (def && normalizeId(def.defname) === "security_invoker") {
|
|
373
|
+
const b = extractLiteralBool(def.arg);
|
|
374
|
+
if (b !== null) securityInvoker = b;
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
const selectRoles = new Set();
|
|
379
|
+
if (exposedSchemas.has(schema)) {
|
|
380
|
+
selectRoles.add("anon");
|
|
381
|
+
selectRoles.add("authenticated");
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
views.set(viewName, {
|
|
385
|
+
name: viewName,
|
|
386
|
+
schema,
|
|
387
|
+
securityInvoker,
|
|
388
|
+
referencedTables: extractReferencedRelations(node.query),
|
|
389
|
+
selectRoles,
|
|
390
|
+
file,
|
|
391
|
+
line,
|
|
392
|
+
});
|
|
393
|
+
continue;
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
if (stmtType === "GrantStmt") {
|
|
397
|
+
const isGrant = node.is_grant === true;
|
|
398
|
+
const grantees = normalizeRoles(node.grantees);
|
|
399
|
+
const privs = (node.privileges || []).map((p) => normalizeId(p?.AccessPriv?.priv_name)).filter(Boolean);
|
|
400
|
+
const isAllPrivs = privs.length === 0;
|
|
401
|
+
|
|
402
|
+
if (node.objtype === "OBJECT_TABLE" && node.targtype === "ACL_TARGET_OBJECT") {
|
|
403
|
+
const tablePrivs = isAllPrivs ? ["select", "insert", "update", "delete"] : privs;
|
|
404
|
+
for (const obj of node.objects || []) {
|
|
405
|
+
const objName = normalizeTableRef(obj?.RangeVar);
|
|
406
|
+
if (!objName) continue;
|
|
407
|
+
const tbl = tables.get(objName);
|
|
408
|
+
if (tbl) {
|
|
409
|
+
for (const g of grantees) {
|
|
410
|
+
const targetRoles = g === "public" ? ["anon", "authenticated"] : [g];
|
|
411
|
+
for (const role of targetRoles) {
|
|
412
|
+
if (!tbl.grants.has(role)) tbl.grants.set(role, new Set());
|
|
413
|
+
const set = tbl.grants.get(role);
|
|
414
|
+
for (const p of tablePrivs) {
|
|
415
|
+
if (isGrant) {
|
|
416
|
+
set.add(p);
|
|
417
|
+
if (role === "anon") tbl.explicitAnonGrants?.add(p);
|
|
418
|
+
} else {
|
|
419
|
+
set.delete(p);
|
|
420
|
+
if (role === "anon") tbl.explicitAnonGrants?.delete(p);
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
const vw = views.get(objName);
|
|
427
|
+
if (vw && (isAllPrivs || tablePrivs.includes("select"))) {
|
|
428
|
+
for (const g of grantees) {
|
|
429
|
+
const targetRoles = g === "public" ? ["anon", "authenticated"] : [g];
|
|
430
|
+
for (const role of targetRoles) {
|
|
431
|
+
if (isGrant) vw.selectRoles.add(role);
|
|
432
|
+
else vw.selectRoles.delete(role);
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
} else if (node.objtype === "OBJECT_TABLE" && node.targtype === "ACL_TARGET_ALL_IN_SCHEMA") {
|
|
438
|
+
const schemas = (node.objects || []).map((o) => normalizeId(o?.String?.sval)).filter(Boolean);
|
|
439
|
+
const tablePrivs = isAllPrivs ? ["select", "insert", "update", "delete"] : privs;
|
|
440
|
+
for (const tbl of tables.values()) {
|
|
441
|
+
if (!schemas.includes(tbl.schema)) continue;
|
|
442
|
+
for (const g of grantees) {
|
|
443
|
+
const targetRoles = g === "public" ? ["anon", "authenticated"] : [g];
|
|
444
|
+
for (const role of targetRoles) {
|
|
445
|
+
if (!tbl.grants.has(role)) tbl.grants.set(role, new Set());
|
|
446
|
+
const set = tbl.grants.get(role);
|
|
447
|
+
for (const p of tablePrivs) {
|
|
448
|
+
if (isGrant) set.add(p);
|
|
449
|
+
else set.delete(p);
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
} else if (node.objtype === "OBJECT_FUNCTION" && node.targtype === "ACL_TARGET_OBJECT") {
|
|
455
|
+
if (isAllPrivs || privs.includes("execute")) {
|
|
456
|
+
for (const obj of node.objects || []) {
|
|
457
|
+
const owa = obj?.ObjectWithArgs;
|
|
458
|
+
if (!owa) continue;
|
|
459
|
+
const fnName = normalizeNameList(owa.objname);
|
|
460
|
+
const [schema, fnShort] = fnName.split(".");
|
|
461
|
+
const argTypes = (owa.objargs || []).map((a) =>
|
|
462
|
+
(a?.TypeName?.names || []).map((n) => n?.String?.sval?.toLowerCase()).filter(Boolean).join(".")
|
|
463
|
+
);
|
|
464
|
+
const exactKey = functionSignatureKey(schema, fnShort, argTypes);
|
|
465
|
+
const matches = functions.has(exactKey)
|
|
466
|
+
? [functions.get(exactKey)]
|
|
467
|
+
: [...functions.values()].filter((f) => f.name === fnName);
|
|
468
|
+
|
|
469
|
+
for (const fn of matches) {
|
|
470
|
+
for (const g of grantees) {
|
|
471
|
+
if (isGrant) {
|
|
472
|
+
fn.executeRoles.add(g);
|
|
473
|
+
fn.explicitExecuteRoles.add(g);
|
|
474
|
+
} else {
|
|
475
|
+
fn.executeRoles.delete(g);
|
|
476
|
+
fn.explicitExecuteRoles.delete(g);
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
continue;
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
if (stmtType === "AlterDefaultPrivilegesStmt") {
|
|
487
|
+
const action = node.action;
|
|
488
|
+
if (action?.objtype === "OBJECT_FUNCTION" && action.is_grant !== true) {
|
|
489
|
+
const grantees = normalizeRoles(action.grantees);
|
|
490
|
+
const privs = (action.privileges || []).map((p) => normalizeId(p?.AccessPriv?.priv_name));
|
|
491
|
+
if ((privs.length === 0 || privs.includes("execute")) && grantees.includes("public")) {
|
|
492
|
+
const schemas = [];
|
|
493
|
+
for (const opt of node.options || []) {
|
|
494
|
+
if (normalizeId(opt?.DefElem?.defname) === "schemas") {
|
|
495
|
+
for (const item of opt.DefElem?.arg?.List?.items || []) {
|
|
496
|
+
if (item?.String?.sval) schemas.push(normalizeId(item.String.sval));
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
if (schemas.length === 0) schemas.push("public");
|
|
501
|
+
for (const s of schemas) defaultRevokedFunctionSchemas.add(s);
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
continue;
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
if (stmtType === "DropStmt") {
|
|
508
|
+
if (node.removeType === "OBJECT_TABLE") {
|
|
509
|
+
for (const obj of node.objects || []) {
|
|
510
|
+
const tableName = normalizeNameList(obj?.List?.items);
|
|
511
|
+
tables.delete(tableName);
|
|
512
|
+
for (const [k, pol] of policies.entries()) {
|
|
513
|
+
if (pol.tableName === tableName) policies.delete(k);
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
} else if (node.removeType === "OBJECT_POLICY") {
|
|
517
|
+
for (const obj of node.objects || []) {
|
|
518
|
+
const parts = (obj?.List?.items || [])
|
|
519
|
+
.map((i) => normalizeId(i?.String?.sval))
|
|
520
|
+
.filter(Boolean);
|
|
521
|
+
if (parts.length === 3) {
|
|
522
|
+
policies.delete(policyKey(`${parts[0]}.${parts[1]}`, parts[2]));
|
|
523
|
+
} else if (parts.length === 2) {
|
|
524
|
+
policies.delete(policyKey(`public.${parts[0]}`, parts[1]));
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
} else if (node.removeType === "OBJECT_FUNCTION") {
|
|
528
|
+
for (const obj of node.objects || []) {
|
|
529
|
+
const owa = obj?.ObjectWithArgs;
|
|
530
|
+
if (!owa) continue;
|
|
531
|
+
const fnName = normalizeNameList(owa.objname);
|
|
532
|
+
const [schema, fnShort] = fnName.split(".");
|
|
533
|
+
if (Array.isArray(owa.objargs)) {
|
|
534
|
+
const argTypes = owa.objargs.map((a) =>
|
|
535
|
+
(a?.TypeName?.names || []).map((n) => n?.String?.sval?.toLowerCase()).filter(Boolean).join(".")
|
|
536
|
+
);
|
|
537
|
+
functions.delete(functionSignatureKey(schema, fnShort, argTypes));
|
|
538
|
+
} else {
|
|
539
|
+
for (const [k, fn] of functions.entries()) {
|
|
540
|
+
if (fn.name === fnName) functions.delete(k);
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
}
|
|
544
|
+
} else if (node.removeType === "OBJECT_VIEW") {
|
|
545
|
+
for (const obj of node.objects || []) {
|
|
546
|
+
const viewName = normalizeNameList(obj?.List?.items);
|
|
547
|
+
views.delete(viewName);
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
continue;
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
if (stmtType === "RenameStmt") {
|
|
554
|
+
if (node.renameType === "OBJECT_POLICY") {
|
|
555
|
+
const tableName = normalizeTableRef(node.relation);
|
|
556
|
+
const oldKey = policyKey(tableName, node.subname);
|
|
557
|
+
const pol = policies.get(oldKey);
|
|
558
|
+
if (pol) {
|
|
559
|
+
policies.delete(oldKey);
|
|
560
|
+
pol.policyName = normalizeId(node.newname);
|
|
561
|
+
pol.rawPolicyName = node.newname;
|
|
562
|
+
policies.set(policyKey(tableName, pol.policyName), pol);
|
|
563
|
+
}
|
|
564
|
+
} else if (node.renameType === "OBJECT_TABLE") {
|
|
565
|
+
const oldName = normalizeTableRef(node.relation);
|
|
566
|
+
const schema = oldName.split(".")[0] || "public";
|
|
567
|
+
const newName = `${schema}.${normalizeId(node.newname)}`;
|
|
568
|
+
const tbl = tables.get(oldName);
|
|
569
|
+
if (tbl) {
|
|
570
|
+
tables.delete(oldName);
|
|
571
|
+
tbl.name = newName;
|
|
572
|
+
tables.set(newName, tbl);
|
|
573
|
+
for (const [k, pol] of [...policies.entries()]) {
|
|
574
|
+
if (pol.tableName === oldName) {
|
|
575
|
+
policies.delete(k);
|
|
576
|
+
pol.tableName = newName;
|
|
577
|
+
policies.set(policyKey(newName, pol.policyName), pol);
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
}
|
|
582
|
+
continue;
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
if (stmtType === "InsertStmt") {
|
|
586
|
+
const rel = normalizeTableRef(node.relation);
|
|
587
|
+
if (rel === "storage.buckets") {
|
|
588
|
+
const cols = (node.cols || []).map((c) => normalizeId(c?.ResTarget?.name));
|
|
589
|
+
const idIdx = cols.indexOf("id");
|
|
590
|
+
const nameIdx = cols.indexOf("name");
|
|
591
|
+
const pubIdx = cols.indexOf("public");
|
|
592
|
+
const valuesLists = node.selectStmt?.SelectStmt?.valuesLists || [];
|
|
593
|
+
for (const row of valuesLists) {
|
|
594
|
+
const items = row?.List?.items || [];
|
|
595
|
+
const id = extractLiteralString(items[idIdx >= 0 ? idIdx : nameIdx]);
|
|
596
|
+
const isPublic = pubIdx >= 0 ? extractLiteralBool(items[pubIdx]) : null;
|
|
597
|
+
if (id) {
|
|
598
|
+
buckets.set(id.toLowerCase(), {
|
|
599
|
+
id,
|
|
600
|
+
isPublic: isPublic === null ? null : Boolean(isPublic),
|
|
601
|
+
file,
|
|
602
|
+
line,
|
|
603
|
+
});
|
|
604
|
+
}
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
continue;
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
if (stmtType === "UpdateStmt") {
|
|
611
|
+
const rel = normalizeTableRef(node.relation);
|
|
612
|
+
if (rel === "storage.buckets") {
|
|
613
|
+
let updatedPublic = null;
|
|
614
|
+
for (const target of node.targetList || []) {
|
|
615
|
+
if (normalizeId(target?.ResTarget?.name) === "public") {
|
|
616
|
+
updatedPublic = extractLiteralBool(target?.ResTarget?.val);
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
if (updatedPublic !== null) {
|
|
620
|
+
const whereStr = text || "";
|
|
621
|
+
const idMatch = whereStr.match(/\b(?:id|name)\s*=\s*'([^']+)'/i);
|
|
622
|
+
if (idMatch) {
|
|
623
|
+
const bId = idMatch[1].toLowerCase();
|
|
624
|
+
const existing = buckets.get(bId) || { id: idMatch[1], isPublic: false, file, line };
|
|
625
|
+
existing.isPublic = updatedPublic;
|
|
626
|
+
existing.file = file;
|
|
627
|
+
existing.line = line;
|
|
628
|
+
buckets.set(bId, existing);
|
|
629
|
+
}
|
|
630
|
+
}
|
|
631
|
+
}
|
|
632
|
+
continue;
|
|
633
|
+
}
|
|
634
|
+
|
|
635
|
+
if (stmtType === "DoStmt") {
|
|
636
|
+
const doBody = (node.args || [])
|
|
637
|
+
.map((a) => a?.DefElem?.arg?.String?.sval || "")
|
|
638
|
+
.join("\n");
|
|
639
|
+
if (/\b(POLICY|ROW LEVEL SECURITY|SECURITY DEFINER|GRANT|REVOKE|CREATE\s+TABLE|CREATE\s+VIEW)\b/i.test(doBody)) {
|
|
640
|
+
unsupported.push({
|
|
641
|
+
type: "DYNAMIC_DO_BLOCK",
|
|
642
|
+
file,
|
|
643
|
+
line,
|
|
644
|
+
reason: redactSecret("Dynamic DO $$ block contains security/DDL keywords that cannot be statically resolved"),
|
|
645
|
+
snippet: sanitizeSnippet(doBody, 120),
|
|
646
|
+
});
|
|
647
|
+
}
|
|
648
|
+
continue;
|
|
649
|
+
}
|
|
650
|
+
|
|
651
|
+
if (stmtType === "RuleStmt") {
|
|
652
|
+
unsupported.push({
|
|
653
|
+
type: "CREATE_RULE",
|
|
654
|
+
file,
|
|
655
|
+
line,
|
|
656
|
+
reason: redactSecret("PostgreSQL CREATE RULE rewrite semantics are outside static RLS analysis"),
|
|
657
|
+
snippet: sanitizeSnippet(text || "", 120),
|
|
658
|
+
});
|
|
659
|
+
}
|
|
660
|
+
}
|
|
661
|
+
|
|
662
|
+
// Inter-procedural pass: propagate hasAuthGuard when a function calls another tracked auth-checking function
|
|
663
|
+
let propagated = true;
|
|
664
|
+
while (propagated) {
|
|
665
|
+
propagated = false;
|
|
666
|
+
const guardedFns = [...functions.values()].filter((f) => f.hasAuthGuard);
|
|
667
|
+
if (guardedFns.length === 0) break;
|
|
668
|
+
for (const fn of functions.values()) {
|
|
669
|
+
if (fn.hasAuthGuard || !fn.bodyText) continue;
|
|
670
|
+
const lowerBody = fn.bodyText.toLowerCase();
|
|
671
|
+
for (const g of guardedFns) {
|
|
672
|
+
const shortName = g.name.split(".")[1] || g.name;
|
|
673
|
+
const escFull = g.name.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
674
|
+
const escShort = shortName.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
675
|
+
const callRe = new RegExp(`(?:\\b${escFull}|\\b${escShort})\\s*\\(`);
|
|
676
|
+
if (callRe.test(lowerBody)) {
|
|
677
|
+
fn.hasAuthGuard = true;
|
|
678
|
+
propagated = true;
|
|
679
|
+
break;
|
|
680
|
+
}
|
|
681
|
+
}
|
|
682
|
+
}
|
|
683
|
+
}
|
|
684
|
+
|
|
685
|
+
return {
|
|
686
|
+
exposedSchemas,
|
|
687
|
+
tables,
|
|
688
|
+
policies,
|
|
689
|
+
functions,
|
|
690
|
+
views,
|
|
691
|
+
buckets,
|
|
692
|
+
unsupported,
|
|
693
|
+
};
|
|
694
|
+
}
|