Skip to content

Commit

Permalink
Fix empty parameter issue with utf8Sentinel in simple mode
Browse files Browse the repository at this point in the history
  • Loading branch information
papandreou committed Jul 31, 2018
1 parent dab286a commit e3fca5d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/types/urlencoded.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,13 @@ function createBodyDecoder (queryparse, charset, utf8Sentinel, interpretNumericE
return function bodyDecoder (body) {
var modifiedBody = body
if (utf8Sentinel) {
modifiedBody = modifiedBody.replace(/(?:^|&)utf8=([^&]+)/, function ($0, value) {
modifiedBody = modifiedBody.replace(/(^|&)utf8=([^&]+)($|&)/, function ($0, ampBefore, value, ampAfter) {
if (charsetBySentinel[value]) {
correctedCharset = charsetBySentinel[value]
}
return ''
// Make sure that we only leave an ampersand when replacing in the middle of the query string
// as the simple parser will add an empty string parameter if it gets &&
return ampBefore && ampAfter ? '&' : ''
})
}
return modifiedBody.length
Expand Down
29 changes: 29 additions & 0 deletions test/urlencoded.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,35 @@ describe('bodyParser.urlencoded()', function () {
.expect(200, '{"user":"ø"}', done)
})

describe('in simple mode', function () {
it('should not leave an empty string parameter when removing the utf8 sentinel from the start of the string', function (done) {
var server = createServer({ utf8Sentinel: true, extended: false })
request(server)
.post('/')
.set('Content-Type', 'application/x-www-form-urlencoded')
.send('utf8=%E2%9C%93&foo=bar')
.expect(200, '{"foo":"bar"}', done)
})

it('should not leave an empty string parameter when removing the utf8 sentinel from the middle of the string', function (done) {
var server = createServer({ utf8Sentinel: true, extended: false })
request(server)
.post('/')
.set('Content-Type', 'application/x-www-form-urlencoded')
.send('foo=bar&utf8=%E2%9C%93&baz=quux')
.expect(200, '{"foo":"bar","baz":"quux"}', done)
})

it('should not leave an empty string parameter when removing the utf8 sentinel from the end of the string', function (done) {
var server = createServer({ utf8Sentinel: true, extended: false })
request(server)
.post('/')
.set('Content-Type', 'application/x-www-form-urlencoded')
.send('foo=bar&baz=quux&utf8=%E2%9C%93')
.expect(200, '{"foo":"bar","baz":"quux"}', done)
})
})

it('should handle empty message-body', function (done) {
request(createServer({ limit: '1kb' }))
.post('/')
Expand Down

0 comments on commit e3fca5d

Please sign in to comment.