Skip to content

Commit

Permalink
fix: usernames should encode all characters in emails
Browse files Browse the repository at this point in the history
  • Loading branch information
dblythy committed Jan 14, 2025
1 parent 28b3ede commit 1f68ff7
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
33 changes: 33 additions & 0 deletions spec/EmailVerificationToken.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,39 @@ describe('Email Verification Token Expiration: ', () => {
});
});

it('should send an HTML or properly escaped plain text password reset email', async () => {
const user = new Parse.User();
let sendEmailOptions;
const emailAdapter = {
sendPasswordResetEmail: (options) => {
sendEmailOptions = options;
},
sendVerificationEmail: async () => {},
sendMail: async () => {},
};

Check failure on line 65 in spec/EmailVerificationToken.spec.js

View workflow job for this annotation

GitHub Actions / Lint

Trailing spaces not allowed
await reconfigureServer({
appName: 'specialCharacterUsernameTest',
publicServerURL: 'http://localhost:8378/1',
emailAdapter: emailAdapter,
});

Check failure on line 71 in spec/EmailVerificationToken.spec.js

View workflow job for this annotation

GitHub Actions / Lint

Trailing spaces not allowed
user.setUsername('hello :)');
user.setPassword('password123');
user.set('email', '[email protected]');
await user.signUp();

Check failure on line 76 in spec/EmailVerificationToken.spec.js

View workflow job for this annotation

GitHub Actions / Lint

Trailing spaces not allowed
await Parse.User.requestPasswordReset('[email protected]');

Check failure on line 78 in spec/EmailVerificationToken.spec.js

View workflow job for this annotation

GitHub Actions / Lint

Trailing spaces not allowed
expect(sendEmailOptions).not.toBeUndefined();

Check failure on line 80 in spec/EmailVerificationToken.spec.js

View workflow job for this annotation

GitHub Actions / Lint

Trailing spaces not allowed
const { link, html, text } = sendEmailOptions;

Check failure on line 81 in spec/EmailVerificationToken.spec.js

View workflow job for this annotation

GitHub Actions / Lint

'html' is assigned a value but never used

Check failure on line 81 in spec/EmailVerificationToken.spec.js

View workflow job for this annotation

GitHub Actions / Lint

'text' is assigned a value but never used

Check failure on line 82 in spec/EmailVerificationToken.spec.js

View workflow job for this annotation

GitHub Actions / Lint

Trailing spaces not allowed
const username = link.split('username=')[1];
expect(username).toBe('%68%65%6C%6C%6F%20%3A%29');
});

Check failure on line 86 in spec/EmailVerificationToken.spec.js

View workflow job for this annotation

GitHub Actions / Lint

Trailing spaces not allowed

it('emailVerified should set to false, if the user does not verify their email before the email verify token expires', done => {
const user = new Parse.User();
let sendEmailOptions;
Expand Down
5 changes: 3 additions & 2 deletions src/Controllers/UserController.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import rest from '../rest';
import Parse from 'parse/node';
import AccountLockout from '../AccountLockout';
import Config from '../Config';
import Utils from '../Utils';

var RestQuery = require('../RestQuery');
var Auth = require('../Auth');
Expand Down Expand Up @@ -173,7 +174,7 @@ export class UserController extends AdaptableController {
if (!shouldSendEmail) {
return;
}
const username = encodeURIComponent(fetchedUser.username);
const username = Utils.encode(fetchedUser.username);

const link = buildEmailLink(this.config.verifyEmailURL, username, token, this.config);
const options = {
Expand Down Expand Up @@ -286,7 +287,7 @@ export class UserController extends AdaptableController {
user = await this.setPasswordResetToken(email);
}
const token = encodeURIComponent(user._perishable_token);
const username = encodeURIComponent(user.username);
const username = Utils.encode(user.username);

const link = buildEmailLink(this.config.requestResetPasswordURL, username, token, this.config);
const options = {
Expand Down
11 changes: 11 additions & 0 deletions src/Utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,17 @@ class Utils {
}
return obj;
}

/**
* Encodes a string to be used in a URL.
* @param {String} input The string to encode.
* @returns {String} The encoded string.
*/
static encode(input) {
return Array.from(input)
.map(char => `%${char.charCodeAt(0).toString(16).padStart(2, '0').toUpperCase()}`)

Check failure on line 410 in src/Utils.js

View workflow job for this annotation

GitHub Actions / Lint

Expected indentation of 6 spaces but found 8
.join('');
}
}

module.exports = Utils;

0 comments on commit 1f68ff7

Please sign in to comment.