Skip to content

Commit

Permalink
refactor: Add internal method Utils.encodeForUrl for properly encod…
Browse files Browse the repository at this point in the history
…ing email addresses for use in URLs (#9541)
  • Loading branch information
dblythy authored Mar 6, 2025
1 parent 22e8568 commit 533a60e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
11 changes: 11 additions & 0 deletions spec/Utils.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
const Utils = require('../src/Utils');

describe('Utils', () => {
describe('encodeForUrl', () => {
it('should properly escape email with all special ASCII characters for use in URLs', async () => {
const values = [
{ input: `!\"'),.:;<>?]^}`, output: '%21%22%27%29%2C%2E%3A%3B%3C%3E%3F%5D%5E%7D' },
]
for (const value of values) {
expect(Utils.encodeForUrl(value.input)).toBe(value.output);
}
});
});

describe('addNestedKeysToRoot', () => {
it('should move the nested keys to root of object', async () => {
const obj = {
Expand Down
1 change: 0 additions & 1 deletion src/Controllers/UserController.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,6 @@ export class UserController extends AdaptableController {
user = await this.setPasswordResetToken(email);
}
const token = encodeURIComponent(user._perishable_token);

const link = buildEmailLink(this.config.requestResetPasswordURL, token, this.config);
const options = {
appName: this.config.appName,
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 encodeForUrl(input) {
return encodeURIComponent(input).replace(/[!'.()*]/g, char =>
'%' + char.charCodeAt(0).toString(16).toUpperCase()
);
}
}

module.exports = Utils;

0 comments on commit 533a60e

Please sign in to comment.