@scenarist/core 0.4.10 → 0.4.13

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.
@@ -1,4 +1,4 @@
1
- import type { SerializedRegex } from "../schemas/match-criteria.js";
1
+ import { type SerializedRegex } from "../schemas/match-criteria.js";
2
2
  /**
3
3
  * Match a value against a regex pattern.
4
4
  *
@@ -17,4 +17,17 @@ import type { SerializedRegex } from "../schemas/match-criteria.js";
17
17
  * @returns true if pattern matches, false otherwise
18
18
  */
19
19
  export declare const matchesRegex: (value: string, pattern: SerializedRegex) => boolean;
20
+ /**
21
+ * Match a value against a native RegExp from trusted developer code.
22
+ *
23
+ * **Security:**
24
+ * - Bypasses serialized regex validation intentionally for native RegExp scenario criteria
25
+ * - Do not use with patterns derived from serialized data or user input
26
+ * - Reconstructs from source and flags before testing to avoid mutating caller-owned lastIndex state
27
+ *
28
+ * @param value - String to test against pattern
29
+ * @param pattern - Native RegExp from trusted code
30
+ * @returns true if pattern matches, false otherwise
31
+ */
32
+ export declare const matchesTrustedNativeRegex: (value: string, pattern: RegExp) => boolean;
20
33
  //# sourceMappingURL=regex-matching.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"regex-matching.d.ts","sourceRoot":"","sources":["../../src/domain/regex-matching.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,8BAA8B,CAAC;AAEpE;;;;;;;;;;;;;;;;GAgBG;AACH,eAAO,MAAM,YAAY,GACvB,OAAO,MAAM,EACb,SAAS,eAAe,KACvB,OAQF,CAAC"}
1
+ {"version":3,"file":"regex-matching.d.ts","sourceRoot":"","sources":["../../src/domain/regex-matching.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,eAAe,EACrB,MAAM,8BAA8B,CAAC;AAiBtC;;;;;;;;;;;;;;;;GAgBG;AACH,eAAO,MAAM,YAAY,GACvB,OAAO,MAAM,EACb,SAAS,eAAe,KACvB,OAQF,CAAC;AAEF;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,yBAAyB,GACpC,OAAO,MAAM,EACb,SAAS,MAAM,KACd,OAKF,CAAC"}
@@ -1,3 +1,14 @@
1
+ import { SerializedRegexSchema, } from "../schemas/match-criteria.js";
2
+ const testRegex = (value, pattern) => {
3
+ try {
4
+ // eslint-disable-next-line security/detect-non-literal-regexp -- Serialized patterns are schema-validated and native RegExp inputs are trusted code.
5
+ const regex = new RegExp(pattern.source, pattern.flags); // nosemgrep: javascript.lang.security.audit.detect-non-literal-regexp.detect-non-literal-regexp
6
+ return regex.test(value);
7
+ }
8
+ catch {
9
+ return false;
10
+ }
11
+ };
1
12
  /**
2
13
  * Match a value against a regex pattern.
3
14
  *
@@ -16,12 +27,27 @@
16
27
  * @returns true if pattern matches, false otherwise
17
28
  */
18
29
  export const matchesRegex = (value, pattern) => {
19
- try {
20
- // eslint-disable-next-line security/detect-non-literal-regexp -- Pattern validated at trust boundary (schema uses redos-detector)
21
- const regex = new RegExp(pattern.source, pattern.flags);
22
- return regex.test(value);
23
- }
24
- catch {
30
+ const validationResult = SerializedRegexSchema.safeParse(pattern);
31
+ if (!validationResult.success) {
25
32
  return false;
26
33
  }
34
+ return testRegex(value, validationResult.data);
35
+ };
36
+ /**
37
+ * Match a value against a native RegExp from trusted developer code.
38
+ *
39
+ * **Security:**
40
+ * - Bypasses serialized regex validation intentionally for native RegExp scenario criteria
41
+ * - Do not use with patterns derived from serialized data or user input
42
+ * - Reconstructs from source and flags before testing to avoid mutating caller-owned lastIndex state
43
+ *
44
+ * @param value - String to test against pattern
45
+ * @param pattern - Native RegExp from trusted code
46
+ * @returns true if pattern matches, false otherwise
47
+ */
48
+ export const matchesTrustedNativeRegex = (value, pattern) => {
49
+ return testRegex(value, {
50
+ source: pattern.source,
51
+ flags: pattern.flags,
52
+ });
27
53
  };
@@ -1,7 +1,7 @@
1
1
  import { ScenaristError, ErrorCodes } from "../types/errors.js";
2
2
  import { extractFromPath } from "./path-extraction.js";
3
3
  import { applyTemplates } from "./template-replacement.js";
4
- import { matchesRegex } from "./regex-matching.js";
4
+ import { matchesRegex, matchesTrustedNativeRegex } from "./regex-matching.js";
5
5
  import { createStateResponseResolver } from "./state-response-resolver.js";
6
6
  import { deepEquals } from "./deep-equals.js";
7
7
  import { isRecord } from "./type-guards.js";
@@ -469,10 +469,7 @@ const matchesValue = (requestValue, criteriaValue) => {
469
469
  }
470
470
  // Native RegExp support (ADR-0016)
471
471
  if (criteriaValue instanceof RegExp) {
472
- return matchesRegex(requestValue, {
473
- source: criteriaValue.source,
474
- flags: criteriaValue.flags,
475
- });
472
+ return matchesTrustedNativeRegex(requestValue, criteriaValue);
476
473
  }
477
474
  if (typeof criteriaValue === "number" || typeof criteriaValue === "boolean") {
478
475
  return requestValue === String(criteriaValue);
@@ -84,18 +84,14 @@ const resolveTemplatePath = (templateData, prefix, path) => {
84
84
  if (!isRecord(current)) {
85
85
  return undefined;
86
86
  }
87
- // Security: Prevent prototype pollution attacks
88
- // This is a READ-only traversal, not a write operation, so prototype pollution is not possible.
89
- // Additionally, we explicitly block dangerous keys (__proto__, constructor, prototype) via isDangerousKey()
90
- // and verify the property exists on the object itself (not prototype) via Object.hasOwn().
91
- // @see https://github.com/citypaul/scenarist/security/code-scanning/165
92
- if (isDangerousKey(segment) || !Object.hasOwn(current, segment)) {
87
+ if (isDangerousKey(segment)) {
93
88
  return undefined;
94
89
  }
95
- // nosemgrep: javascript.lang.security.audit.prototype-pollution.prototype-pollution-loop.prototype-pollution-loop
96
- // eslint-disable-next-line security/detect-object-injection -- Read-only traversal with isDangerousKey and Object.hasOwn guards
97
- current = current[segment];
98
- // Guard: Return undefined if property doesn't exist
90
+ const descriptor = Object.getOwnPropertyDescriptor(current, segment);
91
+ if (descriptor === undefined) {
92
+ return undefined;
93
+ }
94
+ current = descriptor.value;
99
95
  if (current === undefined) {
100
96
  return undefined;
101
97
  }
@@ -1 +1 @@
1
- {"version":3,"file":"match-criteria.d.ts","sourceRoot":"","sources":["../../src/schemas/match-criteria.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AA2CxB;;;;;;;;;;;;;;;;;;;GAmBG;AACH,eAAO,MAAM,qBAAqB;;;iBAchC,CAAC;AAEH,MAAM,MAAM,eAAe,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,qBAAqB,CAAC,CAAC"}
1
+ {"version":3,"file":"match-criteria.d.ts","sourceRoot":"","sources":["../../src/schemas/match-criteria.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAgDxB;;;;;;;;;;;;;;;;;;;GAmBG;AACH,eAAO,MAAM,qBAAqB;;;iBAchC,CAAC;AAEH,MAAM,MAAM,eAAe,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,qBAAqB,CAAC,CAAC"}
@@ -29,13 +29,19 @@ const VALID_REGEX_FLAGS = /^[gimsuvy]*$/;
29
29
  *
30
30
  * Uses redos-detector to analyze the pattern for exponential backtracking.
31
31
  * Patterns that could cause catastrophic backtracking are rejected.
32
+ * redos-detector errors are treated as unsafe.
32
33
  *
33
34
  * @param pattern - The regex pattern to validate
34
35
  * @returns true if the pattern is safe, false otherwise
35
36
  */
36
37
  const isPatternSafeFromReDoS = (pattern) => {
37
- const result = isSafePattern(pattern);
38
- return result.safe;
38
+ try {
39
+ const result = isSafePattern(pattern);
40
+ return result.safe;
41
+ }
42
+ catch {
43
+ return false;
44
+ }
39
45
  };
40
46
  /**
41
47
  * Schema for serialized regex patterns.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@scenarist/core",
3
- "version": "0.4.10",
3
+ "version": "0.4.13",
4
4
  "description": "Internal: Hexagonal architecture core for scenario-based testing with MSW",
5
5
  "author": "Paul Hammond (citypaul) <paul@packsoftware.co.uk>",
6
6
  "license": "MIT",
@@ -50,12 +50,12 @@
50
50
  "zod": "^4.3.6"
51
51
  },
52
52
  "devDependencies": {
53
- "@vitest/coverage-v8": "^4.0.18",
54
- "@vitest/ui": "^4.0.18",
55
- "eslint": "^9.39.3",
53
+ "@vitest/coverage-v8": "^4.1.5",
54
+ "@vitest/ui": "^4.1.5",
55
+ "eslint": "^9.39.4",
56
56
  "fast-check": "^4.5.3",
57
57
  "typescript": "^5.9.3",
58
- "vitest": "^4.0.18",
58
+ "vitest": "^4.1.5",
59
59
  "@scenarist/eslint-config": "0.0.0",
60
60
  "@scenarist/typescript-config": "0.0.0"
61
61
  },