@apidevtools/json-schema-ref-parser 11.1.0 → 11.2.0

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/README.md CHANGED
@@ -1,7 +1,5 @@
1
1
  # JSON Schema $Ref Parser
2
2
 
3
- _**This package needs [a new maintainer](https://github.com/APIDevTools/json-schema-ref-parser/issues/285) or at least some contributors. For more information [please read this article](https://phil.tech/2022/bundling-openapi-with-javascript/). As of v10.0.0 I am no longer spending any time working on this tool, so I can focus on scaling up my [reforestation charity](https://protect.earth/) instead of burning myself out trying to maintain a whole load of OSS projects I don't use in my vanishingly small spare time. Get in touch if you'd like to take over.** - [Phil Sturgeon](https://github.com/philsturgeon)_
4
-
5
3
  #### Parse, Resolve, and Dereference JSON Schema $ref pointers
6
4
 
7
5
  [![Build Status](https://github.com/APIDevTools/json-schema-ref-parser/workflows/CI-CD/badge.svg?branch=master)](https://github.com/APIDevTools/json-schema-ref-parser/actions)
@@ -136,17 +136,17 @@ function inventory$Ref($refParent, $refKey, path, pathFromRoot, indirections, in
136
136
  }
137
137
  }
138
138
  inventory.push({
139
- $ref,
140
- parent: $refParent,
141
- key: $refKey,
142
- pathFromRoot,
143
- depth,
144
- file,
145
- hash,
146
- value: pointer.value,
147
- circular: pointer.circular,
148
- extended,
149
- external,
139
+ $ref, // The JSON Reference (e.g. {$ref: string})
140
+ parent: $refParent, // The object that contains this $ref pointer
141
+ key: $refKey, // The key in `parent` that is the $ref pointer
142
+ pathFromRoot, // The path to the $ref pointer, from the JSON Schema root
143
+ depth, // How far from the JSON Schema root is this $ref pointer?
144
+ file, // The file that the $ref pointer resolves to
145
+ hash, // The hash within `file` that the $ref pointer resolves to
146
+ value: pointer.value, // The resolved value of the $ref pointer
147
+ circular: pointer.circular, // Is this $ref pointer DIRECTLY circular? (i.e. it references itself)
148
+ extended, // Does this $ref extend its resolved value? (i.e. it has extra properties, in addition to "$ref")
149
+ external, // Does this $ref pointer point to a file other than the main JSON Schema file?
150
150
  indirections, // The number of indirect references that were traversed to resolve the value
151
151
  });
152
152
  // Recursively crawl the resolved value
@@ -89,7 +89,7 @@ function crawl(obj, path, pathFromRoot, parents, processedObjects, dereferencedC
89
89
  if (obj[key] !== dereferenced.value) {
90
90
  obj[key] = dereferenced.value;
91
91
  if (options.dereference.onDereference) {
92
- options.dereference.onDereference(value.$ref, obj[key]);
92
+ options.dereference.onDereference(value.$ref, obj[key], obj, key);
93
93
  }
94
94
  }
95
95
  }
@@ -65,10 +65,12 @@ interface $RefParserOptions {
65
65
  /**
66
66
  * Callback invoked during dereferencing.
67
67
  *
68
- * @argument {string} path The path being dereferenced (ie. the `$ref` string).
69
- * @argument {JSONSchemaObject} object The JSON-Schema that the `$ref` resolved to.
68
+ * @argument {string} path - The path being dereferenced (ie. the `$ref` string)
69
+ * @argument {JSONSchemaObject} value - The JSON-Schema that the `$ref` resolved to
70
+ * @argument {JSONSchemaObject} parent - The parent of the dereferenced object
71
+ * @argument {string} parentPropName - The prop name of the parent object whose value was dereferenced
70
72
  */
71
- onDereference?(path: string, value: JSONSchemaObject): void;
73
+ onDereference?(path: string, value: JSONSchemaObject, parent?: JSONSchemaObject, parentPropName?: string): void;
72
74
  /**
73
75
  * Whether a reference should resolve relative to its directory/path, or from the cwd
74
76
  *
@@ -91,7 +91,8 @@ exports.getNewOptions = getNewOptions;
91
91
  */
92
92
  function merge(target, source) {
93
93
  if (isMergeable(source)) {
94
- const keys = Object.keys(source);
94
+ // prevent prototype pollution
95
+ const keys = Object.keys(source).filter((key) => !["__proto__", "constructor", "prototype"].includes(key));
95
96
  for (let i = 0; i < keys.length; i++) {
96
97
  const key = keys[i];
97
98
  const sourceSetting = source[key];
@@ -21,7 +21,7 @@ exports.default = {
21
21
  * Parsers that don't match will be skipped, UNLESS none of the parsers match, in which case
22
22
  * every parser will be tried.
23
23
  */
24
- canParse: [".yaml", ".yml", ".json"],
24
+ canParse: [".yaml", ".yml", ".json"], // JSON is valid YAML
25
25
  /**
26
26
  * Parses the given file as YAML
27
27
  *
@@ -68,11 +68,11 @@ class Pointer {
68
68
  // Crawl the object, one token at a time
69
69
  this.value = unwrapOrThrow(obj);
70
70
  for (let i = 0; i < tokens.length; i++) {
71
- if (resolveIf$Ref(this, options)) {
71
+ if (resolveIf$Ref(this, options, pathFromRoot)) {
72
72
  // The $ref path has changed, so append the remaining tokens to the path
73
73
  this.path = Pointer.join(this.path, tokens.slice(i));
74
74
  }
75
- if (typeof this.value === "object" && this.value !== null && "$ref" in this.value) {
75
+ if (typeof this.value === "object" && this.value !== null && !isRootPath(pathFromRoot) && "$ref" in this.value) {
76
76
  return this;
77
77
  }
78
78
  const token = tokens[i];
@@ -86,7 +86,7 @@ class Pointer {
86
86
  }
87
87
  // Resolve the final value
88
88
  if (!this.value || (this.value.$ref && url.resolve(this.path, this.value.$ref) !== pathFromRoot)) {
89
- resolveIf$Ref(this, options);
89
+ resolveIf$Ref(this, options, pathFromRoot);
90
90
  }
91
91
  return this;
92
92
  }
@@ -190,13 +190,14 @@ class Pointer {
190
190
  *
191
191
  * @param pointer
192
192
  * @param options
193
+ * @param [pathFromRoot] - the path of place that initiated resolving
193
194
  * @returns - Returns `true` if the resolution path changed
194
195
  */
195
- function resolveIf$Ref(pointer, options) {
196
+ function resolveIf$Ref(pointer, options, pathFromRoot) {
196
197
  // Is the value a JSON reference? (and allowed?)
197
198
  if (ref_js_1.default.isAllowed$Ref(pointer.value, options)) {
198
199
  const $refPath = url.resolve(pointer.path, pointer.value.$ref);
199
- if ($refPath === pointer.path) {
200
+ if ($refPath === pointer.path && !isRootPath(pathFromRoot)) {
200
201
  // The value is a reference to itself, so there's nothing to do.
201
202
  pointer.circular = true;
202
203
  }
@@ -254,3 +255,6 @@ function unwrapOrThrow(value) {
254
255
  }
255
256
  return value;
256
257
  }
258
+ function isRootPath(pathFromRoot) {
259
+ return typeof pathFromRoot == "string" && Pointer.parse(pathFromRoot).length == 0;
260
+ }
@@ -44,7 +44,7 @@ exports.default = {
44
44
  /**
45
45
  * HTTP request timeout (in milliseconds).
46
46
  */
47
- timeout: 5000,
47
+ timeout: 5000, // 5 seconds
48
48
  /**
49
49
  * The maximum number of HTTP redirects to follow.
50
50
  * To disable automatic following of redirects, set this to zero.
@@ -9,6 +9,6 @@ function convertPathToPosix(filePath) {
9
9
  if (isExtendedLengthPath) {
10
10
  return filePath;
11
11
  }
12
- return filePath.split(path_1.default.win32.sep).join(path_1.default.posix.sep);
12
+ return filePath.split(path_1.default?.win32?.sep).join(path_1.default.posix.sep);
13
13
  }
14
14
  exports.default = convertPathToPosix;
@@ -107,7 +107,7 @@ function crawl(
107
107
  if (obj[key] !== dereferenced.value) {
108
108
  obj[key] = dereferenced.value;
109
109
  if (options.dereference.onDereference) {
110
- options.dereference.onDereference(value.$ref, obj[key]);
110
+ options.dereference.onDereference(value.$ref, obj[key], obj, key);
111
111
  }
112
112
  }
113
113
  } else {
package/lib/options.ts CHANGED
@@ -79,10 +79,12 @@ interface $RefParserOptions {
79
79
  /**
80
80
  * Callback invoked during dereferencing.
81
81
  *
82
- * @argument {string} path The path being dereferenced (ie. the `$ref` string).
83
- * @argument {JSONSchemaObject} object The JSON-Schema that the `$ref` resolved to.
82
+ * @argument {string} path - The path being dereferenced (ie. the `$ref` string)
83
+ * @argument {JSONSchemaObject} value - The JSON-Schema that the `$ref` resolved to
84
+ * @argument {JSONSchemaObject} parent - The parent of the dereferenced object
85
+ * @argument {string} parentPropName - The prop name of the parent object whose value was dereferenced
84
86
  */
85
- onDereference?(path: string, value: JSONSchemaObject): void;
87
+ onDereference?(path: string, value: JSONSchemaObject, parent?: JSONSchemaObject, parentPropName?: string): void;
86
88
 
87
89
  /**
88
90
  * Whether a reference should resolve relative to its directory/path, or from the cwd
@@ -180,7 +182,8 @@ export type ParserOptions = DeepPartial<$RefParserOptions>;
180
182
  */
181
183
  function merge(target: any, source: any) {
182
184
  if (isMergeable(source)) {
183
- const keys = Object.keys(source);
185
+ // prevent prototype pollution
186
+ const keys = Object.keys(source).filter((key) => !["__proto__", "constructor", "prototype"].includes(key));
184
187
  for (let i = 0; i < keys.length; i++) {
185
188
  const key = keys[i];
186
189
  const sourceSetting = source[key];
package/lib/pointer.ts CHANGED
@@ -83,12 +83,12 @@ class Pointer {
83
83
  this.value = unwrapOrThrow(obj);
84
84
 
85
85
  for (let i = 0; i < tokens.length; i++) {
86
- if (resolveIf$Ref(this, options)) {
86
+ if (resolveIf$Ref(this, options, pathFromRoot)) {
87
87
  // The $ref path has changed, so append the remaining tokens to the path
88
88
  this.path = Pointer.join(this.path, tokens.slice(i));
89
89
  }
90
90
 
91
- if (typeof this.value === "object" && this.value !== null && "$ref" in this.value) {
91
+ if (typeof this.value === "object" && this.value !== null && !isRootPath(pathFromRoot) && "$ref" in this.value) {
92
92
  return this;
93
93
  }
94
94
 
@@ -103,7 +103,7 @@ class Pointer {
103
103
 
104
104
  // Resolve the final value
105
105
  if (!this.value || (this.value.$ref && url.resolve(this.path, this.value.$ref) !== pathFromRoot)) {
106
- resolveIf$Ref(this, options);
106
+ resolveIf$Ref(this, options, pathFromRoot);
107
107
  }
108
108
 
109
109
  return this;
@@ -224,15 +224,16 @@ class Pointer {
224
224
  *
225
225
  * @param pointer
226
226
  * @param options
227
+ * @param [pathFromRoot] - the path of place that initiated resolving
227
228
  * @returns - Returns `true` if the resolution path changed
228
229
  */
229
- function resolveIf$Ref(pointer: any, options: any) {
230
+ function resolveIf$Ref(pointer: any, options: any, pathFromRoot?: any) {
230
231
  // Is the value a JSON reference? (and allowed?)
231
232
 
232
233
  if ($Ref.isAllowed$Ref(pointer.value, options)) {
233
234
  const $refPath = url.resolve(pointer.path, pointer.value.$ref);
234
235
 
235
- if ($refPath === pointer.path) {
236
+ if ($refPath === pointer.path && !isRootPath(pathFromRoot)) {
236
237
  // The value is a reference to itself, so there's nothing to do.
237
238
  pointer.circular = true;
238
239
  } else {
@@ -294,3 +295,7 @@ function unwrapOrThrow(value: any) {
294
295
 
295
296
  return value;
296
297
  }
298
+
299
+ function isRootPath(pathFromRoot: any): boolean {
300
+ return typeof pathFromRoot == "string" && Pointer.parse(pathFromRoot).length == 0;
301
+ }
@@ -7,5 +7,5 @@ export default function convertPathToPosix(filePath: string) {
7
7
  return filePath;
8
8
  }
9
9
 
10
- return filePath.split(path.win32.sep).join(path.posix.sep);
10
+ return filePath.split(path?.win32?.sep).join(path.posix.sep);
11
11
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@apidevtools/json-schema-ref-parser",
3
- "version": "11.1.0",
3
+ "version": "11.2.0",
4
4
  "description": "Parse, Resolve, and Dereference JSON Schema $ref pointers",
5
5
  "keywords": [
6
6
  "json",
@@ -57,7 +57,7 @@
57
57
  "scripts": {
58
58
  "prepublishOnly": "yarn build",
59
59
  "lint": "eslint lib",
60
- "build": "rm -fr dist/* && tsc",
60
+ "build": "rimraf dist && tsc",
61
61
  "typecheck": "tsc --noEmit",
62
62
  "prettier": "prettier --write \"**/*.+(js|jsx|ts|tsx|har||json|css|md)\"",
63
63
  "test": "vitest --coverage",
@@ -67,33 +67,34 @@
67
67
  "test:watch": "vitest -w"
68
68
  },
69
69
  "devDependencies": {
70
- "@types/eslint": "8.44.2",
71
- "@types/js-yaml": "^4.0.6",
72
- "@types/node": "^20.6.2",
73
- "@typescript-eslint/eslint-plugin": "^6.7.2",
74
- "@typescript-eslint/eslint-plugin-tslint": "^6.7.2",
75
- "@typescript-eslint/parser": "^6.7.2",
76
- "@vitest/coverage-v8": "^0.34.4",
70
+ "@types/eslint": "8.56.5",
71
+ "@types/js-yaml": "^4.0.9",
72
+ "@types/node": "^20.11.24",
73
+ "@typescript-eslint/eslint-plugin": "^7.1.1",
74
+ "@typescript-eslint/eslint-plugin-tslint": "^7.0.2",
75
+ "@typescript-eslint/parser": "^7.1.1",
76
+ "@vitest/coverage-v8": "^1.3.1",
77
77
  "abortcontroller-polyfill": "^1.7.5",
78
78
  "cross-env": "^7.0.3",
79
- "eslint": "^8.49.0",
80
- "eslint-config-prettier": "^9.0.0",
79
+ "eslint": "^8.57.0",
80
+ "eslint-config-prettier": "^9.1.0",
81
81
  "eslint-config-standard": "^17.1.0",
82
- "eslint-plugin-import": "^2.28.1",
83
- "eslint-plugin-prettier": "^5.0.0",
82
+ "eslint-plugin-import": "^2.29.1",
83
+ "eslint-plugin-prettier": "^5.1.3",
84
84
  "eslint-plugin-promise": "^6.1.1",
85
- "eslint-plugin-unused-imports": "^3.0.0",
86
- "jsdom": "^22.1.0",
87
- "lint-staged": "^14.0.1",
85
+ "eslint-plugin-unused-imports": "^3.1.0",
86
+ "jsdom": "^24.0.0",
87
+ "lint-staged": "^15.2.2",
88
88
  "node-fetch": "^3.3.2",
89
- "prettier": "^3.0.3",
90
- "typescript": "^5.2.2",
91
- "vitest": "^0.34.4"
89
+ "prettier": "^3.2.5",
90
+ "rimraf": "^5.0.5",
91
+ "typescript": "^5.3.3",
92
+ "vitest": "^1.3.1"
92
93
  },
93
94
  "dependencies": {
94
95
  "@jsdevtools/ono": "^7.1.3",
95
- "@types/json-schema": "^7.0.13",
96
- "@types/lodash.clonedeep": "^4.5.7",
96
+ "@types/json-schema": "^7.0.15",
97
+ "@types/lodash.clonedeep": "^4.5.9",
97
98
  "js-yaml": "^4.1.0",
98
99
  "lodash.clonedeep": "^4.5.0"
99
100
  },
@@ -107,5 +108,6 @@
107
108
  "@semantic-release/npm",
108
109
  "@semantic-release/github"
109
110
  ]
110
- }
111
+ },
112
+ "packageManager": "yarn@4.1.1"
111
113
  }