From 8f9eb42d283b55bf7689c664cf9980a771b1c002 Mon Sep 17 00:00:00 2001 From: Will Hunt Date: Fri, 28 Jun 2019 00:24:47 +0100 Subject: [PATCH 1/4] Catch [] in escape code --- lib/models/users/matrix.js | 5 +++-- spec/unit/matrix-user.spec.js | 4 +++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/models/users/matrix.js b/lib/models/users/matrix.js index 3bc6664b..97e0fa8a 100644 --- a/lib/models/users/matrix.js +++ b/lib/models/users/matrix.js @@ -80,8 +80,9 @@ MatrixUser.prototype.serialize = function() { * Grammar taken from: https://matrix.org/docs/spec/appendices.html#identifier-grammar */ MatrixUser.prototype.escapeUserId = function() { - // Currently Matrix accepts / in the userId, although going forward it will be removed. - const badChars = new Set(this.localpart.replace(/([A-z0-9]|-|\.|=|_)+/g, "")); + // NOTE: Currently Matrix accepts / in the userId, although going forward it will be removed. + // NOTE: We also allow uppercase for the time being. + const badChars = new Set(this.localpart.replace(/([A-Z]|[a-z]|[0-9]|-|\.|=|_)+/g, "")); let res = this.localpart; badChars.forEach((c) => { const hex = c.charCodeAt(0).toString(16).toLowerCase(); diff --git a/spec/unit/matrix-user.spec.js b/spec/unit/matrix-user.spec.js index 24e29a16..813ba534 100644 --- a/spec/unit/matrix-user.spec.js +++ b/spec/unit/matrix-user.spec.js @@ -21,13 +21,15 @@ describe("MatrixUser", function() { "@woah=2a=2a=2a:localhost", "@=d83d:localhost", "@matrix.org=2fspec:localhost", + "@=5bdoggo=5d:localhost" ]; [ new MatrixUser("@$:localhost", null, false), new MatrixUser("@500$ dog:localhost", null, false), new MatrixUser("@woah***:localhost", null, false), new MatrixUser("@🐶:localhost", null, false), - new MatrixUser("@matrix.org/spec:localhost", null, false) + new MatrixUser("@matrix.org/spec:localhost", null, false), + new MatrixUser("@[doggo]:localhost", null, false) ].forEach((user, i) => { user.escapeUserId(); expect(user.getId()).toBe(expected[i]); From 9dd51ccbb77938d91db56458307256a66c27dd50 Mon Sep 17 00:00:00 2001 From: Will Hunt Date: Fri, 28 Jun 2019 00:25:00 +0100 Subject: [PATCH 2/4] Add global to enable/disable all caching --- lib/models/users/matrix.js | 12 +++++++++--- spec/unit/matrix-user.spec.js | 8 ++++++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/lib/models/users/matrix.js b/lib/models/users/matrix.js index 97e0fa8a..e073b846 100644 --- a/lib/models/users/matrix.js +++ b/lib/models/users/matrix.js @@ -5,9 +5,10 @@ * @constructor * @param {string} userId The user_id of the user. * @param {Object=} data Serialized data values - * @param {boolean} escape [true] Escape the user's localpart. + * @param {boolean} escape [true] Escape the user's localpart. Modify {@link MatrixUser~ESCAPE_DEFAULT} + * to change the default value. */ -function MatrixUser(userId, data, escape=true) { +function MatrixUser(userId, data, escape=MatrixUser.ESCAPE_DEFAULT) { if (!userId) { throw new Error("Missing user_id"); } @@ -58,7 +59,7 @@ MatrixUser.prototype.set = function(key, val) { this._data[key] = val; }; -/** +/**u * Get the data value for the given key. * @param {string} key An arbitrary bridge-specific key. * @return {*} Stored data for this key. May be undefined. @@ -75,6 +76,7 @@ MatrixUser.prototype.serialize = function() { this._data.localpart = this.localpart; return this._data; }; + /** * Make a userId conform to the matrix spec using QP escaping. * Grammar taken from: https://matrix.org/docs/spec/appendices.html#identifier-grammar @@ -94,5 +96,9 @@ MatrixUser.prototype.escapeUserId = function() { this.localpart = res; this.userId = `@${this.localpart}:${this.host}`; }; + +MatrixUser.ESCAPE_DEFAULT = true; + + /** The MatrixUser class */ module.exports = MatrixUser; diff --git a/spec/unit/matrix-user.spec.js b/spec/unit/matrix-user.spec.js index 813ba534..5471ae5e 100644 --- a/spec/unit/matrix-user.spec.js +++ b/spec/unit/matrix-user.spec.js @@ -35,5 +35,13 @@ describe("MatrixUser", function() { expect(user.getId()).toBe(expected[i]); }) }); + it("should not escape if ESCAPE_DEFAULT is false", function() { + MatrixUser.ESCAPE_DEFAULT = false; + expect(new MatrixUser("@$:localhost", null).getId()).toBe("@$:localhost"); + }); + it("should escape if ESCAPE_DEFAULT is true", function() { + MatrixUser.ESCAPE_DEFAULT = true; + expect(new MatrixUser("@$:localhost", null).getId()).toBe("@=24:localhost"); + }); }); }); From cab087b26447dc66ad7b93bbfc348470d41e5e86 Mon Sep 17 00:00:00 2001 From: Will Hunt Date: Fri, 28 Jun 2019 00:26:14 +0100 Subject: [PATCH 3/4] Add docstring for static var --- lib/models/users/matrix.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/models/users/matrix.js b/lib/models/users/matrix.js index e073b846..cb74c980 100644 --- a/lib/models/users/matrix.js +++ b/lib/models/users/matrix.js @@ -97,6 +97,10 @@ MatrixUser.prototype.escapeUserId = function() { this.userId = `@${this.localpart}:${this.host}`; }; +/** + * @static + * This is a global variable to modify the default escaping behaviour of MatrixUser. + */ MatrixUser.ESCAPE_DEFAULT = true; From 77364010710db348dbb9fde0ae7fcf0b4b5a9f3b Mon Sep 17 00:00:00 2001 From: Will Hunt Date: Fri, 28 Jun 2019 07:33:23 +0100 Subject: [PATCH 4/4] u --- lib/models/users/matrix.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/models/users/matrix.js b/lib/models/users/matrix.js index cb74c980..017f230a 100644 --- a/lib/models/users/matrix.js +++ b/lib/models/users/matrix.js @@ -59,7 +59,7 @@ MatrixUser.prototype.set = function(key, val) { this._data[key] = val; }; -/**u +/** * Get the data value for the given key. * @param {string} key An arbitrary bridge-specific key. * @return {*} Stored data for this key. May be undefined.