Skip to content

improve performance on error path in JSON.parse #90

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions benchmarks/throw.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const internals = {
const suite = new Benchmark.Suite()

suite
.add('JSON.parse', () => {
.add('JSON.parse valid', () => {
JSON.parse(internals.text)
})
.add('JSON.parse error', () => {
Expand All @@ -21,15 +21,15 @@ suite
})
.add('secure-json-parse parse', () => {
try {
sjson.parse(internals.text)
sjson.parse(internals.invalid)
} catch (ignoreErr) { }
})
.add('secure-json-parse safeParse', () => {
sjson.safeParse(internals.text)
sjson.safeParse(internals.invalid)
})
.add('reviver', () => {
try {
JSON.parse(internals.text, internals.reviver)
JSON.parse(internals.invalid, internals.reviver)
} catch (ignoreErr) { }
})
.on('cycle', (event) => {
Expand Down
20 changes: 17 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const hasBuffer = typeof Buffer !== 'undefined'
const suspectProtoRx = /"(?:_|\\u005[Ff])(?:_|\\u005[Ff])(?:p|\\u0070)(?:r|\\u0072)(?:o|\\u006[Ff])(?:t|\\u0074)(?:o|\\u006[Ff])(?:_|\\u005[Ff])(?:_|\\u005[Ff])"\s*:/
const suspectConstructorRx = /"(?:c|\\u0063)(?:o|\\u006[Ff])(?:n|\\u006[Ee])(?:s|\\u0073)(?:t|\\u0074)(?:r|\\u0072)(?:u|\\u0075)(?:c|\\u0063)(?:t|\\u0074)(?:o|\\u006[Ff])(?:r|\\u0072)"\s*:/

function parse (text, reviver, options) {
function _parse (text, reviver, options) {
// Normalize arguments
if (options == null) {
if (reviver !== null && typeof reviver === 'object') {
Expand Down Expand Up @@ -97,11 +97,25 @@ function filter (obj, { protoAction = 'error', constructorAction = 'error', safe
return obj
}

function parse (text, reviver, options) {
const stackTraceLimit = Error.stackTraceLimit
Error.stackTraceLimit = 0
try {
return _parse(text, reviver, options)
} finally {
Error.stackTraceLimit = stackTraceLimit
}
}

function safeParse (text, reviver) {
const stackTraceLimit = Error.stackTraceLimit
Error.stackTraceLimit = 0
try {
return parse(text, reviver, { safe: true })
} catch (ignoreError) {
return _parse(text, reviver, { safe: true })
} catch (_e) {
return null
} finally {
Error.stackTraceLimit = stackTraceLimit
}
}

Expand Down
16 changes: 16 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,14 @@ test('parse', t => {
t.end()
})

t.test('should reset stackTraceLimit', t => {
const text = '{ "a": 5, "b": 6, "__proto__": { "x": 7 }, "c": { "d": 0, "e": "text", "__proto__": { "y": 8 }, "f": { "g": 2 } } }'
Error.stackTraceLimit = 42
t.throws(() => j.parse(text))
t.same(Error.stackTraceLimit, 42)
t.end()
})

t.end()
})

Expand Down Expand Up @@ -372,6 +380,14 @@ test('safeParse', t => {
t.end()
})

t.test('should reset stackTraceLimit', t => {
const text = '{ "a": 5, "b": 6, "__proto__": { "x": 7 }, "c": { "d": 0, "e": "text", "__proto__": { "y": 8 }, "f": { "g": 2 } } }'
Error.stackTraceLimit = 42
t.same(j.safeParse(text), null)
t.same(Error.stackTraceLimit, 42)
t.end()
})

t.test('sanitizes nested object string', t => {
const text = '{ "a": 5, "b": 6, "__proto__": { "x": 7 }, "c": { "d": 0, "e": "text", "__proto__": { "y": 8 }, "f": { "g": 2 } } }'

Expand Down