Fix double-slash in confirmation URL

Fix the code building the URL used in the email-verification email
to prevent double-slash in the URL when e.g. restApiRoot is '/'.

Before:

  http://example.com//users/confirm?...

Now:

  http://example.com/users/confirm?...
This commit is contained in:
Miroslav Bajtoš 2016-09-06 13:55:54 +02:00
parent 7d1f31cfb4
commit 3df5b2814c
2 changed files with 52 additions and 3 deletions

View File

@ -401,14 +401,18 @@ module.exports = function(User) {
(options.protocol === 'https' && options.port == '443') (options.protocol === 'https' && options.port == '443')
) ? '' : ':' + options.port; ) ? '' : ':' + options.port;
var urlPath = joinUrlPath(
options.restApiRoot,
userModel.http.path,
userModel.sharedClass.find('confirm', true).http.path
);
options.verifyHref = options.verifyHref || options.verifyHref = options.verifyHref ||
options.protocol + options.protocol +
'://' + '://' +
options.host + options.host +
displayPort + displayPort +
options.restApiRoot + urlPath +
userModel.http.path +
userModel.sharedClass.find('confirm', true).http.path +
'?uid=' + '?uid=' +
options.user.id + options.user.id +
'&redirect=' + '&redirect=' +
@ -767,3 +771,13 @@ function emailValidator(err, done) {
if (!isEmail(value)) if (!isEmail(value))
return err('email'); return err('email');
} }
function joinUrlPath(args) {
var result = arguments[0];
for (var ix = 1; ix < arguments.length; ix++) {
var next = arguments[ix];
result += result[result.length - 1] === '/' && next[0] === '/' ?
next.slice(1) : next;
}
return result;
}

View File

@ -1551,6 +1551,41 @@ describe('User', function() {
done(); done();
}); });
it('should squash "//" when restApiRoot is "/"', function(done) {
var emailBody;
User.afterRemote('create', function(ctx, user, next) {
assert(user, 'afterRemote should include result');
var options = {
type: 'email',
to: user.email,
from: 'noreply@myapp.org',
redirect: '/',
host: 'myapp.org',
port: 3000,
restApiRoot: '/',
};
user.verify(options, function(err, result) {
if (err) return next(err);
emailBody = result.email.response.toString('utf-8');
next();
});
});
request(app)
.post('/test-users')
.expect('Content-Type', /json/)
.expect(200)
.send({email: 'user@example.com', password: 'pass'})
.end(function(err, res) {
if (err) return done(err);
expect(emailBody)
.to.contain('http://myapp.org:3000/test-users/confirm');
done();
});
});
}); });
describe('User.confirm(options, fn)', function() { describe('User.confirm(options, fn)', function() {