@dataparade/cli 0.0.35 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -0
- package/dist/package.json +4 -4
- package/dist/src/ai-enrichment/pii-signal-rules.d.ts +5 -0
- package/dist/src/ai-enrichment/pii-signal-rules.js +2 -3
- package/dist/src/ai-enrichment/third-party-data-flow.js +5 -4
- package/dist/src/cli.js +9 -3
- package/dist/src/facts/build-facts.d.ts +2 -0
- package/dist/src/facts/build-facts.js +14 -0
- package/dist/src/facts/layout.d.ts +4 -0
- package/dist/src/facts/layout.js +5 -1
- package/dist/src/facts/scan-stamps.d.ts +7 -0
- package/dist/src/facts/scan-stamps.js +30 -0
- package/dist/src/facts/schema.d.ts +74 -0
- package/dist/src/facts/schema.js +32 -1
- package/dist/src/output/scan-output-excludes.js +2 -0
- package/dist/src/output/write-scan-knowledge.d.ts +5 -1
- package/dist/src/output/write-scan-knowledge.js +21 -0
- package/dist/src/threat-model/build-threat-model.d.ts +3 -0
- package/dist/src/threat-model/build-threat-model.js +71 -0
- package/dist/src/threat-model/carry-forward-person-set-statuses.d.ts +8 -0
- package/dist/src/threat-model/carry-forward-person-set-statuses.js +87 -0
- package/dist/src/threat-model/io.d.ts +19 -0
- package/dist/src/threat-model/io.js +63 -0
- package/dist/src/threat-model/schema.d.ts +214 -0
- package/dist/src/threat-model/schema.js +75 -0
- package/dist/src/ui/scan-progress-renderer.d.ts +8 -1
- package/dist/src/ui/scan-progress-renderer.js +20 -1
- package/dist/src/upload/collect-scan-sidecars.d.ts +2 -1
- package/dist/src/upload/collect-scan-sidecars.js +3 -1
- package/dist/tests/cli/cli.spec.js +10 -1
- package/dist/tests/helpers/facts-fixture.js +16 -16
- package/dist/tests/schema/scan-result.schema.spec.js +1 -1
- package/dist/tests/unit/ai-enrichment/third-party-data-flow.spec.js +5 -5
- package/dist/tests/unit/core/graph-mapping.spec.js +4 -3
- package/dist/tests/unit/discoveries/write-ocsf-discoveries-long-ids.spec.js +3 -3
- package/dist/tests/unit/export/export-ocsf.spec.js +4 -4
- package/dist/tests/unit/facts/apply-answers.spec.js +20 -20
- package/dist/tests/unit/facts/build-facts.spec.js +126 -45
- package/dist/tests/unit/facts/identity.spec.js +13 -0
- package/dist/tests/unit/facts/scan-stamps.spec.js +32 -0
- package/dist/tests/unit/facts/slug.spec.js +2 -0
- package/dist/tests/unit/kb/answer-fingerprint.spec.js +9 -9
- package/dist/tests/unit/kb/kb-interview-loop.spec.js +3 -3
- package/dist/tests/unit/output/scan-output-excludes.spec.js +1 -1
- package/dist/tests/unit/threat-model/build-threat-model.spec.d.ts +1 -0
- package/dist/tests/unit/threat-model/build-threat-model.spec.js +184 -0
- package/dist/tests/unit/threat-model/carry-forward-person-set-statuses.spec.d.ts +1 -0
- package/dist/tests/unit/threat-model/carry-forward-person-set-statuses.spec.js +163 -0
- package/dist/tests/unit/threat-model/io.spec.d.ts +1 -0
- package/dist/tests/unit/threat-model/io.spec.js +103 -0
- package/dist/tests/unit/threat-model/schema.spec.d.ts +1 -0
- package/dist/tests/unit/threat-model/schema.spec.js +42 -0
- package/dist/tests/unit/ui/scan-progress-renderer.spec.js +58 -0
- package/dist/tests/unit/upload/collect-scan-sidecars.spec.js +8 -5
- package/dist/vendor/scanner/dist/src/analyze/code-navigator.d.ts +1 -1
- package/dist/vendor/scanner/dist/src/analyze/code-navigator.js +4 -0
- package/dist/vendor/scanner/dist/src/analyze/column-catalog.d.ts +1 -1
- package/dist/vendor/scanner/dist/src/analyze/column-catalog.js +2 -2
- package/dist/vendor/scanner/dist/src/analyze/concept-profile.d.ts +2 -2
- package/dist/vendor/scanner/dist/src/analyze/concept-profile.js +5 -3
- package/dist/vendor/scanner/dist/src/core/pipeline/build-scan-personal-data-layers.js +1 -1
- package/dist/vendor/scanner/dist/src/core/pipeline/deterministic-scan.js +43 -0
- package/dist/vendor/scanner/dist/src/core/pipeline/graph-mapping.js +23 -2
- package/dist/vendor/scanner/dist/src/core/pipeline/mixed-app-terraform-scan.js +1 -0
- package/dist/vendor/scanner/dist/src/core/pipeline/orchestrator-result.d.ts +2 -2
- package/dist/vendor/scanner/dist/src/core/pipeline/terraform-minimal-services.js +1 -0
- package/dist/vendor/scanner/dist/src/core/schema/scan-result.schema.d.ts +160 -2
- package/dist/vendor/scanner/dist/src/core/schema/scan-result.schema.js +24 -0
- package/dist/vendor/scanner/dist/src/core/types/data-flow.d.ts +48 -0
- package/dist/vendor/scanner/dist/src/core/types/data-flow.js +53 -0
- package/dist/vendor/scanner/dist/src/data-flow/detect.js +1 -0
- package/dist/vendor/scanner/dist/src/data-flow/ensure-hub-linked-assets.js +1 -0
- package/dist/vendor/scanner/dist/src/data-flow/ensure-manifest-declared-flows.js +1 -0
- package/dist/vendor/scanner/dist/src/data-flow/flow-authentication.d.ts +30 -0
- package/dist/vendor/scanner/dist/src/data-flow/flow-authentication.js +277 -0
- package/dist/vendor/scanner/dist/src/data-flow/flow-builder.js +6 -0
- package/dist/vendor/scanner/dist/src/data-flow/flow-controls.d.ts +24 -0
- package/dist/vendor/scanner/dist/src/data-flow/flow-controls.js +238 -0
- package/dist/vendor/scanner/dist/src/data-flow/flow-data-categories.d.ts +22 -0
- package/dist/vendor/scanner/dist/src/data-flow/flow-data-categories.js +69 -0
- package/dist/vendor/scanner/dist/src/data-flow/flow-interaction-description.d.ts +13 -0
- package/dist/vendor/scanner/dist/src/data-flow/flow-interaction-description.js +263 -0
- package/dist/vendor/scanner/dist/src/data-flow/hub-link-policy.d.ts +2 -1
- package/dist/vendor/scanner/dist/src/data-flow/hub-link-policy.js +3 -0
- package/dist/vendor/scanner/dist/src/data-flow/human-interaction-channels.d.ts +37 -0
- package/dist/vendor/scanner/dist/src/data-flow/human-interaction-channels.js +483 -0
- package/dist/vendor/scanner/dist/src/data-flow/intra-component-lineage.d.ts +3 -0
- package/dist/vendor/scanner/dist/src/data-flow/intra-component-lineage.js +12 -17
- package/dist/vendor/scanner/dist/src/data-flow/terraform-flows.js +5 -1
- package/dist/vendor/scanner/dist/src/data-flow/transformation-patterns.d.ts +5 -1
- package/dist/vendor/scanner/dist/src/data-flow/transformation-patterns.js +17 -37
- package/dist/vendor/scanner/dist/src/eval/canonical/concept-map.d.ts +36 -5
- package/dist/vendor/scanner/dist/src/eval/canonical/concept-map.js +53 -60
- package/dist/vendor/scanner/dist/src/eval/canonical/graph/endpoints.js +2 -9
- package/dist/vendor/scanner/dist/src/eval/canonical/scanner/personal-data.js +10 -7
- package/dist/vendor/scanner/dist/src/graph/build-dataparade-graph.js +3 -1
- package/dist/vendor/scanner/dist/src/index.d.ts +6 -0
- package/dist/vendor/scanner/dist/src/index.js +16 -1
- package/dist/vendor/scanner/dist/src/patterns/engine.js +1 -0
- package/dist/vendor/scanner/dist/src/pii-signals/match-pii-signals.d.ts +1 -1
- package/dist/vendor/scanner/dist/src/pii-signals/match-pii-signals.js +2 -2
- package/dist/vendor/scanner/dist/src/pii-signals/occurrence-group.d.ts +3 -3
- package/dist/vendor/scanner/dist/src/pii-signals/occurrence-group.js +12 -11
- package/dist/vendor/scanner/dist/src/pii-signals/personal-data-taxonomy.d.ts +44 -0
- package/dist/vendor/scanner/dist/src/pii-signals/personal-data-taxonomy.js +167 -0
- package/dist/vendor/scanner/dist/src/pii-signals/pii-signal-aliases.js +43 -43
- package/dist/vendor/scanner/dist/src/pii-signals/pii-signal-rules.d.ts +12 -1
- package/dist/vendor/scanner/dist/src/pii-signals/pii-signal-rules.js +28 -7
- package/dist/vendor/scanner/dist/src/pii-signals/signal-token.js +1 -1
- package/dist/vendor/scanner/dist/src/threats/stride.d.ts +98 -0
- package/dist/vendor/scanner/dist/src/threats/stride.js +179 -0
- package/dist/vendor/scanner/package.json +2 -2
- package/dist/vendor/scanner/patterns/concept-profiles.yaml +2 -2
- package/dist/vendor/scanner/patterns/flow-authentication.rules.yaml +75 -0
- package/dist/vendor/scanner/patterns/flow-controls.rules.yaml +68 -0
- package/dist/vendor/scanner/patterns/personal-data-taxonomy.yaml +155 -0
- package/dist/vendor/scanner/patterns/pii-signals.rules.yaml +38 -49
- package/dist/vendor/scanner/patterns/stride.rules.yaml +86 -0
- package/package.json +4 -4
- package/dist/vendor/scanner/dist/src/eval/canonical/concept-map.validate.d.ts +0 -17
- package/dist/vendor/scanner/dist/src/eval/canonical/concept-map.validate.js +0 -108
- package/dist/vendor/scanner/patterns/personal-data-concept-map.yaml +0 -76
|
@@ -1,2 +1,55 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.HUMAN_INTERACTION_CHANNELS = exports.FLOW_TRANSPORTS = exports.FLOW_PROTOCOLS = exports.FLOW_RELATIONSHIPS = void 0;
|
|
4
|
+
exports.FLOW_RELATIONSHIPS = [
|
|
5
|
+
"invokes",
|
|
6
|
+
"queries",
|
|
7
|
+
"publishes",
|
|
8
|
+
"consumes",
|
|
9
|
+
"interacts",
|
|
10
|
+
"authenticates_via",
|
|
11
|
+
"exposes",
|
|
12
|
+
"uses_in_process",
|
|
13
|
+
"depends_on",
|
|
14
|
+
"provisions",
|
|
15
|
+
];
|
|
16
|
+
exports.FLOW_PROTOCOLS = [
|
|
17
|
+
"rest",
|
|
18
|
+
"graphql",
|
|
19
|
+
"grpc",
|
|
20
|
+
"soap",
|
|
21
|
+
"json_rpc",
|
|
22
|
+
"mcp",
|
|
23
|
+
"sql",
|
|
24
|
+
"mongodb_wire",
|
|
25
|
+
"redis",
|
|
26
|
+
"kafka",
|
|
27
|
+
"amqp",
|
|
28
|
+
"mqtt",
|
|
29
|
+
"smtp",
|
|
30
|
+
"oauth_oidc",
|
|
31
|
+
"otlp",
|
|
32
|
+
"vendor_sdk",
|
|
33
|
+
"unknown",
|
|
34
|
+
];
|
|
35
|
+
exports.FLOW_TRANSPORTS = [
|
|
36
|
+
"https",
|
|
37
|
+
"http",
|
|
38
|
+
"websocket",
|
|
39
|
+
"http2",
|
|
40
|
+
"tcp_tls",
|
|
41
|
+
"tcp",
|
|
42
|
+
"stdio",
|
|
43
|
+
"in_process",
|
|
44
|
+
"unknown",
|
|
45
|
+
];
|
|
46
|
+
exports.HUMAN_INTERACTION_CHANNELS = [
|
|
47
|
+
"web_gui",
|
|
48
|
+
"mobile_app",
|
|
49
|
+
"desktop_app",
|
|
50
|
+
"cli",
|
|
51
|
+
"mcp",
|
|
52
|
+
"api_direct",
|
|
53
|
+
"email",
|
|
54
|
+
"unknown",
|
|
55
|
+
];
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { DetectedDataFlow, FlowAuthentication } from "../core/types/data-flow";
|
|
2
|
+
import type { FileInfo, SourceLocation } from "../core/types/file";
|
|
3
|
+
export interface FlowAuthenticationRules {
|
|
4
|
+
version: number;
|
|
5
|
+
maxLinesBefore: number;
|
|
6
|
+
linesAfter: number;
|
|
7
|
+
helperLines: number;
|
|
8
|
+
skipFlowTypes: Set<string>;
|
|
9
|
+
credentialArgument: RegExp;
|
|
10
|
+
kinds: Array<{
|
|
11
|
+
id: string;
|
|
12
|
+
patterns: RegExp[];
|
|
13
|
+
}>;
|
|
14
|
+
}
|
|
15
|
+
export declare function loadFlowAuthenticationRules(): FlowAuthenticationRules;
|
|
16
|
+
export declare const FUNCTION_START: RegExp;
|
|
17
|
+
export declare const IMPORT_LINE: RegExp;
|
|
18
|
+
export declare function escapeRegExp(value: string): string;
|
|
19
|
+
/** Line indexes (0-based) of the code around one request. */
|
|
20
|
+
export declare function requestWindow(lines: string[], index: number, rules: Pick<FlowAuthenticationRules, "maxLinesBefore" | "linesAfter">): number[];
|
|
21
|
+
/**
|
|
22
|
+
* Line indexes of the definition of `name` elsewhere in the file: a function, or a
|
|
23
|
+
* top-level constant. Local variables are already inside the request window.
|
|
24
|
+
*/
|
|
25
|
+
export declare function definitionLines(lines: string[], name: string, rules: Pick<FlowAuthenticationRules, "helperLines">): number[];
|
|
26
|
+
export declare function locationsOf(flow: DetectedDataFlow): SourceLocation[];
|
|
27
|
+
/** Authentication evidence for one flow, or undefined when the flow is not checked. */
|
|
28
|
+
export declare function flowAuthentication(flow: DetectedDataFlow, linesByPath: ReadonlyMap<string, string[]>, rules?: FlowAuthenticationRules): FlowAuthentication | undefined;
|
|
29
|
+
/** Sets `authentication` on each checked flow. Mutates the flows. */
|
|
30
|
+
export declare function annotateFlowAuthentication(flows: DetectedDataFlow[], files: FileInfo[]): void;
|
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.IMPORT_LINE = exports.FUNCTION_START = void 0;
|
|
7
|
+
exports.loadFlowAuthenticationRules = loadFlowAuthenticationRules;
|
|
8
|
+
exports.escapeRegExp = escapeRegExp;
|
|
9
|
+
exports.requestWindow = requestWindow;
|
|
10
|
+
exports.definitionLines = definitionLines;
|
|
11
|
+
exports.locationsOf = locationsOf;
|
|
12
|
+
exports.flowAuthentication = flowAuthentication;
|
|
13
|
+
exports.annotateFlowAuthentication = annotateFlowAuthentication;
|
|
14
|
+
/**
|
|
15
|
+
* Authentication evidence on data flows (DATAP-912).
|
|
16
|
+
*
|
|
17
|
+
* For each evidence location of a flow, read the code around the request (the enclosing
|
|
18
|
+
* function, up to a few lines after the call and never past the end of that function) and any helper function or constant that the
|
|
19
|
+
* call passes as `headers=`, `auth=` and similar. Apply the patterns in
|
|
20
|
+
* patterns/flow-authentication.rules.yaml. Each match records its kind and line.
|
|
21
|
+
*
|
|
22
|
+
* "none_seen" means no evidence was found. It does not mean the flow is unauthenticated.
|
|
23
|
+
*/
|
|
24
|
+
const fs_1 = __importDefault(require("fs"));
|
|
25
|
+
const path_1 = __importDefault(require("path"));
|
|
26
|
+
const yaml_1 = __importDefault(require("yaml"));
|
|
27
|
+
const zod_1 = require("zod");
|
|
28
|
+
const MAX_LOCATIONS_PER_FLOW = 12;
|
|
29
|
+
const MAX_EVIDENCE_PER_FLOW = 6;
|
|
30
|
+
const rulesSchema = zod_1.z.object({
|
|
31
|
+
version: zod_1.z.number(),
|
|
32
|
+
window: zod_1.z.object({
|
|
33
|
+
max_lines_before: zod_1.z.number().int().positive(),
|
|
34
|
+
lines_after: zod_1.z.number().int().nonnegative(),
|
|
35
|
+
helper_lines: zod_1.z.number().int().positive(),
|
|
36
|
+
}),
|
|
37
|
+
skip_flow_types: zod_1.z.array(zod_1.z.string()).default([]),
|
|
38
|
+
credential_arguments: zod_1.z.array(zod_1.z.string().min(1)),
|
|
39
|
+
kinds: zod_1.z.array(zod_1.z.object({ id: zod_1.z.string().min(1), patterns: zod_1.z.array(zod_1.z.string().min(1)).min(1) })),
|
|
40
|
+
});
|
|
41
|
+
let cached;
|
|
42
|
+
function rulesPath() {
|
|
43
|
+
const parts = __dirname.split(path_1.default.sep);
|
|
44
|
+
const distIndex = parts.lastIndexOf("dist");
|
|
45
|
+
const root = distIndex !== -1 ? parts.slice(0, distIndex).join(path_1.default.sep) : path_1.default.resolve(__dirname, "..", "..");
|
|
46
|
+
return path_1.default.join(root, "patterns", "flow-authentication.rules.yaml");
|
|
47
|
+
}
|
|
48
|
+
function loadFlowAuthenticationRules() {
|
|
49
|
+
if (cached)
|
|
50
|
+
return cached;
|
|
51
|
+
const raw = rulesSchema.parse(yaml_1.default.parse(fs_1.default.readFileSync(rulesPath(), "utf8")));
|
|
52
|
+
const args = raw.credential_arguments.map((a) => a.replace(/[^A-Za-z0-9_]/g, "")).join("|");
|
|
53
|
+
cached = {
|
|
54
|
+
version: raw.version,
|
|
55
|
+
maxLinesBefore: raw.window.max_lines_before,
|
|
56
|
+
linesAfter: raw.window.lines_after,
|
|
57
|
+
helperLines: raw.window.helper_lines,
|
|
58
|
+
skipFlowTypes: new Set(raw.skip_flow_types),
|
|
59
|
+
// `headers=_auth_headers()`, `auth=creds`, `headers: buildHeaders(token)`, `headers=self._headers`.
|
|
60
|
+
credentialArgument: new RegExp(`\\b(?:${args})\\s*[=:]\\s*(?:self\\.|this\\.)?([A-Za-z_][A-Za-z0-9_]*)\\s*(\\()?`, "g"),
|
|
61
|
+
kinds: raw.kinds.map((k) => ({ id: k.id, patterns: k.patterns.map((p) => new RegExp(p)) })),
|
|
62
|
+
};
|
|
63
|
+
return cached;
|
|
64
|
+
}
|
|
65
|
+
exports.FUNCTION_START = /^\s*(?:@\w+.*)?(?:async\s+)?def\s+\w+\s*\(|^\s*(?:export\s+)?(?:default\s+)?(?:async\s+)?function\b|^\s*(?:public|private|protected|static|async|\s)*[A-Za-z_$][\w$]*\s*\([^)]*\)\s*(?::\s*[^{=]+)?\{\s*$|=>\s*\{\s*$|^\s*func\b.*\{\s*$|^\s*(?:(?:public|private|protected|internal|static|final|abstract|override|virtual|async|synchronized)\s+)+[\w<>\[\],.?\s]+?\s+\w+\s*\([^)]*\)\s*(?:throws\s+[\w.,\s]+)?\{\s*$/;
|
|
66
|
+
exports.IMPORT_LINE = /^\s*(?:from\s+[\w.]+\s+import\b|import\s+[\w.]+|import\b.*\bfrom\s+["']|const\s+\w+\s*=\s*require\()/;
|
|
67
|
+
/** Kinds that an SDK client takes as a constructor argument. */
|
|
68
|
+
const SDK_CLIENT_KINDS = new Set(["api_key", "oauth"]);
|
|
69
|
+
function escapeRegExp(value) {
|
|
70
|
+
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
71
|
+
}
|
|
72
|
+
/** Line indexes (0-based) of the code around one request. */
|
|
73
|
+
function requestWindow(lines, index, rules) {
|
|
74
|
+
let start = index;
|
|
75
|
+
let enclosing;
|
|
76
|
+
const floor = Math.max(0, index - rules.maxLinesBefore);
|
|
77
|
+
for (let i = index; i >= floor; i -= 1) {
|
|
78
|
+
start = i;
|
|
79
|
+
// A nested function or callback that closes before the call does not enclose it.
|
|
80
|
+
if (exports.FUNCTION_START.test(lines[i] ?? "") && !closesBefore(lines, i, index)) {
|
|
81
|
+
enclosing = i;
|
|
82
|
+
break;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
const limit = Math.min(lines.length - 1, index + rules.linesAfter);
|
|
86
|
+
const end = enclosing === undefined ? nextFunctionBound(lines, index, limit) : functionEnd(lines, enclosing, index, limit);
|
|
87
|
+
const out = [];
|
|
88
|
+
for (let i = start; i <= end; i += 1)
|
|
89
|
+
out.push(i);
|
|
90
|
+
return out;
|
|
91
|
+
}
|
|
92
|
+
const PY_DEF = /^(\s*)(?:async\s+)?def\s/;
|
|
93
|
+
const DECORATOR = /^\s*@\w/;
|
|
94
|
+
function indentOf(line) {
|
|
95
|
+
return line.length - line.trimStart().length;
|
|
96
|
+
}
|
|
97
|
+
/** Last line before the next function definition, up to `limit`. */
|
|
98
|
+
function nextFunctionBound(lines, index, limit) {
|
|
99
|
+
for (let i = index + 1; i <= limit; i += 1) {
|
|
100
|
+
if (exports.FUNCTION_START.test(lines[i] ?? "") || DECORATOR.test(lines[i] ?? ""))
|
|
101
|
+
return i - 1;
|
|
102
|
+
}
|
|
103
|
+
return limit;
|
|
104
|
+
}
|
|
105
|
+
/** Count of `{` minus `}` on a line, ignoring string literals and line comments. */
|
|
106
|
+
function braceDelta(line) {
|
|
107
|
+
const code = line.replace(/(["'`])(?:\\.|(?!\1).)*\1/g, "").replace(/\/\/.*$/, "");
|
|
108
|
+
let delta = 0;
|
|
109
|
+
for (const ch of code) {
|
|
110
|
+
if (ch === "{")
|
|
111
|
+
delta += 1;
|
|
112
|
+
else if (ch === "}")
|
|
113
|
+
delta -= 1;
|
|
114
|
+
}
|
|
115
|
+
return delta;
|
|
116
|
+
}
|
|
117
|
+
/** True when the function that starts at `start` ends before line `index`. */
|
|
118
|
+
function closesBefore(lines, start, index) {
|
|
119
|
+
if (start === index)
|
|
120
|
+
return false;
|
|
121
|
+
const py = PY_DEF.exec(lines[start] ?? "");
|
|
122
|
+
if (py) {
|
|
123
|
+
const defIndent = py[1].length;
|
|
124
|
+
for (let i = start + 1; i < index; i += 1) {
|
|
125
|
+
const line = lines[i] ?? "";
|
|
126
|
+
if (line.trim() !== "" && indentOf(line) <= defIndent)
|
|
127
|
+
return true;
|
|
128
|
+
}
|
|
129
|
+
return false;
|
|
130
|
+
}
|
|
131
|
+
let depth = 0;
|
|
132
|
+
let opened = false;
|
|
133
|
+
for (let i = start; i < index; i += 1) {
|
|
134
|
+
depth += braceDelta(lines[i] ?? "");
|
|
135
|
+
if (depth > 0)
|
|
136
|
+
opened = true;
|
|
137
|
+
if (opened && depth <= 0)
|
|
138
|
+
return true;
|
|
139
|
+
}
|
|
140
|
+
return false;
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Last line of the function that starts at `start` and contains `index`, up to `limit`.
|
|
144
|
+
* Python functions end at the first non-blank line indented no deeper than the `def`.
|
|
145
|
+
* Brace functions end where the braces opened on the first line close.
|
|
146
|
+
*/
|
|
147
|
+
function functionEnd(lines, start, index, limit) {
|
|
148
|
+
const py = PY_DEF.exec(lines[start] ?? "");
|
|
149
|
+
if (py) {
|
|
150
|
+
const defIndent = py[1].length;
|
|
151
|
+
for (let i = index + 1; i <= limit; i += 1) {
|
|
152
|
+
const line = lines[i] ?? "";
|
|
153
|
+
if (line.trim() === "")
|
|
154
|
+
continue;
|
|
155
|
+
if (indentOf(line) <= defIndent)
|
|
156
|
+
return i - 1;
|
|
157
|
+
}
|
|
158
|
+
return limit;
|
|
159
|
+
}
|
|
160
|
+
let depth = 0;
|
|
161
|
+
let opened = false;
|
|
162
|
+
for (let i = start; i <= limit; i += 1) {
|
|
163
|
+
depth += braceDelta(lines[i] ?? "");
|
|
164
|
+
if (depth > 0)
|
|
165
|
+
opened = true;
|
|
166
|
+
if (opened && depth <= 0 && i >= index)
|
|
167
|
+
return i;
|
|
168
|
+
}
|
|
169
|
+
return nextFunctionBound(lines, index, limit);
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* Line indexes of the definition of `name` elsewhere in the file: a function, or a
|
|
173
|
+
* top-level constant. Local variables are already inside the request window.
|
|
174
|
+
*/
|
|
175
|
+
function definitionLines(lines, name, rules) {
|
|
176
|
+
const n = escapeRegExp(name);
|
|
177
|
+
const def = new RegExp(`^\\s*(?:async\\s+)?def\\s+${n}\\s*\\(|^\\s*(?:export\\s+)?(?:async\\s+)?function\\s+${n}\\s*\\(|^(?:export\\s+)?(?:const|let|var)\\s+${n}\\b|^${n}\\s*(?::[^=]+)?=[^=]`);
|
|
178
|
+
for (let i = 0; i < lines.length; i += 1) {
|
|
179
|
+
if (!def.test(lines[i]))
|
|
180
|
+
continue;
|
|
181
|
+
const out = [];
|
|
182
|
+
for (let j = i; j < Math.min(lines.length, i + rules.helperLines); j += 1)
|
|
183
|
+
out.push(j);
|
|
184
|
+
return out;
|
|
185
|
+
}
|
|
186
|
+
return [];
|
|
187
|
+
}
|
|
188
|
+
function evidenceForLocation(lines, location, rules) {
|
|
189
|
+
const index = location.startLine - 1;
|
|
190
|
+
if (index < 0 || index >= lines.length)
|
|
191
|
+
return [];
|
|
192
|
+
const window = requestWindow(lines, index, rules);
|
|
193
|
+
const read = new Set(window);
|
|
194
|
+
const windowText = window.map((i) => lines[i]).join("\n");
|
|
195
|
+
rules.credentialArgument.lastIndex = 0;
|
|
196
|
+
for (const match of windowText.matchAll(rules.credentialArgument)) {
|
|
197
|
+
const name = match[1];
|
|
198
|
+
// A name assigned inside the window is a local; its value is already read.
|
|
199
|
+
const local = new RegExp(`^\\s*${escapeRegExp(name)}\\s*(?::[^=]+)?=[^=]`);
|
|
200
|
+
if (window.some((i) => local.test(lines[i])))
|
|
201
|
+
continue;
|
|
202
|
+
for (const i of definitionLines(lines, name, rules))
|
|
203
|
+
read.add(i);
|
|
204
|
+
}
|
|
205
|
+
// SDK flows cite the import. The credentials go to the client built in the same file,
|
|
206
|
+
// so read the file, but keep only the kinds an SDK client takes.
|
|
207
|
+
const sdkImport = exports.IMPORT_LINE.test(lines[index]);
|
|
208
|
+
if (sdkImport)
|
|
209
|
+
for (let i = 0; i < lines.length; i += 1)
|
|
210
|
+
read.add(i);
|
|
211
|
+
const evidence = [];
|
|
212
|
+
for (const i of [...read].sort((a, b) => a - b)) {
|
|
213
|
+
const line = lines[i];
|
|
214
|
+
for (const kind of rules.kinds) {
|
|
215
|
+
if (sdkImport && !SDK_CLIENT_KINDS.has(kind.id))
|
|
216
|
+
continue;
|
|
217
|
+
if (kind.patterns.some((p) => p.test(line))) {
|
|
218
|
+
evidence.push({ kind: kind.id, filePath: location.filePath, line: i + 1 });
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
return evidence;
|
|
223
|
+
}
|
|
224
|
+
function locationsOf(flow) {
|
|
225
|
+
if (flow.sourceLocations && flow.sourceLocations.length > 0)
|
|
226
|
+
return flow.sourceLocations;
|
|
227
|
+
return flow.sourceLocation ? [flow.sourceLocation] : [];
|
|
228
|
+
}
|
|
229
|
+
/** Authentication evidence for one flow, or undefined when the flow is not checked. */
|
|
230
|
+
function flowAuthentication(flow, linesByPath, rules = loadFlowAuthenticationRules()) {
|
|
231
|
+
if (rules.skipFlowTypes.has(flow.type))
|
|
232
|
+
return undefined;
|
|
233
|
+
const locations = locationsOf(flow).slice(0, MAX_LOCATIONS_PER_FLOW);
|
|
234
|
+
if (locations.length === 0)
|
|
235
|
+
return undefined;
|
|
236
|
+
const seen = new Map();
|
|
237
|
+
for (const location of locations) {
|
|
238
|
+
const lines = linesByPath.get(location.filePath);
|
|
239
|
+
if (!lines)
|
|
240
|
+
continue;
|
|
241
|
+
for (const e of evidenceForLocation(lines, location, rules)) {
|
|
242
|
+
seen.set(`${e.kind}|${e.filePath}|${e.line}`, e);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
const evidence = [...seen.values()].sort((a, b) => a.kind.localeCompare(b.kind) || a.filePath.localeCompare(b.filePath) || a.line - b.line);
|
|
246
|
+
const kinds = [...new Set(evidence.map((e) => e.kind))].sort();
|
|
247
|
+
if (kinds.length === 0)
|
|
248
|
+
return { status: "none_seen", kinds: [], evidence: [] };
|
|
249
|
+
// Keep at least one line for each kind, then fill up to the cap.
|
|
250
|
+
const kept = kinds.map((k) => evidence.find((e) => e.kind === k));
|
|
251
|
+
for (const e of evidence) {
|
|
252
|
+
if (kept.length >= MAX_EVIDENCE_PER_FLOW)
|
|
253
|
+
break;
|
|
254
|
+
if (!kept.includes(e))
|
|
255
|
+
kept.push(e);
|
|
256
|
+
}
|
|
257
|
+
kept.sort((a, b) => a.kind.localeCompare(b.kind) || a.filePath.localeCompare(b.filePath) || a.line - b.line);
|
|
258
|
+
return { status: "seen", kinds, evidence: kept };
|
|
259
|
+
}
|
|
260
|
+
/** Sets `authentication` on each checked flow. Mutates the flows. */
|
|
261
|
+
function annotateFlowAuthentication(flows, files) {
|
|
262
|
+
if (flows.length === 0)
|
|
263
|
+
return;
|
|
264
|
+
const rules = loadFlowAuthenticationRules();
|
|
265
|
+
const linesByPath = new Map();
|
|
266
|
+
for (const file of files) {
|
|
267
|
+
if (typeof file.content === "string")
|
|
268
|
+
linesByPath.set(file.path, file.content.split(/\r?\n/));
|
|
269
|
+
}
|
|
270
|
+
for (const flow of flows) {
|
|
271
|
+
const auth = flowAuthentication(flow, linesByPath, rules);
|
|
272
|
+
if (auth)
|
|
273
|
+
flow.authentication = auth;
|
|
274
|
+
else
|
|
275
|
+
delete flow.authentication;
|
|
276
|
+
}
|
|
277
|
+
}
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.flowTypeForExternalApi = flowTypeForExternalApi;
|
|
4
4
|
exports.buildFlow = buildFlow;
|
|
5
|
+
const flow_interaction_description_1 = require("./flow-interaction-description");
|
|
5
6
|
function getSectionIdFromMaybeUnknown(raw) {
|
|
6
7
|
if (typeof raw !== "string")
|
|
7
8
|
return undefined;
|
|
@@ -64,6 +65,7 @@ function buildFlow(sourceId, targetId, type, finding, index, sourceComponent, ta
|
|
|
64
65
|
? finding.properties.url
|
|
65
66
|
: undefined;
|
|
66
67
|
const scope = inferTargetScope(sourceComponent, targetComponent);
|
|
68
|
+
const interactionDescription = (0, flow_interaction_description_1.describeFlowFromFinding)(finding, type, sourceComponent, targetComponent);
|
|
67
69
|
return {
|
|
68
70
|
id: `flow_${index}`,
|
|
69
71
|
sourceComponentId: sourceId,
|
|
@@ -76,5 +78,9 @@ function buildFlow(sourceId, targetId, type, finding, index, sourceComponent, ta
|
|
|
76
78
|
targetScope: scope.targetScope,
|
|
77
79
|
targetScopeConfidence: scope.targetScopeConfidence,
|
|
78
80
|
targetScopeReason: scope.targetScopeReason,
|
|
81
|
+
...definedPropertiesOnly(interactionDescription),
|
|
79
82
|
};
|
|
80
83
|
}
|
|
84
|
+
function definedPropertiesOnly(value) {
|
|
85
|
+
return Object.fromEntries(Object.entries(value).filter(([, propertyValue]) => propertyValue !== undefined));
|
|
86
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { DetectedDataFlow, FlowControlEffect, FlowControlEvidence } from "../core/types/data-flow";
|
|
2
|
+
import type { FileInfo } from "../core/types/file";
|
|
3
|
+
import { type FlowAuthenticationRules } from "./flow-authentication";
|
|
4
|
+
export interface FlowControlRules {
|
|
5
|
+
version: number;
|
|
6
|
+
skipFlowTypes: Set<string>;
|
|
7
|
+
urlConstant: RegExp;
|
|
8
|
+
commentLine: RegExp;
|
|
9
|
+
controls: Array<{
|
|
10
|
+
id: string;
|
|
11
|
+
effect: FlowControlEffect;
|
|
12
|
+
sdk: boolean;
|
|
13
|
+
patterns: RegExp[];
|
|
14
|
+
}>;
|
|
15
|
+
}
|
|
16
|
+
export declare function loadFlowControlRules(): FlowControlRules;
|
|
17
|
+
/** For each line, whether it is part of a Python triple-quoted string. */
|
|
18
|
+
export declare function docstringLines(lines: string[]): boolean[];
|
|
19
|
+
/** Names an import line binds: `from x import A, B as C`, `import x.y`, `import { A } from`, `const A = require(`. */
|
|
20
|
+
export declare function importedNames(line: string): string[];
|
|
21
|
+
/** Control evidence for one flow, or undefined when the flow is not checked. */
|
|
22
|
+
export declare function flowControls(flow: DetectedDataFlow, linesByPath: ReadonlyMap<string, string[]>, rules?: FlowControlRules, windowRules?: FlowAuthenticationRules): FlowControlEvidence[] | undefined;
|
|
23
|
+
/** Sets `controls` on each checked flow. Mutates the flows. */
|
|
24
|
+
export declare function annotateFlowControls(flows: DetectedDataFlow[], files: FileInfo[]): void;
|
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.loadFlowControlRules = loadFlowControlRules;
|
|
7
|
+
exports.docstringLines = docstringLines;
|
|
8
|
+
exports.importedNames = importedNames;
|
|
9
|
+
exports.flowControls = flowControls;
|
|
10
|
+
exports.annotateFlowControls = annotateFlowControls;
|
|
11
|
+
/**
|
|
12
|
+
* Control evidence on data flows (DATAP-915).
|
|
13
|
+
*
|
|
14
|
+
* For each evidence location of a flow, read the request window (see flow-authentication.ts)
|
|
15
|
+
* and the top-level URL constants it names, and apply patterns/flow-controls.rules.yaml.
|
|
16
|
+
* The flow's `endpoint`, when it is a URL, is read too. Each match records the control,
|
|
17
|
+
* its effect and the line.
|
|
18
|
+
*
|
|
19
|
+
* No evidence means nothing was seen. It does not mean the control is missing.
|
|
20
|
+
*/
|
|
21
|
+
const fs_1 = __importDefault(require("fs"));
|
|
22
|
+
const path_1 = __importDefault(require("path"));
|
|
23
|
+
const yaml_1 = __importDefault(require("yaml"));
|
|
24
|
+
const zod_1 = require("zod");
|
|
25
|
+
const flow_authentication_1 = require("./flow-authentication");
|
|
26
|
+
const MAX_LOCATIONS_PER_FLOW = 12;
|
|
27
|
+
const MAX_EVIDENCE_PER_FLOW = 8;
|
|
28
|
+
/** A URL constant is one line, or a short wrapped expression. */
|
|
29
|
+
const CONSTANT_LINES = 3;
|
|
30
|
+
const rulesSchema = zod_1.z.object({
|
|
31
|
+
version: zod_1.z.number().int().positive(),
|
|
32
|
+
skip_flow_types: zod_1.z.array(zod_1.z.string()).default([]),
|
|
33
|
+
url_constant: zod_1.z.string().min(1),
|
|
34
|
+
comment_line: zod_1.z.string().min(1),
|
|
35
|
+
controls: zod_1.z.array(zod_1.z.object({
|
|
36
|
+
id: zod_1.z.string().min(1),
|
|
37
|
+
effect: zod_1.z.enum(["mitigates", "weakens"]),
|
|
38
|
+
sdk: zod_1.z.boolean().default(false),
|
|
39
|
+
patterns: zod_1.z.array(zod_1.z.string().min(1)).min(1),
|
|
40
|
+
})),
|
|
41
|
+
});
|
|
42
|
+
let cached;
|
|
43
|
+
function rulesPath() {
|
|
44
|
+
const parts = __dirname.split(path_1.default.sep);
|
|
45
|
+
const distIndex = parts.lastIndexOf("dist");
|
|
46
|
+
const root = distIndex !== -1 ? parts.slice(0, distIndex).join(path_1.default.sep) : path_1.default.resolve(__dirname, "..", "..");
|
|
47
|
+
return path_1.default.join(root, "patterns", "flow-controls.rules.yaml");
|
|
48
|
+
}
|
|
49
|
+
function loadFlowControlRules() {
|
|
50
|
+
if (cached)
|
|
51
|
+
return cached;
|
|
52
|
+
const raw = rulesSchema.parse(yaml_1.default.parse(fs_1.default.readFileSync(rulesPath(), "utf8")));
|
|
53
|
+
cached = {
|
|
54
|
+
version: raw.version,
|
|
55
|
+
skipFlowTypes: new Set(raw.skip_flow_types),
|
|
56
|
+
urlConstant: new RegExp(raw.url_constant, "g"),
|
|
57
|
+
commentLine: new RegExp(raw.comment_line),
|
|
58
|
+
controls: raw.controls.map((c) => ({
|
|
59
|
+
id: c.id,
|
|
60
|
+
effect: c.effect,
|
|
61
|
+
sdk: c.sdk,
|
|
62
|
+
patterns: c.patterns.map((p) => new RegExp(p)),
|
|
63
|
+
})),
|
|
64
|
+
};
|
|
65
|
+
return cached;
|
|
66
|
+
}
|
|
67
|
+
const TRIPLE_QUOTE = /[rRbBuUfF]{0,2}("""|\'\'\')/g;
|
|
68
|
+
const docstringCache = new WeakMap();
|
|
69
|
+
/** For each line, whether it is part of a Python triple-quoted string. */
|
|
70
|
+
function docstringLines(lines) {
|
|
71
|
+
const cachedLines = docstringCache.get(lines);
|
|
72
|
+
if (cachedLines)
|
|
73
|
+
return cachedLines;
|
|
74
|
+
const out = new Array(lines.length).fill(false);
|
|
75
|
+
let open;
|
|
76
|
+
for (let i = 0; i < lines.length; i += 1) {
|
|
77
|
+
const line = lines[i];
|
|
78
|
+
const startsOpen = open !== undefined;
|
|
79
|
+
let touched = startsOpen;
|
|
80
|
+
for (const m of line.matchAll(TRIPLE_QUOTE)) {
|
|
81
|
+
touched = true;
|
|
82
|
+
if (open === undefined)
|
|
83
|
+
open = m[1];
|
|
84
|
+
else if (m[1] === open)
|
|
85
|
+
open = undefined;
|
|
86
|
+
}
|
|
87
|
+
// A line that only assigns a triple-quoted value (x = """...) is code, not a docstring.
|
|
88
|
+
const assigns = !startsOpen && /^\s*[\w.]+\s*(?::[^=]+)?=\s*[rRbBuUfF]{0,2}("""|\'\'\')/.test(line);
|
|
89
|
+
out[i] = touched && !assigns;
|
|
90
|
+
}
|
|
91
|
+
docstringCache.set(lines, out);
|
|
92
|
+
return out;
|
|
93
|
+
}
|
|
94
|
+
/** Names an import line binds: `from x import A, B as C`, `import x.y`, `import { A } from`, `const A = require(`. */
|
|
95
|
+
function importedNames(line) {
|
|
96
|
+
const names = new Set();
|
|
97
|
+
const from = line.match(/^\s*from\s+[\w.]+\s+import\s+\(?([^)#]+)/);
|
|
98
|
+
if (from)
|
|
99
|
+
for (const part of from[1].split(","))
|
|
100
|
+
names.add(part.trim().split(/\s+as\s+/).pop().trim());
|
|
101
|
+
const plain = line.match(/^\s*import\s+([\w.]+)(?:\s+as\s+(\w+))?\s*$/);
|
|
102
|
+
if (plain)
|
|
103
|
+
names.add(plain[2] ?? plain[1].split(".")[0]);
|
|
104
|
+
const es = line.match(/^\s*import\s+(.+?)\s+from\s+["']/);
|
|
105
|
+
if (es)
|
|
106
|
+
for (const part of es[1].replace(/[{}*]/g, " ").split(","))
|
|
107
|
+
names.add(part.trim().split(/\s+as\s+/).pop().trim());
|
|
108
|
+
const req = line.match(/^\s*const\s+(?:\{([^}]+)\}|(\w+))\s*=\s*require\(/);
|
|
109
|
+
if (req)
|
|
110
|
+
for (const part of (req[1] ?? req[2]).split(","))
|
|
111
|
+
names.add(part.trim().split(/\s*:\s*/).pop().trim());
|
|
112
|
+
return [...names].filter((n) => /^[A-Za-z_$][\w$]*$/.test(n));
|
|
113
|
+
}
|
|
114
|
+
function evidenceForLocation(lines, location, rules, windowRules) {
|
|
115
|
+
const index = location.startLine - 1;
|
|
116
|
+
if (index < 0 || index >= lines.length)
|
|
117
|
+
return [];
|
|
118
|
+
const sdkImport = flow_authentication_1.IMPORT_LINE.test(lines[index]);
|
|
119
|
+
const read = new Set();
|
|
120
|
+
if (sdkImport) {
|
|
121
|
+
// Read only the lines that use what the import binds, such as `client = OpenAI(timeout=20)`.
|
|
122
|
+
const names = importedNames(lines[index]);
|
|
123
|
+
const uses = names.map((n) => new RegExp(`\\b${n.replace(/\$/g, "\\$")}\\b`));
|
|
124
|
+
for (let i = 0; i < lines.length; i += 1) {
|
|
125
|
+
if (i !== index && uses.some((u) => u.test(lines[i])))
|
|
126
|
+
read.add(i);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
else {
|
|
130
|
+
const window = (0, flow_authentication_1.requestWindow)(lines, index, windowRules);
|
|
131
|
+
for (const i of window)
|
|
132
|
+
read.add(i);
|
|
133
|
+
const text = window.map((i) => lines[i]).join("\n");
|
|
134
|
+
rules.urlConstant.lastIndex = 0;
|
|
135
|
+
const names = new Set([...text.matchAll(rules.urlConstant)].map((m) => m[1]));
|
|
136
|
+
for (const name of names) {
|
|
137
|
+
for (const i of (0, flow_authentication_1.definitionLines)(lines, name, { helperLines: CONSTANT_LINES }))
|
|
138
|
+
read.add(i);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
const docstring = location.filePath.endsWith(".py") ? docstringLines(lines) : undefined;
|
|
142
|
+
const evidence = [];
|
|
143
|
+
for (const i of [...read].sort((a, b) => a - b)) {
|
|
144
|
+
const line = lines[i];
|
|
145
|
+
if (rules.commentLine.test(line) || docstring?.[i])
|
|
146
|
+
continue;
|
|
147
|
+
for (const control of rules.controls) {
|
|
148
|
+
if (sdkImport && !control.sdk)
|
|
149
|
+
continue;
|
|
150
|
+
if (control.patterns.some((p) => p.test(line))) {
|
|
151
|
+
evidence.push({ control: control.id, effect: control.effect, filePath: location.filePath, line: i + 1 });
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
return evidence;
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* TLS or plain HTTP from the flow's `endpoint` URL. The evidence line is the first line in
|
|
159
|
+
* the flow's files that contains the endpoint's scheme and host; without one, no evidence.
|
|
160
|
+
*/
|
|
161
|
+
function endpointEvidence(flow, linesByPath, rules) {
|
|
162
|
+
const endpoint = flow.endpoint;
|
|
163
|
+
const origin = endpoint?.match(/^((?:https?|wss?):\/\/[^/{}?#\s]+)/i)?.[1];
|
|
164
|
+
if (!endpoint || !origin)
|
|
165
|
+
return [];
|
|
166
|
+
let at;
|
|
167
|
+
for (const location of (0, flow_authentication_1.locationsOf)(flow).slice(0, MAX_LOCATIONS_PER_FLOW)) {
|
|
168
|
+
const lines = linesByPath.get(location.filePath);
|
|
169
|
+
const docstring = lines && location.filePath.endsWith(".py") ? docstringLines(lines) : undefined;
|
|
170
|
+
const i = lines?.findIndex((l, n) => l.includes(origin) && !rules.commentLine.test(l) && !docstring?.[n]) ?? -1;
|
|
171
|
+
if (i >= 0) {
|
|
172
|
+
at = { filePath: location.filePath, line: i + 1 };
|
|
173
|
+
break;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
if (!at)
|
|
177
|
+
return [];
|
|
178
|
+
const out = [];
|
|
179
|
+
for (const control of rules.controls) {
|
|
180
|
+
if (control.id !== "tls" && control.id !== "plaintext_http")
|
|
181
|
+
continue;
|
|
182
|
+
if (control.patterns.some((p) => p.test(endpoint)))
|
|
183
|
+
out.push({ control: control.id, effect: control.effect, ...at });
|
|
184
|
+
}
|
|
185
|
+
return out;
|
|
186
|
+
}
|
|
187
|
+
function compareEvidence(a, b) {
|
|
188
|
+
return a.control.localeCompare(b.control) || a.filePath.localeCompare(b.filePath) || a.line - b.line;
|
|
189
|
+
}
|
|
190
|
+
/** Control evidence for one flow, or undefined when the flow is not checked. */
|
|
191
|
+
function flowControls(flow, linesByPath, rules = loadFlowControlRules(), windowRules = (0, flow_authentication_1.loadFlowAuthenticationRules)()) {
|
|
192
|
+
if (rules.skipFlowTypes.has(flow.type))
|
|
193
|
+
return undefined;
|
|
194
|
+
const locations = (0, flow_authentication_1.locationsOf)(flow).slice(0, MAX_LOCATIONS_PER_FLOW);
|
|
195
|
+
if (locations.length === 0)
|
|
196
|
+
return undefined;
|
|
197
|
+
const seen = new Map();
|
|
198
|
+
const add = (e) => seen.set(`${e.control}|${e.filePath}|${e.line}`, e);
|
|
199
|
+
for (const location of locations) {
|
|
200
|
+
const lines = linesByPath.get(location.filePath);
|
|
201
|
+
if (!lines)
|
|
202
|
+
continue;
|
|
203
|
+
for (const e of evidenceForLocation(lines, location, rules, windowRules))
|
|
204
|
+
add(e);
|
|
205
|
+
}
|
|
206
|
+
for (const e of endpointEvidence(flow, linesByPath, rules))
|
|
207
|
+
add(e);
|
|
208
|
+
const evidence = [...seen.values()].sort(compareEvidence);
|
|
209
|
+
const controls = [...new Set(evidence.map((e) => e.control))];
|
|
210
|
+
// Keep at least one line for each control, then fill up to the cap.
|
|
211
|
+
const kept = controls.map((c) => evidence.find((e) => e.control === c));
|
|
212
|
+
for (const e of evidence) {
|
|
213
|
+
if (kept.length >= MAX_EVIDENCE_PER_FLOW)
|
|
214
|
+
break;
|
|
215
|
+
if (!kept.includes(e))
|
|
216
|
+
kept.push(e);
|
|
217
|
+
}
|
|
218
|
+
return kept.sort(compareEvidence);
|
|
219
|
+
}
|
|
220
|
+
/** Sets `controls` on each checked flow. Mutates the flows. */
|
|
221
|
+
function annotateFlowControls(flows, files) {
|
|
222
|
+
if (flows.length === 0)
|
|
223
|
+
return;
|
|
224
|
+
const rules = loadFlowControlRules();
|
|
225
|
+
const windowRules = (0, flow_authentication_1.loadFlowAuthenticationRules)();
|
|
226
|
+
const linesByPath = new Map();
|
|
227
|
+
for (const file of files) {
|
|
228
|
+
if (typeof file.content === "string")
|
|
229
|
+
linesByPath.set(file.path, file.content.split(/\r?\n/));
|
|
230
|
+
}
|
|
231
|
+
for (const flow of flows) {
|
|
232
|
+
const controls = flowControls(flow, linesByPath, rules, windowRules);
|
|
233
|
+
if (controls)
|
|
234
|
+
flow.controls = controls;
|
|
235
|
+
else
|
|
236
|
+
delete flow.controls;
|
|
237
|
+
}
|
|
238
|
+
}
|