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,25 @@
|
|
|
1
|
+
import { redactSecret, sanitizeSnippet } from "../core/findings.js";
|
|
2
|
+
|
|
3
|
+
export function formatJsonReport(result) {
|
|
4
|
+
const safeUnsupported = (result.unsupported || []).map((u) => ({
|
|
5
|
+
...u,
|
|
6
|
+
reason: redactSecret(String(u.reason ?? "")),
|
|
7
|
+
snippet: sanitizeSnippet(String(u.snippet ?? ""), 140),
|
|
8
|
+
}));
|
|
9
|
+
|
|
10
|
+
const payload = {
|
|
11
|
+
tool: result.tool,
|
|
12
|
+
version: result.version,
|
|
13
|
+
scannedPath: result.scannedPath,
|
|
14
|
+
status: result.status,
|
|
15
|
+
durationMs: result.durationMs,
|
|
16
|
+
summary: result.summary,
|
|
17
|
+
suppressedCount: result.suppressedCount,
|
|
18
|
+
baselinedCount: result.baselinedCount,
|
|
19
|
+
parseStats: result.parseStats,
|
|
20
|
+
coverage: result.coverage,
|
|
21
|
+
unsupported: safeUnsupported,
|
|
22
|
+
findings: result.findings,
|
|
23
|
+
};
|
|
24
|
+
return `${JSON.stringify(payload, null, 2)}\n`;
|
|
25
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { redactSecret } from "../core/findings.js";
|
|
2
|
+
|
|
3
|
+
function escapeMdCell(value) {
|
|
4
|
+
return redactSecret(String(value ?? ""))
|
|
5
|
+
.replace(/\|/g, "\\|")
|
|
6
|
+
.replace(/\r?\n/g, " ");
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
function formatStatusBanner(status) {
|
|
10
|
+
switch (status) {
|
|
11
|
+
case "FAIL":
|
|
12
|
+
return "FAIL — Blocking security findings detected";
|
|
13
|
+
case "FAIL_WITH_UNSUPPORTED":
|
|
14
|
+
return "FAIL_WITH_UNSUPPORTED — Blocking security findings detected and unsupported SQL statements skipped";
|
|
15
|
+
case "WARN":
|
|
16
|
+
return "WARN — Findings detected below --fail-on threshold";
|
|
17
|
+
case "WARN_WITH_UNSUPPORTED":
|
|
18
|
+
return "WARN_WITH_UNSUPPORTED — Findings below --fail-on threshold and unsupported SQL statements skipped";
|
|
19
|
+
case "INCOMPLETE":
|
|
20
|
+
return "INCOMPLETE — UNSUPPORTED STATEMENTS PRESENT (static coverage incomplete)";
|
|
21
|
+
case "PASS":
|
|
22
|
+
default:
|
|
23
|
+
return "PASS (STATIC SCOPE — NO FINDINGS)";
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function formatMarkdownReport(result) {
|
|
28
|
+
const lines = [];
|
|
29
|
+
lines.push(`# rlsproof v${result.version} Static Security Report`);
|
|
30
|
+
lines.push("");
|
|
31
|
+
lines.push(`- **Status:** \`${formatStatusBanner(result.status)}\``);
|
|
32
|
+
lines.push(`- **Target:** \`${result.scannedPath}\``);
|
|
33
|
+
lines.push(`- **Duration:** \`${result.durationMs} ms\``);
|
|
34
|
+
lines.push(
|
|
35
|
+
`- **Summary:** Total \`${result.summary.total}\` (CRITICAL \`${result.summary.critical}\`, HIGH \`${result.summary.high}\`, MEDIUM \`${result.summary.medium}\`, LOW \`${result.summary.low}\`)`
|
|
36
|
+
);
|
|
37
|
+
if (result.baselinedCount > 0 || result.suppressedCount > 0) {
|
|
38
|
+
lines.push(
|
|
39
|
+
`- **Filtered:** Baselined \`${result.baselinedCount}\`, Suppressed (\`.rlsproofignore\` / inline) \`${result.suppressedCount}\``
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
lines.push(
|
|
43
|
+
`- **SQL Coverage:** ${result.parseStats.statementsParsed}/${result.parseStats.statementsTotal} statements parsed (${result.parseStats.parseRate}%) across ${result.coverage.sqlFilesScanned} SQL file(s); ${result.coverage.tablesTracked} table(s), ${result.coverage.policiesTracked} active policy(ies), ${result.coverage.functionsTracked} function(s), ${result.coverage.viewsTracked} view(s)`
|
|
44
|
+
);
|
|
45
|
+
lines.push(`- **Repo Surface Scanned:** ${result.coverage.repoFilesScanned} file(s)`);
|
|
46
|
+
lines.push("");
|
|
47
|
+
|
|
48
|
+
lines.push("## Findings");
|
|
49
|
+
lines.push("");
|
|
50
|
+
if (result.findings.length === 0) {
|
|
51
|
+
if (result.unsupported.length > 0) {
|
|
52
|
+
lines.push(
|
|
53
|
+
"No findings were emitted in the parsed static subset, **but unsupported SQL statements were skipped** (see `UNSUPPORTED / SKIPPED` below)."
|
|
54
|
+
);
|
|
55
|
+
} else {
|
|
56
|
+
lines.push(
|
|
57
|
+
"No static security findings detected within the analyzed SQL migration and repo surface rules."
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
} else {
|
|
61
|
+
lines.push("| Severity | Rule | Location | Object | Title | Remediation |");
|
|
62
|
+
lines.push("|---|---|---|---|---|---|");
|
|
63
|
+
for (const f of result.findings) {
|
|
64
|
+
lines.push(
|
|
65
|
+
`| **${escapeMdCell(f.severity)}** | \`${escapeMdCell(f.ruleId)}\` | \`${escapeMdCell(
|
|
66
|
+
`${f.file}:${f.line}`
|
|
67
|
+
)}\` | \`${escapeMdCell(f.object)}\` | ${escapeMdCell(f.title)} | ${escapeMdCell(
|
|
68
|
+
f.remediation
|
|
69
|
+
)} |`
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
lines.push("");
|
|
74
|
+
|
|
75
|
+
if (result.unsupported.length > 0) {
|
|
76
|
+
lines.push("## UNSUPPORTED / SKIPPED");
|
|
77
|
+
lines.push("");
|
|
78
|
+
lines.push(
|
|
79
|
+
"The following SQL statements or blocks could not be statically evaluated and were explicitly skipped rather than assumed clean:"
|
|
80
|
+
);
|
|
81
|
+
lines.push("");
|
|
82
|
+
lines.push("| Type | Location | Reason | Snippet |");
|
|
83
|
+
lines.push("|---|---|---|---|");
|
|
84
|
+
for (const u of result.unsupported) {
|
|
85
|
+
lines.push(
|
|
86
|
+
`| \`${escapeMdCell(u.type)}\` | \`${escapeMdCell(`${u.file}:${u.line}`)}\` | ${escapeMdCell(
|
|
87
|
+
u.reason
|
|
88
|
+
)} | \`${escapeMdCell(u.snippet)}\` |`
|
|
89
|
+
);
|
|
90
|
+
}
|
|
91
|
+
lines.push("");
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
return `${lines.join("\n")}\n`;
|
|
95
|
+
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { redactSecret, sanitizeSnippet } from "../core/findings.js";
|
|
2
|
+
import { RULE_LIST } from "../core/rules.js";
|
|
3
|
+
|
|
4
|
+
function mapSeverityToSarifLevel(severity) {
|
|
5
|
+
switch (severity) {
|
|
6
|
+
case "CRITICAL":
|
|
7
|
+
case "HIGH":
|
|
8
|
+
return "error";
|
|
9
|
+
case "MEDIUM":
|
|
10
|
+
return "warning";
|
|
11
|
+
default:
|
|
12
|
+
return "note";
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function formatSarifReport(result) {
|
|
17
|
+
const sarif = {
|
|
18
|
+
$schema:
|
|
19
|
+
"https://raw.githubusercontent.com/oasis-tcs/sarif-spec/master/Schemata/sarif-schema-2.1.0.json",
|
|
20
|
+
version: "2.1.0",
|
|
21
|
+
runs: [
|
|
22
|
+
{
|
|
23
|
+
tool: {
|
|
24
|
+
driver: {
|
|
25
|
+
name: "rlsproof",
|
|
26
|
+
version: result.version,
|
|
27
|
+
informationUri: "https://github.com/umut-dpc/rlsproof",
|
|
28
|
+
rules: RULE_LIST.map((r) => ({
|
|
29
|
+
id: r.id,
|
|
30
|
+
name: r.id,
|
|
31
|
+
shortDescription: { text: r.title },
|
|
32
|
+
fullDescription: { text: r.description },
|
|
33
|
+
help: { text: r.remediation },
|
|
34
|
+
properties: {
|
|
35
|
+
category: r.category,
|
|
36
|
+
cwe: r.cwe,
|
|
37
|
+
owasp: r.owasp,
|
|
38
|
+
defaultSeverity: r.severity,
|
|
39
|
+
},
|
|
40
|
+
})),
|
|
41
|
+
},
|
|
42
|
+
},
|
|
43
|
+
results: result.findings.map((f) => ({
|
|
44
|
+
ruleId: f.ruleId,
|
|
45
|
+
level: mapSeverityToSarifLevel(f.severity),
|
|
46
|
+
message: {
|
|
47
|
+
text: `${f.title}. ${f.detail} Remediation: ${f.remediation}`,
|
|
48
|
+
},
|
|
49
|
+
locations: [
|
|
50
|
+
{
|
|
51
|
+
physicalLocation: {
|
|
52
|
+
artifactLocation: {
|
|
53
|
+
uri: f.file || ".",
|
|
54
|
+
uriBaseId: "%SRCROOT%",
|
|
55
|
+
},
|
|
56
|
+
region: {
|
|
57
|
+
startLine: f.line || 1,
|
|
58
|
+
},
|
|
59
|
+
},
|
|
60
|
+
},
|
|
61
|
+
],
|
|
62
|
+
partialFingerprints: {
|
|
63
|
+
"rlsproof/v1": f.fingerprint,
|
|
64
|
+
},
|
|
65
|
+
properties: {
|
|
66
|
+
severity: f.severity,
|
|
67
|
+
object: f.object,
|
|
68
|
+
snippet: f.snippet,
|
|
69
|
+
},
|
|
70
|
+
})),
|
|
71
|
+
invocations: [
|
|
72
|
+
{
|
|
73
|
+
executionSuccessful: true,
|
|
74
|
+
toolExecutionNotifications: result.unsupported.map((u) => ({
|
|
75
|
+
descriptor: { id: u.type },
|
|
76
|
+
level: "warning",
|
|
77
|
+
message: {
|
|
78
|
+
text: `UNSUPPORTED / SKIPPED (${u.type}): ${redactSecret(String(u.reason ?? ""))} [${u.file}:${u.line}] (${sanitizeSnippet(String(u.snippet ?? ""), 120)})`,
|
|
79
|
+
},
|
|
80
|
+
locations: [
|
|
81
|
+
{
|
|
82
|
+
physicalLocation: {
|
|
83
|
+
artifactLocation: {
|
|
84
|
+
uri: u.file || ".",
|
|
85
|
+
uriBaseId: "%SRCROOT%",
|
|
86
|
+
},
|
|
87
|
+
region: {
|
|
88
|
+
startLine: u.line || 1,
|
|
89
|
+
},
|
|
90
|
+
},
|
|
91
|
+
},
|
|
92
|
+
],
|
|
93
|
+
})),
|
|
94
|
+
},
|
|
95
|
+
],
|
|
96
|
+
},
|
|
97
|
+
],
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
return `${JSON.stringify(sarif, null, 2)}\n`;
|
|
101
|
+
}
|
|
@@ -0,0 +1,289 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AST expression analyzers for PostgreSQL policy expressions, function bodies,
|
|
3
|
+
* and storage scoping checks.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
export function walkAst(node, visitor) {
|
|
7
|
+
if (!node || typeof node !== "object") return;
|
|
8
|
+
if (Array.isArray(node)) {
|
|
9
|
+
for (const item of node) {
|
|
10
|
+
walkAst(item, visitor);
|
|
11
|
+
}
|
|
12
|
+
return;
|
|
13
|
+
}
|
|
14
|
+
visitor(node);
|
|
15
|
+
for (const key of Object.keys(node)) {
|
|
16
|
+
const val = node[key];
|
|
17
|
+
if (val && typeof val === "object") {
|
|
18
|
+
walkAst(val, visitor);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function extractFuncName(funcCallNode) {
|
|
24
|
+
if (!funcCallNode?.funcname) return "";
|
|
25
|
+
return funcCallNode.funcname
|
|
26
|
+
.map((p) => p?.String?.sval || "")
|
|
27
|
+
.filter(Boolean)
|
|
28
|
+
.join(".")
|
|
29
|
+
.toLowerCase();
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function extractColumnParts(colRefNode) {
|
|
33
|
+
if (!colRefNode?.fields) return [];
|
|
34
|
+
return colRefNode.fields
|
|
35
|
+
.map((f) => f?.String?.sval || (f?.A_Star ? "*" : ""))
|
|
36
|
+
.filter(Boolean)
|
|
37
|
+
.map((s) => s.toLowerCase());
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function getConstLiteralKey(node) {
|
|
41
|
+
if (!node) return null;
|
|
42
|
+
if (node.A_Const) {
|
|
43
|
+
const c = node.A_Const;
|
|
44
|
+
if (c.boolval !== undefined) return `bool:${Boolean(c.boolval.boolval)}`;
|
|
45
|
+
if (c.ival !== undefined) return `num:${Number(c.ival.ival ?? 0)}`;
|
|
46
|
+
if (c.fval !== undefined) return `num:${Number(c.fval.fval)}`;
|
|
47
|
+
if (c.sval !== undefined) return `str:${String(c.sval.sval)}`;
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
if (node.TypeCast?.arg) {
|
|
51
|
+
const typeNames = (node.TypeCast.typeName?.names || [])
|
|
52
|
+
.map((n) => n?.String?.sval?.toLowerCase())
|
|
53
|
+
.filter(Boolean);
|
|
54
|
+
const inner = node.TypeCast.arg;
|
|
55
|
+
if (typeNames.includes("bool") || typeNames.includes("boolean")) {
|
|
56
|
+
const innerStr = inner?.A_Const?.sval?.sval?.toLowerCase();
|
|
57
|
+
if (innerStr === "true" || innerStr === "t" || innerStr === "1") return "bool:true";
|
|
58
|
+
if (innerStr === "false" || innerStr === "f" || innerStr === "0") return "bool:false";
|
|
59
|
+
}
|
|
60
|
+
return getConstLiteralKey(inner);
|
|
61
|
+
}
|
|
62
|
+
return null;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export function isFalseAst(node) {
|
|
66
|
+
if (!node) return false;
|
|
67
|
+
const key = getConstLiteralKey(node);
|
|
68
|
+
if (key === "bool:false") return true;
|
|
69
|
+
if (node.BoolExpr?.boolop === "NOT_EXPR" && node.BoolExpr.args?.length === 1) {
|
|
70
|
+
return isTautologyAst(node.BoolExpr.args[0]);
|
|
71
|
+
}
|
|
72
|
+
return false;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Returns true if the expression AST unconditionally evaluates to TRUE:
|
|
77
|
+
* e.g. `true`, `'true'::boolean`, `1 = 1`, `true = true`, `NOT false`, `... OR true`.
|
|
78
|
+
*/
|
|
79
|
+
export function isTautologyAst(node) {
|
|
80
|
+
if (!node) return false;
|
|
81
|
+
|
|
82
|
+
const constKey = getConstLiteralKey(node);
|
|
83
|
+
if (constKey === "bool:true") return true;
|
|
84
|
+
|
|
85
|
+
if (node.BoolExpr) {
|
|
86
|
+
const { boolop, args = [] } = node.BoolExpr;
|
|
87
|
+
if (boolop === "NOT_EXPR" && args.length === 1) {
|
|
88
|
+
return isFalseAst(args[0]);
|
|
89
|
+
}
|
|
90
|
+
if (boolop === "AND_EXPR" && args.length > 0) {
|
|
91
|
+
return args.every((a) => isTautologyAst(a));
|
|
92
|
+
}
|
|
93
|
+
if (boolop === "OR_EXPR" && args.length > 0) {
|
|
94
|
+
return args.some((a) => isTautologyAst(a));
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
if (node.A_Expr && node.A_Expr.kind === "AEXPR_OP") {
|
|
99
|
+
const op = (node.A_Expr.name || []).map((n) => n?.String?.sval).join("");
|
|
100
|
+
const leftKey = getConstLiteralKey(node.A_Expr.lexpr);
|
|
101
|
+
const rightKey = getConstLiteralKey(node.A_Expr.rexpr);
|
|
102
|
+
if (leftKey && rightKey) {
|
|
103
|
+
if (op === "=") return leftKey === rightKey;
|
|
104
|
+
if (op === "<>" || op === "!=") return leftKey !== rightKey;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
return false;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Returns true ONLY if policy expression trusts client-writable `user_metadata` /
|
|
113
|
+
* `raw_user_meta_data` via auth.jwt() or JWT claims. Never flags `app_metadata`.
|
|
114
|
+
*/
|
|
115
|
+
export function referencesUserMetadata(node, rawText = "") {
|
|
116
|
+
let hasJwtSource = false;
|
|
117
|
+
let hasUserMetaKey = false;
|
|
118
|
+
|
|
119
|
+
walkAst(node, (n) => {
|
|
120
|
+
if (n.FuncCall) {
|
|
121
|
+
const fname = extractFuncName(n.FuncCall);
|
|
122
|
+
if (fname === "auth.jwt" || fname === "current_setting") {
|
|
123
|
+
hasJwtSource = true;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
if (n.A_Const?.sval?.sval) {
|
|
127
|
+
const s = n.A_Const.sval.sval.toLowerCase();
|
|
128
|
+
if (s === "user_metadata" || s === "raw_user_meta_data") {
|
|
129
|
+
hasUserMetaKey = true;
|
|
130
|
+
} else if (s === "request.jwt.claims" || s === "request.jwt.claim") {
|
|
131
|
+
hasJwtSource = true;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
if (hasJwtSource && hasUserMetaKey) return true;
|
|
137
|
+
|
|
138
|
+
// Fallback check on raw expression text in case claims JSON path was concatenated
|
|
139
|
+
if (rawText) {
|
|
140
|
+
const lower = rawText.toLowerCase();
|
|
141
|
+
if (
|
|
142
|
+
(lower.includes("auth.jwt") || lower.includes("request.jwt")) &&
|
|
143
|
+
(/\buser_metadata\b/.test(lower) || /\braw_user_meta_data\b/.test(lower))
|
|
144
|
+
) {
|
|
145
|
+
return true;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
return false;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
const HELPER_PASSIVE_FUNCS = new Set([
|
|
152
|
+
"lower",
|
|
153
|
+
"upper",
|
|
154
|
+
"trim",
|
|
155
|
+
"btrim",
|
|
156
|
+
"ltrim",
|
|
157
|
+
"rtrim",
|
|
158
|
+
"coalesce",
|
|
159
|
+
"nullif",
|
|
160
|
+
"text",
|
|
161
|
+
"uuid",
|
|
162
|
+
"varchar",
|
|
163
|
+
"bpchar",
|
|
164
|
+
"int4",
|
|
165
|
+
"int8",
|
|
166
|
+
"bool",
|
|
167
|
+
"concat",
|
|
168
|
+
]);
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Returns true when a storage.objects policy expression only checks `bucket_id`
|
|
172
|
+
* (or is a tautology) without any folder, owner, tenant, or membership scoping.
|
|
173
|
+
*/
|
|
174
|
+
export function isStorageBucketOnlyCheck(node, rawText = "") {
|
|
175
|
+
if (!node) return false;
|
|
176
|
+
if (isTautologyAst(node)) return true;
|
|
177
|
+
|
|
178
|
+
let mentionsBucketId = false;
|
|
179
|
+
let hasScopingSignal = false;
|
|
180
|
+
|
|
181
|
+
walkAst(node, (n) => {
|
|
182
|
+
if (n.SubLink) {
|
|
183
|
+
// EXISTS (...) or IN (SELECT ...) membership/ownership subquery
|
|
184
|
+
hasScopingSignal = true;
|
|
185
|
+
}
|
|
186
|
+
if (n.ColumnRef) {
|
|
187
|
+
const parts = extractColumnParts(n.ColumnRef);
|
|
188
|
+
const col = parts[parts.length - 1];
|
|
189
|
+
if (col === "bucket_id") {
|
|
190
|
+
mentionsBucketId = true;
|
|
191
|
+
} else if (
|
|
192
|
+
col === "owner" ||
|
|
193
|
+
col === "owner_id" ||
|
|
194
|
+
col === "user_id" ||
|
|
195
|
+
col === "org_id" ||
|
|
196
|
+
col === "organization_id" ||
|
|
197
|
+
col === "tenant_id" ||
|
|
198
|
+
col === "workspace_id" ||
|
|
199
|
+
col === "path_tokens" ||
|
|
200
|
+
col === "name"
|
|
201
|
+
) {
|
|
202
|
+
// Note: `name` alone without a path helper could be bucket+name check;
|
|
203
|
+
// if paired with foldername/split_part/LIKE/auth.uid, FuncCall or A_Expr sets signal.
|
|
204
|
+
if (col !== "name") {
|
|
205
|
+
hasScopingSignal = true;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
if (n.FuncCall) {
|
|
210
|
+
const fname = extractFuncName(n.FuncCall);
|
|
211
|
+
if (fname === "auth.uid" || fname === "auth.jwt") {
|
|
212
|
+
hasScopingSignal = true;
|
|
213
|
+
} else if (fname === "auth.role") {
|
|
214
|
+
// auth.role() alone does not scope tenant or owner paths
|
|
215
|
+
} else if (
|
|
216
|
+
fname === "storage.foldername" ||
|
|
217
|
+
fname === "storage.filename" ||
|
|
218
|
+
fname === "split_part" ||
|
|
219
|
+
fname === "string_to_array" ||
|
|
220
|
+
fname === "regexp_match" ||
|
|
221
|
+
fname === "starts_with"
|
|
222
|
+
) {
|
|
223
|
+
hasScopingSignal = true;
|
|
224
|
+
} else if (!HELPER_PASSIVE_FUNCS.has(fname)) {
|
|
225
|
+
// Custom scoping/membership helper function such as public.is_org_member(...)
|
|
226
|
+
hasScopingSignal = true;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
if (hasScopingSignal) return false;
|
|
232
|
+
if (mentionsBucketId) return true;
|
|
233
|
+
|
|
234
|
+
if (rawText) {
|
|
235
|
+
const lower = rawText.toLowerCase();
|
|
236
|
+
if (
|
|
237
|
+
/\bbucket_id\b/.test(lower) &&
|
|
238
|
+
!/\b(storage\.foldername|auth\.uid|auth\.jwt|owner|owner_id|user_id|org_id|tenant_id|split_part|path_tokens|exists\s*\()\b/.test(
|
|
239
|
+
lower
|
|
240
|
+
)
|
|
241
|
+
) {
|
|
242
|
+
return true;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
return false;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* Checks whether a SQL/PLpgSQL function body enforces caller identity, role,
|
|
250
|
+
* or tenant membership.
|
|
251
|
+
*/
|
|
252
|
+
export function hasAuthOrTenantCheckInSqlBody(bodyText = "") {
|
|
253
|
+
if (!bodyText) return false;
|
|
254
|
+
const lower = bodyText.toLowerCase();
|
|
255
|
+
if (
|
|
256
|
+
/\bauth\s*\.\s*(uid|jwt)\s*\(/.test(lower) ||
|
|
257
|
+
/\bcurrent_setting\s*\(\s*'request\.jwt/i.test(lower) ||
|
|
258
|
+
/\bauth\s*\.\s*role\s*\(/.test(lower) ||
|
|
259
|
+
/\bis_org_member\b|\bis_member\b|\bhas_role\b|\bhas_permission\b|\bcheck_permission\b|\bauthorize\b|\bverify_membership\b|\buser_has_access\b/.test(
|
|
260
|
+
lower
|
|
261
|
+
)
|
|
262
|
+
) {
|
|
263
|
+
return true;
|
|
264
|
+
}
|
|
265
|
+
if (
|
|
266
|
+
/\braise\s+exception\b/.test(lower) &&
|
|
267
|
+
/\b(auth|uid|jwt|role|unauthorized|forbidden|permission|access|owner|member|session_user|current_user)\b/.test(
|
|
268
|
+
lower
|
|
269
|
+
)
|
|
270
|
+
) {
|
|
271
|
+
return true;
|
|
272
|
+
}
|
|
273
|
+
return false;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* Extracts all referenced table RangeVars inside a ViewStmt SelectStmt AST.
|
|
278
|
+
*/
|
|
279
|
+
export function extractReferencedRelations(node) {
|
|
280
|
+
const refs = new Set();
|
|
281
|
+
walkAst(node, (n) => {
|
|
282
|
+
if (n.RangeVar?.relname) {
|
|
283
|
+
const schema = (n.RangeVar.schemaname || "public").toLowerCase();
|
|
284
|
+
const rel = n.RangeVar.relname.toLowerCase();
|
|
285
|
+
refs.add(`${schema}.${rel}`);
|
|
286
|
+
}
|
|
287
|
+
});
|
|
288
|
+
return [...refs];
|
|
289
|
+
}
|