Handle M_INVALID_USERNAME on /register/available (#9237)

* Handle M_INVALID_USERNAME on /register/available

* Add tests

* Make typescript check happier
This commit is contained in:
Michael Telatynski 2022-09-07 22:47:20 +01:00 committed by GitHub
parent a215027c6b
commit c76cc9aee1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 51 additions and 1 deletions

View file

@ -18,6 +18,7 @@ limitations under the License.
import React from 'react';
import { MatrixClient } from 'matrix-js-sdk/src/client';
import { logger } from "matrix-js-sdk/src/logger";
import { MatrixError } from 'matrix-js-sdk/src/matrix';
import * as Email from '../../../email';
import { looksValid as phoneNumberLooksValid } from '../../../phonenumber';
@ -48,6 +49,7 @@ enum UsernameAvailableStatus {
Available,
Unavailable,
Error,
Invalid,
}
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
const available = await this.props.matrixClient.isUsernameAvailable(value);
return available ? UsernameAvailableStatus.Available : UsernameAvailableStatus.Unavailable;
} catch (err) {
if (err instanceof MatrixError && err.errcode === "M_INVALID_USERNAME") {
return UsernameAvailableStatus.Invalid;
}
return UsernameAvailableStatus.Error;
}
},
@ -374,7 +379,8 @@ export default class RegistrationForm extends React.PureComponent<IProps, IState
},
{
key: "safeLocalpart",
test: ({ value }) => !value || SAFE_LOCALPART_REGEX.test(value),
test: ({ value }, usernameAvailable) => (!value || SAFE_LOCALPART_REGEX.test(value))
&& usernameAvailable !== UsernameAvailableStatus.Invalid,
invalid: () => _t("Some characters not allowed"),
},
{