-
-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathindex.js
More file actions
465 lines (417 loc) · 13.9 KB
/
index.js
File metadata and controls
465 lines (417 loc) · 13.9 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
/**
* CycloneDX Schema Linter - Core Engine
*
* A modular linter for CycloneDX 2.0 JSON schemas, enforcing:
* - ISO House Style conventions
* - Oxford English spelling for descriptions
* - American English for property names
* - Consistent formatting and structure
*
* @license Apache-2.0
*/
import { readFileSync } from 'fs';
// Check registry - maps check names to their modules
const checkRegistry = new Map();
/**
* Severity levels for lint issues
*/
export const Severity = {
ERROR: 'error',
WARNING: 'warning',
INFO: 'info'
};
/**
* Represents a single lint issue found in a schema
*/
export class LintIssue {
/**
* @param {string} checkId - Unique identifier for the check
* @param {string} severity - Severity level (error, warning, info)
* @param {string} message - Human-readable description of the issue
* @param {string} path - JSON path to the problematic location
* @param {object} [context] - Additional context about the issue
*/
constructor(checkId, severity, message, path, context = {}) {
this.checkId = checkId;
this.severity = severity;
this.message = message;
this.path = path;
this.context = context;
}
/**
* Convert to plain object for serialisation
*/
toJSON() {
return {
checkId: this.checkId,
severity: this.severity,
message: this.message,
path: this.path,
context: this.context
};
}
/**
* Format as a human-readable string
*/
toString() {
const severityIcon = {
error: '✖',
warning: '⚠',
info: 'ℹ'
};
return `${severityIcon[this.severity] || '•'} [${this.checkId}] ${this.path}: ${this.message}`;
}
}
/**
* Result of linting a single schema file
*/
export class LintResult {
/**
* @param {string} filePath - Path to the schema file
*/
constructor(filePath) {
this.filePath = filePath;
this.issues = [];
this.checksRun = [];
this.startTime = Date.now();
this.endTime = null;
}
/**
* Add an issue to the result
* @param {LintIssue} issue
*/
addIssue(issue) {
this.issues.push(issue);
}
/**
* Mark a check as having been run
* @param {string} checkId
*/
markCheckRun(checkId) {
this.checksRun.push(checkId);
}
/**
* Finalise the result
*/
finalise() {
this.endTime = Date.now();
return this;
}
/**
* Get issues by severity
* @param {string} severity
*/
getIssuesBySeverity(severity) {
return this.issues.filter(issue => issue.severity === severity);
}
/**
* Check if there are any errors
*/
hasErrors() {
return this.issues.some(issue => issue.severity === Severity.ERROR);
}
/**
* Get summary statistics
*/
getSummary() {
return {
filePath: this.filePath,
totalIssues: this.issues.length,
errors: this.getIssuesBySeverity(Severity.ERROR).length,
warnings: this.getIssuesBySeverity(Severity.WARNING).length,
info: this.getIssuesBySeverity(Severity.INFO).length,
checksRun: this.checksRun.length,
duration: this.endTime - this.startTime
};
}
}
/**
* Base class for all lint checks
*/
export class LintCheck {
/**
* @param {string} id - Unique identifier for the check
* @param {string} name - Human-readable name
* @param {string} description - Detailed description of what the check validates
* @param {string} severity - Default severity level
*/
constructor(id, name, description, severity = Severity.ERROR) {
this.id = id;
this.name = name;
this.description = description;
this.defaultSeverity = severity;
}
/**
* Run the check against a schema
* @param {object} schema - The parsed JSON schema
* @param {string} rawContent - The raw file content (for formatting checks)
* @param {object} config - Configuration options for this check
* @returns {LintIssue[]} Array of issues found
*/
async run(schema, rawContent, config = {}) {
throw new Error('LintCheck.run() must be implemented by subclass');
}
/**
* Create an issue with this check's ID
* @param {string} message
* @param {string} path
* @param {object} context
* @param {string} [severity]
*/
createIssue(message, path, context = {}, severity = null) {
return new LintIssue(
this.id,
severity || this.defaultSeverity,
message,
path,
context
);
}
}
/**
* Register a check in the global registry
* @param {LintCheck} check
*/
export function registerCheck(check) {
if (!(check instanceof LintCheck)) {
throw new Error('Check must be an instance of LintCheck');
}
checkRegistry.set(check.id, check);
}
/**
* Get all registered checks
* @returns {Map<string, LintCheck>}
*/
export function getRegisteredChecks() {
return new Map(checkRegistry);
}
/**
* Get a specific check by ID
* @param {string} id
* @returns {LintCheck|undefined}
*/
export function getCheck(id) {
return checkRegistry.get(id);
}
/**
* Main linter class
*/
export class SchemaLinter {
/**
* @param {object} [config] - Configuration options
*/
constructor(config = {}) {
this.config = {
checks: config.checks || {},
excludeChecks: config.excludeChecks || [],
includeChecks: config.includeChecks || null, // null means all checks
...config
};
}
/**
* Lint a schema file
* @param {string} filePath - Path to the schema file
* @returns {Promise<LintResult>}
*/
async lintFile(filePath) {
const result = new LintResult(filePath);
let rawContent;
let schema;
try {
rawContent = readFileSync(filePath, 'utf-8');
} catch (err) {
result.addIssue(new LintIssue(
'file-read',
Severity.ERROR,
`Failed to read file: ${err.message}`,
filePath
));
return result.finalise();
}
try {
schema = JSON.parse(rawContent);
} catch (err) {
result.addIssue(new LintIssue(
'json-parse',
Severity.ERROR,
`Invalid JSON: ${err.message}`,
filePath
));
return result.finalise();
}
// Run all applicable checks
const checks = this.getApplicableChecks();
for (const check of checks) {
const checkConfig = this.config.checks[check.id] || {};
if (checkConfig.enabled === false) {
continue;
}
try {
const issues = await check.run(schema, rawContent, checkConfig, filePath);
issues.forEach(issue => result.addIssue(issue));
result.markCheckRun(check.id);
} catch (err) {
result.addIssue(new LintIssue(
'check-error',
Severity.ERROR,
`Check '${check.id}' failed: ${err.message}`,
filePath,
{ stack: err.stack }
));
}
}
return result.finalise();
}
/**
* Lint a schema from a string
* @param {string} content - Raw JSON content
* @param {string} [virtualPath] - Virtual path for error reporting
* @returns {Promise<LintResult>}
*/
async lintString(content, virtualPath = '<string>') {
const result = new LintResult(virtualPath);
let schema;
try {
schema = JSON.parse(content);
} catch (err) {
result.addIssue(new LintIssue(
'json-parse',
Severity.ERROR,
`Invalid JSON: ${err.message}`,
virtualPath
));
return result.finalise();
}
// Run all applicable checks
const checks = this.getApplicableChecks();
for (const check of checks) {
const checkConfig = this.config.checks[check.id] || {};
if (checkConfig.enabled === false) {
continue;
}
try {
const issues = await check.run(schema, content, checkConfig);
issues.forEach(issue => result.addIssue(issue));
result.markCheckRun(check.id);
} catch (err) {
result.addIssue(new LintIssue(
'check-error',
Severity.ERROR,
`Check '${check.id}' failed: ${err.message}`,
virtualPath,
{ stack: err.stack }
));
}
}
return result.finalise();
}
/**
* Lint multiple files
* @param {string[]} filePaths
* @returns {Promise<LintResult[]>}
*/
async lintFiles(filePaths) {
const results = [];
for (const filePath of filePaths) {
results.push(await this.lintFile(filePath));
}
return results;
}
/**
* Get checks that should be run based on configuration
* @returns {LintCheck[]}
*/
getApplicableChecks() {
const allChecks = Array.from(checkRegistry.values());
let checks = allChecks;
// Filter to only included checks if specified
if (this.config.includeChecks) {
checks = checks.filter(c => this.config.includeChecks.includes(c.id));
}
// Exclude specified checks
if (this.config.excludeChecks.length > 0) {
checks = checks.filter(c => !this.config.excludeChecks.includes(c.id));
}
return checks;
}
}
/**
* Build an unambiguous path segment for a key (bracket notation when key contains . or non-identifier chars)
* @param {string} base - Current path (e.g. '$' or '$.definitions')
* @param {string} key - Property key
* @returns {string}
*/
function safePathJoin(base, key) {
if (/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(key)) {
return `${base}.${key}`;
}
return `${base}[${JSON.stringify(key)}]`;
}
/** Keys that, if present in schema and later used in a path-based setter (e.g. lodash.set), could lead to prototype pollution. Exported so path consumers can reject/escape these segments; in traverseSchema used only for optional onDangerousKey notification (traversal is not skipped). */
export const DANGEROUS_PATH_KEYS = new Set(['__proto__', 'constructor', 'prototype']);
/** Default max traversal depth: no limit (Infinity), so default behaviour matches original—no truncation. Set a finite maxDepth (e.g. 1000) via config to guard against depth-only DoS; use onDepthLimit to warn when hit. */
export const DEFAULT_MAX_DEPTH = Number.POSITIVE_INFINITY;
/**
* Utility to traverse a JSON schema and call a visitor function.
*
* Behavior notes:
* - **Cycle detection (stack-based):** Objects/arrays on the current recursion path are kept in a WeakSet
* (inProgress). When leaving a node it is removed. So the same object reachable via different paths
* (shared refs / DAG / YAML aliases) is visited once per path; only when we would re-enter a node
* already on the current path do we skip (true cycle). This preserves coverage for path-sensitive
* checks while preventing infinite recursion.
* - **Paths:** Keys that are not simple identifiers (e.g. contain '.' or special chars) use bracket
* notation so paths are unambiguous (e.g. `$["a.b"]` instead of `$.a.b`).
* - **Depth limit:** Beyond maxDepth, traversal stops without visiting deeper nodes. Pass onDepthLimit
* to be notified when this happens so lint results are not silently incomplete. Recommended: have
* the runner (e.g. SchemaLinter) always pass onDepthLimit and emit a LintIssue so truncation is never silent.
* - **Dangerous path keys:** This function does not traverse the prototype chain and does not mutate
* objects. If path strings are only used for reporting, prototype pollution risk is negligible. If
* any consumer uses a path in a path-based setter (e.g. lodash.set), segments like __proto__,
* constructor, prototype can be dangerous. Pass onDangerousKey to be notified when such a key is
* encountered (traversal is not skipped; coverage is unchanged).
*
* @param {object} schema - The schema to traverse
* @param {function} visitor - Function called for each node: (node, path, key, parent)
* @param {string} [path] - Current path (used internally)
* @param {string} [key] - Current key (used internally)
* @param {object} [parent] - Parent node (used internally)
* @param {WeakSet} [inProgress] - Set of objects on the current recursion path for cycle detection (used internally)
* @param {number} [depth] - Current depth (used internally)
* @param {number} [maxDepth] - Stop recursing beyond this depth (used internally; default no limit so behaviour is unchanged unless set)
* @param {function(string, number): void} [onDepthLimit] - When provided, called with (path, depth) when traversal stops due to depth limit
* @param {function(string, string|null, object|null): void} [onCycle] - When provided, called with (path, key, parent) when a cycle is detected (node already on current path)
* @param {function(string, string): void} [onDangerousKey] - When provided, called with (path, key) when key is __proto__, constructor, or prototype (path could be dangerous if given to a setter); traversal still continues
*/
export function traverseSchema(schema, visitor, path = '$', key = null, parent = null, inProgress = new WeakSet(), depth = 0, maxDepth = DEFAULT_MAX_DEPTH, onDepthLimit = undefined, onCycle = undefined, onDangerousKey = undefined) {
if (typeof schema !== 'object' || schema === null) {
visitor(schema, path, key, parent);
return;
}
if (inProgress.has(schema)) {
if (typeof onCycle === 'function') onCycle(path, key, parent);
return;
}
inProgress.add(schema);
try {
visitor(schema, path, key, parent);
if (depth >= maxDepth) {
if (typeof onDepthLimit === 'function') onDepthLimit(path, depth);
return;
}
if (Array.isArray(schema)) {
schema.forEach((item, index) => {
traverseSchema(item, visitor, `${path}[${index}]`, index, schema, inProgress, depth + 1, maxDepth, onDepthLimit, onCycle, onDangerousKey);
});
} else {
for (const [k, v] of Object.entries(schema)) {
const nextPath = safePathJoin(path, k);
if (DANGEROUS_PATH_KEYS.has(k) && typeof onDangerousKey === 'function') onDangerousKey(nextPath, k);
traverseSchema(v, visitor, nextPath, k, schema, inProgress, depth + 1, maxDepth, onDepthLimit, onCycle, onDangerousKey);
}
}
} finally {
inProgress.delete(schema);
}
}
export default SchemaLinter;