flecto 3.0.0 → 3.0.2
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 +534 -1
- package/README.md +59 -1
- package/index.js +427 -54
- package/package.json +4 -1
- package/schemas/flecto-policy-pack-2.0.json +2 -0
- package/src/baseline.js +193 -0
- package/src/config.js +463 -19
- package/src/encrypted.js +16 -13
- package/src/packs/github-actions.json +92 -0
- package/src/parser.js +212 -22
- package/src/policy-test.js +5 -1
- package/src/policy.js +96 -23
- package/src/pr-comment.js +53 -87
- package/src/pr-providers.js +261 -0
- package/src/renderer.js +7 -7
- package/src/report.js +39 -1
- package/src/sarif.js +144 -0
- package/src/secrets.js +41 -7
- package/src/suppressions.js +431 -0
- package/src/terraform.js +28 -0
package/src/config.js
CHANGED
|
@@ -1,14 +1,34 @@
|
|
|
1
|
-
import { existsSync, readFileSync, readdirSync, writeFileSync } from 'fs';
|
|
2
|
-
import { resolve } from 'path';
|
|
1
|
+
import { existsSync, readFileSync, readdirSync, realpathSync, statSync, writeFileSync } from 'fs';
|
|
2
|
+
import { basename, dirname, join, relative, resolve, sep } from 'path';
|
|
3
3
|
import fg from 'fast-glob';
|
|
4
4
|
import yaml from 'js-yaml';
|
|
5
|
-
import { isEnvFilename } from './parser.js';
|
|
5
|
+
import { isEnvFilename, parseContent } from './parser.js';
|
|
6
|
+
import { encryptionState } from './encrypted.js';
|
|
6
7
|
|
|
7
8
|
const RC_CANDIDATES = ['.flectorc', '.flectorc.json', '.flectorc.yaml', '.flectorc.yml'];
|
|
8
9
|
const COMPOSE_FILENAMES = ['docker-compose.yml', 'docker-compose.yaml', 'compose.yml', 'compose.yaml'];
|
|
9
10
|
const CONFIG_DIR_PATTERN = 'config/**/*.{yaml,yml,json,toml,ini}';
|
|
10
11
|
const ENV_FILE_PATTERNS = ['.env', '.env.*', '*.env'];
|
|
11
12
|
const GENERIC_FILE_PATTERNS = [CONFIG_DIR_PATTERN, ...ENV_FILE_PATTERNS];
|
|
13
|
+
const GITHUB_ACTIONS_WORKFLOW_PATTERN = '.github/workflows/**/*.{yaml,yml}';
|
|
14
|
+
|
|
15
|
+
// Conventional places a Kubernetes/SOPS repo keeps manifests, searched in
|
|
16
|
+
// addition to the repo root. Detection is content-based (see sniffManifestDirs),
|
|
17
|
+
// so these only bound *where* Flecto looks — a manifest named deploy.yaml still
|
|
18
|
+
// has to actually carry `apiVersion` + `kind` to count.
|
|
19
|
+
const MANIFEST_DIRS = ['k8s', 'kubernetes', 'manifests', 'deploy'];
|
|
20
|
+
|
|
21
|
+
// Cost guards. `flecto init` must not read a large repo exhaustively: the issue
|
|
22
|
+
// (#123) is explicit that reading every YAML file is the wrong trade. Sniffing
|
|
23
|
+
// stops after this many files, and a single file larger than the byte cap is
|
|
24
|
+
// skipped rather than read — a hand-written manifest is never megabytes, and a
|
|
25
|
+
// generated blob that large is not what we want to gate on anyway.
|
|
26
|
+
const MAX_SNIFF_FILES = 50;
|
|
27
|
+
const MAX_SNIFF_BYTES = 256 * 1024;
|
|
28
|
+
const SNIFF_EXTENSIONS = ['.yaml', '.yml', '.json'];
|
|
29
|
+
|
|
30
|
+
const K8S_ROOT_PATTERN = '*.{yaml,yml}';
|
|
31
|
+
const K8S_DIR_PATTERN = '**/*.{yaml,yml}';
|
|
12
32
|
|
|
13
33
|
/**
|
|
14
34
|
* @typedef {{
|
|
@@ -80,24 +100,177 @@ export function resolveEffectiveOptions(config, profile, cliOverrides = {}) {
|
|
|
80
100
|
return { ...defaults, ...profileOptions, ...cliOverrides };
|
|
81
101
|
}
|
|
82
102
|
|
|
103
|
+
/**
|
|
104
|
+
* Has the operator explicitly opted in to plugins declared in `.flectorc`?
|
|
105
|
+
* @returns {boolean}
|
|
106
|
+
*/
|
|
107
|
+
function rcPluginsAllowed() {
|
|
108
|
+
const raw = process.env.FLECTO_ALLOW_RC_PLUGINS;
|
|
109
|
+
return raw === '1' || String(raw).toLowerCase() === 'true';
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Has the operator explicitly opted in to targets that leave the project through
|
|
114
|
+
* a symlink?
|
|
115
|
+
* @returns {boolean}
|
|
116
|
+
*/
|
|
117
|
+
function symlinkTargetsAllowed() {
|
|
118
|
+
const raw = process.env.FLECTO_ALLOW_SYMLINK_TARGETS;
|
|
119
|
+
return raw === '1' || String(raw).toLowerCase() === 'true';
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Resolve symlinks where possible, falling back to the input for a path that
|
|
124
|
+
* does not exist yet.
|
|
125
|
+
*
|
|
126
|
+
* `realpathSync.native` first because on Windows it asks the OS for the final
|
|
127
|
+
* path, resolving 8.3 short names and normalizing case — two spellings of one
|
|
128
|
+
* directory would otherwise compare as different and make a contained path look
|
|
129
|
+
* like an escape.
|
|
130
|
+
* @param {string} path
|
|
131
|
+
* @returns {string}
|
|
132
|
+
*/
|
|
133
|
+
function canonical(path) {
|
|
134
|
+
try {
|
|
135
|
+
return realpathSync.native(path);
|
|
136
|
+
} catch {
|
|
137
|
+
// Falls through for a path that does not exist yet.
|
|
138
|
+
}
|
|
139
|
+
try {
|
|
140
|
+
return realpathSync(path);
|
|
141
|
+
} catch {
|
|
142
|
+
return resolve(path);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* @param {string} candidate
|
|
148
|
+
* @param {string} root both already canonical
|
|
149
|
+
* @returns {boolean}
|
|
150
|
+
*/
|
|
151
|
+
function isInside(candidate, root) {
|
|
152
|
+
return candidate === root || candidate.startsWith(root + sep);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Refuse a path that lives inside the project but reads from outside it through
|
|
157
|
+
* a symlink.
|
|
158
|
+
*
|
|
159
|
+
* On an untrusted pull request the file *names* are attacker-controlled, and so
|
|
160
|
+
* is what they point at. A pull request adding `config/app.ini` as a symlink to
|
|
161
|
+
* `~/.aws/credentials` gets that file parsed and its contents emitted — into the
|
|
162
|
+
* job log, the JSON envelope, and, with `--format pr-comment --pr-comment-post`,
|
|
163
|
+
* into a comment on the pull request itself. The attacker never controls the
|
|
164
|
+
* linked-to file, which is exactly what makes it worth reading.
|
|
165
|
+
*
|
|
166
|
+
* The rule is deliberately narrow, and it is about *escape*, not about location:
|
|
167
|
+
*
|
|
168
|
+
* - A path given from outside the project is operator intent — `flecto compare
|
|
169
|
+
* /etc/a.yaml /etc/b.yaml` is a real thing to do — and is untouched.
|
|
170
|
+
* - A path inside the project that resolves to somewhere inside it is fine, so
|
|
171
|
+
* symlinks within a repository keep working.
|
|
172
|
+
* - A path inside the project that resolves *out* of it is refused, because that
|
|
173
|
+
* is the one shape an untrusted pull request can author to reach a file it
|
|
174
|
+
* could not otherwise commit.
|
|
175
|
+
*
|
|
176
|
+
* `FLECTO_ALLOW_SYMLINK_TARGETS=1` opts out, for a checkout that genuinely links
|
|
177
|
+
* config in from a sibling directory. Refusing loudly rather than skipping is
|
|
178
|
+
* deliberate, for the reason rc-declared plugins are: a target that stops being
|
|
179
|
+
* scanned without saying so weakens a gate the operator believes is in place.
|
|
180
|
+
* @param {string} file absolute path as Flecto was given it
|
|
181
|
+
* @param {string} [cwd]
|
|
182
|
+
* @throws {Error} when the path escapes the project through a link
|
|
183
|
+
*/
|
|
184
|
+
export function assertTargetContained(file, cwd = process.cwd()) {
|
|
185
|
+
if (symlinkTargetsAllowed()) return;
|
|
186
|
+
|
|
187
|
+
const root = canonical(resolve(cwd));
|
|
188
|
+
const given = resolve(file);
|
|
189
|
+
// A path with nothing readable behind it cannot leak a file: either it does
|
|
190
|
+
// not exist, or it is a symlink whose target does not exist (existsSync
|
|
191
|
+
// follows the link). Missing targets are reported elsewhere; refusing one here
|
|
192
|
+
// as an "escape" is a false positive -- and canonical() cannot resolve a
|
|
193
|
+
// nonexistent path, so its symlink-normalized fallback would not even match
|
|
194
|
+
// `root` on a platform where the temp root is itself a symlink (macOS /var).
|
|
195
|
+
if (!existsSync(given)) return;
|
|
196
|
+
// Whether the target is *nominally* inside the project. Canonicalize its
|
|
197
|
+
// containing directory rather than the path itself: the directory is a real
|
|
198
|
+
// directory, never the symlink under test, so this normalizes Windows drive-
|
|
199
|
+
// letter case and 8.3 short names -- which would otherwise make an in-repo
|
|
200
|
+
// path compare as external and skip the check entirely -- without following
|
|
201
|
+
// the final link. On POSIX the two forms already agree, so this is a no-op
|
|
202
|
+
// there.
|
|
203
|
+
const nominal = join(canonical(dirname(given)), basename(given));
|
|
204
|
+
// Named from outside the project: nothing was escaped, it was never inside.
|
|
205
|
+
if (!isInside(nominal, root)) return;
|
|
206
|
+
|
|
207
|
+
const real = canonical(given);
|
|
208
|
+
if (isInside(real, root)) return;
|
|
209
|
+
|
|
210
|
+
throw new Error(
|
|
211
|
+
`Refusing to read "${relative(root, nominal).split(sep).join('/') || given}": it is a link out of the project, `
|
|
212
|
+
+ `resolving to ${real}.\n`
|
|
213
|
+
+ 'File names and links are attacker-controlled on an untrusted pull request, and '
|
|
214
|
+
+ 'reading one would put a file from outside the repository into Flecto\'s output.\n'
|
|
215
|
+
+ 'Set FLECTO_ALLOW_SYMLINK_TARGETS=1 if this link is intentional.',
|
|
216
|
+
);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* Split a policy list that may arrive as an array or a comma-separated string.
|
|
221
|
+
* @param {unknown} raw
|
|
222
|
+
* @param {string[]} fallback
|
|
223
|
+
* @returns {string[]}
|
|
224
|
+
*/
|
|
225
|
+
function toList(raw, fallback) {
|
|
226
|
+
if (Array.isArray(raw)) return raw.map(String);
|
|
227
|
+
if (typeof raw === 'string') return raw.split(',').map((s) => s.trim()).filter(Boolean);
|
|
228
|
+
return fallback;
|
|
229
|
+
}
|
|
230
|
+
|
|
83
231
|
/**
|
|
84
232
|
* Normalize policy-related effective options.
|
|
233
|
+
*
|
|
234
|
+
* Plugins execute code, and `.flectorc` is attacker-controlled on an untrusted
|
|
235
|
+
* pull request, so a plugin that came from the rc file rather than an explicit
|
|
236
|
+
* `--plugins` flag is refused unless the operator opts in with
|
|
237
|
+
* `FLECTO_ALLOW_RC_PLUGINS=1`. Refusing loudly rather than skipping silently is
|
|
238
|
+
* deliberate: a plugin that stops running without saying so would weaken a
|
|
239
|
+
* policy gate the operator believes is in place.
|
|
85
240
|
* @param {Record<string, unknown>} effective
|
|
241
|
+
* @param {{ pluginsFromCli?: boolean, cwd?: string }} [provenance]
|
|
86
242
|
*/
|
|
87
|
-
export function resolvePolicyOptions(effective) {
|
|
88
|
-
const policiesRaw = effective.policies;
|
|
89
|
-
const pluginsRaw = effective.plugins;
|
|
243
|
+
export function resolvePolicyOptions(effective, provenance = {}) {
|
|
90
244
|
const severityRemapRaw = effective.severityRemap;
|
|
91
|
-
const policies =
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
245
|
+
const policies = toList(effective.policies, ['default']);
|
|
246
|
+
const plugins = toList(effective.plugins, []);
|
|
247
|
+
|
|
248
|
+
if (plugins.length > 0 && !provenance.pluginsFromCli) {
|
|
249
|
+
if (!rcPluginsAllowed()) {
|
|
250
|
+
throw new Error(
|
|
251
|
+
'Refusing to load policy plugins declared in .flectorc: plugins execute code, '
|
|
252
|
+
+ 'and a config file can come from an untrusted pull request.\n'
|
|
253
|
+
+ `Declared: ${plugins.join(', ')}\n`
|
|
254
|
+
+ 'Pass them on the command line with --plugins instead, or set '
|
|
255
|
+
+ 'FLECTO_ALLOW_RC_PLUGINS=1 if this config is trusted.',
|
|
256
|
+
);
|
|
257
|
+
}
|
|
258
|
+
// Opted in, but the rc file may still be attacker-authored. Keep rc-declared
|
|
259
|
+
// plugins inside the project so `../../../../tmp/x.mjs` cannot reach a module
|
|
260
|
+
// planted elsewhere on the runner. An explicit --plugins is operator intent
|
|
261
|
+
// and stays unrestricted: shared policies outside the cwd are a real setup.
|
|
262
|
+
const root = resolve(provenance.cwd ?? process.cwd());
|
|
263
|
+
for (const pluginPath of plugins) {
|
|
264
|
+
const abs = resolve(root, pluginPath);
|
|
265
|
+
if (abs !== root && !abs.startsWith(root + sep)) {
|
|
266
|
+
throw new Error(
|
|
267
|
+
`Policy plugin declared in .flectorc is outside the project: ${pluginPath}\n`
|
|
268
|
+
+ 'Plugins execute code, so an rc-declared plugin must live inside the '
|
|
269
|
+
+ 'directory Flecto is running in.',
|
|
270
|
+
);
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
}
|
|
101
274
|
if (
|
|
102
275
|
severityRemapRaw !== undefined
|
|
103
276
|
&& (severityRemapRaw === null || Array.isArray(severityRemapRaw) || typeof severityRemapRaw !== 'object')
|
|
@@ -116,29 +289,249 @@ export function resolvePolicyOptions(effective) {
|
|
|
116
289
|
return { policies, plugins, severityRemap };
|
|
117
290
|
}
|
|
118
291
|
|
|
292
|
+
/**
|
|
293
|
+
* Convert a glob pattern to the separators `fast-glob` requires.
|
|
294
|
+
*
|
|
295
|
+
* fast-glob only understands POSIX separators and treats a backslash as an
|
|
296
|
+
* escape character, so a Windows path used as a pattern matches nothing at all:
|
|
297
|
+
* `config\*.yaml` asks for a file literally named `config*.yaml`. Since
|
|
298
|
+
* PowerShell and cmd tab-completion produce backslash paths, that is the
|
|
299
|
+
* default way a Windows user would invoke Flecto, and the failure looks like
|
|
300
|
+
* "no files matched" rather than like a platform bug.
|
|
301
|
+
*
|
|
302
|
+
* The rewrite is deliberately **Windows-only**. On POSIX a backslash is a legal
|
|
303
|
+
* character in a filename *and* a meaningful glob escape, so rewriting there
|
|
304
|
+
* would break patterns that work today. On Windows a backslash can only ever be
|
|
305
|
+
* a separator — the filesystem forbids it in a name — so there is nothing to
|
|
306
|
+
* lose.
|
|
307
|
+
*
|
|
308
|
+
* Only patterns are rewritten. Resolved paths stay native, which is what every
|
|
309
|
+
* `fs` call and the snapshot key derivation expect.
|
|
310
|
+
* @param {string} pattern
|
|
311
|
+
* @param {NodeJS.Platform} [platform] Injectable so the Windows behavior is
|
|
312
|
+
* testable from any host.
|
|
313
|
+
* @returns {string}
|
|
314
|
+
*/
|
|
315
|
+
export function normalizeGlobPattern(pattern, platform = process.platform) {
|
|
316
|
+
if (platform !== 'win32') return pattern;
|
|
317
|
+
return String(pattern).replaceAll('\\', '/');
|
|
318
|
+
}
|
|
319
|
+
|
|
119
320
|
/**
|
|
120
321
|
* Expand file patterns from rc include/files and direct CLI inputs.
|
|
121
|
-
* @param {{
|
|
322
|
+
* @param {{
|
|
323
|
+
* cwd?: string,
|
|
324
|
+
* files?: string[],
|
|
325
|
+
* include?: string[],
|
|
326
|
+
* exclude?: string[],
|
|
327
|
+
* platform?: NodeJS.Platform
|
|
328
|
+
* }} input
|
|
122
329
|
* @returns {Promise<string[]>}
|
|
123
330
|
*/
|
|
124
331
|
export async function resolveFiles(input) {
|
|
125
332
|
const cwd = input.cwd ?? process.cwd();
|
|
333
|
+
const platform = input.platform ?? process.platform;
|
|
126
334
|
const files = input.files ?? [];
|
|
127
335
|
const include = input.include ?? [];
|
|
128
336
|
const exclude = input.exclude ?? [];
|
|
129
|
-
const patterns = [...files, ...include]
|
|
337
|
+
const patterns = [...files, ...include]
|
|
338
|
+
.filter(Boolean)
|
|
339
|
+
.map((pattern) => normalizeGlobPattern(pattern, platform));
|
|
130
340
|
if (patterns.length === 0) return [];
|
|
341
|
+
// `exclude` is matched against the same pattern syntax, so it needs the same
|
|
342
|
+
// rewrite -- an exclude that silently stops excluding is the worse failure.
|
|
343
|
+
const ignore = exclude.filter(Boolean).map((pattern) => normalizeGlobPattern(pattern, platform));
|
|
131
344
|
const matches = await fg(patterns, {
|
|
132
345
|
cwd,
|
|
133
346
|
absolute: true,
|
|
134
347
|
onlyFiles: true,
|
|
135
348
|
unique: true,
|
|
136
|
-
ignore
|
|
349
|
+
ignore,
|
|
137
350
|
dot: true,
|
|
138
351
|
});
|
|
352
|
+
// Back to native separators: everything downstream reads these off disk.
|
|
139
353
|
return matches.map((p) => resolve(p));
|
|
140
354
|
}
|
|
141
355
|
|
|
356
|
+
/**
|
|
357
|
+
* Collect candidate files to sniff: YAML/JSON at the repo root plus, one level
|
|
358
|
+
* of recursion deep, the conventional manifest directories. Bounded by
|
|
359
|
+
* MAX_SNIFF_FILES so a large repo never turns `init` into a full-tree read.
|
|
360
|
+
* @param {string} cwd
|
|
361
|
+
* @param {string[]} rootFileNames
|
|
362
|
+
* @param {Set<string>} dirNames
|
|
363
|
+
* @returns {string[]} absolute paths, root files first
|
|
364
|
+
*/
|
|
365
|
+
function sniffCandidates(cwd, rootFileNames, dirNames) {
|
|
366
|
+
/** @type {string[]} */
|
|
367
|
+
const candidates = [];
|
|
368
|
+
const push = (abs) => {
|
|
369
|
+
if (candidates.length < MAX_SNIFF_FILES) candidates.push(abs);
|
|
370
|
+
};
|
|
371
|
+
|
|
372
|
+
for (const name of rootFileNames) {
|
|
373
|
+
if (SNIFF_EXTENSIONS.includes(extLower(name))) push(resolve(cwd, name));
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
for (const dir of MANIFEST_DIRS) {
|
|
377
|
+
if (!dirNames.has(dir)) continue;
|
|
378
|
+
for (const abs of walkYamlish(resolve(cwd, dir))) {
|
|
379
|
+
push(abs);
|
|
380
|
+
if (candidates.length >= MAX_SNIFF_FILES) break;
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
return candidates.slice(0, MAX_SNIFF_FILES);
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
/**
|
|
388
|
+
* Depth-first list of sniffable files under a directory, skipping node_modules
|
|
389
|
+
* and dot directories, capped by MAX_SNIFF_FILES so a deep tree cannot run away.
|
|
390
|
+
* @param {string} root
|
|
391
|
+
* @returns {string[]}
|
|
392
|
+
*/
|
|
393
|
+
function walkYamlish(root) {
|
|
394
|
+
/** @type {string[]} */
|
|
395
|
+
const out = [];
|
|
396
|
+
/** @type {string[]} */
|
|
397
|
+
const stack = [root];
|
|
398
|
+
while (stack.length > 0 && out.length < MAX_SNIFF_FILES) {
|
|
399
|
+
const dir = stack.pop();
|
|
400
|
+
let entries;
|
|
401
|
+
try {
|
|
402
|
+
entries = readdirSync(dir, { withFileTypes: true });
|
|
403
|
+
} catch {
|
|
404
|
+
continue;
|
|
405
|
+
}
|
|
406
|
+
for (const entry of entries) {
|
|
407
|
+
if (entry.isDirectory()) {
|
|
408
|
+
if (entry.name === 'node_modules' || entry.name.startsWith('.')) continue;
|
|
409
|
+
stack.push(join(dir, entry.name));
|
|
410
|
+
} else if (entry.isFile() && SNIFF_EXTENSIONS.includes(extLower(entry.name))) {
|
|
411
|
+
out.push(join(dir, entry.name));
|
|
412
|
+
if (out.length >= MAX_SNIFF_FILES) break;
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
return out;
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
/**
|
|
420
|
+
* @param {string} name
|
|
421
|
+
* @returns {string} lower-cased extension including the dot, or ''
|
|
422
|
+
*/
|
|
423
|
+
function extLower(name) {
|
|
424
|
+
const dot = name.lastIndexOf('.');
|
|
425
|
+
return dot === -1 ? '' : name.slice(dot).toLowerCase();
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
/**
|
|
429
|
+
* Parse a candidate file for detection, cheaply and defensively. Returns null
|
|
430
|
+
* for anything too large, unreadable, or unparseable — detection never fails a
|
|
431
|
+
* run, it just learns less. The byte cap is enforced before the read so a huge
|
|
432
|
+
* file is skipped rather than slurped.
|
|
433
|
+
* @param {string} abs
|
|
434
|
+
* @returns {unknown}
|
|
435
|
+
*/
|
|
436
|
+
function sniffParse(abs) {
|
|
437
|
+
try {
|
|
438
|
+
if (statSync(abs).size > MAX_SNIFF_BYTES) return null;
|
|
439
|
+
return parseContent(abs, readFileSync(abs, 'utf8'));
|
|
440
|
+
} catch {
|
|
441
|
+
return null;
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
/**
|
|
446
|
+
* True when a parsed tree (single- or multi-document) contains a
|
|
447
|
+
* Kubernetes-shaped document: `apiVersion` + `kind`. A multi-document file is
|
|
448
|
+
* the identity-keyed wrapper, so its documents are one level down.
|
|
449
|
+
* @param {unknown} tree
|
|
450
|
+
* @returns {boolean}
|
|
451
|
+
*/
|
|
452
|
+
function hasKubernetesDocument(tree) {
|
|
453
|
+
if (!isPlainObjectLike(tree)) return false;
|
|
454
|
+
if (isKubernetesShaped(tree)) return true;
|
|
455
|
+
return Object.values(tree).some((value) => isKubernetesShaped(value));
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
/**
|
|
459
|
+
* @param {unknown} doc
|
|
460
|
+
* @returns {boolean}
|
|
461
|
+
*/
|
|
462
|
+
function isKubernetesShaped(doc) {
|
|
463
|
+
return isPlainObjectLike(doc)
|
|
464
|
+
&& typeof doc.apiVersion === 'string' && doc.apiVersion.trim() !== ''
|
|
465
|
+
&& typeof doc.kind === 'string' && doc.kind.trim() !== '';
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
/**
|
|
469
|
+
* @param {unknown} v
|
|
470
|
+
* @returns {v is Record<string, unknown>}
|
|
471
|
+
*/
|
|
472
|
+
function isPlainObjectLike(v) {
|
|
473
|
+
return v !== null && typeof v === 'object' && !Array.isArray(v);
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
/**
|
|
477
|
+
* Sniff the manifest locations for Kubernetes and SOPS signals. Content-based
|
|
478
|
+
* and cost-bounded; see MAX_SNIFF_FILES / MAX_SNIFF_BYTES. `.sops.yaml` is a
|
|
479
|
+
* plaintext creation-rules config, not an encrypted file, but its presence is
|
|
480
|
+
* still a reliable sign the repo uses SOPS, so it counts on its own.
|
|
481
|
+
* @param {string} cwd
|
|
482
|
+
* @param {string[]} rootFileNames
|
|
483
|
+
* @param {Set<string>} dirNames
|
|
484
|
+
* @returns {{
|
|
485
|
+
* kubernetes: { files: string[] } | null,
|
|
486
|
+
* sops: { files: string[], creationRules: boolean } | null
|
|
487
|
+
* }}
|
|
488
|
+
*/
|
|
489
|
+
function sniffManifestDirs(cwd, rootFileNames, dirNames) {
|
|
490
|
+
const candidates = sniffCandidates(cwd, rootFileNames, dirNames);
|
|
491
|
+
|
|
492
|
+
/** @type {Set<string>} */
|
|
493
|
+
const k8sRel = new Set();
|
|
494
|
+
/** @type {Set<string>} */
|
|
495
|
+
const sopsRel = new Set();
|
|
496
|
+
const creationRules = rootFileNames.some((name) => name === '.sops.yaml' || name === '.sops.yml');
|
|
497
|
+
|
|
498
|
+
for (const abs of candidates) {
|
|
499
|
+
const tree = sniffParse(abs);
|
|
500
|
+
if (tree == null) continue;
|
|
501
|
+
const rel = relative(cwd, abs).split(sep).join('/');
|
|
502
|
+
if (hasKubernetesDocument(tree)) k8sRel.add(rel);
|
|
503
|
+
if (encryptionState(tree) !== 'plaintext') sopsRel.add(rel);
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
return {
|
|
507
|
+
kubernetes: k8sRel.size > 0 ? { files: [...k8sRel].sort() } : null,
|
|
508
|
+
sops: sopsRel.size > 0 || creationRules
|
|
509
|
+
? { files: [...sopsRel].sort(), creationRules }
|
|
510
|
+
: null,
|
|
511
|
+
};
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
/**
|
|
515
|
+
* Watch patterns for the directories that held Kubernetes manifests, plus the
|
|
516
|
+
* repo root when a manifest lived there. One pattern per conventional dir keeps
|
|
517
|
+
* the generated config short instead of listing every file.
|
|
518
|
+
* @param {string[]} relFiles
|
|
519
|
+
* @returns {string[]}
|
|
520
|
+
*/
|
|
521
|
+
function manifestWatchPatterns(relFiles) {
|
|
522
|
+
/** @type {Set<string>} */
|
|
523
|
+
const patterns = new Set();
|
|
524
|
+
for (const rel of relFiles) {
|
|
525
|
+
const top = rel.includes('/') ? rel.slice(0, rel.indexOf('/')) : null;
|
|
526
|
+
if (top && MANIFEST_DIRS.includes(top)) {
|
|
527
|
+
patterns.add(`${top}/${K8S_DIR_PATTERN}`);
|
|
528
|
+
} else {
|
|
529
|
+
patterns.add(K8S_ROOT_PATTERN);
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
return [...patterns].sort();
|
|
533
|
+
}
|
|
534
|
+
|
|
142
535
|
/**
|
|
143
536
|
* Detect stack signals in a directory and map them to policy packs and file
|
|
144
537
|
* patterns. Only built-in pack ids and patterns Flecto can actually parse are
|
|
@@ -189,6 +582,18 @@ export function detectStack(cwd = process.cwd()) {
|
|
|
189
582
|
});
|
|
190
583
|
}
|
|
191
584
|
|
|
585
|
+
const githubWorkflowsDir = resolve(cwd, '.github', 'workflows');
|
|
586
|
+
if (dirNames.has('.github') && existsSync(githubWorkflowsDir) && statSync(githubWorkflowsDir).isDirectory()) {
|
|
587
|
+
packs.push('github-actions');
|
|
588
|
+
files.push(GITHUB_ACTIONS_WORKFLOW_PATTERN);
|
|
589
|
+
signals.push({
|
|
590
|
+
id: 'github-actions',
|
|
591
|
+
evidence: ['.github/workflows/'],
|
|
592
|
+
pack: 'github-actions',
|
|
593
|
+
summary: 'Detected .github/workflows/ → enabled the `github-actions` policy pack and watched workflow YAML',
|
|
594
|
+
});
|
|
595
|
+
}
|
|
596
|
+
|
|
192
597
|
const terraformFiles = fileNames.filter((name) => name.toLowerCase().endsWith('.tf')).sort();
|
|
193
598
|
if (terraformFiles.length > 0) {
|
|
194
599
|
signals.push({
|
|
@@ -220,6 +625,45 @@ export function detectStack(cwd = process.cwd()) {
|
|
|
220
625
|
});
|
|
221
626
|
}
|
|
222
627
|
|
|
628
|
+
// Content-based signals for the two shapes 3.0 was built around. Sniffed, not
|
|
629
|
+
// guessed from filenames, and bounded (#123). Enabling `kubernetes` on a repo
|
|
630
|
+
// that is not Kubernetes would produce confusing findings on the first run, so
|
|
631
|
+
// a manifest must actually carry apiVersion + kind to count.
|
|
632
|
+
const manifests = sniffManifestDirs(cwd, fileNames, dirNames);
|
|
633
|
+
|
|
634
|
+
if (manifests.kubernetes) {
|
|
635
|
+
packs.push('kubernetes');
|
|
636
|
+
const watched = manifestWatchPatterns(manifests.kubernetes.files);
|
|
637
|
+
files.push(...watched);
|
|
638
|
+
const shown = manifests.kubernetes.files.slice(0, 3);
|
|
639
|
+
const more = manifests.kubernetes.files.length - shown.length;
|
|
640
|
+
const evidenceList = more > 0 ? `${shown.join(', ')}, +${more} more` : shown.join(', ');
|
|
641
|
+
signals.push({
|
|
642
|
+
id: 'kubernetes',
|
|
643
|
+
evidence: manifests.kubernetes.files,
|
|
644
|
+
pack: 'kubernetes',
|
|
645
|
+
summary: `Detected Kubernetes manifests (${evidenceList}) → enabled the \`kubernetes\` policy pack and watched ${watched.join(', ')}`,
|
|
646
|
+
});
|
|
647
|
+
}
|
|
648
|
+
|
|
649
|
+
if (manifests.sops) {
|
|
650
|
+
packs.push('sops');
|
|
651
|
+
if (manifests.sops.files.length > 0) files.push(...manifests.sops.files);
|
|
652
|
+
const evidence = [
|
|
653
|
+
...(manifests.sops.creationRules ? ['.sops.yaml'] : []),
|
|
654
|
+
...manifests.sops.files,
|
|
655
|
+
];
|
|
656
|
+
const watchedNote = manifests.sops.files.length > 0
|
|
657
|
+
? ` and watched ${manifests.sops.files.join(', ')}`
|
|
658
|
+
: ' (no encrypted files found yet; the pack applies when they appear)';
|
|
659
|
+
signals.push({
|
|
660
|
+
id: 'sops',
|
|
661
|
+
evidence,
|
|
662
|
+
pack: 'sops',
|
|
663
|
+
summary: `Detected SOPS usage (${evidence.join(', ')}) → enabled the \`sops\` policy pack${watchedNote}`,
|
|
664
|
+
});
|
|
665
|
+
}
|
|
666
|
+
|
|
223
667
|
return { signals, packs, files: [...new Set(files)] };
|
|
224
668
|
}
|
|
225
669
|
|
package/src/encrypted.js
CHANGED
|
@@ -292,14 +292,14 @@ function mapStrings(value, mapString) {
|
|
|
292
292
|
|
|
293
293
|
if (isPlainObject(value)) {
|
|
294
294
|
let changed = false;
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
295
|
+
// Rebuilt with fromEntries, not `out[key] = next`: a key literally named
|
|
296
|
+
// "__proto__" would run the prototype setter and drop the subtree.
|
|
297
|
+
const entries = Object.entries(value).map(([key, item]) => {
|
|
298
298
|
const next = mapStrings(item, mapString);
|
|
299
299
|
if (next !== item) changed = true;
|
|
300
|
-
|
|
301
|
-
}
|
|
302
|
-
return changed ?
|
|
300
|
+
return [key, next];
|
|
301
|
+
});
|
|
302
|
+
return changed ? Object.fromEntries(entries) : value;
|
|
303
303
|
}
|
|
304
304
|
|
|
305
305
|
return value;
|
|
@@ -385,14 +385,12 @@ function normalizeRecipientGroup(group, value) {
|
|
|
385
385
|
*/
|
|
386
386
|
function normalizeSopsBlock(block) {
|
|
387
387
|
let changed = false;
|
|
388
|
-
|
|
389
|
-
const out = {};
|
|
390
|
-
for (const [key, value] of Object.entries(block)) {
|
|
388
|
+
const entries = Object.entries(block).map(([key, value]) => {
|
|
391
389
|
const next = RECIPIENT_GROUPS.includes(key) ? normalizeRecipientGroup(key, value) : value;
|
|
392
390
|
if (next !== value) changed = true;
|
|
393
|
-
|
|
394
|
-
}
|
|
395
|
-
return changed ?
|
|
391
|
+
return [key, next];
|
|
392
|
+
});
|
|
393
|
+
return changed ? Object.fromEntries(entries) : block;
|
|
396
394
|
}
|
|
397
395
|
|
|
398
396
|
/**
|
|
@@ -436,7 +434,12 @@ export function normalizeEncrypted(tree, documentKeys = []) {
|
|
|
436
434
|
const normalized = normalizeSopsOwner(doc);
|
|
437
435
|
if (normalized === doc) continue;
|
|
438
436
|
if (out === redacted) out = { ...redacted };
|
|
439
|
-
out
|
|
437
|
+
Object.defineProperty(out, key, {
|
|
438
|
+
value: normalized,
|
|
439
|
+
writable: true,
|
|
440
|
+
enumerable: true,
|
|
441
|
+
configurable: true,
|
|
442
|
+
});
|
|
440
443
|
}
|
|
441
444
|
return out;
|
|
442
445
|
}
|