Repository navigation
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·1974 lines (1875 loc) · 74.1 KB
/
Copy pathcli.js
File metadata and controls
executable file
·1974 lines (1875 loc) · 74.1 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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env node
// Copyright 2026 will Farrell, and sast-json-schema contributors.
// SPDX-License-Identifier: MIT
import { lookup as dnsLookup } from "node:dns/promises";
import { realpathSync } from "node:fs";
import { glob, readFile, stat } from "node:fs/promises";
import { BlockList, isIP } from "node:net";
import { relative, resolve } from "node:path";
import { pathToFileURL } from "node:url";
import { parseArgs } from "node:util";
import { setFlagsFromString } from "node:v8";
import { runInNewContext } from "node:vm";
import Ajv from "ajv/dist/2020.js";
import { isSafePattern } from "redos-detector";
import schema201909 from "./2019-09.json" with { type: "json" };
import schema202012 from "./2020-12.json" with { type: "json" };
import schemaDraft04 from "./draft-04.json" with { type: "json" };
import schemaDraft06 from "./draft-06.json" with { type: "json" };
import schemaDraft07 from "./draft-07.json" with { type: "json" };
import pkg from "./package.json" with { type: "json" };
const defaultOptions = {
strictTypes: false,
allErrors: true,
};
const DEFAULT_VERSION = "2020-12";
export const DNS_TIMEOUT_MS = 5_000;
export const DNS_CONCURRENCY = 10;
export const MAX_SSRF_HOSTNAMES = 256;
export const DNS_TOTAL_TIMEOUT_MS = 30_000;
// SAST meta-schemas keyed by draft version. Validators are compiled lazily in
// sast() below and memoized: a run compiles only the drafts its files declare,
// and eager compilation of all five cost ~0.4s of every CLI invocation
// (including --help/--version, which use none).
const metaSchemas = new Map([
["2020-12", schema202012],
["2019-09", schema201909],
["draft-07", schemaDraft07],
["draft-06", schemaDraft06],
["draft-04", schemaDraft04],
]);
const compiledSchemas = new Map();
// Known $schema URLs mapped to their draft version. URLs are stored in
// normalised form (no protocol, no trailing #) so callers can pass any
// http/https variant with or without a fragment.
const knownSchemaUrls = new Map([
["json-schema.org/draft/2020-12/schema", "2020-12"],
["json-schema.org/draft/2019-09/schema", "2019-09"],
["json-schema.org/draft-07/schema", "draft-07"],
["json-schema.org/draft-06/schema", "draft-06"],
["json-schema.org/draft-04/schema", "draft-04"],
]);
// Maps a user schema's $schema URL to the matching draft version.
const schemaVersion = (url) => {
if (!url) return DEFAULT_VERSION;
// A truthy non-string (e.g. `"$schema": 42`) is unsupported, not a crash.
if (typeof url !== "string") return undefined;
// Stryker disable next-line Regex: dropping the ^/$ anchors only changes the
// result for URLs with an interior "//" or "#", none of which are in the known
// set, so they resolve to the same (unsupported) lookup either way.
const normalized = url.replace(/^(?:https?:)?\/\//, "").replace(/#$/, "");
return knownSchemaUrls.get(normalized);
};
export const MAX_DEPTH = 32;
export const MAX_SCHEMA_SIZE = 64 * 1024 * 1024; // 64 MiB
// Hard cap on the total number of remote $ref/$dynamicRef entries crawlSchema
// will collect into result.refs. The distinct-hostname cap (MAX_SSRF_HOSTNAMES)
// only applies later, after every ref has already been buffered, so a schema
// with a huge number of refs to the same few hosts would still accumulate an
// unbounded array. This is a backstop (overall bounded by MAX_SCHEMA_SIZE); set
// well above what realistic schemas need (multiple refs per distinct host).
export const MAX_COLLECTED_REFS = 4 * MAX_SSRF_HOSTNAMES;
// Per-pattern budget for ReDoS analysis. Patterns that exceed this are
// fail-closed (reported as unsafe with reason "timedOut") to keep total
// scan time bounded on adversarial input.
export const REDOS_TIMEOUT_MS = 1_000;
// HEAP CIRCUIT BREAKER: the PRIMARY memory bound for ReDoS analysis. The
// `timeout` option bounds time but NOT memory, and redos-detector's `maxSteps`
// cannot serve as the memory control: a value low enough to bound a catastrophic
// pattern (which retains ~7MB post-GC and ~270MB pre-GC each, so a handful OOM a
// 600MB heap) also wrongly fail-closes legitimate complex-but-safe patterns
// (e.g. semver is reported hitMaxSteps at maxSteps<=250 but is SAFE at the
// library default). So we drop maxSteps and instead read the live heap before
// each pattern against a baseline captured at the first pattern. Raw heapUsed
// counts garbage, and GC timing under parallel load made that reading trip on
// provably safe schemas, so an apparent exceedance is CONFIRMED before it
// fires: force a full GC (see forceGc below), re-read, and trip only when
// growth above the ORIGINAL baseline survives collection, i.e. is retained.
// The forced GC also reclaims the garbage itself, so catastrophic patterns'
// ~270MB transients are collected between patterns instead of accumulating,
// while their ~7MB retained residue still trips the breaker after ~19 evil
// patterns (well before OOM at --max-old-space-size=600). When no GC can be
// obtained the raw reading trips as before (fail closed). On trip, analysis
// STOPS and one fail-closed (incomplete) finding is emitted. Injectable via
// crawlSchema options (memoryUsage, forceGc, redosHeapBudgetBytes) for
// deterministic testing.
export const REDOS_HEAP_BUDGET_BYTES = 128 * 1024 * 1024;
// Forces a full V8 GC so the heap breaker measures RETAINED growth instead of
// GC-timing-dependent garbage. Uses globalThis.gc when the process runs with
// --expose-gc; otherwise enables the flag at runtime and captures gc from a
// throwaway context (the standard expose-gc trick; the already-created main
// context is unaffected). Returns false when no gc handle could be obtained,
// letting callers fail closed.
let cachedGc;
// Stryker disable ConditionalExpression,BlockStatement: the branches below only
// diverge on runtimes where gc is pre-exposed (--expose-gc, reachable only in a
// subprocess) or where the flag capture fails (not drivable on stock Node); on a
// plain run every surviving branch yields the same working handle. The plain-run test pins the capture
// path; the --expose-gc subprocess test pins the pre-exposed path.
export const forceGc = () => {
// Stryker disable next-line EqualityOperator: inverting the cache check only
// re-resolves the handle on every call; memoization is a pure optimization.
if (cachedGc === undefined) {
// Stryker disable next-line StringLiteral: mutating "function" sends the
// pre-exposed case down the runtime capture, which yields an equally
// working handle, so no test can observe the difference.
if (typeof globalThis.gc === "function") {
cachedGc = globalThis.gc;
} else {
try {
setFlagsFromString("--expose-gc");
const gc = runInNewContext("gc");
cachedGc = typeof gc === "function" ? gc : null;
} catch {
cachedGc = null;
}
}
}
// Stryker disable next-line BooleanLiteral: the null branch is reachable only
// when the flag capture fails, which stock Node cannot be driven to do, so
// this return value is unobservable in tests; callers fail closed on false.
if (!cachedGc) return false;
cachedGc();
return true;
};
// Stryker restore ConditionalExpression,BlockStatement
// Defense in depth: a hard cap on the TOTAL number of regex patterns crawlSchema
// will ReDoS-analyze in a single crawl. The heap circuit breaker above is the
// primary memory control; this is an independent backstop against an adversary
// supplying a huge number of patterns. Set well above what realistic schemas
// need (they can legitimately carry hundreds of simple patterns, which are
// cheap).
export const MAX_REDOS_PATTERNS = 256;
export const ANALYSIS_TIMEOUT_MS = 60_000;
// Property names that act as deserialization / type-confusion vectors in
// each downstream language ecosystem. Selected at the analyze() / CLI layer
// via the `lang` option (default: "default", the union of every named lang,
// because JSON specs typically flow through multiple language toolchains
// and the safe default is to catch them all). Set --lang explicitly to
// narrow scope when you control the consumer environment.
//
// "default": union of every named language entry below. Most paranoid baseline.
// "js": V8 prototype-pollution: __proto__, constructor, prototype.
// "py": js + Python introspection / pickle gadget keys.
// "rb": js + Ruby reflection / JSON.load(create_additions: true).
// "rs": js baseline (Rust serde itself is type-safe, but specs often
// pass through JS tooling that is not).
// "java": js + Jackson/Fastjson polymorphic deserialization markers.
// "kotlin": alias of java (JVM/Jackson).
// "clojure": alias of java (JVM/Cheshire).
// "cs": js + .NET JSON deserialization markers. Covers C#, VB.NET,
// ASP.NET, and ASPX (they all share the same serializer stack:
// Json.NET $type, DataContractJsonSerializer __type, OData @odata.type).
// "vb": alias of cs (.NET stack).
// "fsharp": alias of cs (.NET stack).
// "php": js + PHP magic methods invoked during object hydration
// (Symfony Serializer / JMS Serializer / unserialize gadget chains).
// "objc": js + Objective-C runtime keys: isa, class, superclass,
// description, init, _cmd (KVC + performSelector: vectors).
// "swift": alias of objc (Obj-C runtime exposure via interop; pure Codable
// is type-safe but mixed projects share the same surface).
// "ex": js + Elixir/BEAM struct-identifier keys: __struct__,
// __exception__, __protocol__ (auto-recognized when JSON is
// decoded with :keys => :atoms and hydrated into a struct).
// "lua": js + Lua metamethod names (__index, __newindex, __call,
// __metatable, __tostring, __gc, __close, etc.) for libraries
// that auto-bind metatables onto JSON-decoded tables.
//
// There is no "off" switch: the meta-schema enforces __proto__/constructor/
// prototype universally, so the narrowest opt-out is --lang js (which adds
// no extras over the meta-schema baseline). For per-path false positives,
// use --ignore <instancePath>.
const JS_NAMES = ["__proto__", "constructor", "prototype"];
const JVM_NAMES = [...JS_NAMES, "@type", "@class"];
const DOTNET_NAMES = [...JS_NAMES, "$type", "__type", "@odata.type"];
const OBJC_NAMES = [
...JS_NAMES,
"isa",
"class",
"superclass",
"description",
"init",
"_cmd",
];
export const DANGEROUS_NAMES_BY_LANG = {
js: JS_NAMES,
py: [
...JS_NAMES,
"__class__",
"__init__",
"__globals__",
"__builtins__",
"__import__",
"__reduce__",
"__subclasses__",
"__dict__",
"__mro__",
],
rb: [
...JS_NAMES,
"__send__",
"json_class",
"instance_eval",
"instance_variable_set",
"singleton_class",
],
rs: JS_NAMES,
java: JVM_NAMES,
kotlin: JVM_NAMES,
clojure: JVM_NAMES,
cs: DOTNET_NAMES,
vb: DOTNET_NAMES,
fsharp: DOTNET_NAMES,
php: [
...JS_NAMES,
"__construct",
"__destruct",
"__wakeup",
"__sleep",
"__serialize",
"__unserialize",
"__call",
"__callStatic",
"__get",
"__set",
"__isset",
"__unset",
"__toString",
"__invoke",
"__set_state",
"__clone",
"__debugInfo",
],
objc: OBJC_NAMES,
swift: OBJC_NAMES,
ex: [...JS_NAMES, "__struct__", "__exception__", "__protocol__"],
lua: [
...JS_NAMES,
"__index",
"__newindex",
"__call",
"__metatable",
"__tostring",
"__name",
"__pairs",
"__eq",
"__lt",
"__le",
"__add",
"__sub",
"__mul",
"__div",
"__mod",
"__pow",
"__concat",
"__len",
"__unm",
"__band",
"__bor",
"__bxor",
"__bnot",
"__shl",
"__shr",
"__idiv",
"__close",
"__gc",
],
};
DANGEROUS_NAMES_BY_LANG.default = [
...new Set(Object.values(DANGEROUS_NAMES_BY_LANG).flat()),
];
export const DEFAULT_LANG = "default";
const resolveDangerousNames = (lang) => {
if (lang == null) return DANGEROUS_NAMES_BY_LANG[DEFAULT_LANG];
if (Array.isArray(lang)) return lang;
// hasOwn: "__proto__" or "toString" must not resolve to Object.prototype.
const list =
Object.hasOwn(DANGEROUS_NAMES_BY_LANG, lang) &&
DANGEROUS_NAMES_BY_LANG[lang];
if (!list) {
throw new TypeError(
`unknown lang "${lang}", expected one of: ${Object.keys(DANGEROUS_NAMES_BY_LANG).join(", ")}`,
);
}
return list;
};
// AJV schema paths used by override filters. Verified by regression tests
// in cli.analyze.test.js to match what AJV actually emits.
const SCHEMA_PATH_MAX_ITEMS = "#/$defs/safeArrayItemsLimits/maxItems";
const SCHEMA_PATH_MAX_PROPERTIES =
"#/$defs/safeObjectPropertiesLimits/maxProperties";
// Returns the SAST validator for the draft declared by `schema.$schema`,
// compiling it on first use (memoized in compiledSchemas). Defaults to
// 2020-12 when $schema is absent.
export const sast = (schema) => {
const url = schema?.$schema;
const version = schemaVersion(url);
const metaSchema = metaSchemas.get(version);
if (!metaSchema) {
throw new Error(`Unsupported $schema: ${url}`);
}
let validate = compiledSchemas.get(version);
if (!validate) {
// allErrors:true is required so a single pass surfaces EVERY meta-schema
// violation for the security report; the schemas are trusted, not attacker
// input, so unbounded error allocation is not a DoS vector here.
// nosemgrep: javascript.ajv.security.audit.ajv-allerrors-true.ajv-allerrors-true
validate = new Ajv(defaultOptions).compile(metaSchema);
compiledSchemas.set(version, validate);
}
return validate;
};
export default sast;
// Checks whether a numeric schema's min/max bounds describe an impossible
// range. Returns an AJV-style error object when they do, or null otherwise.
const checkNumericRange = (current, path) => {
// Number.isFinite is true only for an actual finite number, so it already
// implies `typeof === "number"`; no separate type check is needed.
const hasMin =
Object.hasOwn(current, "minimum") && Number.isFinite(current.minimum);
const hasExMin =
Object.hasOwn(current, "exclusiveMinimum") &&
Number.isFinite(current.exclusiveMinimum);
const hasMax =
Object.hasOwn(current, "maximum") && Number.isFinite(current.maximum);
const hasExMax =
Object.hasOwn(current, "exclusiveMaximum") &&
Number.isFinite(current.exclusiveMaximum);
// draft-04 spells an exclusive bound as `exclusiveMinimum: true` beside
// `minimum` (and likewise for maximum), so the boolean form makes the plain
// bound exclusive instead of carrying its own value.
const boolExMin = current.exclusiveMinimum === true;
const boolExMax = current.exclusiveMaximum === true;
if (!(hasMin || hasExMin) || !(hasMax || hasExMax)) return null;
let effectiveMin;
let minIsExclusive = false;
if (hasMin && hasExMin) {
if (current.exclusiveMinimum >= current.minimum) {
effectiveMin = current.exclusiveMinimum;
minIsExclusive = true;
} else {
effectiveMin = current.minimum;
}
} else if (hasExMin) {
effectiveMin = current.exclusiveMinimum;
minIsExclusive = true;
} else {
effectiveMin = current.minimum;
minIsExclusive = boolExMin;
}
let effectiveMax;
let maxIsExclusive = false;
if (hasMax && hasExMax) {
if (current.exclusiveMaximum <= current.maximum) {
effectiveMax = current.exclusiveMaximum;
maxIsExclusive = true;
} else {
effectiveMax = current.maximum;
}
} else if (hasExMax) {
effectiveMax = current.exclusiveMaximum;
maxIsExclusive = true;
} else {
effectiveMax = current.maximum;
maxIsExclusive = boolExMax;
}
const impossible =
minIsExclusive || maxIsExclusive
? !(effectiveMin < effectiveMax)
: effectiveMin > effectiveMax;
if (!impossible) return null;
const keyword = maxIsExclusive
? "exclusiveMaximum"
: minIsExclusive
? "exclusiveMinimum"
: "minimum";
return {
instancePath: path,
schemaPath: `#/${keyword}`,
keyword,
params: {
...(hasMin && { minimum: current.minimum }),
...((hasExMin || boolExMin) && {
exclusiveMinimum: current.exclusiveMinimum,
}),
...(hasMax && { maximum: current.maximum }),
...((hasExMax || boolExMax) && {
exclusiveMaximum: current.exclusiveMaximum,
}),
},
message: "numeric range is unsatisfiable",
};
};
const INSTANCE_DATA_KEYS = new Set([
"const",
"enum",
"default",
"examples",
"errorMessage",
]);
// Keywords whose value maps NAMES to subschemas. Inside one of these, a key
// such as "default" or "enum" is a property/definition name, not instance
// data, so INSTANCE_DATA_KEYS must not skip it (otherwise naming a property
// "default" hides its whole subtree from every crawl check).
const SCHEMA_MAP_KEYS = new Set([
"properties",
"patternProperties",
"$defs",
"definitions",
"dependentSchemas",
"dependencies",
]);
// [type, minKeyword, maxKeyword] triples whose min > max makes a schema
// unsatisfiable. Each is only checked when the schema declares the matching
// type, so `{minItems: 5, maxItems: 1}` on a string is not a finding.
const MIN_MAX_PAIRS = [
["string", "minLength", "maxLength"],
["array", "minItems", "maxItems"],
["array", "minContains", "maxContains"],
["object", "minProperties", "maxProperties"],
];
// Index of the ] closing the character class opened at `open`, or -1 when it
// never closes. Escapes inside the class are skipped as pairs, so \] does not
// close it. Patterns are untrusted, so the regex scanners below are hand-written
// and linear: a global regex over "[[[[..." or "\p{\p{..." would be quadratic.
// A class that never closes means no later [ can close either (the scan
// from it sees the same tail), so the scanners stop looking after one failure.
const classEnd = (pattern, open) => {
// Stryker disable next-line EqualityOperator: pattern[length] is undefined and matches neither branch.
for (let i = open + 1; i < pattern.length; i++) {
if (pattern[i] === "\\") i++;
else if (pattern[i] === "]") return i;
}
return -1;
};
// True when `pattern` has a `|` outside every group and character class, so
// ^A|B$ parses as (^A)|(B$) and the anchors no longer bind the whole string.
// Escapes and whole classes are skipped: their inner ( ) | are literals.
// The meta-schema can only see a few group levels deep; this scan has no limit.
const hasTopLevelAlternation = (pattern) => {
let depth = 0;
let classesClose = true;
// Stryker disable next-line EqualityOperator: pattern[length] is undefined and matches no branch.
for (let i = 0; i < pattern.length; i++) {
const c = pattern[i];
if (c === "\\") i++;
else if (c === "[" && classesClose) {
const end = classEnd(pattern, i);
if (end === -1) classesClose = false;
else i = end;
} else if (c === "(") depth++;
else if (c === ")") depth--;
else if (c === "|" && depth === 0) return true;
}
return false;
};
// C0 controls except the whitespace \s covers (\t-\r), DEL, and C1: every
// code point below U+00A0 that is neither \t-\r nor printable ASCII.
const CONTROL_CHARS = Array.from({ length: 0xa0 }, (_, c) =>
String.fromCodePoint(c),
).filter((ch) => /[^\t-\r -~]/u.test(ch));
// A fixed-length escape at the sticky index: \xHH, \uHHHH, \cX, or \ plus one
// code point. Bounded, so it cannot backtrack.
const FIXED_ESCAPE = /\\(?:x[0-9a-fA-F]{2}|u[0-9a-fA-F]{4}|c[A-Za-z]|.)/suy;
// Each single-character atom of a regex: a character class, an escape with its
// full argument, or any other character. Group syntax, quantifiers and anchors
// also come out as "atoms", but none of them compiles on its own under the u
// flag, so matchesControlChar skips them. An unterminated [ and a trailing \
// are not atoms. Linear, like hasTopLevelAlternation: once a class or a \p{
// fails to close, later ones cannot close either, so neither is searched again.
function* regexAtoms(pattern) {
let classesClose = true;
let bracesClose = true;
let i = 0;
while (i < pattern.length) {
// Index of the atom's last code unit.
let end = -1;
if (pattern[i] === "[") {
if (classesClose) end = classEnd(pattern, i);
if (end === -1) {
classesClose = false;
i++;
continue;
}
} else if (pattern[i] === "\\") {
// \p{...}, \P{...} and \u{...} run to the next }.
if (pattern[i + 2] === "{" && "pPu".includes(pattern[i + 1])) {
// Stryker disable next-line ConditionalExpression: skips only a search that would fail.
if (bracesClose) end = pattern.indexOf("}", i + 3);
// Stryker disable next-line ConditionalExpression,UnaryOperator,BooleanLiteral: speed only.
if (end === -1) bracesClose = false;
}
if (end === -1) {
FIXED_ESCAPE.lastIndex = i;
const fixed = FIXED_ESCAPE.exec(pattern);
if (fixed === null) return;
end = i + fixed[0].length - 1;
}
} else {
end = i + String.fromCodePoint(pattern.codePointAt(i)).length - 1;
}
yield pattern.slice(i, end + 1);
i = end + 1;
}
}
// True when any atom of `pattern` can match a control character, whatever the
// spelling: a range spanning one ([ -]), a property (\p{Any}), an escape
// or a raw character. The regex engine decides, so no spelling is missed.
const matchesControlChar = (pattern) => {
for (const atom of regexAtoms(pattern)) {
let re;
try {
// nosemgrep: javascript.lang.security.audit.detect-non-literal-regexp.detect-non-literal-regexp
re = new RegExp(`^(?:${atom})$`, "u");
} catch {
continue; // not a standalone atom (e.g. "(" or "{"); nothing to test
}
if (CONTROL_CHARS.some((ch) => re.test(ch))) return true;
}
return false;
};
// RFC 6901 JSON Pointer token escaping: ~ → ~0, / → ~1.
// https://datatracker.ietf.org/doc/html/rfc6901#section-3
const escapeJsonPointer = (token) =>
token.replace(/~/g, "~0").replace(/\//g, "~1");
// Single-pass crawler that records: max depth, range/length inconsistencies,
// ReDoS patterns, and remote $ref URLs (for later SSRF resolution).
// Depth semantics: each object-valued key counts as one level, so a schema
// `{properties: {a: {properties: {b: {...}}}}}` reaches depth 5 (root,
// properties, a, properties, b). With MAX_DEPTH=32 this corresponds to roughly
// 16 levels of real schema nesting.
export const crawlSchema = (obj, maxDepth = MAX_DEPTH, options = {}) => {
const result = {
depth: 0,
depthExceeded: false,
timedOut: false,
errors: [],
refs: [],
};
if (typeof obj !== "object" || obj === null) return result;
const deadline = options.deadline;
// Injectable monotonic clock (defaults to the real wall clock). Reading the
// clock through this indirection lets tests drive the deadline branches
// deterministically (the same pattern as options.memoryUsage below).
const now = typeof options.now === "function" ? options.now : Date.now;
const denylist = resolveDangerousNames(options.lang);
const denySet = new Set(denylist);
const lang = options.lang ?? DEFAULT_LANG;
// Records one dangerous-name finding; every site shares this shape.
const flagDangerous = (instancePath, keyword, params, what, suffix = "") => {
result.errors.push({
instancePath,
schemaPath: "#/dangerous-name",
keyword,
params: { ...params, lang },
message: `${what} for lang="${lang}"${suffix}`,
});
};
const visited = new WeakSet();
visited.add(obj);
result.depth = 1;
// Entries: [node, path, depth, role, base]. role is "map" for a keyword
// value that maps names to subschemas, "names" for a propertyNames
// subschema, undefined otherwise.
const stack = [[obj, "", 1, undefined, undefined]];
// Defense in depth: total number of regex patterns ReDoS-analyzed so far.
// Shared across top-level `pattern` and patternProperties keys. Once it
// exceeds MAX_REDOS_PATTERNS, no further pattern is analyzed and a single
// fail-closed budget finding is emitted (see redosBudgetExceeded).
let redosPatternCount = 0;
// Number() because the CLI hands this through as a string; analyze() has
// already rejected anything that is not a non-negative integer.
const maxRedosPatterns = Number(
options.maxRedosPatterns ?? MAX_REDOS_PATTERNS,
);
let redosBudgetReported = false;
// One-time flag: have we already recorded the collected-refs truncation
// finding? (See MAX_COLLECTED_REFS in the $ref/$dynamicRef collection below.)
let refsTruncated = false;
// Pushes the timeout finding and flags the result as timed out. Used both at
// the top of the stack loop and before each individual ReDoS analysis (the
// per-pattern loops can run many isSafePattern() calls in one stack frame, so
// the once-per-pop check is not enough on adversarial input).
const timeoutBail = () => {
result.errors.push({
instancePath: "",
schemaPath: "#/timeout",
keyword: "timeout",
params: {},
message: "schema analysis exceeded time budget",
});
result.timedOut = true;
};
// True when a configured deadline has passed. Without a deadline the option
// is undefined and `now() > undefined` is false, so no guard is needed. The
// `>` boundary is exclusive (a clock reading EXACTLY at the deadline does
// NOT bail), pinned by the injected-clock deadline tests in
// cli.crawl.test.js.
const deadlinePassed = () => now() > deadline;
// Returns true (and emits one #/redos-budget finding the first time) when the
// total-pattern cap has been exceeded, so callers can skip further analysis.
// Marked incomplete:true so --ignore cannot suppress it (analysis stopped).
const redosBudgetExceeded = (path) => {
if (redosPatternCount <= maxRedosPatterns) return false;
if (!redosBudgetReported) {
redosBudgetReported = true;
result.errors.push({
instancePath: path,
schemaPath: "#/redos-budget",
keyword: "pattern",
params: { limit: maxRedosPatterns, incomplete: true },
message: `refusing to ReDoS-analyze more than ${maxRedosPatterns} patterns; remaining patterns not analyzed`,
});
}
return true;
};
// HEAP CIRCUIT BREAKER (primary memory bound). Reads live heap usage before
// each pattern; once it has grown beyond redosHeapBudgetBytes above the
// baseline captured at the first pattern, analysis stops and one fail-closed
// finding is emitted. Marked incomplete:true so --ignore cannot suppress it.
// Injectable: options.memoryUsage / options.redosHeapBudgetBytes for tests.
const memoryUsage =
typeof options.memoryUsage === "function"
? options.memoryUsage
: () => process.memoryUsage().heapUsed;
// Number() because the CLI hands these through as strings; analyze() has already
// rejected anything that is not a non-negative integer.
const redosHeapBudgetBytes = Number(
options.redosHeapBudgetBytes ?? REDOS_HEAP_BUDGET_BYTES,
);
// Per-pattern TIME budget, injectable alongside the heap budget above. Raise it
// for trusted first-party schemas whose patterns are complex but provably safe:
// a large \p{...} class next to a wide bounded repeat takes seconds to analyze
// and so fails closed at the 1s default.
const redosTimeoutMs = Number(options.redosTimeoutMs ?? REDOS_TIMEOUT_MS);
// GC hook for the breaker's confirm step; injectable for deterministic tests
// (same pattern as options.memoryUsage above).
const runGc =
typeof options.forceGc === "function" ? options.forceGc : forceGc;
let redosHeapBaseline = null;
let redosHeapReported = false;
// Returns true (and emits one #/redos-budget heap finding the first time) when
// RETAINED heap growth exceeds the budget above the baseline. The first call
// captures the baseline (delta 0, never trips). An apparent exceedance may be
// uncollected garbage (raw heapUsed depends on GC timing, which false-tripped
// safe schemas under parallel load), so it is confirmed with one forced GC and
// a re-read before it fires. The baseline is deliberately NOT reset by a
// confirm, so retained growth is always measured against the first-pattern
// reading and repeated confirms cannot ratchet past the budget. When no GC is
// available the raw reading trips as before (fail closed).
const redosHeapExceeded = (path) => {
let current = memoryUsage();
if (redosHeapBaseline === null) redosHeapBaseline = current;
if (current - redosHeapBaseline > redosHeapBudgetBytes && runGc()) {
current = memoryUsage();
}
if (current - redosHeapBaseline <= redosHeapBudgetBytes) return false;
if (!redosHeapReported) {
redosHeapReported = true;
result.errors.push({
instancePath: path,
schemaPath: "#/redos-budget",
keyword: "heap",
params: { budget: redosHeapBudgetBytes, incomplete: true },
message: `ReDoS analysis heap budget of ${redosHeapBudgetBytes} bytes exceeded; remaining patterns not analyzed`,
});
}
return true;
};
// Flags a regex (already vetted by analyzePattern) that matches any
// denylisted name; `label` names the site in the message.
const flagPatternMatches = (pattern, instancePath, keyword, label) => {
try {
// "u" like AJV, so \p{...} and friends mean what the validator reads.
// nosemgrep: javascript.lang.security.audit.detect-non-literal-regexp.detect-non-literal-regexp
const re = new RegExp(pattern, "u");
const matches = denylist.filter((n) => re.test(n));
if (matches.length > 0) {
flagDangerous(
instancePath,
keyword,
{ pattern, matches },
`${label} "${pattern}" matches deserialization vector(s)`,
`: ${matches.join(", ")}`,
);
}
} catch {
// unparseable regex; meta-schema safePattern rejects it
}
};
// Records the structural regex findings the meta-schema cannot see fully:
// alternation escaping the ^...$ anchors, and reachable control characters.
const checkPatternShape = (pattern, instancePath, keyword) => {
if (hasTopLevelAlternation(pattern)) {
result.errors.push({
instancePath,
schemaPath: "#/anchor",
keyword,
params: { pattern },
message: `${keyword} has a top-level | that escapes its ^...$ anchors; wrap the alternation in (?:...)`,
});
}
if (matchesControlChar(pattern)) {
result.errors.push({
instancePath,
schemaPath: "#/control",
keyword,
params: { pattern },
message: `${keyword} can match control characters (U+0000-U+001F, U+007F-U+009F other than \\t \\n \\v \\f \\r)`,
});
}
};
// ReDoS-analyzes one regex (a `pattern` value or a patternProperties key) and
// records any unsafe verdict. `label` prefixes the message so both sites read
// the same way. Returns false when the pattern is unsafe or unparseable.
const analyzePattern = (pattern, instancePath, keyword, label) => {
let reason;
try {
// The timeout bounds analysis TIME; the heap breaker above bounds
// MEMORY. No maxSteps: it would fail-close legitimate safe patterns.
// Stryker disable next-line ObjectLiteral: the timeout option bounds
// analysis TIME only; for any pattern fast enough for a test the
// safe/unsafe verdict is identical with or without it, so dropping it
// (-> {}) is an equivalent (timing-only) mutant.
// unicode: AJV compiles patterns with the u flag; without it \p{L} is
// read as a literal "p{L}" and overlapping classes look safe.
const patternResult = isSafePattern(pattern, {
timeout: redosTimeoutMs,
unicode: true,
});
if (patternResult.safe) return true;
// redos-detector always sets error (hitMaxScore) on unsafe verdicts.
reason = patternResult.error;
} catch {
reason = "parseError";
}
// hitMaxSteps only arises on a real library step limit, which cannot be
// triggered deterministically in a fast test.
// Stryker disable ConditionalExpression,StringLiteral
const tail =
reason === "parseError"
? "could not be parsed for ReDoS analysis"
: reason === "timedOut"
? `analysis timed out after ${redosTimeoutMs}ms (fail-closed as ReDoS)`
: reason === "hitMaxSteps"
? "analysis exceeded step limit (fail-closed as ReDoS)"
: "is vulnerable to ReDoS";
// Stryker restore ConditionalExpression,StringLiteral
result.errors.push({
instancePath,
schemaPath: "#/redos",
keyword,
params: { pattern, reason },
message: `${label} ${tail}`,
});
return false;
};
while (stack.length > 0) {
if (deadlinePassed()) {
timeoutBail();
return result;
}
const [current, path, currentDepth, role, parentBase] = stack.pop();
const isMap = role === "map";
// Base URI for resolving relative refs: an absolute $id (resolved against
// the enclosing base) replaces it. Inside a name map, "$id" is a property
// name, not a keyword.
let base = parentBase;
if (!isMap && typeof current.$id === "string") {
try {
base = new URL(current.$id, base).href;
} catch {
// relative $id with no absolute base, or not a URL: keep the parent's
}
}
const currentType = current.type;
const isType = (t) =>
currentType === t ||
(Array.isArray(currentType) && currentType.includes(t));
// Unsatisfiable min/max pairs. Every pair reports identically (keyword and
// schemaPath are the min side), so they are driven from one table; the
// numeric range check below is separate because exclusive bounds make it
// a four-keyword comparison rather than a straight min > max.
for (const [type, minKey, maxKey] of MIN_MAX_PAIRS) {
if (
isType(type) &&
Object.hasOwn(current, minKey) &&
Object.hasOwn(current, maxKey) &&
current[minKey] > current[maxKey]
) {
result.errors.push({
instancePath: path,
schemaPath: `#/${minKey}`,
keyword: minKey,
params: {
[minKey]: current[minKey],
[maxKey]: current[maxKey],
},
message: `${minKey} must be less than or equal to ${maxKey}`,
});
}
}
if (isType("integer") || isType("number")) {
const rangeError = checkNumericRange(current, path);
if (rangeError) result.errors.push(rangeError);
}
if (
Object.hasOwn(current, "pattern") &&
typeof current.pattern === "string"
) {
// Check the deadline BEFORE the (potentially expensive) analysis: the
// once-per-pop check above is not enough when one stack frame holds many
// patterns. Bail to the timeout path on an expired deadline. Exercised
// deterministically by an injected clock that is under-deadline at the
// once-per-pop check and over it here (see cli.crawl.test.js).
if (deadlinePassed()) {
timeoutBail();
return result;
}
checkPatternShape(current.pattern, `${path}/pattern`, "pattern");
redosPatternCount++;
// Skip analysis (of this and every later pattern) once a backstop trips:
// the total-pattern cap or the heap circuit breaker (primary memory bound).
if (
!redosBudgetExceeded(`${path}/pattern`) &&
!redosHeapExceeded(`${path}/pattern`)
) {
const safe = analyzePattern(
current.pattern,
`${path}/pattern`,
"pattern",
"pattern",
);
// A propertyNames pattern admits every key it matches. Only a pattern
// that just passed ReDoS analysis is executed against the denylist.
if (safe && role === "names") {
flagPatternMatches(
current.pattern,
`${path}/pattern`,
"propertyNames",
"propertyNames pattern",
);
}
}
}
for (const siteKey of [
"properties",
"$defs",
"definitions",
"dependentSchemas",
"dependentRequired",
"dependencies",
]) {
const site = current[siteKey];
if (typeof site === "object" && site !== null && !Array.isArray(site)) {
for (const name of Object.keys(site)) {
if (denySet.has(name)) {
flagDangerous(
`${path}/${siteKey}/${escapeJsonPointer(name)}`,
siteKey,
{ name },
`${siteKey} key "${name}" is a deserialization vector`,
);
}
}
}
}
// propertyNames is an allowlist of instance keys: an enum entry or const
// naming a deserialization vector admits it like declaring the property.
// Anything else (format, $ref, a combinator, a bare maxLength) cannot be
// checked against the denylist, so it fails closed.
if (role === "names") {
if (
!Array.isArray(current.enum) &&
!Object.hasOwn(current, "const") &&
typeof current.pattern !== "string"
) {
flagDangerous(
path,
"propertyNames",
{},
"propertyNames has no enum, const or pattern to rule out deserialization vectors",
);
}
if (Array.isArray(current.enum)) {
for (const [i, name] of current.enum.entries()) {
if (denySet.has(name)) {
flagDangerous(
`${path}/enum/${i}`,
"propertyNames",
{ name },
`propertyNames entry "${name}" is a deserialization vector`,
);
}
}
}
if (denySet.has(current.const)) {
flagDangerous(
`${path}/const`,
"propertyNames",
{ name: current.const },
`propertyNames const "${current.const}" is a deserialization vector`,
);
}
}
if (Array.isArray(current.required)) {
for (const [i, name] of current.required.entries()) {
// denySet only holds strings, so has(non-string) is already false.
if (denySet.has(name)) {
flagDangerous(
`${path}/required/${i}`,
"required",
{ name },
`required entry "${name}" is a deserialization vector`,
);
}
}
}
// `dependencies` is the pre-2019-09 spelling of dependentRequired (array of
// property names) fused with dependentSchemas (subschema). Only the array
// shape names properties, and the Array.isArray guard below skips the rest.
for (const depKey of ["dependentRequired", "dependencies"]) {
const site = current[depKey];
if (typeof site === "object" && site !== null && !Array.isArray(site)) {
for (const [trigger, deps] of Object.entries(site)) {
// A non-array deps value (malformed, or a `dependencies` subschema)
// has no .entries(); skipping it keeps adversarial input from
// throwing (pinned by a crawl test).
if (Array.isArray(deps)) {
for (const [i, name] of deps.entries()) {
// denySet only holds strings, so has(non-string) is already false.
if (denySet.has(name)) {
flagDangerous(
`${path}/${depKey}/${escapeJsonPointer(trigger)}/${i}`,
depKey,
{ name },
`${depKey} entry "${name}" is a deserialization vector`,
);
}
}
}