forked from mxx1111/sparepack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.mjs
More file actions
186 lines (164 loc) · 6.56 KB
/
Copy pathconfig.mjs
File metadata and controls
186 lines (164 loc) · 6.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// Loading and validating sparepack.yaml.
//
// The config is an allowlist and only an allowlist. There is deliberately no `exclude`
// key: "ship everything except these" is the shape that leaks, because a file you forgot
// to think about defaults to being published. Here, a file you forgot about stays home.
import { readFile } from 'node:fs/promises'
import { glob } from 'node:fs/promises'
import { isAbsolute, relative, resolve, sep } from 'node:path'
import { parse } from 'yaml'
import { compileCustomRule } from './scan.mjs'
export const CONFIG_NAMES = ['sparepack.yaml', 'sparepack.yml']
const FILE_KEYS = ['include', 'interfaces', 'tests']
const KNOWN_KEYS = new Set([...FILE_KEYS, 'task', 'fixtures', 'redact', 'scanRules', 'allowFindings', 'out'])
class ConfigError extends Error {}
function fail(message) {
throw new ConfigError(message)
}
function asArray(value, key) {
if (value === undefined || value === null) return []
if (!Array.isArray(value)) fail(`"${key}" must be a list`)
return value
}
/**
* Reject anything that would resolve outside the repository root.
* Absolute paths, `..` traversal, and symlink escapes all land here.
*/
export function assertInsideRoot(root, candidate, what = 'path') {
const resolvedRoot = resolve(root)
const resolved = resolve(resolvedRoot, candidate)
if (resolved !== resolvedRoot && !resolved.startsWith(`${resolvedRoot}${sep}`)) {
fail(`${what} "${candidate}" resolves outside the repository root`)
}
return resolved
}
function validatePattern(pattern, key) {
if (typeof pattern !== 'string' || !pattern.trim()) {
fail(`"${key}" entries must be non-empty strings`)
}
if (isAbsolute(pattern)) {
fail(`"${key}" entry "${pattern}" must be relative to the repository root, not absolute`)
}
if (pattern.split(/[\\/]/).includes('..')) {
fail(`"${key}" entry "${pattern}" must not contain ".."`)
}
return pattern
}
function parseRedact(raw) {
return asArray(raw, 'redact').map((entry, i) => {
if (typeof entry !== 'object' || entry === null) {
fail(`redact[${i}] must be a mapping with "pattern" and "replace"`)
}
if (typeof entry.pattern !== 'string' || !entry.pattern) {
fail(`redact[${i}].pattern must be a non-empty string`)
}
if (typeof entry.replace !== 'string') {
fail(`redact[${i}].replace must be a string (use "" to delete)`)
}
let re
try {
re = new RegExp(entry.pattern, entry.flags ?? 'g')
} catch (err) {
fail(`redact[${i}]: invalid regex — ${err.message}`)
}
if (!re.global) re = new RegExp(re.source, `${re.flags}g`)
return { source: entry.pattern, re, replace: entry.replace }
})
}
function parseFixtures(raw) {
if (raw === undefined || raw === null) return []
if (typeof raw !== 'object' || Array.isArray(raw)) {
fail('"fixtures" must be a mapping of path -> generator')
}
return Object.entries(raw).map(([path, spec]) => {
validatePattern(path, 'fixtures')
if (typeof spec !== 'string' || !spec.trim()) {
fail(`fixtures["${path}"] must be a non-empty generator string`)
}
return { path, spec: spec.trim() }
})
}
/** Parse config text. Separated from disk access so tests need no fixtures on disk. */
export function parseConfig(text, { source = 'sparepack.yaml' } = {}) {
let raw
try {
raw = parse(text)
} catch (err) {
fail(`${source} is not valid YAML — ${err.message}`)
}
if (raw === null || raw === undefined) fail(`${source} is empty`)
if (typeof raw !== 'object' || Array.isArray(raw)) fail(`${source} must be a mapping at the top level`)
if ('exclude' in raw) {
fail(
'sparepack has no "exclude" key, by design. Denylists leak: a file nobody thought ' +
'about ends up published. List what you want to expose under include/interfaces/tests instead.',
)
}
const unknown = Object.keys(raw).filter((k) => !KNOWN_KEYS.has(k) && !k.startsWith('_'))
if (unknown.length) {
fail(`unknown key(s) in ${source}: ${unknown.join(', ')} (prefix a key with "_" for notes)`)
}
if (typeof raw.task !== 'string' || !raw.task.trim()) {
fail('"task" is required: one line saying what this pack is for. The worker reads it first.')
}
const config = {
task: raw.task.trim(),
out: typeof raw.out === 'string' && raw.out.trim() ? raw.out.trim() : 'sparepack-out',
include: asArray(raw.include, 'include').map((p) => validatePattern(p, 'include')),
interfaces: asArray(raw.interfaces, 'interfaces').map((p) => validatePattern(p, 'interfaces')),
tests: asArray(raw.tests, 'tests').map((p) => validatePattern(p, 'tests')),
fixtures: parseFixtures(raw.fixtures),
redact: parseRedact(raw.redact),
scanRules: asArray(raw.scanRules, 'scanRules').map(compileCustomRule),
allowFindings: asArray(raw.allowFindings, 'allowFindings').map((entry, i) => {
if (typeof entry !== 'string' || !entry.includes(':')) {
fail(`allowFindings[${i}] must look like "rule-id:path" or "rule-id:path:line"`)
}
return entry
}),
}
validatePattern(config.out, 'out')
const total = FILE_KEYS.reduce((n, key) => n + config[key].length, 0)
if (total === 0) {
fail('nothing to pack: list at least one file under include, interfaces, or tests')
}
if (config.tests.length === 0) {
// Not fatal — some packs are pure interface handoffs — but it is nearly always a mistake.
config.warnings = [
'no "tests" listed. Tests are how a worker knows what "done" means; without them ' +
'the acceptance criteria live only in prose and disputes get expensive.',
]
} else {
config.warnings = []
}
return config
}
/** Expand a glob pattern to repo-relative file paths, rejecting anything outside root. */
export async function expand(root, patterns, key) {
const seen = new Set()
for (const pattern of patterns) {
let matched = 0
for await (const entry of glob(pattern, { cwd: root, withFileTypes: true })) {
if (!entry.isFile()) continue
const abs = resolve(entry.parentPath ?? entry.path, entry.name)
assertInsideRoot(root, relative(root, abs), `${key} match`)
seen.add(relative(root, abs))
matched++
}
if (matched === 0) {
fail(`"${key}" pattern "${pattern}" matched no files. A typo here means silently shipping less than you meant to.`)
}
}
return [...seen].sort()
}
export async function loadConfig(path) {
let text
try {
text = await readFile(path, 'utf8')
} catch (err) {
if (err.code === 'ENOENT') fail(`no config at ${path} — run "sparepack init" first`)
throw err
}
return parseConfig(text, { source: path })
}
export { ConfigError }