Skip to content
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

Allow colons in passwords for req.auth #1462

Merged
merged 1 commit into from Jan 9, 2013
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/request.js
Expand Up @@ -393,8 +393,9 @@ req.__defineGetter__('auth', function(){
auth = parts[1];

// credentials
auth = new Buffer(auth, 'base64').toString().split(':');
return { username: auth[0], password: auth[1] };
auth = new Buffer(auth, 'base64').toString().match(/^([^:]*):(.*)$/);
if (!auth) return;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

might be more elegant to just do auth.shift() and auth.join(':') there

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

meh whatever it's all good haha

return { username: auth[1], password: auth[2] };
});

/**
Expand Down
30 changes: 30 additions & 0 deletions test/req.auth.js
Expand Up @@ -48,6 +48,36 @@ describe('req', function(){
})
})

describe('when encoded string is malformed', function(){
it('should return undefined', function(done){
var app = express();

app.get('/', function(req, res){
res.send(req.auth || 'none');
});

request(app)
.get('/')
.set('Authorization', 'Basic Z21ldGh2aW4=')
.expect('none', done)
})
})

describe('when password contains a colon', function(){
it('should return .username and .password', function(done){
var app = express();

app.get('/', function(req, res){
res.send(req.auth || 'none');
});

request(app)
.get('/')
.set('Authorization', 'Basic dG9iaTpmZXJyZXQ6ZmVycmV0')
.expect('{"username":"tobi","password":"ferret:ferret"}', done)
})
})

it('should return .username and .password', function(done){
var app = express();

Expand Down