vigilsutra 0.4.1 → 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/CHANGELOG.md +55 -0
- package/README.md +34 -2
- package/dist/cli.js +95 -20
- package/dist/clock.js +8 -0
- package/dist/components.js +127 -53
- package/dist/custom-rules.js +86 -0
- package/dist/decode.js +9 -0
- package/dist/engine.js +52 -15
- package/dist/fix.js +2 -3
- package/dist/git-objects.js +17 -7
- package/dist/policy.js +23 -10
- package/dist/redact.js +1 -1
- package/dist/reporters/annotations.js +13 -0
- package/dist/reporters/index.js +9 -2
- package/dist/reporters/rich.js +10 -7
- package/dist/rules/agents-privacy.js +28 -0
- package/dist/rules/code.js +74 -2
- package/dist/rules/index.js +1 -1
- package/dist/rules/infra.js +184 -15
- package/dist/rules/secrets.js +17 -7
- package/dist/rules/util.js +54 -14
- package/dist/scope.js +2 -5
- package/dist/suggest.js +18 -0
- package/docs/rules.md +132 -6
- package/llms.txt +2 -2
- package/package.json +2 -3
- package/policies/safe-default.yaml +3 -2
- package/docs/index.html +0 -76
package/dist/rules/code.js
CHANGED
|
@@ -1,5 +1,76 @@
|
|
|
1
|
-
import { isJs, isPy, isCode, lineRule, insideString } from "./util.js";
|
|
1
|
+
import { isJs, isPy, isCode, lineRule, insideString, lookBack } from "./util.js";
|
|
2
2
|
const b = { version: "1.0.0", category: "code", agent: "CodeGuard" };
|
|
3
|
+
/** A web rule: user input from a request reaches a sensitive call on the same line. */
|
|
4
|
+
function webRule(meta, js, py) {
|
|
5
|
+
return {
|
|
6
|
+
...b,
|
|
7
|
+
...meta,
|
|
8
|
+
confidence: "medium",
|
|
9
|
+
status: "potential",
|
|
10
|
+
applies: isCode,
|
|
11
|
+
check: (f) => lineRule(f, isPy(f) ? py : js, { skipComments: true, filter: (m, line) => !insideString(line, m.index) })
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
const webRules = [
|
|
15
|
+
webRule({
|
|
16
|
+
id: "VS-CODE-008",
|
|
17
|
+
title: "Request data used as a database query (NoSQL injection)",
|
|
18
|
+
severity: "high",
|
|
19
|
+
cwe: ["CWE-943"],
|
|
20
|
+
description: "A request object is passed straight into a query, or text is built into a `$where` clause. An attacker can send operators such as `$ne` or script and change what the query matches.",
|
|
21
|
+
remediation: "Pick the expected fields, check their type, and avoid `$where`.",
|
|
22
|
+
falsePositives: "Input already validated against a schema before this line.",
|
|
23
|
+
verification: "Send an object where a string is expected and confirm it is rejected."
|
|
24
|
+
}, /\.(?:find|findOne|findOneAndUpdate|updateOne|updateMany|deleteOne|deleteMany|countDocuments)\s*\(\s*req(?:uest)?\.(?:body|query)\s*[,)]|(?<![\w"'])\$where\s*:\s*(?:`[^`]*\$\{|["'][^"']*["']\s*\+|req(?:uest)?\.)/, /\.(?:find|find_one|update_one|delete_one|delete_many)\s*\(\s*request\.(?:json|get_json\(\))\s*[,)]/),
|
|
25
|
+
webRule({
|
|
26
|
+
id: "VS-CODE-009",
|
|
27
|
+
title: "Request data used as a file path (path traversal)",
|
|
28
|
+
severity: "high",
|
|
29
|
+
cwe: ["CWE-22"],
|
|
30
|
+
description: "A file is opened, sent or deleted using a request value, so `../` sequences can reach files outside the intended folder.",
|
|
31
|
+
remediation: "Resolve the path and require it to stay inside an allowed folder, or map an id to a known file name.",
|
|
32
|
+
falsePositives: "Values already reduced to a base name or checked against an allow list.",
|
|
33
|
+
verification: "Request `../` and an absolute path and confirm both are refused."
|
|
34
|
+
}, /\b(?:readFile|readFileSync|createReadStream|createWriteStream|sendFile|unlink|unlinkSync|writeFile|writeFileSync)\s*\(\s*req(?:uest)?\.(?:params|query|body)\b/, /(?<![\w.])(?:open|send_file)\s*\(\s*request\.(?:args|form|values)\b/),
|
|
35
|
+
webRule({
|
|
36
|
+
id: "VS-CODE-010",
|
|
37
|
+
title: "Request data used as an outbound URL (SSRF)",
|
|
38
|
+
severity: "high",
|
|
39
|
+
cwe: ["CWE-918"],
|
|
40
|
+
description: "The server fetches a URL taken from the request, which can reach internal services and cloud metadata endpoints.",
|
|
41
|
+
remediation: "Allow only known hosts, resolve and block private address ranges, and do not follow redirects to them.",
|
|
42
|
+
falsePositives: "A value checked against an allow list before this line.",
|
|
43
|
+
verification: "Request a loopback or link-local address and confirm it is refused."
|
|
44
|
+
}, /(?<![\w.])(?:fetch|got|axios(?:\.(?:get|post|put|delete|request))?)\s*\(\s*req(?:uest)?\.(?:params|query|body)\b/, /\b(?:requests|httpx)\.(?:get|post|put|delete|head)\s*\(\s*request\.(?:args|form|values)\b/),
|
|
45
|
+
webRule({
|
|
46
|
+
id: "VS-CODE-011",
|
|
47
|
+
title: "Redirect target taken from the request (open redirect)",
|
|
48
|
+
severity: "medium",
|
|
49
|
+
cwe: ["CWE-601"],
|
|
50
|
+
description: "The redirect destination comes from the request, so a link to this site can send people to an attacker's site.",
|
|
51
|
+
remediation: "Redirect to relative paths or to an allow list of destinations.",
|
|
52
|
+
falsePositives: "A value validated as a local path before this line.",
|
|
53
|
+
verification: "Request an external destination and confirm it is refused."
|
|
54
|
+
}, /\.redirect\s*\(\s*(?:\d{3}\s*,\s*)?req(?:uest)?\.(?:params|query|body)\b/, /(?<![\w.])redirect\s*\(\s*request\.(?:args|form|values)\b/),
|
|
55
|
+
{
|
|
56
|
+
...b,
|
|
57
|
+
id: "VS-CODE-012",
|
|
58
|
+
title: "Session cookie protection switched off",
|
|
59
|
+
severity: "medium",
|
|
60
|
+
confidence: "medium",
|
|
61
|
+
status: "potential",
|
|
62
|
+
cwe: ["CWE-1004", "CWE-614"],
|
|
63
|
+
description: "`httpOnly` or `secure` is disabled for a cookie, which exposes it to scripts or to unencrypted connections.",
|
|
64
|
+
remediation: "Set `httpOnly` and `secure` to true and choose a `sameSite` value.",
|
|
65
|
+
falsePositives: "Cookies that scripts must read, or settings used only for local development.",
|
|
66
|
+
verification: "The Set-Cookie response header carries HttpOnly and Secure.",
|
|
67
|
+
applies: isCode,
|
|
68
|
+
check: (f) => lineRule(f, /\b(?:httpOnly|httponly|secure|SESSION_COOKIE_(?:SECURE|HTTPONLY))\s*[:=]\s*(?:false|False)\b/, {
|
|
69
|
+
skipComments: true,
|
|
70
|
+
filter: (m, line, lineNo) => !insideString(line, m.index) && (/cookie|session/i.test(line) || lookBack(f, lineNo, 6, /cookie|session/i))
|
|
71
|
+
})
|
|
72
|
+
}
|
|
73
|
+
];
|
|
3
74
|
export const codeRules = [
|
|
4
75
|
{
|
|
5
76
|
...b,
|
|
@@ -146,5 +217,6 @@ export const codeRules = [
|
|
|
146
217
|
// and content passed through an obvious sanitizer.
|
|
147
218
|
filter: (m, line) => !insideString(line, m.index) && !/\.innerHTML\s*=\s*(?:"[^"]*"|'[^']*'|`[^`$]*`)\s*;?\s*(?:\/\/.*)?$/.test(line) && !/sanitiz|DOMPurify|escapeHtml|xss\(/i.test(line)
|
|
148
219
|
})
|
|
149
|
-
}
|
|
220
|
+
},
|
|
221
|
+
...webRules
|
|
150
222
|
];
|
package/dist/rules/index.js
CHANGED
|
@@ -3,7 +3,7 @@ import { infraRules } from "./infra.js";
|
|
|
3
3
|
import { codeRules } from "./code.js";
|
|
4
4
|
import { agentPrivacyRules, processRules } from "./agents-privacy.js";
|
|
5
5
|
import { supplyRules } from "./supply.js";
|
|
6
|
-
export const RULES_VERSION = "1.
|
|
6
|
+
export const RULES_VERSION = "1.6.0";
|
|
7
7
|
export const ALL_RULES = [...secretRules, ...infraRules, ...codeRules, ...agentPrivacyRules, ...processRules, ...supplyRules];
|
|
8
8
|
export const CWE_NOTE = "CWE ids are orientation only, taken from the MITRE CWE list; no OWASP/NIST control mapping is asserted (mapping unverified).";
|
|
9
9
|
export function findRule(id) {
|
package/dist/rules/infra.js
CHANGED
|
@@ -1,9 +1,159 @@
|
|
|
1
|
-
import { isDockerfile, isK8s, isTerraform, isWorkflow, isYaml, lineRule, lookBack } from "./util.js";
|
|
1
|
+
import { isDockerfile, isK8s, isTerraform, isWorkflow, isYaml, lineRule, lookBack, enclosingBlock } from "./util.js";
|
|
2
2
|
const wf = { version: "1.0.0", category: "cicd", agent: "PipelineGuard" };
|
|
3
3
|
const dk = { version: "1.0.0", category: "containers", agent: "ContainerGuard" };
|
|
4
4
|
const k8 = { version: "1.0.0", category: "containers", agent: "ContainerGuard" };
|
|
5
5
|
const iac = { version: "1.0.0", category: "iac", agent: "InfraGuard" };
|
|
6
|
+
const isCompose = (f) => isYaml(f) && /^(?:docker-)?compose(?:[.-].*)?\.ya?ml$/.test(f.name);
|
|
7
|
+
const isRbac = (f) => isYaml(f) && /^[ \t]*kind:[ \t]*(?:Cluster)?Role[ \t]*$/m.test(f.text);
|
|
8
|
+
/** The key that owns a YAML list line: the line itself, or the nearest key above its `- item` lines. */
|
|
9
|
+
function listKey(f, lineNo) {
|
|
10
|
+
for (let i = lineNo - 1; i >= 0; i--) {
|
|
11
|
+
const key = /^\s*(?:-\s+)?([A-Za-z_]+):/.exec(f.lines[i]);
|
|
12
|
+
if (key)
|
|
13
|
+
return key[1];
|
|
14
|
+
if (!/^\s*(?:-|#|$)/.test(f.lines[i]))
|
|
15
|
+
break;
|
|
16
|
+
}
|
|
17
|
+
return "";
|
|
18
|
+
}
|
|
19
|
+
const isDbConfig = (f) => isYaml(f) || isDockerfile(f) || /\.(?:env|conf|cfg|ini|properties|sh)$|^\.env/.test(f.name);
|
|
6
20
|
export const infraRules = [
|
|
21
|
+
{
|
|
22
|
+
...iac,
|
|
23
|
+
id: "VS-DB-001",
|
|
24
|
+
title: "Database accepts connections without a password",
|
|
25
|
+
severity: "high",
|
|
26
|
+
confidence: "high",
|
|
27
|
+
status: "potential",
|
|
28
|
+
cwe: ["CWE-306"],
|
|
29
|
+
description: "A setting lets a database accept logins with no password (PostgreSQL `trust`, or an allowed empty MySQL or MariaDB password).",
|
|
30
|
+
remediation: "Require a password from a secret store and use `scram-sha-256` for PostgreSQL.",
|
|
31
|
+
falsePositives: "Throw-away databases in local development or CI that no other host can reach.",
|
|
32
|
+
verification: "A connection without a password is refused.",
|
|
33
|
+
applies: isDbConfig,
|
|
34
|
+
check: (f) => lineRule(f, /\bPOSTGRES_HOST_AUTH_METHOD["']?\s*[:=]\s*["']?trust\b|\b(?:MYSQL|MARIADB)?_?ALLOW_EMPTY(?:_ROOT)?_PASSWORD["']?\s*[:=]\s*["']?(?:yes|true|1)\b/i, { skipComments: true })
|
|
35
|
+
},
|
|
36
|
+
{
|
|
37
|
+
...iac,
|
|
38
|
+
id: "VS-DB-002",
|
|
39
|
+
title: "Database or search engine security switched off",
|
|
40
|
+
severity: "high",
|
|
41
|
+
confidence: "medium",
|
|
42
|
+
status: "potential",
|
|
43
|
+
cwe: ["CWE-306"],
|
|
44
|
+
description: "Redis protected mode, Elasticsearch or OpenSearch security, or MongoDB authorization is disabled.",
|
|
45
|
+
remediation: "Enable authentication and bind the service to a private network.",
|
|
46
|
+
falsePositives: "Local development setups that no other host can reach.",
|
|
47
|
+
verification: "An unauthenticated request is refused.",
|
|
48
|
+
applies: isDbConfig,
|
|
49
|
+
check: (f) => lineRule(f, /^\s*protected-mode\s+no\b|\bxpack\.security\.enabled["']?\s*[:=]\s*["']?false\b|\bplugins\.security\.disabled["']?\s*[:=]\s*["']?true\b|^\s*authorization:\s*["']?disabled\b/, { skipComments: true })
|
|
50
|
+
},
|
|
51
|
+
{
|
|
52
|
+
...iac,
|
|
53
|
+
id: "VS-IAC-004",
|
|
54
|
+
title: "Database or storage exposed publicly",
|
|
55
|
+
severity: "high",
|
|
56
|
+
confidence: "high",
|
|
57
|
+
status: "potential",
|
|
58
|
+
cwe: ["CWE-284"],
|
|
59
|
+
description: "A Terraform setting makes a database publicly accessible, or turns off a block on public storage access.",
|
|
60
|
+
remediation: "Keep databases in private networks and leave public access blocks enabled.",
|
|
61
|
+
falsePositives: "Buckets or databases that are public by documented design.",
|
|
62
|
+
verification: "The plan shows public access disabled.",
|
|
63
|
+
applies: isTerraform,
|
|
64
|
+
check: (f) => lineRule(f, /^\s*(?:publicly_accessible|allow_blob_public_access|allow_nested_items_to_be_public)\s*=\s*true\b|^\s*(?:block_public_acls|block_public_policy|ignore_public_acls|restrict_public_buckets)\s*=\s*false\b/)
|
|
65
|
+
},
|
|
66
|
+
{
|
|
67
|
+
...iac,
|
|
68
|
+
id: "VS-IAC-005",
|
|
69
|
+
title: "Access granted to everyone",
|
|
70
|
+
severity: "high",
|
|
71
|
+
confidence: "medium",
|
|
72
|
+
status: "potential",
|
|
73
|
+
cwe: ["CWE-732"],
|
|
74
|
+
description: "A policy grants access to `*`, `allUsers` or `allAuthenticatedUsers`.",
|
|
75
|
+
remediation: "Name the accounts, roles or service principals that need access, or add a restricting condition.",
|
|
76
|
+
falsePositives: "Public website objects, and statements narrowed by a condition. Check the condition.",
|
|
77
|
+
verification: "The grant names specific principals.",
|
|
78
|
+
applies: (f) => isTerraform(f) || f.ext === ".json" || isYaml(f),
|
|
79
|
+
check: (f) => lineRule(f, /["'](?:allUsers|allAuthenticatedUsers)["']|["']?Principal["']?\s*[:=]\s*(?:\{\s*["']?AWS["']?\s*[:=]\s*)?["']\*["']/, {
|
|
80
|
+
skipComments: true,
|
|
81
|
+
// A Deny statement for everyone is a guardrail.
|
|
82
|
+
filter: (_m, _line, lineNo) => !/\b[Ee]ffect["']?\s*[:=]\s*["']Deny["']/.test(enclosingBlock(f, lineNo).body)
|
|
83
|
+
})
|
|
84
|
+
},
|
|
85
|
+
{
|
|
86
|
+
...k8,
|
|
87
|
+
id: "VS-K8S-004",
|
|
88
|
+
title: "Pod mounts a host path",
|
|
89
|
+
severity: "medium",
|
|
90
|
+
confidence: "high",
|
|
91
|
+
status: "potential",
|
|
92
|
+
cwe: ["CWE-668"],
|
|
93
|
+
description: "A `hostPath` volume exposes files of the node to the pod. A writable or sensitive path lets a compromised container take over the node.",
|
|
94
|
+
remediation: "Use a persistent volume, ConfigMap or emptyDir. If a node agent needs the path, mount it read-only and as narrowly as possible.",
|
|
95
|
+
falsePositives: "Node agents such as log collectors that must read one specific path.",
|
|
96
|
+
verification: "No `hostPath` volume remains, or the exception is documented and read-only.",
|
|
97
|
+
applies: isK8s,
|
|
98
|
+
check: (f) => lineRule(f, /^\s*(?:-\s+)?hostPath:\s*(?:\{|$)/)
|
|
99
|
+
},
|
|
100
|
+
{
|
|
101
|
+
...k8,
|
|
102
|
+
id: "VS-K8S-005",
|
|
103
|
+
title: "Container adds a dangerous Linux capability",
|
|
104
|
+
severity: "high",
|
|
105
|
+
confidence: "medium",
|
|
106
|
+
status: "potential",
|
|
107
|
+
cwe: ["CWE-250"],
|
|
108
|
+
description: "Adding `ALL`, `SYS_ADMIN`, `SYS_MODULE`, `SYS_PTRACE` or `NET_ADMIN` gives the container powers close to root on the host.",
|
|
109
|
+
remediation: "Drop all capabilities and add only a narrow one the workload is shown to need.",
|
|
110
|
+
falsePositives: "Network and storage agents that need the capability.",
|
|
111
|
+
verification: "The capability is gone from `add` or `cap_add` and the workload still runs.",
|
|
112
|
+
applies: (f) => isK8s(f) || isCompose(f),
|
|
113
|
+
check: (f) => lineRule(f, /\b(?:ALL|SYS_ADMIN|SYS_MODULE|SYS_PTRACE|NET_ADMIN)\b/, {
|
|
114
|
+
skipComments: true,
|
|
115
|
+
filter: (_m, line, lineNo) => {
|
|
116
|
+
const inline = /\b(add|cap_add|drop|cap_drop):/.exec(line);
|
|
117
|
+
return ["add", "cap_add"].includes(inline ? inline[1] : listKey(f, lineNo));
|
|
118
|
+
}
|
|
119
|
+
})
|
|
120
|
+
},
|
|
121
|
+
{
|
|
122
|
+
...k8,
|
|
123
|
+
id: "VS-K8S-006",
|
|
124
|
+
title: "RBAC role grants a wildcard",
|
|
125
|
+
severity: "high",
|
|
126
|
+
confidence: "high",
|
|
127
|
+
status: "potential",
|
|
128
|
+
cwe: ["CWE-269"],
|
|
129
|
+
description: "A Role or ClusterRole lists `*` as a verb or resource, which grants far more than a workload needs.",
|
|
130
|
+
remediation: "List the exact verbs and resources, and prefer a namespaced Role.",
|
|
131
|
+
falsePositives: "Cluster administration roles that are bound only to operators.",
|
|
132
|
+
verification: "No `*` remains in `verbs` or `resources`.",
|
|
133
|
+
applies: isRbac,
|
|
134
|
+
check: (f) => lineRule(f, /["']\*["']/, {
|
|
135
|
+
skipComments: true,
|
|
136
|
+
filter: (_m, line, lineNo) => {
|
|
137
|
+
const inline = /\b(verbs|resources):/.exec(line);
|
|
138
|
+
return ["verbs", "resources"].includes(inline ? inline[1] : listKey(f, lineNo));
|
|
139
|
+
}
|
|
140
|
+
})
|
|
141
|
+
},
|
|
142
|
+
{
|
|
143
|
+
...k8,
|
|
144
|
+
id: "VS-COMPOSE-001",
|
|
145
|
+
title: "Compose service has host-level access",
|
|
146
|
+
severity: "high",
|
|
147
|
+
confidence: "high",
|
|
148
|
+
status: "confirmed",
|
|
149
|
+
cwe: ["CWE-250"],
|
|
150
|
+
description: "`privileged: true`, host network or PID mode, or a mounted Docker socket lets the service control the host.",
|
|
151
|
+
remediation: "Remove the setting. If a tool needs the Docker API, put a filtering proxy in front of the socket.",
|
|
152
|
+
falsePositives: "Local development tools that manage containers by design.",
|
|
153
|
+
verification: "The setting is gone and the service still starts.",
|
|
154
|
+
applies: isCompose,
|
|
155
|
+
check: (f) => lineRule(f, /^\s*privileged:\s*true\b|^\s*(?:network_mode|pid):\s*["']?host\b|^\s*-\s*["']?\/var\/run\/docker\.sock:/)
|
|
156
|
+
},
|
|
7
157
|
{
|
|
8
158
|
...wf,
|
|
9
159
|
id: "VS-CICD-001",
|
|
@@ -42,7 +192,7 @@ export const infraRules = [
|
|
|
42
192
|
check: (f) => {
|
|
43
193
|
if (!/^[ \t]*pull_request_target[ \t]*:|\bon:[ \t]*\[?[^\n]*pull_request_target/m.test(f.text))
|
|
44
194
|
return [];
|
|
45
|
-
return lineRule(f, /ref:\s
|
|
195
|
+
return lineRule(f, /ref:\s*["']?(?:\$\{\{\s*github\.(?:event\.pull_request\.head\.(?:sha|ref)|head_ref)\s*\}\}|refs\/pull\/\$\{\{[^}]*\}\}\/(?:merge|head))/);
|
|
46
196
|
}
|
|
47
197
|
},
|
|
48
198
|
{
|
|
@@ -63,12 +213,12 @@ export const infraRules = [
|
|
|
63
213
|
{
|
|
64
214
|
...wf,
|
|
65
215
|
id: "VS-CICD-004",
|
|
66
|
-
title: "Untrusted GitHub context interpolated into a
|
|
216
|
+
title: "Untrusted GitHub context interpolated into a script step",
|
|
67
217
|
severity: "high",
|
|
68
218
|
confidence: "medium",
|
|
69
219
|
status: "potential",
|
|
70
220
|
cwe: ["CWE-78", "CWE-77"],
|
|
71
|
-
description: "Attacker-controlled fields (PR title/body, branch name, issue or comment text) are expanded directly inside a `run:` script. A crafted value can inject
|
|
221
|
+
description: "Attacker-controlled fields (PR title/body, branch name, issue or comment text) are expanded directly inside a `run:` or `actions/github-script` script. A crafted value can inject commands.",
|
|
72
222
|
remediation: "Pass the value through an environment variable (`env: TITLE: ${{ github.event.pull_request.title }}`) and reference it as `\"$TITLE\"` in the script.",
|
|
73
223
|
falsePositives: "Fields restricted by GitHub to safe characters. Only matches inside run blocks are reported when detectable; verify context.",
|
|
74
224
|
verification: "The expression no longer appears inside a `run:` body.",
|
|
@@ -78,13 +228,15 @@ export const infraRules = [
|
|
|
78
228
|
let inRun = false;
|
|
79
229
|
let runIndent = 0;
|
|
80
230
|
f.lines.forEach((line, i) => {
|
|
81
|
-
const
|
|
231
|
+
const found = /^(\s*)(?:-\s*)?(run|script):\s*(.*)$/.exec(line);
|
|
232
|
+
// `script:` is code only as an input of actions/github-script.
|
|
233
|
+
const run = found && (found[2] === "run" || lookBack(f, i + 1, 8, /uses:\s*["']?actions\/github-script@/)) ? found : null;
|
|
82
234
|
const indent = line.search(/\S|$/);
|
|
83
235
|
if (run) {
|
|
84
236
|
inRun = true;
|
|
85
237
|
runIndent = run[1].length;
|
|
86
|
-
if (run[
|
|
87
|
-
if (risky(run[
|
|
238
|
+
if (run[3] && !/^[|>][-+]?$/.test(run[3])) {
|
|
239
|
+
if (risky(run[3]))
|
|
88
240
|
hits.push({ line: i + 1, excerpt: line });
|
|
89
241
|
inRun = false;
|
|
90
242
|
}
|
|
@@ -226,18 +378,21 @@ export const infraRules = [
|
|
|
226
378
|
confidence: "medium",
|
|
227
379
|
status: "potential",
|
|
228
380
|
cwe: ["CWE-284"],
|
|
229
|
-
description: "A Terraform rule allows 0.0.0.0/0
|
|
381
|
+
description: "A Terraform rule allows 0.0.0.0/0, ::/0 or an Azure `*`/`Internet` source. Open ingress to administrative or database ports exposes them to internet scanning and attack.",
|
|
230
382
|
remediation: "Restrict CIDR ranges to known addresses or place the service behind a load balancer, VPN or bastion.",
|
|
231
383
|
falsePositives: "Intentionally public web ports (80/443) and egress rules. Review the port and direction.",
|
|
232
384
|
verification: "Ingress CIDRs are restricted or documented as intentionally public.",
|
|
233
385
|
applies: isTerraform,
|
|
234
|
-
check: (f) => lineRule(f, /["'](?:0\.0\.0\.0\/0|::\/0)["']/, {
|
|
386
|
+
check: (f) => lineRule(f, /["'](?:0\.0\.0\.0\/0|::\/0)["']|\bsource_address_prefix(?:es)?\s*=\s*\[?\s*["'](?:\*|Internet)["']/, {
|
|
235
387
|
skipComments: true,
|
|
236
388
|
filter: (_m, line, lineNo) => {
|
|
237
|
-
// Routes and
|
|
238
|
-
if (/destination_(?:ipv6_)?cidr_block/.test(line))
|
|
389
|
+
// Routes, egress rules and deny rules are not internet-facing ingress. Only the enclosing blocks count.
|
|
390
|
+
if (/destination_(?:ipv6_)?(?:cidr_block|address)|\begress\b|egress_/.test(line))
|
|
391
|
+
return false;
|
|
392
|
+
const { openers, body } = enclosingBlock(f, lineNo);
|
|
393
|
+
if (openers.some((o) => /^\s*(?:route|egress|deny)\b/.test(o) || /egress_/.test(o)))
|
|
239
394
|
return false;
|
|
240
|
-
return
|
|
395
|
+
return !/\btype\s*=\s*["']egress["']|\bdirection\s*=\s*["'](?:egress|outbound)["']|\baccess\s*=\s*["']deny["']|^\s*deny\s*\{/im.test(body);
|
|
241
396
|
}
|
|
242
397
|
})
|
|
243
398
|
},
|
|
@@ -269,9 +424,23 @@ export const infraRules = [
|
|
|
269
424
|
falsePositives: "Deny statements and break-glass roles that are tightly controlled. Check Effect.",
|
|
270
425
|
verification: "Wildcard replaced with an explicit action list.",
|
|
271
426
|
applies: (f) => isTerraform(f) || f.ext === ".json" || isYaml(f),
|
|
272
|
-
check: (f) => lineRule(f, /["']
|
|
273
|
-
|
|
274
|
-
|
|
427
|
+
check: (f) => lineRule(f, /["']\*["']/, {
|
|
428
|
+
filter: (_m, line, lineNo) => {
|
|
429
|
+
let isAction = /\b(?:Action|actions)["']?\s*[:=]\s*(?:\[[^\]]*)?["']\*["']/i.test(line);
|
|
430
|
+
// A `*` on its own line inside a multi-line action list.
|
|
431
|
+
for (let i = lineNo - 2; !isAction && !line.includes("=") && i >= 0 && !f.lines[i].includes("]"); i--) {
|
|
432
|
+
if (/\b(?:Action|actions)["']?\s*[:=]\s*\[/i.test(f.lines[i]))
|
|
433
|
+
isAction = true;
|
|
434
|
+
else if (/[:=]\s*\[/.test(f.lines[i]))
|
|
435
|
+
break;
|
|
436
|
+
}
|
|
437
|
+
if (!isAction)
|
|
438
|
+
return false;
|
|
439
|
+
// A Deny statement with a wildcard is a guardrail, not a grant.
|
|
440
|
+
const deny = /\b[Ee]ffect["']?\s*[:=]\s*["']Deny["']/;
|
|
441
|
+
const { body } = enclosingBlock(f, lineNo);
|
|
442
|
+
return !(body ? deny.test(body) : lookBack(f, lineNo, 4, deny));
|
|
443
|
+
}
|
|
275
444
|
})
|
|
276
445
|
}
|
|
277
446
|
];
|
package/dist/rules/secrets.js
CHANGED
|
@@ -1,4 +1,10 @@
|
|
|
1
1
|
import { isPlaceholder, lineRule } from "./util.js";
|
|
2
|
+
const SECRET_NAME = String.raw `(?:pass(?:word|wd)?|secret|api[_-]?key|auth[_-]?token|access[_-]?token|private[_-]?key|client[_-]?secret|crypto[_-]?key|\btoken)`;
|
|
3
|
+
const QUOTED = new RegExp(SECRET_NAME + String.raw `["']?\s*[:=]\s*["']([^"'\s]{8,})["']`, "i");
|
|
4
|
+
// `key: value` and `KEY=value` lines in configuration files, where quotes are optional.
|
|
5
|
+
const UNQUOTED = /^\s*(?:-\s+)?(?:(?:export|ENV|set)\s+)?[\w./:-]*?(?:pass(?:word|wd)?|secret|api[_-]?key|token|private[_-]?key|crypto[_-]?key)\s*[:=]\s*([^\s"'#,;{}[\]<>$`|&()]{8,})\s*(?:#.*)?$/i;
|
|
6
|
+
const CONFIG_FILE = /\.(ya?ml|properties|ini|conf|cfg|toml|sh|bash)$|^\.npmrc$|^dockerfile|^\.env.*\.(example|sample|template)$/;
|
|
7
|
+
const realValue = (v) => !isPlaceholder(v) && !/^\$\{?\w+\}?$/.test(v) && /[A-Za-z0-9]/.test(v);
|
|
2
8
|
const base = {
|
|
3
9
|
version: "1.0.0",
|
|
4
10
|
category: "secrets",
|
|
@@ -33,7 +39,7 @@ export const secretRules = [
|
|
|
33
39
|
applies: () => true,
|
|
34
40
|
check: (f) => {
|
|
35
41
|
const hits = lineRule(f, /-----BEGIN (?:RSA |EC |DSA |OPENSSH |ENCRYPTED |PGP )?PRIVATE KEY(?: BLOCK)?-----/);
|
|
36
|
-
return hits.map((h) => ({ ...h, excerpt: "-----BEGIN PRIVATE KEY----- [REDACTED]",
|
|
42
|
+
return hits.map((h) => ({ ...h, excerpt: "-----BEGIN PRIVATE KEY----- [REDACTED]", at: undefined, secrets: undefined }));
|
|
37
43
|
}
|
|
38
44
|
},
|
|
39
45
|
{
|
|
@@ -58,13 +64,17 @@ export const secretRules = [
|
|
|
58
64
|
description: "A variable or key named like a password, secret, token or API key is assigned a non-placeholder string literal.",
|
|
59
65
|
falsePositives: "Test data, example config, i18n labels. Values that look like placeholders, environment lookups or templates are skipped.",
|
|
60
66
|
applies: (f) => !/\.(md|txt|lock)$/.test(f.name) && !/(^|\/)(i18n|locales?|lang|translations?)\//i.test(f.path),
|
|
61
|
-
check: (f) =>
|
|
62
|
-
skipComments: true,
|
|
63
|
-
secretGroup: 1,
|
|
67
|
+
check: (f) => {
|
|
64
68
|
// Skip placeholders and variable references such as '$password' used in templates or SQL text.
|
|
65
69
|
// A real credential contains ASCII letters or digits; UI labels in other scripts do not.
|
|
66
|
-
filter: (m) =>
|
|
67
|
-
|
|
70
|
+
const quoted = lineRule(f, QUOTED, { skipComments: true, secretGroup: 1, filter: (m) => realValue(m[1]) });
|
|
71
|
+
if (!CONFIG_FILE.test(f.name))
|
|
72
|
+
return quoted;
|
|
73
|
+
// Without quotes, a plain word or a file path is usually a name or a reference, so require a digit or symbol.
|
|
74
|
+
// No comment skipping: `.npmrc` keys start with `//`, and UNQUOTED cannot match a `#` comment.
|
|
75
|
+
const bare = lineRule(f, UNQUOTED, { secretGroup: 1, filter: (m) => realValue(m[1]) && /[\d!@%^*+=]/.test(m[1]) && !/^(\d+|[./~].*)$/.test(m[1]) });
|
|
76
|
+
return [...quoted, ...bare.filter((h) => !quoted.some((q) => q.line === h.line))];
|
|
77
|
+
}
|
|
68
78
|
},
|
|
69
79
|
{
|
|
70
80
|
...base,
|
|
@@ -87,7 +97,7 @@ export const secretRules = [
|
|
|
87
97
|
status: "potential",
|
|
88
98
|
cwe: ["CWE-312", "CWE-798"],
|
|
89
99
|
description: "A `.env` file (not an example, sample, or template) contains a secret-named variable with a non-placeholder value. File contents are not echoed. When a git index is present, environment files that are not listed in it are not read. When there is no usable git index, the file is still reviewed.",
|
|
90
|
-
falsePositives: "Placeholder values and non-secret settings. `.env.example`, `.env.sample`, and `.env.template` are
|
|
100
|
+
falsePositives: "Placeholder values and non-secret settings. `.env.example`, `.env.sample`, and `.env.template` are left to VS-SECRET-004, which reports values that look real. A local `.env` that git does not track is not a finding.",
|
|
91
101
|
applies: (f) => /^\.env(\..+)?$/.test(f.name) && !/\.(example|sample|template)$/.test(f.name),
|
|
92
102
|
check: (f) => {
|
|
93
103
|
const idx = f.lines.findIndex((l) => {
|
package/dist/rules/util.js
CHANGED
|
@@ -12,12 +12,15 @@ export const isK8s = (f) => isYaml(f) && /^[ \t]*apiVersion:/m.test(f.text) && /
|
|
|
12
12
|
export function isCommentLine(line) {
|
|
13
13
|
return /^\s*(\/\/|#|\*|\/\*|<!--)/.test(line);
|
|
14
14
|
}
|
|
15
|
+
const FILLER = new Set("your my example change me changeme placeholder dummy sample test todo password secret none null undefined key token api here value xxx".split(" "));
|
|
15
16
|
/** Values that are clearly placeholders, examples or templating. */
|
|
16
17
|
export function isPlaceholder(value) {
|
|
17
18
|
const v = value.trim();
|
|
18
19
|
return (v.length < 8 ||
|
|
19
20
|
/^\[?redacted\]?$/i.test(v) ||
|
|
20
|
-
/^(x+|\*+|\.+|<.*>|\$\{.*\}|\{\{.*\}\}
|
|
21
|
+
/^(x+|\*+|\.+|<.*>|\$\{.*\}|\{\{.*\}\}|%.*%)$|^(your[-_ ]|process\.env|os\.environ|env\()/i.test(v) ||
|
|
22
|
+
// Only a value made entirely of filler words is a placeholder: "test-password-123" is, "password-Xk9fQ2" is not.
|
|
23
|
+
v.toLowerCase().split(/[-_ .!]+|\d+/).every((w) => !w || FILLER.has(w)) ||
|
|
21
24
|
/(^|[^a-z])(example|placeholder|dummy|changeme|xxxx)/i.test(v));
|
|
22
25
|
}
|
|
23
26
|
export function lineRule(file, re, opts = {}) {
|
|
@@ -27,24 +30,30 @@ export function lineRule(file, re, opts = {}) {
|
|
|
27
30
|
// Long lines are scanned in overlapping windows so a token past 2,000 characters is still seen.
|
|
28
31
|
// The excerpt is the window, not the whole line.
|
|
29
32
|
const parts = line.length > 2000 ? windows(line, 2000, 1900) : [{ text: line }];
|
|
30
|
-
|
|
33
|
+
let hit;
|
|
34
|
+
const secrets = [];
|
|
35
|
+
const matched = new Set(); // line offsets; windows overlap
|
|
36
|
+
for (const [p, part] of parts.entries()) {
|
|
31
37
|
if (opts.skipComments && isCommentLine(part.text))
|
|
32
38
|
continue;
|
|
33
39
|
g.lastIndex = 0;
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
40
|
+
// Every match is read: to count them, and so that no second secret on the line survives in an excerpt.
|
|
41
|
+
for (let m = g.exec(part.text); m; m = g.exec(part.text)) {
|
|
42
|
+
if (opts.secretGroup !== undefined)
|
|
43
|
+
secrets.push(m[opts.secretGroup] ?? m[0]);
|
|
44
|
+
if (!opts.filter || opts.filter(m, part.text, i + 1)) {
|
|
45
|
+
hit ??= { line: i + 1, excerpt: part.text, at: m.index };
|
|
46
|
+
matched.add(p * 1900 + m.index);
|
|
47
|
+
}
|
|
48
|
+
if (!m[0])
|
|
49
|
+
g.lastIndex++;
|
|
44
50
|
}
|
|
45
|
-
hits.push(hit);
|
|
46
|
-
break;
|
|
47
51
|
}
|
|
52
|
+
if (!hit)
|
|
53
|
+
return;
|
|
54
|
+
if (matched.size > 1)
|
|
55
|
+
hit.note = `${matched.size - 1} more match(es) on this line.`;
|
|
56
|
+
hits.push(secrets.length ? { ...hit, secrets } : hit);
|
|
48
57
|
});
|
|
49
58
|
return hits;
|
|
50
59
|
}
|
|
@@ -77,6 +86,37 @@ export function insideString(line, idx) {
|
|
|
77
86
|
export function stripStrings(line) {
|
|
78
87
|
return line.replace(/"(?:[^"\\]|\\.)*"|'(?:[^'\\]|\\.)*'|`(?:[^`\\]|\\.)*`/g, '""');
|
|
79
88
|
}
|
|
89
|
+
const blockCache = new WeakMap();
|
|
90
|
+
/**
|
|
91
|
+
* The `{}` blocks around a line: the opener lines, outermost first, and the text of the innermost block.
|
|
92
|
+
* Braces are counted per line; comment lines are ignored.
|
|
93
|
+
*/
|
|
94
|
+
export function enclosingBlock(f, line) {
|
|
95
|
+
let b = blockCache.get(f);
|
|
96
|
+
if (!b) {
|
|
97
|
+
const open = [];
|
|
98
|
+
const end = new Map();
|
|
99
|
+
const stack = [];
|
|
100
|
+
f.lines.forEach((text, i) => {
|
|
101
|
+
open.push(stack.slice(-8));
|
|
102
|
+
if (isCommentLine(text))
|
|
103
|
+
return;
|
|
104
|
+
for (const c of text) {
|
|
105
|
+
if (c === "{")
|
|
106
|
+
stack.push(i);
|
|
107
|
+
else if (c === "}" && stack.length)
|
|
108
|
+
end.set(stack.pop(), i);
|
|
109
|
+
}
|
|
110
|
+
});
|
|
111
|
+
blockCache.set(f, (b = { open, end }));
|
|
112
|
+
}
|
|
113
|
+
const chain = b.open[line - 1] ?? [];
|
|
114
|
+
const inner = chain[chain.length - 1];
|
|
115
|
+
return {
|
|
116
|
+
openers: chain.map((i) => f.lines[i]),
|
|
117
|
+
body: inner === undefined ? "" : f.lines.slice(inner, (b.end.get(inner) ?? f.lines.length - 1) + 1).join("\n")
|
|
118
|
+
};
|
|
119
|
+
}
|
|
80
120
|
/** Looks back up to `n` lines (inclusive of the hit line) for a pattern. */
|
|
81
121
|
export function lookBack(f, line, n, re) {
|
|
82
122
|
for (let i = Math.max(0, line - 1 - n); i <= line - 1; i++)
|
package/dist/scope.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { lstatSync, readdirSync, realpathSync, openSync, readSync, closeSync, fstatSync } from "node:fs";
|
|
2
2
|
import { join, basename, extname, relative, isAbsolute } from "node:path";
|
|
3
3
|
import { makeIgnoreMatcher } from "./policy.js";
|
|
4
|
+
import { decodeText } from "./decode.js";
|
|
4
5
|
import { createGitCache, sameIdentity, shouldSkipUntrackedDotenv } from "./git-index.js";
|
|
5
6
|
/** True when `p` is `root` or lies beneath it (case-insensitive on Windows). */
|
|
6
7
|
export function isInside(root, p) {
|
|
@@ -27,11 +28,7 @@ function readChecked(abs, expected) {
|
|
|
27
28
|
break;
|
|
28
29
|
off += n;
|
|
29
30
|
}
|
|
30
|
-
|
|
31
|
-
for (let i = 0; i < head; i++)
|
|
32
|
-
if (buf[i] === 0)
|
|
33
|
-
return "binary";
|
|
34
|
-
return buf.toString("utf8", 0, off);
|
|
31
|
+
return decodeText(buf.subarray(0, off)) ?? "binary";
|
|
35
32
|
}
|
|
36
33
|
finally {
|
|
37
34
|
closeSync(fd);
|
package/dist/suggest.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/** The candidate within two edits of `input`, if there is one. */
|
|
2
|
+
export function closest(input, candidates) {
|
|
3
|
+
const distance = (a, b) => {
|
|
4
|
+
let row = Array.from({ length: b.length + 1 }, (_v, i) => i);
|
|
5
|
+
for (let i = 1; i <= a.length; i++) {
|
|
6
|
+
const next = [i];
|
|
7
|
+
for (let j = 1; j <= b.length; j++)
|
|
8
|
+
next[j] = Math.min(next[j - 1] + 1, row[j] + 1, row[j - 1] + (a[i - 1] === b[j - 1] ? 0 : 1));
|
|
9
|
+
row = next;
|
|
10
|
+
}
|
|
11
|
+
return row[b.length];
|
|
12
|
+
};
|
|
13
|
+
// Long input cannot be a typo of a short word, and skipping it bounds the work.
|
|
14
|
+
if (input.length > 40)
|
|
15
|
+
return undefined;
|
|
16
|
+
const [best] = candidates.map((c) => [distance(input, c), c]).sort((a, b) => a[0] - b[0]);
|
|
17
|
+
return best && best[0] <= 2 ? best[1] : undefined;
|
|
18
|
+
}
|