Skip to content

Commit 533a60e

Browse files
authored
refactor: Add internal method Utils.encodeForUrl for properly encoding email addresses for use in URLs (#9541)
1 parent 22e8568 commit 533a60e

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

spec/Utils.spec.js

+11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
const Utils = require('../src/Utils');
22

33
describe('Utils', () => {
4+
describe('encodeForUrl', () => {
5+
it('should properly escape email with all special ASCII characters for use in URLs', async () => {
6+
const values = [
7+
{ input: `!\"'),.:;<>?]^}`, output: '%21%22%27%29%2C%2E%3A%3B%3C%3E%3F%5D%5E%7D' },
8+
]
9+
for (const value of values) {
10+
expect(Utils.encodeForUrl(value.input)).toBe(value.output);
11+
}
12+
});
13+
});
14+
415
describe('addNestedKeysToRoot', () => {
516
it('should move the nested keys to root of object', async () => {
617
const obj = {

src/Controllers/UserController.js

-1
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,6 @@ export class UserController extends AdaptableController {
282282
user = await this.setPasswordResetToken(email);
283283
}
284284
const token = encodeURIComponent(user._perishable_token);
285-
286285
const link = buildEmailLink(this.config.requestResetPasswordURL, token, this.config);
287286
const options = {
288287
appName: this.config.appName,

src/Utils.js

+11
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,17 @@ class Utils {
399399
}
400400
return obj;
401401
}
402+
403+
/**
404+
* Encodes a string to be used in a URL.
405+
* @param {String} input The string to encode.
406+
* @returns {String} The encoded string.
407+
*/
408+
static encodeForUrl(input) {
409+
return encodeURIComponent(input).replace(/[!'.()*]/g, char =>
410+
'%' + char.charCodeAt(0).toString(16).toUpperCase()
411+
);
412+
}
402413
}
403414

404415
module.exports = Utils;

0 commit comments

Comments
 (0)