Skip to content

Commit

Permalink
Merge pull request #1496 from MGDIS/master
Browse files Browse the repository at this point in the history
Parameters encoded to base 64 should be decoded as UTF-8, not ASCII.
  • Loading branch information
simov committed Mar 21, 2015
2 parents 5d4c83d + 6a80bb5 commit cd375d1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/helpers.js
Expand Up @@ -74,7 +74,7 @@ function isReadStream (rs) {
}

function toBase64 (str) {
return (new Buffer(str || '', 'ascii')).toString('base64')
return (new Buffer(str || '', 'utf8')).toString('base64')
}

exports.isFunction = isFunction
Expand Down
27 changes: 25 additions & 2 deletions tests/test-basic-auth.js
Expand Up @@ -22,6 +22,8 @@ tape('setup', function(t) {
ok = true
} else if ( req.headers.authorization === 'Basic ' + new Buffer(':pass').toString('base64')) {
ok = true
} else if ( req.headers.authorization === 'Basic ' + new Buffer('user:pâss').toString('base64')) {
ok = true
} else {
// Bad auth header, don't send back WWW-Authenticate header
ok = false
Expand Down Expand Up @@ -155,14 +157,35 @@ tape('pass - undefined', function(t) {
})
})


tape('pass - utf8', function(t) {
t.doesNotThrow( function() {
var r = request({
'method': 'GET',
'uri': 'http://localhost:6767/allow_undefined_password/',
'auth': {
'user': 'user',
'pass': 'pâss',
'sendImmediately': false
}
}, function(error, res, body ) {
t.equal(r._auth.user, 'user')
t.equal(r._auth.pass, 'pâss')
t.equal(res.statusCode, 200)
t.equal(numBasicRequests, 12)
t.end()
})
})
})

tape('auth method', function(t) {
var r = request
.get('http://localhost:6767/test/')
.auth('user', '', false)
.on('response', function (res) {
t.equal(r._auth.user, 'user')
t.equal(res.statusCode, 200)
t.equal(numBasicRequests, 12)
t.equal(numBasicRequests, 14)
t.end()
})
})
Expand All @@ -179,7 +202,7 @@ tape('get method', function(t) {
t.equal(r._auth.user, 'user')
t.equal(err, null)
t.equal(res.statusCode, 200)
t.equal(numBasicRequests, 14)
t.equal(numBasicRequests, 16)
t.end()
})
})
Expand Down

0 comments on commit cd375d1

Please sign in to comment.