@@ -38,17 +38,17 @@ var resolveToken = require( './../../../json-pointer/base/resolve-token' );
3838*
3939* - Implements resolution per RFC 6901.
4040* - An empty string pointer refers to the root of the document.
41+ * - If a pointer references a nonexistent location in the document, the function returns `undefined`.
4142* - The `-` token on the last segment of a pointer targeting an array resolves to the array's length (i.e., past-the-end index).
4243* - Tokens `__proto__`, `constructor`, and `prototype` are rejected to prevent prototype pollution.
4344*
4445* @param {(Object|Array) } document - JSON document
4546* @param {string } pointer - JSON Pointer string
46- * @throws {TypeError } first argument must be an object or array
47+ * @throws {TypeError } first argument must be an object or an array
4748* @throws {TypeError } second argument must be a string
48- * @throws {Error } pointer must resolve to an existing location in the document
49+ * @throws {Error } pointer must be an empty string or begin with `/`
4950* @throws {Error } tokens must not be prototype pollution vectors
50- * @throws {RangeError } array index must be within bounds
51- * @returns {Object } result object
51+ * @returns {(Object|void) } result object or undefined
5252*
5353* @example
5454* var out = resolve( { 'foo': [ 'bar', 'baz' ] }, '/foo/0' );
@@ -61,6 +61,10 @@ var resolveToken = require( './../../../json-pointer/base/resolve-token' );
6161* @example
6262* var out = resolve( { 'foo': 'bar' }, '' );
6363* // returns { 'target': null, 'key': null, 'value': { 'foo': 'bar' } }
64+ *
65+ * @example
66+ * var out = resolve( { 'foo': 'bar' }, '/missing' );
67+ * // returns undefined
6468*/
6569function resolve ( document , pointer ) {
6670 var current ;
@@ -76,6 +80,13 @@ function resolve( document, pointer ) {
7680 }
7781 tokens = parse ( pointer ) ;
7882
83+ // Validate all tokens upfront for security to guarantee deterministic rejection:
84+ for ( i = 0 ; i < tokens . length ; i ++ ) {
85+ if ( ! isSafeToken ( tokens [ i ] ) ) {
86+ throw new Error ( format ( 'invalid argument. Pointer contains an unsafe token. Value: `%s`.' , tokens [ i ] ) ) ;
87+ }
88+ }
89+
7990 // An empty pointer refers to the root:
8091 if ( tokens . length === 0 ) {
8192 return {
@@ -91,42 +102,42 @@ function resolve( document, pointer ) {
91102 // Traverse all tokens except the last to find the target:
92103 for ( i = 0 ; i < last ; i ++ ) {
93104 token = tokens [ i ] ;
94- if ( ! isSafeToken ( token ) ) {
95- throw new Error ( format ( 'invalid argument. Pointer contains an unsafe token. Value: `%s`.' , token ) ) ;
96- }
97105 if ( isArray ( current ) ) {
98106 idx = resolveToken ( current , token ) ;
99107 if ( idx >= current . length ) {
100- throw new RangeError ( format ( 'invalid argument. Array index out of bounds. Index: `%d`. Length: `%d`.' , idx , current . length ) ) ;
108+ // Array index is out-of-bounds...
109+ return ;
101110 }
102111 current = current [ idx ] ;
103112 } else if ( isPlainObject ( current ) ) {
104113 if ( ! hasOwnProp ( current , token ) ) {
105- throw new Error ( format ( 'invalid argument. Pointer references a nonexistent property. Value: `%s`.' , token ) ) ;
114+ // Pointer references a nonexistent property...
115+ return ;
106116 }
107117 current = current [ token ] ;
108118 } else {
109- throw new TypeError ( format ( 'invalid argument. Pointer references a non-traversable value. Value: `%s`.' , token ) ) ;
119+ // Pointer references a non-traversable value...
120+ return ;
110121 }
111122 }
112123
113124 // Handle the last token to determine the final key and value:
114125 token = tokens [ last ] ;
115- if ( ! isSafeToken ( token ) ) {
116- throw new Error ( format ( 'invalid argument. Pointer contains an unsafe token. Value: `%s`.' , token ) ) ;
117- }
118126 if ( isArray ( current ) ) {
119127 key = resolveToken ( current , token ) ;
120128 if ( token !== '-' && key >= current . length ) {
121- throw new RangeError ( format ( 'invalid argument. Array index out of bounds. Index: `%d`. Length: `%d`.' , key , current . length ) ) ;
129+ // Array index is out-of-bounds...
130+ return ;
122131 }
123132 } else if ( isPlainObject ( current ) ) {
124133 if ( ! hasOwnProp ( current , token ) ) {
125- throw new Error ( format ( 'invalid argument. Pointer references a nonexistent property. Value: `%s`.' , token ) ) ;
134+ // Pointer references a nonexistent property...
135+ return ;
126136 }
127137 key = token ;
128138 } else {
129- throw new TypeError ( format ( 'invalid argument. Pointer references a non-traversable value. Value: `%s`.' , token ) ) ;
139+ // Pointer references a non-traversable value...
140+ return ;
130141 }
131142 return {
132143 'target' : current ,
0 commit comments