Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit c76cc9a

Browse files
authored
Handle M_INVALID_USERNAME on /register/available (#9237)
* Handle M_INVALID_USERNAME on /register/available * Add tests * Make typescript check happier
1 parent a215027 commit c76cc9a

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

cypress/e2e/register/register.spec.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,48 @@ describe("Registration", () => {
8585
cy.get(".mx_DevicesPanel_myDevice .mx_DevicesPanel_deviceTrust .mx_E2EIcon")
8686
.should("have.class", "mx_E2EIcon_verified");
8787
});
88+
89+
it("should require username to fulfil requirements and be available", () => {
90+
cy.get(".mx_ServerPicker_change", { timeout: 15000 }).click();
91+
cy.get(".mx_ServerPickerDialog_continue").should("be.visible");
92+
cy.get(".mx_ServerPickerDialog_otherHomeserver").type(synapse.baseUrl);
93+
cy.get(".mx_ServerPickerDialog_continue").click();
94+
// wait for the dialog to go away
95+
cy.get('.mx_ServerPickerDialog').should('not.exist');
96+
97+
cy.get("#mx_RegistrationForm_username").should("be.visible");
98+
99+
cy.intercept("**/_matrix/client/*/register/available?username=_alice", {
100+
statusCode: 400,
101+
headers: {
102+
"Content-Type": "application/json",
103+
},
104+
body: {
105+
errcode: "M_INVALID_USERNAME",
106+
error: "User ID may not begin with _",
107+
},
108+
});
109+
cy.get("#mx_RegistrationForm_username").type("_alice");
110+
cy.get(".mx_Field_tooltip")
111+
.should("have.class", "mx_Tooltip_visible")
112+
.should("contain.text", "Some characters not allowed");
113+
114+
cy.intercept("**/_matrix/client/*/register/available?username=bob", {
115+
statusCode: 400,
116+
headers: {
117+
"Content-Type": "application/json",
118+
},
119+
body: {
120+
errcode: "M_USER_IN_USE",
121+
error: "The desired username is already taken",
122+
},
123+
});
124+
cy.get("#mx_RegistrationForm_username").type("{selectAll}{backspace}bob");
125+
cy.get(".mx_Field_tooltip")
126+
.should("have.class", "mx_Tooltip_visible")
127+
.should("contain.text", "Someone already has that username");
128+
129+
cy.get("#mx_RegistrationForm_username").type("{selectAll}{backspace}foobar");
130+
cy.get(".mx_Field_tooltip").should("not.have.class", "mx_Tooltip_visible");
131+
});
88132
});

src/components/views/auth/RegistrationForm.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ limitations under the License.
1818
import React from 'react';
1919
import { MatrixClient } from 'matrix-js-sdk/src/client';
2020
import { logger } from "matrix-js-sdk/src/logger";
21+
import { MatrixError } from 'matrix-js-sdk/src/matrix';
2122

2223
import * as Email from '../../../email';
2324
import { looksValid as phoneNumberLooksValid } from '../../../phonenumber';
@@ -48,6 +49,7 @@ enum UsernameAvailableStatus {
4849
Available,
4950
Unavailable,
5051
Error,
52+
Invalid,
5153
}
5254

5355
export const PASSWORD_MIN_SCORE = 3; // safely unguessable: moderate protection from offline slow-hash scenario.
@@ -363,6 +365,9 @@ export default class RegistrationForm extends React.PureComponent<IProps, IState
363365
const available = await this.props.matrixClient.isUsernameAvailable(value);
364366
return available ? UsernameAvailableStatus.Available : UsernameAvailableStatus.Unavailable;
365367
} catch (err) {
368+
if (err instanceof MatrixError && err.errcode === "M_INVALID_USERNAME") {
369+
return UsernameAvailableStatus.Invalid;
370+
}
366371
return UsernameAvailableStatus.Error;
367372
}
368373
},
@@ -374,7 +379,8 @@ export default class RegistrationForm extends React.PureComponent<IProps, IState
374379
},
375380
{
376381
key: "safeLocalpart",
377-
test: ({ value }) => !value || SAFE_LOCALPART_REGEX.test(value),
382+
test: ({ value }, usernameAvailable) => (!value || SAFE_LOCALPART_REGEX.test(value))
383+
&& usernameAvailable !== UsernameAvailableStatus.Invalid,
378384
invalid: () => _t("Some characters not allowed"),
379385
},
380386
{

0 commit comments

Comments
 (0)