Skip to content

Commit b1ca85f

Browse files
committed
fix(evaluate): add max safe integer check for array evaluation
1 parent 6fc6a81 commit b1ca85f

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

src/evaluate/index.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import JSONPointerTypeError from '../errors/JSONPointerTypeError.js';
88
import JSONPointerIndexError from '../errors/JSONPointerIndexError.js';
99
import JSONPointerKeyError from '../errors/JSONPointerKeyError.js';
1010

11+
const MAX_SAFE_INTEGER = Number.MAX_SAFE_INTEGER.toString();
12+
1113
const evaluate = (
1214
value,
1315
jsonPointer,
@@ -103,6 +105,29 @@ const evaluate = (
103105
});
104106
}
105107

108+
if (
109+
referenceToken.length > MAX_SAFE_INTEGER.length ||
110+
(referenceToken.length === MAX_SAFE_INTEGER.length && referenceToken > MAX_SAFE_INTEGER)
111+
) {
112+
const message = `Invalid array index "${referenceToken}" at position ${referenceTokenPosition} in "${jsonPointer}": must be a non-negative integer within the I-JSON safe integer range (0 to 2^53 - 1)`;
113+
114+
tracer?.step({
115+
referenceToken,
116+
input: current,
117+
success: false,
118+
reason: message,
119+
});
120+
121+
throw new JSONPointerIndexError(message, {
122+
jsonPointer,
123+
referenceTokens,
124+
referenceToken,
125+
referenceTokenPosition,
126+
currentValue: current,
127+
realm: realm.name,
128+
});
129+
}
130+
106131
const index = Number(referenceToken);
107132
if (index >= realm.sizeOf(current) && strictArrays) {
108133
const message = `Invalid array index "${index}" at position ${referenceTokenPosition} in "${jsonPointer}": out of bounds`;
@@ -117,7 +142,7 @@ const evaluate = (
117142
throw new JSONPointerIndexError(message, {
118143
jsonPointer,
119144
referenceTokens,
120-
referenceToken: index,
145+
referenceToken,
121146
referenceTokenPosition,
122147
currentValue: current,
123148
realm: realm.name,

test/evaluate/index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,14 @@ describe('evaluate', function () {
104104
assert.throws(() => evaluate(data, '/foo/x'), JSONPointerIndexError);
105105
});
106106

107+
specify('should throw JSONPointerIndexError for unsafe integer array index', function () {
108+
assert.throws(
109+
() => evaluate(data, '/foo/9007199254740992'),
110+
JSONPointerIndexError,
111+
/I-JSON safe integer range \(0 to 2\^53 - 1\)/,
112+
);
113+
});
114+
107115
specify('should throw JSONPointerIndexError for out-of-bounds array index', function () {
108116
assert.throws(() => evaluate(data, '/foo/5'), JSONPointerIndexError);
109117
});

0 commit comments

Comments
 (0)