@hardkeel/benthic 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +121 -0
- package/LICENSE +92 -0
- package/NOTICE +16 -0
- package/README.md +424 -2
- package/action.yml +54 -0
- package/dist/agility.js +80 -0
- package/dist/badge.js +62 -0
- package/dist/baseline.js +81 -0
- package/dist/cbom-diff.js +273 -0
- package/dist/cbom.js +418 -0
- package/dist/cli.js +834 -0
- package/dist/cloud-aws-services.js +375 -0
- package/dist/cloud-aws.js +354 -0
- package/dist/cloud-azure-services.js +73 -0
- package/dist/cloud-azure.js +144 -0
- package/dist/cloud-common.js +242 -0
- package/dist/cloud-gcp-services.js +90 -0
- package/dist/cloud-gcp.js +150 -0
- package/dist/comments.js +138 -0
- package/dist/community-rules.js +37 -0
- package/dist/community.js +30 -0
- package/dist/deps.js +406 -0
- package/dist/der.js +571 -0
- package/dist/evidence-cli.js +235 -0
- package/dist/evidence-live.js +109 -0
- package/dist/evidence.js +512 -0
- package/dist/ignore.js +90 -0
- package/dist/image.js +1224 -0
- package/dist/infra.js +635 -0
- package/dist/inventory.js +277 -0
- package/dist/map-activity.js +24 -0
- package/dist/map-areas.js +60 -0
- package/dist/map-cloud-keys.js +110 -0
- package/dist/map-cloud.js +34 -0
- package/dist/map-ct.js +107 -0
- package/dist/map-host.js +335 -0
- package/dist/map-network.js +520 -0
- package/dist/map-people.js +79 -0
- package/dist/map-rank.js +70 -0
- package/dist/map-report.js +304 -0
- package/dist/map-run.js +327 -0
- package/dist/map-sarif.js +61 -0
- package/dist/map-signin.js +247 -0
- package/dist/map-types.js +19 -0
- package/dist/map-web.js +666 -0
- package/dist/native.js +168 -0
- package/dist/policy.js +221 -0
- package/dist/pqc.js +46 -0
- package/dist/profile.js +65 -0
- package/dist/project.js +928 -0
- package/dist/prose.js +159 -0
- package/dist/protocols.js +207 -0
- package/dist/registry.js +326 -0
- package/dist/report.js +190 -0
- package/dist/rules.js +230 -0
- package/dist/sarif-common.js +30 -0
- package/dist/sarif.js +114 -0
- package/dist/scan.js +601 -0
- package/dist/score.js +124 -0
- package/dist/secrets.js +192 -0
- package/dist/share-config.js +129 -0
- package/dist/share-lists.js +19 -0
- package/dist/share.js +125 -0
- package/dist/sonar.js +83 -0
- package/dist/structure.js +218 -0
- package/dist/suites.js +402 -0
- package/dist/tls-check.js +122 -0
- package/dist/types.js +4 -0
- package/dist/walk.js +78 -0
- package/dist/yaml.js +173 -0
- package/package.json +73 -4
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
// SPDX-License-Identifier: PolyForm-Shield-1.0.0
|
|
2
|
+
// Copyright 2026 Hard Keel (https://hardkeel.com)
|
|
3
|
+
// Shared parts of the live cloud checks: the read-only command runner, scope parsing and wording.
|
|
4
|
+
// Calls run through the provider's own command-line tool (aws, az, gcloud) and its existing sign-in.
|
|
5
|
+
// This code never reads, stores or sends credentials or keys.
|
|
6
|
+
import { execFile } from "node:child_process";
|
|
7
|
+
import { emptyResult } from "./map-types.js";
|
|
8
|
+
export const PROVIDERS = ["aws", "azure", "gcp"];
|
|
9
|
+
/**
|
|
10
|
+
* Every command this code may run, by tool. Each one only reads. The runner refuses anything else,
|
|
11
|
+
* so a change elsewhere can't turn a scan into a write.
|
|
12
|
+
*/
|
|
13
|
+
export const READ_ONLY_COMMANDS = {
|
|
14
|
+
aws: [
|
|
15
|
+
"sts get-caller-identity",
|
|
16
|
+
"configure get",
|
|
17
|
+
"ec2 describe-regions",
|
|
18
|
+
"kms list-keys",
|
|
19
|
+
"kms list-aliases",
|
|
20
|
+
"kms describe-key",
|
|
21
|
+
"acm list-certificates",
|
|
22
|
+
"elbv2 describe-load-balancers",
|
|
23
|
+
"elbv2 describe-listeners",
|
|
24
|
+
"elbv2 describe-ssl-policies",
|
|
25
|
+
"elb describe-load-balancers",
|
|
26
|
+
"elb describe-load-balancer-policies",
|
|
27
|
+
"cloudfront list-distributions",
|
|
28
|
+
"apigateway get-domain-names",
|
|
29
|
+
"apigateway get-rest-apis",
|
|
30
|
+
"opensearch list-domain-names",
|
|
31
|
+
"opensearch describe-domains",
|
|
32
|
+
"elasticache describe-replication-groups",
|
|
33
|
+
"elasticache describe-cache-clusters",
|
|
34
|
+
"rds describe-db-instances",
|
|
35
|
+
"rds describe-db-clusters",
|
|
36
|
+
"redshift describe-clusters",
|
|
37
|
+
"efs describe-file-systems",
|
|
38
|
+
"kafka list-clusters-v2",
|
|
39
|
+
"ec2 get-ebs-encryption-by-default",
|
|
40
|
+
"ec2 describe-key-pairs",
|
|
41
|
+
"acm-pca list-certificate-authorities",
|
|
42
|
+
"transfer list-servers",
|
|
43
|
+
"transfer describe-server",
|
|
44
|
+
"transfer describe-security-policy",
|
|
45
|
+
"iam list-server-certificates",
|
|
46
|
+
"iam get-server-certificate",
|
|
47
|
+
"s3api list-buckets",
|
|
48
|
+
"s3api get-bucket-policy",
|
|
49
|
+
],
|
|
50
|
+
az: ["account show", "keyvault list", "keyvault key list", "keyvault key show", "keyvault certificate list", "keyvault certificate show", "storage account list", "webapp list", "webapp config show", "redis list"],
|
|
51
|
+
gcloud: [
|
|
52
|
+
"config get-value",
|
|
53
|
+
"kms locations list",
|
|
54
|
+
"kms keyrings list",
|
|
55
|
+
"kms keys list",
|
|
56
|
+
"compute ssl-policies list",
|
|
57
|
+
"compute target-https-proxies list",
|
|
58
|
+
"compute target-ssl-proxies list",
|
|
59
|
+
"compute ssl-certificates list",
|
|
60
|
+
"sql instances list",
|
|
61
|
+
],
|
|
62
|
+
};
|
|
63
|
+
/** Letters, digits and a few separators. No spaces, quotes or shell characters, so nothing can be injected. */
|
|
64
|
+
export const SAFE_ARG = /^[A-Za-z0-9._:/=@,+-]{1,512}$/;
|
|
65
|
+
/** The command words before the first flag, like "kms describe-key". */
|
|
66
|
+
export function commandOf(args) {
|
|
67
|
+
const words = [];
|
|
68
|
+
for (const a of args) {
|
|
69
|
+
if (a.startsWith("-"))
|
|
70
|
+
break;
|
|
71
|
+
words.push(a);
|
|
72
|
+
}
|
|
73
|
+
return words.join(" ");
|
|
74
|
+
}
|
|
75
|
+
/** The only settings the two config commands may read: which region or project is selected. */
|
|
76
|
+
const CONFIG_READS = { "configure get": ["region"], "config get-value": ["project"] };
|
|
77
|
+
export function allowed(tool, args) {
|
|
78
|
+
let cmd = commandOf(args);
|
|
79
|
+
for (const [c, keys] of Object.entries(CONFIG_READS))
|
|
80
|
+
if (keys.some((k) => cmd === `${c} ${k}`))
|
|
81
|
+
cmd = c;
|
|
82
|
+
if (!READ_ONLY_COMMANDS[tool].includes(cmd))
|
|
83
|
+
return false;
|
|
84
|
+
const verb = cmd.split(" ").pop() ?? "";
|
|
85
|
+
if (!/^(list|describe|get|show)/.test(verb))
|
|
86
|
+
return false;
|
|
87
|
+
return args.every((a) => SAFE_ARG.test(a));
|
|
88
|
+
}
|
|
89
|
+
const SIGNIN = /Unable to locate credentials|ExpiredToken|expired|InvalidClientTokenId|UnrecognizedClientException|could not be found|aws sso login|az login|Please run 'az login'|AADSTS|no active account|do not currently have an active account|gcloud auth login|Reauthentication|reauth|NoCredentialProviders|InvalidSignatureException|SignatureDoesNotMatch/i;
|
|
90
|
+
const DENIED = /AccessDenied|UnauthorizedOperation|not authorized to perform|AuthorizationError|AuthorizationFailed|Forbidden|does not have authorization|PERMISSION_DENIED|does not have permission|Caller is not authorized|is not authorized/i;
|
|
91
|
+
const UNSUPPORTED = /SERVICE_DISABLED|has not been used in project|is not enabled|Could not connect to the endpoint URL|OptInRequired|not supported in this region|InvalidAction|UnknownOperation/i;
|
|
92
|
+
const MISSING = /is not recognized as an internal or external command|command not found|No such file or directory|ENOENT/i;
|
|
93
|
+
export function classify(stderr) {
|
|
94
|
+
if (MISSING.test(stderr))
|
|
95
|
+
return "missing";
|
|
96
|
+
if (DENIED.test(stderr))
|
|
97
|
+
return "denied";
|
|
98
|
+
if (SIGNIN.test(stderr))
|
|
99
|
+
return "signin";
|
|
100
|
+
if (UNSUPPORTED.test(stderr))
|
|
101
|
+
return "unsupported";
|
|
102
|
+
return "other";
|
|
103
|
+
}
|
|
104
|
+
/** The first useful line of an error, without long request ids. Local report only. */
|
|
105
|
+
export function shortMessage(stderr) {
|
|
106
|
+
const line = stderr.split(/\r?\n/).map((l) => l.trim()).filter(Boolean).find((l) => !/^(WARNING|DEBUG)\b/.test(l)) ?? "no details";
|
|
107
|
+
return line.replace(/\(Request ID: [^)]+\)/gi, "").replace(/\s+/g, " ").slice(0, 300);
|
|
108
|
+
}
|
|
109
|
+
/** Runs the provider's own command-line tool. Read-only commands only, and nothing goes through a shell except on Windows, where the tools are .cmd files and every argument has already been checked. */
|
|
110
|
+
export const realRunner = (tool, args) => new Promise((resolve) => {
|
|
111
|
+
if (!allowed(tool, args)) {
|
|
112
|
+
resolve({ ok: false, kind: "other", message: `refused to run "${tool} ${commandOf(args)}": not a read-only command` });
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
const env = { ...process.env, AWS_PAGER: "", AWS_CLI_AUTO_PROMPT: "off", CLOUDSDK_CORE_DISABLE_PROMPTS: "1" };
|
|
116
|
+
// On Windows, az and gcloud are .cmd files, which only start through the command shell.
|
|
117
|
+
// The AWS CLI is an .exe there, so it runs directly unless only a .cmd copy is installed.
|
|
118
|
+
const win = process.platform === "win32";
|
|
119
|
+
const once = (shell) => execFile(tool, args, { env, timeout: 120_000, maxBuffer: 64 * 1024 * 1024, windowsHide: true, shell }, (err, stdout, stderr) => {
|
|
120
|
+
if (err) {
|
|
121
|
+
const e = err;
|
|
122
|
+
if (e.code === "ENOENT" && win && !shell)
|
|
123
|
+
return once(true);
|
|
124
|
+
if (e.code === "ENOENT")
|
|
125
|
+
return resolve({ ok: false, kind: "missing", message: `${tool} isn't installed` });
|
|
126
|
+
if (e.killed)
|
|
127
|
+
return resolve({ ok: false, kind: "other", message: `${tool} ${commandOf(args)} took too long` });
|
|
128
|
+
const text = String(stderr || stdout || e.message);
|
|
129
|
+
return resolve({ ok: false, kind: classify(text), message: shortMessage(text) });
|
|
130
|
+
}
|
|
131
|
+
const out = String(stdout).trim();
|
|
132
|
+
if (!out)
|
|
133
|
+
return resolve({ ok: true, data: null });
|
|
134
|
+
try {
|
|
135
|
+
resolve({ ok: true, data: JSON.parse(out) });
|
|
136
|
+
}
|
|
137
|
+
catch {
|
|
138
|
+
// `configure get` and `config get-value` can print a bare value.
|
|
139
|
+
resolve({ ok: true, data: out });
|
|
140
|
+
}
|
|
141
|
+
});
|
|
142
|
+
once(win && tool !== "aws");
|
|
143
|
+
});
|
|
144
|
+
/** Runs fn over items, a few at a time. */
|
|
145
|
+
export async function pool(items, n, fn) {
|
|
146
|
+
const out = new Array(items.length);
|
|
147
|
+
let next = 0;
|
|
148
|
+
const worker = async () => {
|
|
149
|
+
while (next < items.length) {
|
|
150
|
+
const i = next++;
|
|
151
|
+
out[i] = await fn(items[i]);
|
|
152
|
+
}
|
|
153
|
+
};
|
|
154
|
+
await Promise.all(Array.from({ length: Math.min(n, items.length) }, worker));
|
|
155
|
+
return out;
|
|
156
|
+
}
|
|
157
|
+
export function errorKind(k) {
|
|
158
|
+
return k === "denied" || k === "signin" ? "permission" : "other";
|
|
159
|
+
}
|
|
160
|
+
export const INSTALL = {
|
|
161
|
+
aws: "Install the AWS CLI (https://aws.amazon.com/cli/), then sign in with aws configure or aws sso login.",
|
|
162
|
+
az: "Install the Azure CLI (https://learn.microsoft.com/cli/azure/install-azure-cli), then sign in with az login.",
|
|
163
|
+
gcloud: "Install the Google Cloud CLI (https://cloud.google.com/sdk/docs/install), then sign in with gcloud auth login.",
|
|
164
|
+
};
|
|
165
|
+
export const SIGN_IN = {
|
|
166
|
+
aws: "Sign in first: aws sso login, or aws configure for access keys. Add --profile <name> to use a named profile.",
|
|
167
|
+
az: "Sign in first: az login.",
|
|
168
|
+
gcloud: "Sign in first: gcloud auth login.",
|
|
169
|
+
};
|
|
170
|
+
export function emptyCloud() {
|
|
171
|
+
return { result: emptyResult("cloud"), inventory: [], notes: [] };
|
|
172
|
+
}
|
|
173
|
+
export function parseCloudScope(v) {
|
|
174
|
+
if (!v || typeof v !== "object")
|
|
175
|
+
return { error: "cloud needs at least one provider: aws, azure or gcp" };
|
|
176
|
+
const strings = (x, re) => (Array.isArray(x) ? x.filter((y) => typeof y === "string" && re.test(y)).slice(0, 64) : undefined);
|
|
177
|
+
const out = {};
|
|
178
|
+
if (v.aws) {
|
|
179
|
+
out.aws = {};
|
|
180
|
+
if (typeof v.aws.profile === "string" && PROFILE.test(v.aws.profile))
|
|
181
|
+
out.aws.profile = v.aws.profile;
|
|
182
|
+
const r = strings(v.aws.regions, REGION);
|
|
183
|
+
if (r?.length)
|
|
184
|
+
out.aws.regions = r;
|
|
185
|
+
}
|
|
186
|
+
if (v.azure) {
|
|
187
|
+
out.azure = {};
|
|
188
|
+
const s = strings(v.azure.subscriptions, SUBSCRIPTION);
|
|
189
|
+
if (s?.length)
|
|
190
|
+
out.azure.subscriptions = s;
|
|
191
|
+
}
|
|
192
|
+
if (v.gcp) {
|
|
193
|
+
out.gcp = {};
|
|
194
|
+
const p = strings(v.gcp.projects, PROJECT);
|
|
195
|
+
if (p?.length)
|
|
196
|
+
out.gcp.projects = p;
|
|
197
|
+
const l = strings(v.gcp.locations, LOCATION);
|
|
198
|
+
if (l?.length)
|
|
199
|
+
out.gcp.locations = l;
|
|
200
|
+
}
|
|
201
|
+
const unknown = Object.keys(v).filter((k) => !PROVIDERS.includes(k));
|
|
202
|
+
if (unknown.length)
|
|
203
|
+
return { error: `cloud providers this version can't check: ${unknown.join(", ")}` };
|
|
204
|
+
if (!out.aws && !out.azure && !out.gcp)
|
|
205
|
+
return { error: "cloud needs at least one provider: aws, azure or gcp" };
|
|
206
|
+
return out;
|
|
207
|
+
}
|
|
208
|
+
export const PROFILE = /^[A-Za-z0-9._-]{1,64}$/;
|
|
209
|
+
export const REGION = /^[a-z]{2}(-[a-z]+)+-\d{1,2}$/;
|
|
210
|
+
export const SUBSCRIPTION = /^[A-Za-z0-9-]{1,64}$/;
|
|
211
|
+
export const PROJECT = /^[a-z][a-z0-9-]{4,29}$|^[a-z0-9.-]+:[a-z][a-z0-9-]{4,29}$/;
|
|
212
|
+
export const LOCATION = /^[a-z0-9-]{2,40}$/;
|
|
213
|
+
/** Turns a failed call into a line of the report: what couldn't be checked and what to do about it. */
|
|
214
|
+
export function failure(res, tool, target, call, what) {
|
|
215
|
+
if (call.kind === "unsupported") {
|
|
216
|
+
res.result.skipped.push({ target, reason: `${what}: not available here (${call.message})` });
|
|
217
|
+
return;
|
|
218
|
+
}
|
|
219
|
+
const hint = call.kind === "missing" ? INSTALL[tool] : call.kind === "signin" ? SIGN_IN[tool] : call.kind === "denied" ? "The signed-in identity isn't allowed to read this. See the read-only permissions in the README." : call.message;
|
|
220
|
+
res.result.errors.push({ area: "cloud", target, kind: errorKind(call.kind), message: `couldn't ${what}: ${call.kind === "other" ? call.message : `${call.message}. ${hint}`}` });
|
|
221
|
+
}
|
|
222
|
+
/** Key size in bits from a base64 or base64url modulus. */
|
|
223
|
+
export function modulusBits(n) {
|
|
224
|
+
if (typeof n !== "string" || !n)
|
|
225
|
+
return undefined;
|
|
226
|
+
const b = Buffer.from(n.replace(/-/g, "+").replace(/_/g, "/"), "base64");
|
|
227
|
+
let i = 0;
|
|
228
|
+
while (i < b.length && b[i] === 0)
|
|
229
|
+
i++;
|
|
230
|
+
if (i >= b.length)
|
|
231
|
+
return undefined;
|
|
232
|
+
const bits = (b.length - i - 1) * 8 + Math.floor(Math.log2(b[i])) + 1;
|
|
233
|
+
// Round to the usual sizes, so 2047-bit moduli read as 2048.
|
|
234
|
+
for (const s of [1024, 2048, 3072, 4096, 8192])
|
|
235
|
+
if (Math.abs(bits - s) <= 8)
|
|
236
|
+
return s;
|
|
237
|
+
return bits;
|
|
238
|
+
}
|
|
239
|
+
export const SIG_FIX = "Plan the move to ML-DSA (FIPS 204) signing keys. Make a new key, switch what signs with it, then retire the old key.";
|
|
240
|
+
export const PKE_FIX = "Plan the move to ML-KEM (FIPS 203). For data at rest, a symmetric key (AES-256) is already quantum-safe.";
|
|
241
|
+
export const KEX_FIX = "Plan the move to ML-KEM (FIPS 203) or hybrid key agreement.";
|
|
242
|
+
export const CERT_FIX = "Plan to move to ML-DSA certificates once your certificate authority offers them. Short-lived certificates make the switch easy.";
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
// SPDX-License-Identifier: PolyForm-Shield-1.0.0
|
|
2
|
+
// Copyright 2026 Hard Keel (https://hardkeel.com)
|
|
3
|
+
// Google Cloud beyond KMS: load balancer TLS policies and certificates, and Cloud SQL connections.
|
|
4
|
+
// Every call lists settings.
|
|
5
|
+
import { certFromPem, oldTls, plainTraffic } from "./cloud-aws-services.js";
|
|
6
|
+
const list = (v) => (Array.isArray(v) ? v : []);
|
|
7
|
+
const WEAK_CIPHER = /3DES|RC4|NULL|EXPORT|_DES_/i;
|
|
8
|
+
const POLICY_FIX = "Attach an SSL policy with the MODERN or RESTRICTED profile and a minimum of TLS 1.2 to the load balancer's target proxy.";
|
|
9
|
+
export function gcpPolicy(p) {
|
|
10
|
+
return { name: String(p?.name ?? ""), min: String(p?.minTlsVersion ?? "TLS_1_0"), weak: list(p?.enabledFeatures).map(String).filter((c) => WEAK_CIPHER.test(c)) };
|
|
11
|
+
}
|
|
12
|
+
/** A target HTTPS or SSL proxy: the front of a load balancer. No SSL policy means Google's default, which accepts TLS 1.0. */
|
|
13
|
+
export function targetProxy(proxy, policies, target) {
|
|
14
|
+
const out = [];
|
|
15
|
+
const internetFacing = !proxy?.region;
|
|
16
|
+
const ref = String(proxy?.sslPolicy ?? "");
|
|
17
|
+
const p = ref ? policies.get(ref) ?? policies.get(ref.split("/").pop() ?? "") : undefined;
|
|
18
|
+
if (!ref) {
|
|
19
|
+
out.push(oldTls(target, "Load balancer uses Google's default TLS policy, which accepts TLS 1.0 and 1.1", POLICY_FIX, internetFacing, "medium"));
|
|
20
|
+
return out;
|
|
21
|
+
}
|
|
22
|
+
if (!p)
|
|
23
|
+
return out;
|
|
24
|
+
if (p.min === "TLS_1_0" || p.min === "TLS_1_1")
|
|
25
|
+
out.push(oldTls(target, `SSL policy ${p.name} accepts TLS ${p.min === "TLS_1_0" ? "1.0 and 1.1" : "1.1"}`, POLICY_FIX, internetFacing));
|
|
26
|
+
if (p.weak.length)
|
|
27
|
+
out.push({ target, ruleId: "cloud-tls-weak-cipher", title: `SSL policy ${p.name} allows ${p.weak.slice(0, 2).join(" and ")}${p.weak.length > 2 ? ` and ${p.weak.length - 2} more weak ciphers` : ""}`, algorithm: "Weak TLS ciphers", primitive: "protocol", risk: "weak-now", severity: "high", fix: POLICY_FIX, internetFacing });
|
|
28
|
+
return out;
|
|
29
|
+
}
|
|
30
|
+
/** One Cloud SQL instance, from `gcloud sql instances list`. */
|
|
31
|
+
export function sqlInstance(i, target, now) {
|
|
32
|
+
const out = [];
|
|
33
|
+
const ip = i?.settings?.ipConfiguration ?? {};
|
|
34
|
+
const open = ip.ipv4Enabled === true && list(ip.authorizedNetworks).some((n) => String(n?.value ?? "") === "0.0.0.0/0");
|
|
35
|
+
const mode = String(ip.sslMode ?? "");
|
|
36
|
+
const plain = mode ? mode === "ALLOW_UNENCRYPTED_AND_ENCRYPTED" : ip.requireSsl !== true;
|
|
37
|
+
if (plain)
|
|
38
|
+
out.push(plainTraffic(target, open ? "Database accepts unencrypted connections from any address" : "Database accepts unencrypted connections", "Set the instance's SSL mode to Allow only SSL/TLS connections (ENCRYPTED_ONLY).", open, open ? "high" : "medium"));
|
|
39
|
+
const exp = Date.parse(String(i?.serverCaCert?.expirationTime ?? ""));
|
|
40
|
+
if (!Number.isNaN(exp)) {
|
|
41
|
+
if (exp < now.getTime())
|
|
42
|
+
out.push({ target, ruleId: "cloud-cert-expired", title: "Database's server certificate authority has expired", algorithm: "Expired certificate", primitive: "protocol", risk: "weak-now", severity: "high", fix: "Rotate the server CA certificate, and update the CA your apps trust.", internetFacing: open });
|
|
43
|
+
else if (exp - now.getTime() < 60 * 86_400_000)
|
|
44
|
+
out.push({ target, ruleId: "cloud-cert-expiring", title: `Database's server certificate authority expires on ${new Date(exp).toISOString().slice(0, 10)}`, algorithm: "Expiring certificate", primitive: "protocol", risk: "weak-now", severity: "medium", fix: "Add the new server CA, update the CA your apps trust, then rotate before that date.", internetFacing: open });
|
|
45
|
+
}
|
|
46
|
+
return out;
|
|
47
|
+
}
|
|
48
|
+
/** The extra services for one project. Returns what was counted, for the checked line. */
|
|
49
|
+
export async function gcpServices(ctx, tag, project) {
|
|
50
|
+
const { g, push, now } = ctx;
|
|
51
|
+
const p = ["--project", project];
|
|
52
|
+
const counted = [];
|
|
53
|
+
// Load balancers: TLS policies, then the target proxies that use them.
|
|
54
|
+
const pols = await g(tag, ["compute", "ssl-policies", "list", ...p], "list load balancer TLS policies");
|
|
55
|
+
const policies = new Map();
|
|
56
|
+
for (const x of list(pols)) {
|
|
57
|
+
const pol = gcpPolicy(x);
|
|
58
|
+
if (x?.selfLink)
|
|
59
|
+
policies.set(String(x.selfLink), pol);
|
|
60
|
+
policies.set(pol.name, pol);
|
|
61
|
+
}
|
|
62
|
+
let fronts = 0;
|
|
63
|
+
if (pols !== null) {
|
|
64
|
+
for (const kind of ["target-https-proxies", "target-ssl-proxies"]) {
|
|
65
|
+
const proxies = await g(tag, ["compute", kind, "list", ...p], `list load balancer ${kind === "target-ssl-proxies" ? "SSL" : "HTTPS"} front ends`);
|
|
66
|
+
for (const x of list(proxies)) {
|
|
67
|
+
fronts++;
|
|
68
|
+
for (const f of targetProxy(x, policies, `${tag} load balancer ${String(x?.name ?? "")}`))
|
|
69
|
+
push(f);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
if (fronts)
|
|
74
|
+
counted.push(`${fronts} load balancer front end${fronts === 1 ? "" : "s"}`);
|
|
75
|
+
const certs = list(await g(tag, ["compute", "ssl-certificates", "list", ...p], "list load balancer certificates (public certificates only)"));
|
|
76
|
+
for (const c of certs) {
|
|
77
|
+
const f = certFromPem(String(c?.certificate ?? ""), `${tag} certificate ${String(c?.name ?? "")}`, now, true, c?.type === "MANAGED" ? "Google-managed certificate" : "Certificate");
|
|
78
|
+
if (f)
|
|
79
|
+
push(f);
|
|
80
|
+
}
|
|
81
|
+
if (certs.length)
|
|
82
|
+
counted.push(`${certs.length} certificate${certs.length === 1 ? "" : "s"}`);
|
|
83
|
+
const dbs = list(await g(tag, ["sql", "instances", "list", ...p], "list Cloud SQL databases and their connection settings"));
|
|
84
|
+
for (const i of dbs)
|
|
85
|
+
for (const f of sqlInstance(i, `${tag} database ${String(i?.name ?? "")}`, now))
|
|
86
|
+
push(f);
|
|
87
|
+
if (dbs.length)
|
|
88
|
+
counted.push(`${dbs.length} database${dbs.length === 1 ? "" : "s"}`);
|
|
89
|
+
return counted;
|
|
90
|
+
}
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
// SPDX-License-Identifier: PolyForm-Shield-1.0.0
|
|
2
|
+
// Copyright 2026 Hard Keel (https://hardkeel.com)
|
|
3
|
+
// Google Cloud KMS: the algorithm of each key. Load balancers and Cloud SQL are in cloud-gcp-services.ts.
|
|
4
|
+
// Read-only: list calls through the Google Cloud CLI (gcloud), with the sign-in it already has.
|
|
5
|
+
import { gcpServices } from "./cloud-gcp-services.js";
|
|
6
|
+
import { commandOf, emptyCloud, failure, KEX_FIX, LOCATION, PKE_FIX, pool, PROJECT, SIG_FIX } from "./cloud-common.js";
|
|
7
|
+
const list = (v) => (Array.isArray(v) ? v : []);
|
|
8
|
+
const KEYRING = /^projects\/[a-z0-9.:-]{1,64}\/locations\/[a-z0-9-]{1,40}\/keyRings\/[A-Za-z0-9_-]{1,63}$/;
|
|
9
|
+
/** One Cloud KMS key, from `gcloud kms keys list`. Uses the primary version's algorithm, else the template for new versions. */
|
|
10
|
+
export function kmsKeyGcp(k, target) {
|
|
11
|
+
const state = String(k?.primary?.state ?? "");
|
|
12
|
+
if (/DESTROY/.test(state))
|
|
13
|
+
return {};
|
|
14
|
+
const alg = String(k?.primary?.algorithm ?? k?.versionTemplate?.algorithm ?? "");
|
|
15
|
+
const off = state && state !== "ENABLED" ? ` (${state.toLowerCase()})` : "";
|
|
16
|
+
if (alg === "GOOGLE_SYMMETRIC_ENCRYPTION" || /^AES_256_GCM$/.test(alg))
|
|
17
|
+
return { inventory: { algorithm: "AES-256-GCM", family: "AES", primitive: "ae", functions: ["encrypt", "decrypt"], keySize: 256, mode: "gcm", nistQuantumLevel: 5, note: "Google Cloud KMS symmetric key" } };
|
|
18
|
+
const aes = alg.match(/^AES_(128|256)_(GCM|CBC|CTR)$/);
|
|
19
|
+
if (aes)
|
|
20
|
+
return { inventory: { algorithm: `AES-${aes[1]}-${aes[2]}`, family: "AES", primitive: aes[2] === "GCM" ? "ae" : "block-cipher", functions: ["encrypt", "decrypt"], keySize: Number(aes[1]), mode: aes[2].toLowerCase(), nistQuantumLevel: aes[1] === "256" ? 5 : 1, note: "Google Cloud KMS raw key" } };
|
|
21
|
+
const hmac = alg.match(/^HMAC_SHA(1|224|256|384|512)$/);
|
|
22
|
+
if (hmac)
|
|
23
|
+
return { inventory: { algorithm: `HMAC-SHA${hmac[1]}`, family: "HMAC", primitive: "mac", functions: ["tag"], note: "Google Cloud KMS HMAC key" } };
|
|
24
|
+
const mldsa = alg.match(/ML_DSA_(44|65|87)/);
|
|
25
|
+
if (mldsa)
|
|
26
|
+
return { strength: { target, algorithm: "ML-DSA", title: `Cloud KMS signing key uses ML-DSA-${mldsa[1]}` }, inventory: { algorithm: `ML-DSA-${mldsa[1]}`, family: "ML-DSA", primitive: "signature", functions: ["sign", "verify"], nistQuantumLevel: { "44": 2, "65": 3, "87": 5 }[mldsa[1]] } };
|
|
27
|
+
if (/SLH_DSA/.test(alg))
|
|
28
|
+
return { strength: { target, algorithm: "SLH-DSA", title: "Cloud KMS signing key uses SLH-DSA" } };
|
|
29
|
+
const mlkem = alg.match(/^ML_KEM_(512|768|1024)$/);
|
|
30
|
+
if (mlkem)
|
|
31
|
+
return { strength: { target, algorithm: "ML-KEM", title: `Cloud KMS key uses ML-KEM-${mlkem[1]}` }, inventory: { algorithm: `ML-KEM-${mlkem[1]}`, family: "ML-KEM", primitive: "kem", functions: ["keygen", "encapsulate", "decapsulate"], nistQuantumLevel: { 512: 1, 768: 3, 1024: 5 }[Number(mlkem[1])] } };
|
|
32
|
+
if (alg === "KEM_XWING")
|
|
33
|
+
return { strength: { target, algorithm: "ML-KEM", title: "Cloud KMS key uses X-Wing (hybrid ML-KEM-768)" } };
|
|
34
|
+
const rsaSign = alg.match(/^RSA_SIGN_(?:RAW_)?(?:PSS|PKCS1)_(\d{4})/);
|
|
35
|
+
if (rsaSign) {
|
|
36
|
+
const bits = Number(rsaSign[1]);
|
|
37
|
+
return { finding: { target, ruleId: "cloud-kms-rsa-sign", title: `Cloud KMS signing key uses RSA-${bits}${off}`, algorithm: "RSA signature", primitive: "signature", risk: "quantum-vulnerable", severity: "medium", keySize: bits, fix: SIG_FIX, internetFacing: false, longLived: true } };
|
|
38
|
+
}
|
|
39
|
+
const rsaDec = alg.match(/^RSA_DECRYPT_OAEP_(\d{4})/);
|
|
40
|
+
if (rsaDec) {
|
|
41
|
+
const bits = Number(rsaDec[1]);
|
|
42
|
+
return { finding: { target, ruleId: "cloud-kms-rsa-encrypt", title: `Cloud KMS encryption key uses RSA-${bits}${off}`, algorithm: "RSA encryption", primitive: "public-key-encryption", risk: "quantum-vulnerable", severity: "high", keySize: bits, fix: PKE_FIX, internetFacing: false } };
|
|
43
|
+
}
|
|
44
|
+
const ec = alg.match(/^EC_SIGN_(P256|P384|SECP256K1|ED25519)/);
|
|
45
|
+
if (ec) {
|
|
46
|
+
const ed = ec[1] === "ED25519";
|
|
47
|
+
return { finding: { target, ruleId: "cloud-kms-ec-sign", title: `Cloud KMS signing key uses ${ed ? "Ed25519" : ec[1].replace("P", "P-")}${off}`, algorithm: ed ? "Ed25519" : "ECDSA", primitive: "signature", risk: "quantum-vulnerable", severity: "medium", keySize: ec[1] === "P384" ? 384 : 256, fix: SIG_FIX, internetFacing: false, longLived: true } };
|
|
48
|
+
}
|
|
49
|
+
if (/^(ECDH|EC_KEY_AGREEMENT|X25519)/.test(alg))
|
|
50
|
+
return { finding: { target, ruleId: "cloud-kms-ecdh", title: `Cloud KMS key agreement key uses ${alg}${off}`, algorithm: "ECDH", primitive: "key-agreement", risk: "quantum-vulnerable", severity: "high", fix: KEX_FIX, internetFacing: false } };
|
|
51
|
+
return {};
|
|
52
|
+
}
|
|
53
|
+
export async function runGcp(scope, log, o) {
|
|
54
|
+
const res = emptyCloud();
|
|
55
|
+
const g = (args) => o.runner("gcloud", [...args, "--format=json", "--quiet"]);
|
|
56
|
+
const push = (f, s) => {
|
|
57
|
+
if (f)
|
|
58
|
+
res.result.findings.push({ area: "cloud", ...f });
|
|
59
|
+
if (s)
|
|
60
|
+
res.result.strengths.push({ area: "cloud", ...s });
|
|
61
|
+
};
|
|
62
|
+
let projects = (scope.projects ?? []).filter((p) => PROJECT.test(p));
|
|
63
|
+
if (!projects.length) {
|
|
64
|
+
log.record("cloud", "Google Cloud", "gcloud config get-value project: which project is selected");
|
|
65
|
+
const p = await g(["config", "get-value", "project"]);
|
|
66
|
+
if (!p.ok) {
|
|
67
|
+
failure(res, "gcloud", "Google Cloud", p, "read the selected Google Cloud project");
|
|
68
|
+
return res;
|
|
69
|
+
}
|
|
70
|
+
const v = typeof p.data === "string" ? p.data.trim() : "";
|
|
71
|
+
if (!PROJECT.test(v)) {
|
|
72
|
+
res.result.errors.push({ area: "cloud", target: "Google Cloud", kind: "other", message: "no Google Cloud project is selected. Name one with --project, or run gcloud config set project <id>." });
|
|
73
|
+
return res;
|
|
74
|
+
}
|
|
75
|
+
projects = [v];
|
|
76
|
+
}
|
|
77
|
+
for (const project of projects) {
|
|
78
|
+
const tag = `Google Cloud ${project}`;
|
|
79
|
+
let locations = (scope.locations ?? []).filter((l) => LOCATION.test(l));
|
|
80
|
+
if (!locations.length) {
|
|
81
|
+
log.record("cloud", tag, "gcloud kms locations list: list Cloud KMS locations");
|
|
82
|
+
const l = await g(["kms", "locations", "list", "--project", project]);
|
|
83
|
+
if (!l.ok)
|
|
84
|
+
failure(res, "gcloud", tag, l, "list Cloud KMS locations");
|
|
85
|
+
else
|
|
86
|
+
locations = list(l.data).map((x) => String(x?.locationId ?? "")).filter((x) => LOCATION.test(x));
|
|
87
|
+
}
|
|
88
|
+
res.notes.push(`Google Cloud project ${project}, ${locations.length} Cloud KMS location${locations.length === 1 ? "" : "s"}`);
|
|
89
|
+
let rings = 0;
|
|
90
|
+
let keys = 0;
|
|
91
|
+
let stop = false;
|
|
92
|
+
await pool(locations, o.concurrency, async (loc) => {
|
|
93
|
+
if (stop)
|
|
94
|
+
return;
|
|
95
|
+
log.record("cloud", `${tag} ${loc}`, "gcloud kms keyrings list: list key rings");
|
|
96
|
+
const r = await g(["kms", "keyrings", "list", "--location", loc, "--project", project]);
|
|
97
|
+
if (!r.ok) {
|
|
98
|
+
// A permission problem is the same in every location. Report it once.
|
|
99
|
+
if (r.kind === "denied" || r.kind === "signin" || r.kind === "unsupported")
|
|
100
|
+
stop = true;
|
|
101
|
+
failure(res, "gcloud", `${tag} ${loc}`, r, "list key rings");
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
for (const ring of list(r.data).map((x) => String(x?.name ?? "")).filter((n) => KEYRING.test(n))) {
|
|
105
|
+
rings++;
|
|
106
|
+
const short = ring.split("/").slice(-1)[0];
|
|
107
|
+
log.record("cloud", `${tag} ${loc} key ring ${short}`, "gcloud kms keys list: list keys and their algorithms");
|
|
108
|
+
const k = await g(["kms", "keys", "list", "--keyring", ring]);
|
|
109
|
+
if (!k.ok) {
|
|
110
|
+
failure(res, "gcloud", `${tag} ${loc} key ring ${short}`, k, "list keys");
|
|
111
|
+
continue;
|
|
112
|
+
}
|
|
113
|
+
for (const key of list(k.data)) {
|
|
114
|
+
keys++;
|
|
115
|
+
const target = `${tag} ${loc} key ${short}/${String(key?.name ?? "").split("/").pop()}`;
|
|
116
|
+
const out = kmsKeyGcp(key, target);
|
|
117
|
+
push(out.finding, out.strength);
|
|
118
|
+
if (out.inventory)
|
|
119
|
+
res.inventory.push({ ...out.inventory, file: target, line: 0 });
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
// A permission problem or a service that isn't turned on is reported once per service for the project.
|
|
124
|
+
const off = new Set();
|
|
125
|
+
const more = await gcpServices({
|
|
126
|
+
g: async (target, args, what) => {
|
|
127
|
+
const service = args[0] === "compute" ? "Compute Engine" : "Cloud SQL";
|
|
128
|
+
if (off.has(service))
|
|
129
|
+
return null;
|
|
130
|
+
log.record("cloud", target, `gcloud ${commandOf(args)}: ${what}`);
|
|
131
|
+
const r = await g(args);
|
|
132
|
+
if (r.ok)
|
|
133
|
+
return r.data ?? [];
|
|
134
|
+
if (r.kind === "denied" || r.kind === "signin" || r.kind === "unsupported") {
|
|
135
|
+
if (off.has(service))
|
|
136
|
+
return null;
|
|
137
|
+
off.add(service);
|
|
138
|
+
failure(res, "gcloud", `${tag} ${service}`, r, what);
|
|
139
|
+
}
|
|
140
|
+
else
|
|
141
|
+
failure(res, "gcloud", target, r, what);
|
|
142
|
+
return null;
|
|
143
|
+
},
|
|
144
|
+
push: (f) => push(f),
|
|
145
|
+
now: o.now,
|
|
146
|
+
}, tag, project);
|
|
147
|
+
res.result.checked.push(`${tag}: ${[`${rings} key ring${rings === 1 ? "" : "s"}`, `${keys} key${keys === 1 ? "" : "s"}`, ...more].join(", ")}`);
|
|
148
|
+
}
|
|
149
|
+
return res;
|
|
150
|
+
}
|
package/dist/comments.js
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
// SPDX-License-Identifier: PolyForm-Shield-1.0.0
|
|
2
|
+
// Copyright 2026 Hard Keel (https://hardkeel.com)
|
|
3
|
+
// Trailing comments: `x = rsa_sign(m) # TODO move off RSA` uses RSA once, and the comment must not
|
|
4
|
+
// count a second time; `timeout = 30 # was RSA-1024 before` uses nothing. Strings are left alone.
|
|
5
|
+
const HASH_COMMENT = /\.(py|pyw|rb|sh|bash|zsh|ya?ml|toml|ini|conf|cfg|cnf|tf|tfvars|hcl|pl|pm|r|ex|exs|nim|ps1|properties|env|mk)$|(^|\/)(dockerfile|containerfile|makefile|caddyfile|nginx[^/]*|httpd[^/]*|\.env[^/]*)$/i;
|
|
6
|
+
const SLASH_COMMENT = /\.(js|mjs|cjs|jsx|ts|mts|cts|tsx|go|java|kt|kts|scala|groovy|gradle|c|h|cc|cpp|cxx|hpp|hh|cs|rs|swift|m|mm|sol|dart|zig|php|tf|hcl|jsonc|json5|proto)$/i;
|
|
7
|
+
/** A line that is only a comment: //, /*, a * continuation, or # (not a shebang, Rust attribute or C preprocessor line). */
|
|
8
|
+
export const COMMENT_LINE = /^\s*(?:\/\/|\/\*|\*(?:\s|\/|$)|#(?![!\[]|include|define|if|else|endif|pragma|import|undef|error))/;
|
|
9
|
+
/** Which comment markers a file's language uses after code on the same line. */
|
|
10
|
+
export function commentStyle(path) {
|
|
11
|
+
return { hash: HASH_COMMENT.test(path) || /\.php$/i.test(path), slash: SLASH_COMMENT.test(path) };
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* The line without any comment that follows code on it, and without inline block comments.
|
|
15
|
+
* Quotes are tracked so that "#fff", "http://x" and '// not a comment' stay. A # only starts a
|
|
16
|
+
* comment at the line start or after whitespace (YAML, shell and TOML rules); a // never directly
|
|
17
|
+
* after a colon, so URLs outside quotes survive.
|
|
18
|
+
*/
|
|
19
|
+
export function stripTrailingComment(line, path) {
|
|
20
|
+
const style = commentStyle(path);
|
|
21
|
+
if (!style.hash && !style.slash)
|
|
22
|
+
return line;
|
|
23
|
+
if (!(style.hash && line.includes("#")) && !(style.slash && line.includes("/")))
|
|
24
|
+
return line;
|
|
25
|
+
let out = "";
|
|
26
|
+
let quote = null;
|
|
27
|
+
for (let i = 0; i < line.length; i++) {
|
|
28
|
+
const c = line[i];
|
|
29
|
+
if (quote) {
|
|
30
|
+
out += c;
|
|
31
|
+
if (c === "\\") {
|
|
32
|
+
out += line[i + 1] ?? "";
|
|
33
|
+
i++;
|
|
34
|
+
}
|
|
35
|
+
else if (c === quote)
|
|
36
|
+
quote = null;
|
|
37
|
+
continue;
|
|
38
|
+
}
|
|
39
|
+
if (c === '"' || c === "'" || c === "`") {
|
|
40
|
+
// An apostrophe inside a word (don't, it's) in a shell or YAML comment-free value isn't a quote.
|
|
41
|
+
if (c === "'" && /[A-Za-z]/.test(line[i - 1] ?? "") && /[A-Za-z]/.test(line[i + 1] ?? "") && style.hash && !style.slash) {
|
|
42
|
+
out += c;
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
quote = c;
|
|
46
|
+
out += c;
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
if (style.hash && c === "#" && (i === 0 || /\s/.test(line[i - 1]))) {
|
|
50
|
+
// Not a Rust attribute (#[...]) or a C-style preprocessor line, which never reach here as hash files.
|
|
51
|
+
if (line[i + 1] === "[" || line[i + 1] === "!") {
|
|
52
|
+
out += c;
|
|
53
|
+
continue;
|
|
54
|
+
}
|
|
55
|
+
return out.trimEnd();
|
|
56
|
+
}
|
|
57
|
+
if (style.slash && c === "/" && line[i + 1] === "/" && line[i - 1] !== ":")
|
|
58
|
+
return out.trimEnd();
|
|
59
|
+
if (style.slash && c === "/" && line[i + 1] === "*") {
|
|
60
|
+
const end = line.indexOf("*/", i + 2);
|
|
61
|
+
if (end < 0)
|
|
62
|
+
return out.trimEnd();
|
|
63
|
+
out += " ";
|
|
64
|
+
i = end + 1;
|
|
65
|
+
continue;
|
|
66
|
+
}
|
|
67
|
+
out += c;
|
|
68
|
+
}
|
|
69
|
+
return out;
|
|
70
|
+
}
|
|
71
|
+
const JS_FILE = /\.(js|mjs|cjs|jsx|ts|mts|cts|tsx)$/i;
|
|
72
|
+
// Characters and words after which a / starts a regular expression rather than a division.
|
|
73
|
+
const REGEX_BEFORE = /[(,=:[!&|?{};+\-*%<>~^]$/;
|
|
74
|
+
const REGEX_WORD = /(?:^|[^\w$.])(?:return|typeof|case|in|of|delete|void|throw|new|yield|await)$/;
|
|
75
|
+
/**
|
|
76
|
+
* JavaScript and TypeScript regular expression literals with their insides blanked: a pattern that
|
|
77
|
+
* looks for "RSA" names the algorithm, it doesn't use it. The slashes and flags stay, so the line
|
|
78
|
+
* keeps its shape and its length.
|
|
79
|
+
*/
|
|
80
|
+
export function blankRegexLiterals(line, path) {
|
|
81
|
+
if (!JS_FILE.test(path) || !line.includes("/"))
|
|
82
|
+
return line;
|
|
83
|
+
let out = "";
|
|
84
|
+
let quote = null;
|
|
85
|
+
// Once a literal runs to the end of the line unclosed, no later slash can close one either,
|
|
86
|
+
// so stop looking. Keeps a line of many slashes linear rather than quadratic.
|
|
87
|
+
let unclosed = false;
|
|
88
|
+
for (let i = 0; i < line.length; i++) {
|
|
89
|
+
const c = line[i];
|
|
90
|
+
if (quote) {
|
|
91
|
+
out += c;
|
|
92
|
+
if (c === "\\") {
|
|
93
|
+
out += line[i + 1] ?? "";
|
|
94
|
+
i++;
|
|
95
|
+
}
|
|
96
|
+
else if (c === quote)
|
|
97
|
+
quote = null;
|
|
98
|
+
continue;
|
|
99
|
+
}
|
|
100
|
+
if (c === '"' || c === "'" || c === "`") {
|
|
101
|
+
quote = c;
|
|
102
|
+
out += c;
|
|
103
|
+
continue;
|
|
104
|
+
}
|
|
105
|
+
if (!unclosed && c === "/" && line[i + 1] !== "/" && line[i + 1] !== "*") {
|
|
106
|
+
const before = out.trimEnd();
|
|
107
|
+
if (before === "" || REGEX_BEFORE.test(before) || REGEX_WORD.test(before)) {
|
|
108
|
+
// Find the closing slash: not escaped, not inside a [...] class.
|
|
109
|
+
let j = i + 1;
|
|
110
|
+
let inClass = false;
|
|
111
|
+
for (; j < line.length; j++) {
|
|
112
|
+
const d = line[j];
|
|
113
|
+
if (d === "\\") {
|
|
114
|
+
j++;
|
|
115
|
+
continue;
|
|
116
|
+
}
|
|
117
|
+
if (inClass) {
|
|
118
|
+
if (d === "]")
|
|
119
|
+
inClass = false;
|
|
120
|
+
}
|
|
121
|
+
else if (d === "[")
|
|
122
|
+
inClass = true;
|
|
123
|
+
else if (d === "/")
|
|
124
|
+
break;
|
|
125
|
+
}
|
|
126
|
+
if (j < line.length && j > i + 1) {
|
|
127
|
+
out += "/" + " ".repeat(j - i - 1) + "/";
|
|
128
|
+
i = j;
|
|
129
|
+
continue;
|
|
130
|
+
}
|
|
131
|
+
if (j >= line.length)
|
|
132
|
+
unclosed = true;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
out += c;
|
|
136
|
+
}
|
|
137
|
+
return out;
|
|
138
|
+
}
|