Skip to content

Commit

Permalink
Fix strict json error message on Node.js 19+
Browse files Browse the repository at this point in the history
closes #475
  • Loading branch information
dougwilson committed Feb 21, 2023
1 parent 0385872 commit 368a93a
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 8 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ jobs:
- Node.js 16.x
- Node.js 17.x
- Node.js 18.x
- Node.js 19.x

include:
- name: Node.js 0.8
Expand Down Expand Up @@ -113,6 +114,9 @@ jobs:
- name: Node.js 18.x
node-version: "18.14"

- name: Node.js 19.x
node-version: "19.7"

steps:
- uses: actions/checkout@v3

Expand Down
1 change: 1 addition & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
unreleased
==========

* Fix strict json error message on Node.js 19+
* deps: content-type@~1.0.5
- perf: skip value escaping when unnecessary
* deps: raw-body@2.5.2
Expand Down
19 changes: 15 additions & 4 deletions lib/types/json.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ module.exports = json

var FIRST_CHAR_REGEXP = /^[\x20\x09\x0a\x0d]*([^\x20\x09\x0a\x0d])/ // eslint-disable-line no-control-regex

var JSON_SYNTAX_CHAR = '#'
var JSON_SYNTAX_REGEXP = /#+/g

/**
* Create a middleware to parse JSON bodies.
*
Expand Down Expand Up @@ -152,15 +155,23 @@ function json (options) {

function createStrictSyntaxError (str, char) {
var index = str.indexOf(char)
var partial = index !== -1
? str.substring(0, index) + '#'
: ''
var partial = ''

if (index !== -1) {
partial = str.substring(0, index) + JSON_SYNTAX_CHAR

for (var i = index + 1; i < str.length; i++) {
partial += JSON_SYNTAX_CHAR
}
}

try {
JSON.parse(partial); /* istanbul ignore next */ throw new SyntaxError('strict violation')
} catch (e) {
return normalizeJsonSyntaxError(e, {
message: e.message.replace('#', char),
message: e.message.replace(JSON_SYNTAX_REGEXP, function (placeholder) {
return str.substring(index, index + placeholder.length)
}),
stack: e.stack
})
}
Expand Down
8 changes: 4 additions & 4 deletions test/json.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ describe('bodyParser.json()', function () {
.post('/')
.set('Content-Type', 'application/json')
.send('true')
.expect(400, '[entity.parse.failed] ' + parseError('#rue').replace('#', 't'), done)
.expect(400, '[entity.parse.failed] ' + parseError('#rue').replace(/#/g, 't'), done)
})
})

Expand Down Expand Up @@ -273,15 +273,15 @@ describe('bodyParser.json()', function () {
.post('/')
.set('Content-Type', 'application/json')
.send('true')
.expect(400, '[entity.parse.failed] ' + parseError('#rue').replace('#', 't'), done)
.expect(400, '[entity.parse.failed] ' + parseError('#rue').replace(/#/g, 't'), done)
})

it('should not parse primitives with leading whitespaces', function (done) {
request(this.server)
.post('/')
.set('Content-Type', 'application/json')
.send(' true')
.expect(400, '[entity.parse.failed] ' + parseError(' #rue').replace('#', 't'), done)
.expect(400, '[entity.parse.failed] ' + parseError(' #rue').replace(/#/g, 't'), done)
})

it('should allow leading whitespaces in JSON', function (done) {
Expand All @@ -299,7 +299,7 @@ describe('bodyParser.json()', function () {
.set('X-Error-Property', 'stack')
.send('true')
.expect(400)
.expect(shouldContainInBody(parseError('#rue').replace('#', 't')))
.expect(shouldContainInBody(parseError('#rue').replace(/#/g, 't')))
.end(done)
})
})
Expand Down

0 comments on commit 368a93a

Please sign in to comment.