hullproof 0.0.0-stage → 0.5.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 +222 -0
- package/NOTICE +32 -0
- package/README.md +313 -2
- package/checks/db.firebase-rules-not-open.json +24 -0
- package/checks/db.rls-every-table.json +25 -0
- package/checks/log.no-credential-in-logs.json +30 -0
- package/checks/secrets.no-env-file-committed.json +31 -0
- package/checks/secrets.no-secret-in-public-env.json +37 -0
- package/checks/storage.firebase-rules-not-open.json +22 -0
- package/checks/supply.no-untrusted-event-in-run.json +23 -0
- package/checks/web.cors-no-reflected-origin.json +31 -0
- package/detectors/ai.json +25 -0
- package/detectors/auth.json +27 -0
- package/detectors/ci.json +19 -0
- package/detectors/containers.json +8 -0
- package/detectors/data_stores.json +38 -0
- package/detectors/frameworks.json +94 -0
- package/detectors/hosting.json +44 -0
- package/detectors/iac.json +43 -0
- package/detectors/languages.json +48 -0
- package/detectors/mobile.json +22 -0
- package/detectors/package_managers.json +39 -0
- package/detectors/payments.json +19 -0
- package/detectors/runtimes.json +17 -0
- package/dist/checks/code/admin-role.js +46 -0
- package/dist/checks/code/owner-filter.js +91 -0
- package/dist/checks/code/shared.js +84 -0
- package/dist/checks/code/webhook-verify.js +77 -0
- package/dist/checks/context.js +64 -0
- package/dist/checks/map/config-checks.js +74 -0
- package/dist/checks/map/entry-checks.js +97 -0
- package/dist/checks/map/shared.js +44 -0
- package/dist/checks/ops.js +307 -0
- package/dist/checks/pro-map.js +1 -0
- package/dist/checks/run.js +92 -0
- package/dist/checks/types.js +1 -0
- package/dist/ci/comment.js +44 -0
- package/dist/ci/sarif-check.js +157 -0
- package/dist/ci/sarif.js +61 -0
- package/dist/cli.js +358 -0
- package/dist/commands/ci.js +115 -0
- package/dist/commands/git.js +58 -0
- package/dist/commands/hooks.js +30 -0
- package/dist/commands/hunt-pro.js +1 -0
- package/dist/commands/hunt.js +86 -0
- package/dist/commands/map.js +33 -0
- package/dist/commands/mcp.js +17 -0
- package/dist/commands/pack.js +48 -0
- package/dist/commands/prove-pro.js +1 -0
- package/dist/commands/prove.js +173 -0
- package/dist/commands/report-gate.js +92 -0
- package/dist/commands/scan.js +158 -0
- package/dist/confirm/agreement.js +18 -0
- package/dist/confirm/chain.js +84 -0
- package/dist/confirm/confirm.js +112 -0
- package/dist/confirm/honesty.js +75 -0
- package/dist/confirm/lead.js +92 -0
- package/dist/confirm/merge.js +88 -0
- package/dist/confirm/query.js +145 -0
- package/dist/confirm/rule.js +86 -0
- package/dist/confirm/store.js +42 -0
- package/dist/confirm/summary.js +6 -0
- package/dist/confirm/synthesis.js +28 -0
- package/dist/discover/detectors.js +50 -0
- package/dist/discover/discover.js +270 -0
- package/dist/discover/extract.js +282 -0
- package/dist/discover/git.js +40 -0
- package/dist/discover/kit-files.js +47 -0
- package/dist/discover/walk.js +110 -0
- package/dist/env.js +57 -0
- package/dist/gate/gate.js +173 -0
- package/dist/hooks/install.js +153 -0
- package/dist/hooks/run.js +112 -0
- package/dist/map/analyze.js +201 -0
- package/dist/map/build.js +253 -0
- package/dist/map/candidates.js +49 -0
- package/dist/map/cblocks.js +77 -0
- package/dist/map/config.js +291 -0
- package/dist/map/dotnet.js +242 -0
- package/dist/map/entry.js +78 -0
- package/dist/map/expo.js +190 -0
- package/dist/map/go.js +292 -0
- package/dist/map/js-express.js +143 -0
- package/dist/map/js-misc.js +295 -0
- package/dist/map/js-next.js +292 -0
- package/dist/map/laravel.js +389 -0
- package/dist/map/lex-more.js +305 -0
- package/dist/map/lex.js +425 -0
- package/dist/map/light-index.js +191 -0
- package/dist/map/light-profiles.js +324 -0
- package/dist/map/light.js +574 -0
- package/dist/map/local-vocab.js +27 -0
- package/dist/map/mobile.js +326 -0
- package/dist/map/py-django.js +281 -0
- package/dist/map/py.js +391 -0
- package/dist/map/rails.js +391 -0
- package/dist/map/repo.js +178 -0
- package/dist/map/rust.js +312 -0
- package/dist/map/spring.js +207 -0
- package/dist/map/taint.js +96 -0
- package/dist/map/touches.js +355 -0
- package/dist/map/vocab.js +88 -0
- package/dist/mapping/controls.js +34 -0
- package/dist/mapping/dependency-policy.js +44 -0
- package/dist/mapping/map.js +44 -0
- package/dist/mapping/maps/gitleaks.json +12 -0
- package/dist/mapping/maps/osv-scanner.json +7 -0
- package/dist/mapping/maps/trivy.json +13 -0
- package/dist/mcp/confine.js +59 -0
- package/dist/mcp/protocol.js +140 -0
- package/dist/mcp/schema.js +43 -0
- package/dist/mcp/server.js +37 -0
- package/dist/mcp/tools.js +238 -0
- package/dist/packs/coverage.js +79 -0
- package/dist/packs/manifest.js +36 -0
- package/dist/packs/validate.js +271 -0
- package/dist/prove/container.js +276 -0
- package/dist/prove/exec-main.js +20 -0
- package/dist/prove/executor.js +149 -0
- package/dist/prove/local.js +131 -0
- package/dist/prove/probes.js +65 -0
- package/dist/prove/recipe.js +278 -0
- package/dist/prove/run.js +136 -0
- package/dist/prove/runner.js +1 -0
- package/dist/prove/safety.js +126 -0
- package/dist/prove/spec.js +357 -0
- package/dist/prove/store.js +65 -0
- package/dist/report/build.js +297 -0
- package/dist/report/entry-points.js +75 -0
- package/dist/report/hunt-section.js +71 -0
- package/dist/report/kit.js +41 -0
- package/dist/report/load.js +35 -0
- package/dist/report/messages.js +273 -0
- package/dist/report/prove-section.js +65 -0
- package/dist/report/report.js +156 -0
- package/dist/report/surface.js +21 -0
- package/dist/report/test-code.js +51 -0
- package/dist/report/tiers.js +44 -0
- package/dist/scanners/common.js +162 -0
- package/dist/scanners/gitleaks.js +90 -0
- package/dist/scanners/index.js +113 -0
- package/dist/scanners/osv-scanner.js +77 -0
- package/dist/scanners/run-targets.js +34 -0
- package/dist/scanners/secret-noise.js +89 -0
- package/dist/scanners/semgrep.js +142 -0
- package/dist/scanners/trivy.js +65 -0
- package/dist/scanners/types.js +1 -0
- package/dist/types/check.js +1 -0
- package/dist/types/codemap.js +1 -0
- package/dist/types/findings.js +5 -0
- package/dist/types/hunt.js +1 -0
- package/dist/types/index.js +1 -0
- package/dist/types/prove.js +1 -0
- package/dist/types/results.js +2 -0
- package/dist/types/scan.js +1 -0
- package/dist/types/stack.js +5 -0
- package/dist/util.js +71 -0
- package/dist/version.js +2 -0
- package/package.json +46 -4
- package/packs/android/fixture-result.json +43 -0
- package/packs/android/pack.json +56 -0
- package/packs/dotnet/fixture-result.json +62 -0
- package/packs/dotnet/pack.json +88 -0
- package/packs/expo/answer-key.json +51 -0
- package/packs/expo/fixture-result.json +62 -0
- package/packs/expo/pack.json +79 -0
- package/packs/firebase/answer-key.json +27 -0
- package/packs/firebase/fixture-result.json +43 -0
- package/packs/firebase/pack.json +58 -0
- package/packs/go/answer-key.json +101 -0
- package/packs/go/fixture-result.json +64 -0
- package/packs/go/pack.json +99 -0
- package/packs/ios/fixture-result.json +43 -0
- package/packs/ios/pack.json +55 -0
- package/packs/laravel/answer-key.json +84 -0
- package/packs/laravel/fixture-result.json +60 -0
- package/packs/laravel/pack.json +91 -0
- package/packs/node-ts/answer-key.json +220 -0
- package/packs/node-ts/fixture-result.json +114 -0
- package/packs/node-ts/pack.json +134 -0
- package/packs/python/answer-key.json +195 -0
- package/packs/python/fixture-result.json +119 -0
- package/packs/python/pack.json +121 -0
- package/packs/rails/answer-key.json +84 -0
- package/packs/rails/fixture-result.json +60 -0
- package/packs/rails/pack.json +78 -0
- package/packs/rust/fixture-result.json +43 -0
- package/packs/rust/pack.json +69 -0
- package/packs/spring/fixture-result.json +63 -0
- package/packs/spring/pack.json +103 -0
- package/packs/supabase/answer-key.json +52 -0
- package/packs/supabase/fixture-result.json +58 -0
- package/packs/supabase/pack.json +60 -0
- package/rules/gitleaks.toml +256 -0
- package/rules/semgrep/api-route-no-auth.yaml +31 -0
- package/rules/semgrep/cors-reflect-origin.yaml +48 -0
- package/rules/semgrep/eval-or-new-function.yaml +185 -0
- package/rules/semgrep/log-session-token.yaml +48 -0
- package/rules/semgrep/math-random-tokens.yaml +24 -0
- package/rules/semgrep/next-public-secret-name.yaml +32 -0
- package/rules/semgrep/postgrest-filter-injection.yaml +78 -0
- package/rules/semgrep/raw-sql-concat.yaml +42 -0
- package/rules/semgrep/read-by-id-no-owner.yaml +49 -0
- package/rules/semgrep/server-action-no-auth.yaml +26 -0
- package/rules/semgrep/sql-variants.yaml +99 -0
- package/rules/semgrep/ssrf-unguarded-url.yaml +137 -0
- package/rules/semgrep/subprocess-injection.yaml +163 -0
- package/rules/semgrep/taint-go.yaml +333 -0
- package/rules/semgrep/taint-java.yaml +317 -0
- package/rules/semgrep/taint-javascript.yaml +412 -0
- package/rules/semgrep/taint-php.yaml +361 -0
- package/rules/semgrep/taint-python.yaml +414 -0
- package/rules/semgrep/tool-endpoint-admin-client.yaml +86 -0
- package/rules/semgrep/user-metadata-authz.yaml +22 -0
- package/rules/semgrep/verify-result-unused.yaml +62 -0
- package/rules/semgrep/webhook-parse-before-verify.yaml +29 -0
- package/rules/test-artifact-ids.json +4 -0
- package/rules/tiers.json +427 -0
- package/schemas/check-results.schema.json +55 -0
- package/schemas/check.schema.json +41 -0
- package/schemas/codemap.schema.json +216 -0
- package/schemas/findings.schema.json +1048 -0
- package/schemas/hunt-output.schema.json +46 -0
- package/schemas/hunt-result.schema.json +58 -0
- package/schemas/results.schema.json +17 -0
- package/schemas/scan-hits.schema.json +32 -0
- package/schemas/scan-meta.schema.json +25 -0
- package/schemas/stack.schema.json +93 -0
- package/src/checks/README.md +32 -0
- package/src/checks/code/admin-role.ts +49 -0
- package/src/checks/code/owner-filter.ts +95 -0
- package/src/checks/code/shared.ts +66 -0
- package/src/checks/code/webhook-verify.ts +75 -0
- package/src/checks/context.ts +72 -0
- package/src/checks/map/config-checks.ts +79 -0
- package/src/checks/map/entry-checks.ts +116 -0
- package/src/checks/map/shared.ts +63 -0
- package/src/checks/ops.ts +280 -0
- package/src/checks/pro-map.ts +4 -0
- package/src/checks/run.ts +80 -0
- package/src/checks/types.ts +12 -0
- package/src/ci/comment.ts +52 -0
- package/src/ci/sarif-check.ts +100 -0
- package/src/ci/sarif.ts +72 -0
- package/src/cli.ts +273 -0
- package/src/commands/ci.ts +104 -0
- package/src/commands/git.ts +59 -0
- package/src/commands/hooks.ts +27 -0
- package/src/commands/hunt-pro.ts +4 -0
- package/src/commands/hunt.ts +68 -0
- package/src/commands/map.ts +27 -0
- package/src/commands/mcp.ts +18 -0
- package/src/commands/pack.ts +34 -0
- package/src/commands/prove-pro.ts +4 -0
- package/src/commands/prove.ts +126 -0
- package/src/commands/report-gate.ts +75 -0
- package/src/commands/scan.ts +156 -0
- package/src/confirm/agreement.ts +23 -0
- package/src/confirm/chain.ts +67 -0
- package/src/confirm/confirm.ts +107 -0
- package/src/confirm/honesty.ts +65 -0
- package/src/confirm/lead.ts +76 -0
- package/src/confirm/merge.ts +89 -0
- package/src/confirm/query.ts +126 -0
- package/src/confirm/rule.ts +76 -0
- package/src/confirm/store.ts +42 -0
- package/src/confirm/summary.ts +9 -0
- package/src/confirm/synthesis.ts +33 -0
- package/src/discover/detectors.ts +71 -0
- package/src/discover/discover.ts +253 -0
- package/src/discover/extract.ts +235 -0
- package/src/discover/git.ts +31 -0
- package/src/discover/kit-files.ts +37 -0
- package/src/discover/walk.ts +88 -0
- package/src/env.ts +51 -0
- package/src/gate/gate.ts +169 -0
- package/src/hooks/install.ts +142 -0
- package/src/hooks/run.ts +121 -0
- package/src/map/analyze.ts +219 -0
- package/src/map/build.ts +206 -0
- package/src/map/candidates.ts +47 -0
- package/src/map/cblocks.ts +55 -0
- package/src/map/config.ts +207 -0
- package/src/map/dotnet.ts +205 -0
- package/src/map/entry.ts +80 -0
- package/src/map/expo.ts +171 -0
- package/src/map/go.ts +271 -0
- package/src/map/js-express.ts +135 -0
- package/src/map/js-misc.ts +221 -0
- package/src/map/js-next.ts +282 -0
- package/src/map/laravel.ts +327 -0
- package/src/map/lex-more.ts +152 -0
- package/src/map/lex.ts +274 -0
- package/src/map/light-index.ts +134 -0
- package/src/map/light-profiles.ts +363 -0
- package/src/map/light.ts +484 -0
- package/src/map/local-vocab.ts +30 -0
- package/src/map/mobile.ts +263 -0
- package/src/map/py-django.ts +239 -0
- package/src/map/py.ts +329 -0
- package/src/map/rails.ts +291 -0
- package/src/map/repo.ts +150 -0
- package/src/map/rust.ts +256 -0
- package/src/map/spring.ts +185 -0
- package/src/map/taint.ts +86 -0
- package/src/map/touches.ts +296 -0
- package/src/map/vocab.ts +106 -0
- package/src/mapping/controls.ts +47 -0
- package/src/mapping/dependency-policy.ts +55 -0
- package/src/mapping/map.ts +59 -0
- package/src/mapping/maps/gitleaks.json +12 -0
- package/src/mapping/maps/osv-scanner.json +7 -0
- package/src/mapping/maps/trivy.json +13 -0
- package/src/mcp/confine.ts +44 -0
- package/src/mcp/protocol.ts +144 -0
- package/src/mcp/schema.ts +35 -0
- package/src/mcp/server.ts +32 -0
- package/src/mcp/tools.ts +235 -0
- package/src/packs/coverage.ts +98 -0
- package/src/packs/manifest.ts +97 -0
- package/src/packs/validate.ts +204 -0
- package/src/prove/container.ts +237 -0
- package/src/prove/exec-main.ts +16 -0
- package/src/prove/executor.ts +130 -0
- package/src/prove/local.ts +100 -0
- package/src/prove/probes.ts +68 -0
- package/src/prove/recipe.ts +215 -0
- package/src/prove/run.ts +124 -0
- package/src/prove/runner.ts +28 -0
- package/src/prove/safety.ts +98 -0
- package/src/prove/spec.ts +256 -0
- package/src/prove/store.ts +48 -0
- package/src/report/build.ts +304 -0
- package/src/report/entry-points.ts +78 -0
- package/src/report/hunt-section.ts +67 -0
- package/src/report/kit.ts +59 -0
- package/src/report/load.ts +49 -0
- package/src/report/messages.ts +270 -0
- package/src/report/prove-section.ts +61 -0
- package/src/report/report.ts +160 -0
- package/src/report/surface.ts +32 -0
- package/src/report/test-code.ts +56 -0
- package/src/report/tiers.ts +50 -0
- package/src/scanners/common.ts +149 -0
- package/src/scanners/gitleaks.ts +89 -0
- package/src/scanners/index.ts +103 -0
- package/src/scanners/osv-scanner.ts +90 -0
- package/src/scanners/run-targets.ts +35 -0
- package/src/scanners/secret-noise.ts +80 -0
- package/src/scanners/semgrep.ts +139 -0
- package/src/scanners/trivy.ts +66 -0
- package/src/scanners/types.ts +98 -0
- package/src/types/check.ts +52 -0
- package/src/types/codemap.ts +142 -0
- package/src/types/findings.ts +107 -0
- package/src/types/hunt.ts +97 -0
- package/src/types/index.ts +10 -0
- package/src/types/prove.ts +83 -0
- package/src/types/results.ts +15 -0
- package/src/types/scan.ts +63 -0
- package/src/types/stack.ts +61 -0
- package/src/util.ts +59 -0
- package/src/version.ts +2 -0
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
import { cmp } from '../util.js';
|
|
2
|
+
import { Lines } from './lex.js';
|
|
3
|
+
// SQL statements with their start line. Comments are dropped, and ";" inside quotes, dollar quoted bodies and brackets does not split.
|
|
4
|
+
export function sqlStatements(sql) {
|
|
5
|
+
const lines = new Lines(sql);
|
|
6
|
+
const out = [];
|
|
7
|
+
let i = 0;
|
|
8
|
+
let start = 0;
|
|
9
|
+
let depth = 0;
|
|
10
|
+
const n = sql.length;
|
|
11
|
+
let buf = '';
|
|
12
|
+
const flush = (end) => {
|
|
13
|
+
const t = buf.trim();
|
|
14
|
+
if (t)
|
|
15
|
+
out.push({ text: t, line: lines.at(start + (buf.length - buf.trimStart().length)) });
|
|
16
|
+
buf = '';
|
|
17
|
+
start = end;
|
|
18
|
+
};
|
|
19
|
+
while (i < n) {
|
|
20
|
+
const c = sql[i];
|
|
21
|
+
if (c === '-' && sql[i + 1] === '-') {
|
|
22
|
+
while (i < n && sql[i] !== '\n')
|
|
23
|
+
i++;
|
|
24
|
+
continue;
|
|
25
|
+
}
|
|
26
|
+
if (c === '/' && sql[i + 1] === '*') {
|
|
27
|
+
const e = sql.indexOf('*/', i + 2);
|
|
28
|
+
i = e < 0 ? n : e + 2;
|
|
29
|
+
continue;
|
|
30
|
+
}
|
|
31
|
+
if (c === "'") {
|
|
32
|
+
let j = i + 1;
|
|
33
|
+
while (j < n && !(sql[j] === "'" && sql[j + 1] !== "'")) {
|
|
34
|
+
if (sql[j] === "'")
|
|
35
|
+
j++;
|
|
36
|
+
j++;
|
|
37
|
+
}
|
|
38
|
+
buf += sql.slice(i, j + 1);
|
|
39
|
+
i = j + 1;
|
|
40
|
+
continue;
|
|
41
|
+
}
|
|
42
|
+
if (c === '$') {
|
|
43
|
+
const tag = /^\$[A-Za-z_]*\$/.exec(sql.slice(i, i + 40));
|
|
44
|
+
if (tag) {
|
|
45
|
+
const e = sql.indexOf(tag[0], i + tag[0].length);
|
|
46
|
+
const end = e < 0 ? n : e + tag[0].length;
|
|
47
|
+
buf += sql.slice(i, end);
|
|
48
|
+
i = end;
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
if (c === '(')
|
|
53
|
+
depth++;
|
|
54
|
+
else if (c === ')')
|
|
55
|
+
depth--;
|
|
56
|
+
if (c === ';' && depth <= 0) {
|
|
57
|
+
i++;
|
|
58
|
+
flush(i);
|
|
59
|
+
depth = 0;
|
|
60
|
+
continue;
|
|
61
|
+
}
|
|
62
|
+
buf += c;
|
|
63
|
+
i++;
|
|
64
|
+
}
|
|
65
|
+
flush(n);
|
|
66
|
+
return out;
|
|
67
|
+
}
|
|
68
|
+
const IDENT = String.raw `(?:"[^"]+"|[A-Za-z_][\w$]*)`;
|
|
69
|
+
const QNAME = String.raw `(${IDENT}(?:\s*\.\s*${IDENT})?)`;
|
|
70
|
+
const unq = (s) => s.replace(/"/g, '').replace(/\s+/g, '').replace(/^public\./i, '');
|
|
71
|
+
function splitTop(s, sep = ',') {
|
|
72
|
+
const out = [];
|
|
73
|
+
let depth = 0;
|
|
74
|
+
let cur = '';
|
|
75
|
+
for (const c of s) {
|
|
76
|
+
if (c === '(')
|
|
77
|
+
depth++;
|
|
78
|
+
else if (c === ')')
|
|
79
|
+
depth--;
|
|
80
|
+
if (c === sep && depth === 0) {
|
|
81
|
+
out.push(cur);
|
|
82
|
+
cur = '';
|
|
83
|
+
}
|
|
84
|
+
else
|
|
85
|
+
cur += c;
|
|
86
|
+
}
|
|
87
|
+
if (cur.trim())
|
|
88
|
+
out.push(cur);
|
|
89
|
+
return out;
|
|
90
|
+
}
|
|
91
|
+
function parenBody(s, from) {
|
|
92
|
+
const open = s.indexOf('(', from);
|
|
93
|
+
if (open < 0)
|
|
94
|
+
return null;
|
|
95
|
+
let depth = 0;
|
|
96
|
+
for (let i = open; i < s.length; i++) {
|
|
97
|
+
if (s[i] === '(')
|
|
98
|
+
depth++;
|
|
99
|
+
else if (s[i] === ')') {
|
|
100
|
+
depth--;
|
|
101
|
+
if (depth === 0)
|
|
102
|
+
return s.slice(open + 1, i);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
return null;
|
|
106
|
+
}
|
|
107
|
+
export function supabaseMap(repo) {
|
|
108
|
+
const files = repo.ctx.files.filter((f) => /(?:^|\/)supabase\/(?:migrations\/.*|schema|seed)\.sql$|(?:^|\/)migrations\/.*\.sql$|(?:^|\/)schema\.sql$/i.test(f) && !/seed/i.test(f)).sort(cmp);
|
|
109
|
+
if (!files.length)
|
|
110
|
+
return null;
|
|
111
|
+
const tables = new Map();
|
|
112
|
+
const get = (name, file, line) => {
|
|
113
|
+
let t = tables.get(name);
|
|
114
|
+
if (!t) {
|
|
115
|
+
t = { name, file, line, rls_enabled: null, columns: [], policies: [], grants: [] };
|
|
116
|
+
tables.set(name, t);
|
|
117
|
+
}
|
|
118
|
+
return t;
|
|
119
|
+
};
|
|
120
|
+
for (const file of files) {
|
|
121
|
+
for (const st of sqlStatements(repo.ctx.text(file))) {
|
|
122
|
+
const s = st.text;
|
|
123
|
+
let m;
|
|
124
|
+
if ((m = new RegExp(String.raw `^create\s+(?:unlogged\s+)?table\s+(?:if\s+not\s+exists\s+)?${QNAME}`, 'i').exec(s))) {
|
|
125
|
+
const name = unq(m[1]);
|
|
126
|
+
const t = get(name, file, st.line);
|
|
127
|
+
t.file = file;
|
|
128
|
+
t.line = st.line;
|
|
129
|
+
t.policies = [];
|
|
130
|
+
t.grants = [];
|
|
131
|
+
t.rls_enabled = null;
|
|
132
|
+
const body = parenBody(s, m[0].length) ?? '';
|
|
133
|
+
t.columns = splitTop(body).map((c) => c.trim()).filter((c) => c && !/^(?:primary\s+key|foreign\s+key|unique|constraint|check|like|exclude)\b/i.test(c)).map((c) => unq(/^(?:"[^"]+"|[A-Za-z_][\w$]*)/.exec(c)?.[0] ?? '')).filter(Boolean).sort();
|
|
134
|
+
}
|
|
135
|
+
else if ((m = new RegExp(String.raw `^alter\s+table\s+(?:if\s+exists\s+)?(?:only\s+)?${QNAME}\s+(enable|disable|force|no\s+force)\s+row\s+level\s+security`, 'i').exec(s))) {
|
|
136
|
+
const t = get(unq(m[1]), file, st.line);
|
|
137
|
+
const w = m[2].toLowerCase();
|
|
138
|
+
if (w === 'enable')
|
|
139
|
+
t.rls_enabled = true;
|
|
140
|
+
else if (w === 'disable')
|
|
141
|
+
t.rls_enabled = false;
|
|
142
|
+
}
|
|
143
|
+
else if ((m = new RegExp(String.raw `^alter\s+table\s+(?:if\s+exists\s+)?(?:only\s+)?${QNAME}\s+add\s+column\s+(?:if\s+not\s+exists\s+)?(${IDENT})`, 'i').exec(s))) {
|
|
144
|
+
const t = get(unq(m[1]), file, st.line);
|
|
145
|
+
t.columns = [...new Set([...t.columns, unq(m[2])])].sort();
|
|
146
|
+
}
|
|
147
|
+
else if ((m = new RegExp(String.raw `^create\s+policy\s+(${IDENT}|'[^']*')\s+on\s+${QNAME}`, 'i').exec(s))) {
|
|
148
|
+
const t = get(unq(m[2]), file, st.line);
|
|
149
|
+
const rest = s.slice(m[0].length);
|
|
150
|
+
const op = /\bfor\s+(all|select|insert|update|delete)\b/i.exec(rest)?.[1]?.toLowerCase() ?? 'all';
|
|
151
|
+
const rolesText = /\bto\s+([\w",\s]+?)(?=\s+(?:using|with)\b|$)/i.exec(rest)?.[1] ?? 'public';
|
|
152
|
+
const roles = rolesText.split(',').map((r) => r.trim().replace(/"/g, '').toLowerCase()).filter(Boolean);
|
|
153
|
+
const u = /\busing\s*\(/i.exec(rest);
|
|
154
|
+
const c = /\bwith\s+check\s*\(/i.exec(rest);
|
|
155
|
+
t.policies.push({
|
|
156
|
+
name: m[1].replace(/["']/g, ''), op, roles: roles.sort(), using: u ? (parenBody(rest, u.index) ?? '').replace(/\s+/g, ' ').trim() : null,
|
|
157
|
+
check: c ? (parenBody(rest, c.index) ?? '').replace(/\s+/g, ' ').trim() : null, line: st.line, permissive: !/\bas\s+restrictive\b/i.test(rest),
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
else if ((m = new RegExp(String.raw `^drop\s+policy\s+(?:if\s+exists\s+)?(${IDENT}|'[^']*')\s+on\s+${QNAME}`, 'i').exec(s))) {
|
|
161
|
+
const t = tables.get(unq(m[2]));
|
|
162
|
+
if (t)
|
|
163
|
+
t.policies = t.policies.filter((p) => p.name !== m[1].replace(/["']/g, ''));
|
|
164
|
+
}
|
|
165
|
+
else if ((m = new RegExp(String.raw `^drop\s+table\s+(?:if\s+exists\s+)?${QNAME}`, 'i').exec(s))) {
|
|
166
|
+
tables.delete(unq(m[1]));
|
|
167
|
+
}
|
|
168
|
+
else if ((m = /^grant\s+([\s\S]+?)\s+on\s+(?:table\s+)?([\s\S]+?)\s+to\s+([\s\S]+?)(?:\s+with\s+grant\s+option)?$/i.exec(s)) && !/^grant\s+\w+\s+to\s/i.test(s) && !/\bon\s+(?:function|sequence|schema|type|all\s+functions|all\s+sequences)/i.test(s)) {
|
|
169
|
+
const all = /^all\s+tables\s+in\s+schema/i.test(m[2]);
|
|
170
|
+
const targets = all ? ['*'] : splitTop(m[2]).map((x) => unq(x.trim().replace(/^table\s+/i, '')));
|
|
171
|
+
const privs = splitTop(m[1]).map((p) => p.trim()).filter(Boolean).map((p) => {
|
|
172
|
+
const pm = /^(all(?:\s+privileges)?|select|insert|update|delete|truncate|references|trigger)\s*(?:\(([^)]*)\))?$/i.exec(p);
|
|
173
|
+
return pm ? { priv: pm[1].toLowerCase().replace(/\s+privileges/, ''), cols: pm[2] ? pm[2].split(',').map((c) => c.trim().replace(/"/g, '')).sort() : null } : null;
|
|
174
|
+
}).filter((x) => x !== null);
|
|
175
|
+
const roles = splitTop(m[3]).map((r) => r.trim().replace(/"/g, '').toLowerCase());
|
|
176
|
+
for (const tn of targets) {
|
|
177
|
+
const t = get(tn, file, st.line);
|
|
178
|
+
for (const role of roles) {
|
|
179
|
+
for (const p of privs)
|
|
180
|
+
t.grants.push({ role, privileges: [p.priv], columns: p.cols, line: st.line });
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
else if ((m = /^revoke\s+([\s\S]+?)\s+on\s+(?:table\s+)?([\s\S]+?)\s+from\s+([\s\S]+)$/i.exec(s))) {
|
|
185
|
+
const targets = splitTop(m[2]).map((x) => unq(x.trim().replace(/^table\s+/i, '')));
|
|
186
|
+
const roles = splitTop(m[3]).map((r) => r.trim().replace(/"/g, '').toLowerCase());
|
|
187
|
+
const privs = splitTop(m[1]).map((p) => p.trim().toLowerCase().replace(/\s+privileges/, '').replace(/\s*\(.*\)/, ''));
|
|
188
|
+
for (const tn of targets) {
|
|
189
|
+
const t = tables.get(tn);
|
|
190
|
+
if (!t)
|
|
191
|
+
continue;
|
|
192
|
+
t.grants = t.grants.filter((g) => !(roles.includes(g.role) && (privs.includes('all') || g.privileges.some((p) => privs.includes(p)))));
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
// Default privileges and schema wide grants ("*") apply to every table.
|
|
198
|
+
const star = tables.get('*');
|
|
199
|
+
tables.delete('*');
|
|
200
|
+
const list = [...tables.values()].filter((t) => t.name !== '').sort((a, b) => cmp(a.name, b.name));
|
|
201
|
+
if (star)
|
|
202
|
+
for (const t of list)
|
|
203
|
+
t.grants = [...t.grants, ...star.grants.map((g) => ({ ...g }))];
|
|
204
|
+
for (const t of list) {
|
|
205
|
+
t.policies.sort((a, b) => a.line - b.line || cmp(a.name, b.name));
|
|
206
|
+
t.grants.sort((a, b) => cmp(a.role, b.role) || cmp(a.privileges[0], b.privileges[0]) || a.line - b.line);
|
|
207
|
+
}
|
|
208
|
+
return { tables: list };
|
|
209
|
+
}
|
|
210
|
+
const OWNER_VAR = /uid|user_?id|owner|account|author|member|tenant/i;
|
|
211
|
+
export function firebaseMap(repo) {
|
|
212
|
+
const files = repo.ctx.files.filter((f) => /(?:^|\/)[^/]*\.rules$|(?:^|\/)database\.rules\.json$|(?:^|\/)[^/]*\.rules\.json$/.test(f)).sort(cmp);
|
|
213
|
+
if (!files.length)
|
|
214
|
+
return null;
|
|
215
|
+
const rules = [];
|
|
216
|
+
for (const file of files) {
|
|
217
|
+
const text = repo.ctx.text(file);
|
|
218
|
+
if (/\.json$/.test(file)) {
|
|
219
|
+
realtimeRules(file, text, rules);
|
|
220
|
+
continue;
|
|
221
|
+
}
|
|
222
|
+
const service = /storage/i.test(file) || /service\s+firebase\.storage/.test(text) ? 'storage' : 'firestore';
|
|
223
|
+
// Rules files have // and /* */ comments and paths with slashes, which the JavaScript masker would read as regular expressions.
|
|
224
|
+
const masked = text.replace(/\/\/[^\n]*|\/\*[\s\S]*?\*\//g, (c) => c.replace(/[^\n]/g, ' '));
|
|
225
|
+
const lines = new Lines(text);
|
|
226
|
+
const stack = [];
|
|
227
|
+
const re = /\bmatch\s+((?:\/(?:\{[^}]*\}|[^\s{/])*)+)\s*\{|\ballow\s+([\w,\s]+?)\s*(?::\s*if\s+([^;]*?))?\s*;/g;
|
|
228
|
+
for (let m = re.exec(masked); m; m = re.exec(masked)) {
|
|
229
|
+
while (stack.length && m.index >= stack[stack.length - 1].close)
|
|
230
|
+
stack.pop();
|
|
231
|
+
if (m[1] !== undefined) {
|
|
232
|
+
const open = m.index + m[0].length - 1;
|
|
233
|
+
let depth = 0;
|
|
234
|
+
let close = masked.length;
|
|
235
|
+
for (let i = open; i < masked.length; i++) {
|
|
236
|
+
if (masked[i] === '{')
|
|
237
|
+
depth++;
|
|
238
|
+
else if (masked[i] === '}') {
|
|
239
|
+
depth--;
|
|
240
|
+
if (depth === 0) {
|
|
241
|
+
close = i;
|
|
242
|
+
break;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
stack.push({ path: m[1].trim(), close });
|
|
247
|
+
}
|
|
248
|
+
else {
|
|
249
|
+
const ops = m[2].split(',').map((o) => o.trim()).filter(Boolean).sort();
|
|
250
|
+
const cond = (text.slice(m.index, m.index + m[0].length).replace(/^allow\s+[\w,\s]+?\s*(?::\s*if\s+)?/, '').replace(/;$/, '')).replace(/\s+/g, ' ').trim();
|
|
251
|
+
const fullPath = stack.map((s) => s.path).join('').replace(/\/+/g, '/');
|
|
252
|
+
const wild = [...fullPath.matchAll(/\{(\w+)(?:=\*\*)?\}/g)].map((x) => x[1]);
|
|
253
|
+
const uidWild = wild.filter((w) => OWNER_VAR.test(w));
|
|
254
|
+
const ownerBound = new RegExp(`request\\.auth\\.uid\\s*==\\s*(?:${wild.join('|') || '@@'})\\b|\\b(?:${wild.join('|') || '@@'})\\s*==\\s*request\\.auth\\.uid|resource(?:\\.data)?\\.\\w+\\s*==\\s*request\\.auth\\.uid|request\\.resource\\.data\\.\\w+\\s*==\\s*request\\.auth\\.uid|request\\.auth\\.uid\\s+in\\s|\\b(?:isOwner|owns|isUser|isOwnerOf|belongsToUser)\\s*\\(`).test(cond)
|
|
255
|
+
|| (/\b[a-z]\w*\s*\(/i.test(cond) && !/request\.auth\s*!=\s*null|request\.auth\s*==\s*null/.test(cond) && /owner|isUser|uid/i.test(cond));
|
|
256
|
+
const open = cond === '' || /^true$/.test(cond) || /^request\.time\s*<\s*timestamp\.date\(/.test(cond);
|
|
257
|
+
rules.push({ service, file, line: lines.at(m.index), path: fullPath || '/', ops, condition: cond.slice(0, 200), uses_auth: /request\.auth/.test(cond), owner_bound: ownerBound, always_true: open, owner_wildcard: uidWild.length > 0 });
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
return { rules: rules.sort((a, b) => cmp(a.file, b.file) || a.line - b.line || cmp(a.ops.join(), b.ops.join())) };
|
|
262
|
+
}
|
|
263
|
+
function realtimeRules(file, text, out) {
|
|
264
|
+
let json;
|
|
265
|
+
try {
|
|
266
|
+
json = JSON.parse(text);
|
|
267
|
+
}
|
|
268
|
+
catch {
|
|
269
|
+
return;
|
|
270
|
+
}
|
|
271
|
+
const lines = new Lines(text);
|
|
272
|
+
let cursor = 0;
|
|
273
|
+
const walkNode = (node, path) => {
|
|
274
|
+
if (!node || typeof node !== 'object')
|
|
275
|
+
return;
|
|
276
|
+
for (const [k, v] of Object.entries(node)) {
|
|
277
|
+
if ((k === '.read' || k === '.write') && (typeof v === 'string' || typeof v === 'boolean')) {
|
|
278
|
+
const cond = String(v);
|
|
279
|
+
const at = text.indexOf(`"${k}"`, cursor);
|
|
280
|
+
if (at >= 0)
|
|
281
|
+
cursor = at + 1;
|
|
282
|
+
const wild = path.filter((p) => p.startsWith('$'));
|
|
283
|
+
const ownerBound = /\$\w+\s*===?\s*auth\.uid|auth\.uid\s*===?\s*\$\w+|\.val\(\)\s*===?\s*auth\.uid|auth\.uid\s*===?\s*(?:data|newData)/.test(cond);
|
|
284
|
+
out.push({ service: 'realtime', file, line: at >= 0 ? lines.at(at) : 1, path: '/' + path.join('/'), ops: [k.slice(1)], condition: cond.slice(0, 200), uses_auth: /\bauth\b/.test(cond), owner_bound: ownerBound, always_true: cond.trim() === 'true', owner_wildcard: wild.some((w) => OWNER_VAR.test(w)) });
|
|
285
|
+
}
|
|
286
|
+
else if (!k.startsWith('.'))
|
|
287
|
+
walkNode(v, [...path, k]);
|
|
288
|
+
}
|
|
289
|
+
};
|
|
290
|
+
walkNode(json.rules, []);
|
|
291
|
+
}
|
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
import { splitTop, strings } from './cblocks.js';
|
|
2
|
+
import { empty, joinPath, makeEntry, normPath } from './entry.js';
|
|
3
|
+
import { cMethod } from './light-index.js';
|
|
4
|
+
import { analyzeLight } from './light.js';
|
|
5
|
+
import { closeOf, splitArgs } from './lex.js';
|
|
6
|
+
const HTTP = { HttpGet: 'GET', HttpPost: 'POST', HttpPut: 'PUT', HttpDelete: 'DELETE', HttpPatch: 'PATCH', HttpHead: 'HEAD', HttpOptions: 'OPTIONS' };
|
|
7
|
+
const SERVICE_TYPE = /^(?:I[A-Z]\w+|\w*(?:Db|DbContext|Context|Service|Repository|Logger|Options|Factory|Manager|Client|Provider|Handler)|CancellationToken|HttpContext|HttpRequest|HttpResponse|ClaimsPrincipal|IConfiguration|LinkGenerator|TokenService)\b/;
|
|
8
|
+
const AUTH_KINDS = new Set(['session_read', 'role_check']);
|
|
9
|
+
export function dotnetFiles(repo) {
|
|
10
|
+
return repo.filesOf('cs').filter((f) => { const v = repo.view(f); return !!v && (/Controller\b|\[ApiController\]/.test(v.masked) || /\.\s*Map(?:Get|Post|Put|Delete|Patch|Methods|Group)\s*\(/.test(v.masked)); });
|
|
11
|
+
}
|
|
12
|
+
// Attribute lists ([A], [A, B(1)], [A][B]) that stand alone at brace depth 0 of [from, to): the first thing on their line, or right after another list.
|
|
13
|
+
function attributeBlocks(v, from, to) {
|
|
14
|
+
const m = v.masked;
|
|
15
|
+
const out = [];
|
|
16
|
+
let depth = 0;
|
|
17
|
+
let cur = null;
|
|
18
|
+
for (let i = from; i < to; i++) {
|
|
19
|
+
const c = m[i];
|
|
20
|
+
if (c === '{' || c === '(') {
|
|
21
|
+
depth++;
|
|
22
|
+
continue;
|
|
23
|
+
}
|
|
24
|
+
if (c === '}' || c === ')') {
|
|
25
|
+
depth--;
|
|
26
|
+
continue;
|
|
27
|
+
}
|
|
28
|
+
if (c === '[' && depth === 0) {
|
|
29
|
+
const lineStart = m.lastIndexOf('\n', i) + 1;
|
|
30
|
+
const first = /^[ \t]*$/.test(m.slice(Math.max(lineStart, from), i));
|
|
31
|
+
const chained = cur !== null && /^\s*$/.test(m.slice(cur.end, i));
|
|
32
|
+
if (!first && !chained) {
|
|
33
|
+
const e = closeOf(m, i);
|
|
34
|
+
if (e > 0)
|
|
35
|
+
i = e - 1;
|
|
36
|
+
continue;
|
|
37
|
+
}
|
|
38
|
+
const close = closeOf(m, i);
|
|
39
|
+
if (close < 0)
|
|
40
|
+
continue;
|
|
41
|
+
const attrs = splitTop(v.text.slice(i + 1, close - 1)).map((t) => {
|
|
42
|
+
const nm = /^(?:\w+\s*:\s*)?([\w.]+)\s*(?:\(([\s\S]*)\))?$/.exec(t);
|
|
43
|
+
const at = i + 1 + v.text.slice(i + 1, close - 1).indexOf(t);
|
|
44
|
+
return { name: (nm?.[1] ?? t).replace(/^.*\./, '').replace(/Attribute$/, ''), start: at, end: at + t.length, args: nm?.[2] ?? '' };
|
|
45
|
+
});
|
|
46
|
+
if (chained && cur) {
|
|
47
|
+
cur.attrs.push(...attrs);
|
|
48
|
+
cur.end = close;
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
cur = { attrs, start: i, end: close, declStart: 0 };
|
|
52
|
+
out.push(cur);
|
|
53
|
+
}
|
|
54
|
+
i = close - 1;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
for (const b of out) {
|
|
58
|
+
let d = b.end;
|
|
59
|
+
while (/\s/.test(m[d] ?? ''))
|
|
60
|
+
d++;
|
|
61
|
+
b.declStart = d;
|
|
62
|
+
}
|
|
63
|
+
return out;
|
|
64
|
+
}
|
|
65
|
+
const tokens = (route, controller, action) => route.replace(/\[controller\]/gi, controller.toLowerCase()).replace(/\[action\]/gi, action.toLowerCase()).replace(/\[area\]/gi, '');
|
|
66
|
+
const cleanPath = (p) => normPath(p.replace(/\{(\w+)\?\}/g, '{$1}').replace(/\{\*{1,2}(\w+)\}/g, '{$1}'));
|
|
67
|
+
function seedsOf(paramsText) {
|
|
68
|
+
const seeds = [];
|
|
69
|
+
for (const part of splitTop(paramsText)) {
|
|
70
|
+
if (/\[FromServices\]/.test(part))
|
|
71
|
+
continue;
|
|
72
|
+
const bare = part.replace(/\[[^\]]*\]\s*/g, '').replace(/\s*=\s*.*$/, '').trim();
|
|
73
|
+
const mm = /^(?:this\s+)?(?:ref\s+|out\s+|in\s+)?([\w.<>?,\[\]]+)\s+(\w+)$/.exec(bare);
|
|
74
|
+
if (!mm || SERVICE_TYPE.test(mm[1]))
|
|
75
|
+
continue;
|
|
76
|
+
seeds.push(mm[2]);
|
|
77
|
+
}
|
|
78
|
+
return seeds;
|
|
79
|
+
}
|
|
80
|
+
export function extractDotnet(repo, fn, local) {
|
|
81
|
+
const out = empty();
|
|
82
|
+
// A fallback policy that needs a sign in, set anywhere in the project, covers every endpoint that is not [AllowAnonymous].
|
|
83
|
+
let fallback = null;
|
|
84
|
+
let controllersAuthorized = null;
|
|
85
|
+
for (const file of repo.filesOf('cs')) {
|
|
86
|
+
const v = repo.view(file);
|
|
87
|
+
if (!v)
|
|
88
|
+
continue;
|
|
89
|
+
const fb = /\bFallbackPolicy\s*=/.exec(v.masked);
|
|
90
|
+
if (fb && !fallback)
|
|
91
|
+
fallback = { kind: 'session_read', scope: 'global', file, line: v.lines.at(fb.index), via: 'FallbackPolicy requires an authenticated user', enforced: true, confidence: 'high' };
|
|
92
|
+
const mc = /\.\s*MapControllers\s*\(\s*\)\s*\.\s*RequireAuthorization\b/.exec(v.masked);
|
|
93
|
+
if (mc && !controllersAuthorized)
|
|
94
|
+
controllersAuthorized = { kind: 'session_read', scope: 'router', file, line: v.lines.at(mc.index), via: 'MapControllers().RequireAuthorization()', enforced: true, confidence: 'high' };
|
|
95
|
+
}
|
|
96
|
+
const analyse = (v, start, end, seeds, decos, extra, anonymous) => {
|
|
97
|
+
const a = analyzeLight(v, { start, end, seeds, decorators: decos, extra, lookup: fn.find, local });
|
|
98
|
+
if (anonymous)
|
|
99
|
+
a.protections = a.protections.filter((p) => !(AUTH_KINDS.has(p.kind) && (p.scope === 'router' || p.scope === 'wrapper' || p.scope === 'global' || p.scope === 'middleware')));
|
|
100
|
+
return a;
|
|
101
|
+
};
|
|
102
|
+
for (const file of dotnetFiles(repo)) {
|
|
103
|
+
const v = repo.view(file);
|
|
104
|
+
if (!v)
|
|
105
|
+
continue;
|
|
106
|
+
const m = v.masked;
|
|
107
|
+
// ---- controllers (a class is at the top of the file, or inside a block scoped namespace)
|
|
108
|
+
const ranges = [[0, m.length]];
|
|
109
|
+
for (const ns of m.matchAll(/\bnamespace\s+[\w.]+\s*\{/g)) {
|
|
110
|
+
const open = ns.index + ns[0].length - 1;
|
|
111
|
+
const close = closeOf(m, open);
|
|
112
|
+
if (close > 0)
|
|
113
|
+
ranges.push([open + 1, close - 1]);
|
|
114
|
+
}
|
|
115
|
+
for (const cls of ranges.flatMap(([a, b]) => attributeBlocks(v, a, b))) {
|
|
116
|
+
const decl = /^(?:(?:public|internal|abstract|sealed|partial)\s+)*class\s+(\w+)\s*(?:\((?:[^()]|\([^()]*\))*\))?\s*(?::\s*([^{]+?))?\s*\{/.exec(m.slice(cls.declStart, cls.declStart + 500));
|
|
117
|
+
if (!decl)
|
|
118
|
+
continue;
|
|
119
|
+
const names = cls.attrs.map((a) => a.name);
|
|
120
|
+
if (!(names.includes('ApiController') || names.includes('Controller') || /\b(?:ControllerBase|Controller)\b/.test(decl[2] ?? '') || /\wController$/.test(decl[1])))
|
|
121
|
+
continue;
|
|
122
|
+
const className = decl[1];
|
|
123
|
+
const controller = className.replace(/Controller$/, '');
|
|
124
|
+
const bodyOpen = cls.declStart + decl[0].length - 1;
|
|
125
|
+
const bodyClose = closeOf(m, bodyOpen);
|
|
126
|
+
if (bodyClose < 0)
|
|
127
|
+
continue;
|
|
128
|
+
const classRoute = cls.attrs.find((a) => a.name === 'Route');
|
|
129
|
+
const classTemplate = classRoute ? strings(classRoute.args)[0] ?? '' : '';
|
|
130
|
+
const classAnon = names.includes('AllowAnonymous');
|
|
131
|
+
const classDeco = { view: v, start: cls.start, end: cls.end, scope: 'router' };
|
|
132
|
+
for (const blk of attributeBlocks(v, bodyOpen + 1, bodyClose - 1)) {
|
|
133
|
+
const verbs = blk.attrs.filter((a) => HTTP[a.name] || a.name === 'AcceptVerbs');
|
|
134
|
+
const routeAttr = blk.attrs.find((a) => a.name === 'Route');
|
|
135
|
+
if (!verbs.length && !routeAttr)
|
|
136
|
+
continue;
|
|
137
|
+
const nameAt = /(\w+)\s*(?:<[^>]*>)?\s*\(/.exec(m.slice(blk.declStart, blk.declStart + 400));
|
|
138
|
+
if (!nameAt)
|
|
139
|
+
continue;
|
|
140
|
+
const f = cMethod(m, blk.declStart + nameAt.index);
|
|
141
|
+
if (!f)
|
|
142
|
+
continue;
|
|
143
|
+
const action = nameAt[1];
|
|
144
|
+
const anonymous = classAnon || blk.attrs.some((a) => a.name === 'AllowAnonymous');
|
|
145
|
+
const decos = [{ view: v, start: blk.start, end: blk.end, scope: 'wrapper' }];
|
|
146
|
+
if (cls.attrs.length)
|
|
147
|
+
decos.push(classDeco);
|
|
148
|
+
const extra = anonymous ? [] : [fallback, controllersAuthorized].filter((x) => !!x);
|
|
149
|
+
const seeds = seedsOf(f.params);
|
|
150
|
+
const methodList = verbs.length ? verbs.flatMap((a) => (HTTP[a.name] ? [{ verb: HTTP[a.name], attr: a }] : [...a.args.matchAll(/"([A-Za-z]+)"/g)].map((x) => ({ verb: x[1].toUpperCase(), attr: a })))) : [{ verb: 'ANY', attr: routeAttr }];
|
|
151
|
+
for (const { verb, attr } of methodList) {
|
|
152
|
+
const own = strings(attr.args)[0];
|
|
153
|
+
const template = own === undefined && routeAttr && attr !== routeAttr ? strings(routeAttr.args)[0] : own;
|
|
154
|
+
let full;
|
|
155
|
+
const tpl = template === undefined ? undefined : tokens(template, controller, action);
|
|
156
|
+
const ctpl = tokens(classTemplate, controller, action);
|
|
157
|
+
if (tpl !== undefined && /^~?\//.test(tpl))
|
|
158
|
+
full = tpl.replace(/^~/, '');
|
|
159
|
+
else
|
|
160
|
+
full = joinPath(ctpl ? '/' + ctpl.replace(/^\//, '') : '', tpl ?? '') || '/';
|
|
161
|
+
const path = cleanPath(full);
|
|
162
|
+
const an = analyse(v, blk.declStart, f.end, seeds, decos, extra, anonymous);
|
|
163
|
+
out.entries.push(makeEntry(repo, {
|
|
164
|
+
kind: 'http_route', framework: 'aspnet', file, line: v.lines.at(attr.start), method: verb, path: path.startsWith('/') ? path : '/' + path, name: `${className}.${action}`,
|
|
165
|
+
confidence: classTemplate || template !== undefined ? 'high' : 'medium', handlerView: v, spec: { start: blk.declStart, end: f.end, seeds }, analysis: an,
|
|
166
|
+
}));
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
// ---- minimal APIs
|
|
171
|
+
const groups = new Map();
|
|
172
|
+
for (const g of m.matchAll(/\b(?:var|RouteGroupBuilder)\s+(\w+)\s*=\s*(\w+)\s*\.\s*MapGroup\s*\(/g)) {
|
|
173
|
+
const open = g.index + g[0].length - 1;
|
|
174
|
+
const close = closeOf(m, open);
|
|
175
|
+
if (close < 0)
|
|
176
|
+
continue;
|
|
177
|
+
const semi = m.indexOf(';', close);
|
|
178
|
+
const parent = groups.get(g[2]);
|
|
179
|
+
const lit = strings(v.text.slice(open, close))[0] ?? '';
|
|
180
|
+
const chainText = m.slice(close, semi < 0 ? close : semi);
|
|
181
|
+
groups.set(g[1], { prefix: joinPath(parent?.prefix ?? '', lit), chain: { start: close, end: semi < 0 ? close : semi, view: v }, anonymous: (parent?.anonymous ?? false) || /\.\s*AllowAnonymous\s*\(/.test(chainText) });
|
|
182
|
+
}
|
|
183
|
+
for (const mp of m.matchAll(/\b(\w+)\s*\.\s*Map(Get|Post|Put|Delete|Patch|Methods)\s*\(/g)) {
|
|
184
|
+
const recv = mp[1];
|
|
185
|
+
const grp = groups.get(recv);
|
|
186
|
+
if (!grp && recv !== 'app' && recv !== 'endpoints' && recv !== 'builder')
|
|
187
|
+
continue;
|
|
188
|
+
const open = mp.index + mp[0].length - 1;
|
|
189
|
+
const close = closeOf(m, open);
|
|
190
|
+
if (close < 0)
|
|
191
|
+
continue;
|
|
192
|
+
const args = splitArgs(m, open);
|
|
193
|
+
if (!args.length)
|
|
194
|
+
continue;
|
|
195
|
+
const pathLit = strings(v.text.slice(args[0].start, args[0].end))[0];
|
|
196
|
+
if (pathLit === undefined)
|
|
197
|
+
continue;
|
|
198
|
+
const semi = m.indexOf(';', close);
|
|
199
|
+
const chain = { start: close, end: semi < 0 ? close : semi };
|
|
200
|
+
const chainText = m.slice(chain.start, chain.end);
|
|
201
|
+
const anonymous = /\.\s*AllowAnonymous\s*\(/.test(chainText) || !!grp?.anonymous;
|
|
202
|
+
const verbs = mp[2] === 'Methods' ? [...v.text.slice(args[1]?.start ?? 0, args[1]?.end ?? 0).matchAll(/"([A-Za-z]+)"/g)].map((x) => x[1].toUpperCase()) : [mp[2].toUpperCase()];
|
|
203
|
+
const handler = args[args.length - 1];
|
|
204
|
+
if (!handler || args.length < 2)
|
|
205
|
+
continue;
|
|
206
|
+
const htext = m.slice(handler.start, handler.end);
|
|
207
|
+
let seeds = [];
|
|
208
|
+
let start = handler.start;
|
|
209
|
+
let end = handler.end;
|
|
210
|
+
let hv = v;
|
|
211
|
+
const lam = /^(?:static\s+)?(?:async\s+)?\(([^)]*)\)\s*=>/.exec(htext) ?? /^(?:static\s+)?(?:async\s+)?(\w+)\s*=>/.exec(htext);
|
|
212
|
+
if (lam)
|
|
213
|
+
seeds = seedsOf(v.text.slice(handler.start + htext.indexOf(lam[1]), handler.start + htext.indexOf(lam[1]) + lam[1].length));
|
|
214
|
+
else {
|
|
215
|
+
const nm = /(\w+)\s*$/.exec(htext)?.[1];
|
|
216
|
+
const ref = nm ? fn.find(nm, file) : null;
|
|
217
|
+
if (ref) {
|
|
218
|
+
hv = ref.view;
|
|
219
|
+
start = ref.start;
|
|
220
|
+
end = ref.end;
|
|
221
|
+
const op = ref.view.masked.indexOf('(', ref.start);
|
|
222
|
+
const pc = op >= 0 ? closeOf(ref.view.masked, op) : -1;
|
|
223
|
+
if (pc > 0)
|
|
224
|
+
seeds = seedsOf(ref.view.text.slice(op + 1, pc - 1));
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
const decos = [{ view: v, start: chain.start, end: chain.end, scope: 'wrapper' }];
|
|
228
|
+
if (grp?.chain)
|
|
229
|
+
decos.push({ view: grp.chain.view, start: grp.chain.start, end: grp.chain.end, scope: 'router' });
|
|
230
|
+
const extra = anonymous ? [] : [fallback].filter((x) => !!x);
|
|
231
|
+
for (const verb of verbs) {
|
|
232
|
+
const path = cleanPath(joinPath(grp?.prefix ?? '', pathLit));
|
|
233
|
+
const an = analyse(hv, start, end, seeds, decos, extra, anonymous);
|
|
234
|
+
out.entries.push(makeEntry(repo, {
|
|
235
|
+
kind: 'http_route', framework: 'aspnet', file, line: v.lines.at(mp.index), method: verb, path, name: null,
|
|
236
|
+
confidence: 'high', handlerView: hv, spec: { start, end, seeds }, analysis: an,
|
|
237
|
+
}));
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
return out;
|
|
242
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { testCodeReason } from '../report/test-code.js';
|
|
2
|
+
import { analyze } from './analyze.js';
|
|
3
|
+
import { ADMIN_PATH, AUTH_ENDPOINT, PUBLIC_PATH } from './vocab.js';
|
|
4
|
+
const STATE_METHODS = new Set(['POST', 'PUT', 'PATCH', 'DELETE']);
|
|
5
|
+
const WEBHOOK_HEADER = /[xX]-[\w-]*[sS]ignature|stripe-signature|paystack-signature|svix-signature|webhook-signature|HTTP_X_\w*SIGNATURE|x-hub-signature/i;
|
|
6
|
+
// Path text with every parameter style written :name, and catch-alls written *name.
|
|
7
|
+
export function normPath(p) {
|
|
8
|
+
return p
|
|
9
|
+
.replace(/\[\[?\.\.\.(\w+)\]?\]/g, '*$1').replace(/\[(\w+)\]/g, ':$1')
|
|
10
|
+
.replace(/<(?:[\w.]+:)?(\w+)>/g, ':$1').replace(/\{(\w+)(?::[^}]*)?\}/g, ':$1')
|
|
11
|
+
.replace(/\(\?P<(\w+)>[^)]*\)/g, ':$1')
|
|
12
|
+
.replace(/\/{2,}/g, '/');
|
|
13
|
+
}
|
|
14
|
+
export function joinPath(prefix, path) {
|
|
15
|
+
const j = `${prefix.replace(/\/+$/, '')}/${path.replace(/^\/+/, '')}`;
|
|
16
|
+
return normPath(j === '/' ? '/' : j.replace(/\/+$/, '') || '/');
|
|
17
|
+
}
|
|
18
|
+
export function makeEntry(repo, s) {
|
|
19
|
+
const hv = s.handlerView;
|
|
20
|
+
const a = s.analysis ?? analyze(hv, s.spec, repo.helpers(hv.lang));
|
|
21
|
+
const pathText = `${s.path ?? ''} ${s.name ?? ''}`;
|
|
22
|
+
const handlerText = hv.text.slice(s.spec.start, s.spec.end);
|
|
23
|
+
const hasSig = a.protections.some((p) => p.kind === 'signature_verify' && p.confidence === 'high' && /signature verify|hmac|svix/.test(p.via));
|
|
24
|
+
const webhook = s.kind === 'webhook' || (s.kind === 'http_route' && (/webhook/i.test(pathText) || /(^|\/)webhooks?(\/|\.|$)/i.test(s.file) || WEBHOOK_HEADER.test(handlerText) || hasSig && /hook/i.test(`${s.file} ${pathText}`)));
|
|
25
|
+
const kind = webhook ? 'webhook' : s.kind;
|
|
26
|
+
const method = s.method;
|
|
27
|
+
const tags = new Set(s.tags ?? []);
|
|
28
|
+
if (webhook)
|
|
29
|
+
tags.add('webhook');
|
|
30
|
+
const nameIsHandler = s.path === null || ['flask', 'fastapi', 'django', 'nest', 'quart', 'sanic', 'next-app', 'next-pages'].includes(s.framework);
|
|
31
|
+
if (AUTH_ENDPOINT.test(s.path ?? '') || (nameIsHandler && s.framework !== 'next-app' && AUTH_ENDPOINT.test((s.name ?? '').replace(/([a-z])([A-Z])/g, '$1_$2'))))
|
|
32
|
+
tags.add('auth_endpoint');
|
|
33
|
+
if (ADMIN_PATH.test(s.path ?? '') || ADMIN_PATH.test(s.file))
|
|
34
|
+
tags.add('admin');
|
|
35
|
+
if (PUBLIC_PATH.test(s.path ?? ''))
|
|
36
|
+
tags.add('public');
|
|
37
|
+
if (a.reads.some((r) => r.source === 'file'))
|
|
38
|
+
tags.add('upload');
|
|
39
|
+
const writes = a.data.some((d) => d.op === 'write' || d.op === 'delete');
|
|
40
|
+
if (kind === 'server_action' || (method && STATE_METHODS.has(method)) || ((method === 'ANY' || method === null) && (writes || a.reads.some((r) => r.source === 'body' || r.source === 'form')) && (kind === 'http_route' || kind === 'webhook')))
|
|
41
|
+
tags.add('state_changing');
|
|
42
|
+
const clientPage = s.kind === 'page' && /^\s*(?:\/\/[^\n]*\n\s*)*['"]use client['"]/.test(hv.text.slice(0, 400));
|
|
43
|
+
if (clientPage)
|
|
44
|
+
tags.add('client_component');
|
|
45
|
+
const handler = hv.file === s.file && s.spec.start >= 0 && hv.lines.at(s.spec.start) === s.line ? null : { file: hv.file, line: hv.lines.at(s.spec.start) };
|
|
46
|
+
return {
|
|
47
|
+
id: `${s.file}:${s.line}:${kind}:${method ?? '-'}:${s.path ?? '-'}:${s.name ?? '-'}`,
|
|
48
|
+
kind, framework: s.framework, file: s.file, line: s.line,
|
|
49
|
+
end_line: hv.lines.at(Math.max(s.spec.start, s.spec.end - 1)),
|
|
50
|
+
method, path: s.path, name: s.name, handler, confidence: s.confidence,
|
|
51
|
+
test_code: testCodeReason(s.file) !== null, tags: [...tags].sort(),
|
|
52
|
+
protections: clientPage ? [] : a.protections, reads: clientPage ? [] : a.reads, data: clientPage ? [] : a.data, outbound: clientPage ? [] : a.outbound,
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
// Parameter names of the function whose "(" is at `open` in masked text: plain names and the names inside destructuring.
|
|
56
|
+
export function paramNames(masked, open) {
|
|
57
|
+
let depth = 0;
|
|
58
|
+
let end = open;
|
|
59
|
+
for (let i = open; i < masked.length; i++) {
|
|
60
|
+
const c = masked[i];
|
|
61
|
+
if (c === '(')
|
|
62
|
+
depth++;
|
|
63
|
+
else if (c === ')') {
|
|
64
|
+
depth--;
|
|
65
|
+
if (depth === 0) {
|
|
66
|
+
end = i;
|
|
67
|
+
break;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
const inner = masked.slice(open + 1, end).replace(/:[^,={}()]*(?:<[^>]*>)?/g, ' ').replace(/=[^,}]*/g, ' ');
|
|
72
|
+
const out = [];
|
|
73
|
+
for (const m of inner.matchAll(/(?<![.\w$@])([A-Za-z_$][\w$]*)/g))
|
|
74
|
+
if (!['async', 'this', 'self', 'cls'].includes(m[1]))
|
|
75
|
+
out.push(m[1]);
|
|
76
|
+
return out;
|
|
77
|
+
}
|
|
78
|
+
export const empty = () => ({ entries: [], gates: [], mounts: [] });
|