@@ -25,7 +25,6 @@ import { MatrixHandler, MatrixSimpleMessage } from "./MatrixHandler";
2525import logging from "../logging" ;
2626import * as RoomCreation from "./RoomCreation" ;
2727import { getBridgeVersion } from "matrix-appservice-bridge" ;
28- import { IdentGenerator } from "../irc/IdentGenerator" ;
2928import { Provisioner } from "../provisioning/Provisioner" ;
3029import { IrcProvisioningError } from "../provisioning/Schema" ;
3130
@@ -39,6 +38,17 @@ enum CommandPermission {
3938// This is just a length to avoid silly long usernames
4039const SANE_USERNAME_LENGTH = 64 ;
4140
41+ // Technically, anything but \0 is allowed as username (aka. authcid and authzid):
42+ // https://www.rfc-editor.org/rfc/rfc4616#section-2
43+ //
44+ // However, IRC services are very unlikely to allow the username to contain CR (0x0A)
45+ // or LF (0x0D) because they would not fit in the wire format or spaces (0x20) because
46+ // usernames are usually followed by passwords in "PRIVMSG NickServ :REGISTER" commands
47+ // and IRCv3 draft/account-registration.
48+ // Since we are at it, we might as well ban other non-printable ASCII characters
49+ // (0x00 to 0x1F, plus DEL (0x7F)), as they are most likely mistakes.
50+ const SASL_USERNAME_INVALID_CHARS_PATTERN = / [ \x00 - \x20 \x7F ] + / ; // eslint-disable-line
51+
4252interface Command {
4353 example : string ;
4454 summary : string ;
@@ -490,6 +500,7 @@ export class AdminRoomHandler {
490500 try {
491501 // Allow passwords with spaces
492502 const username = args [ 0 ] ?. trim ( ) ;
503+ const invalidChars = SASL_USERNAME_INVALID_CHARS_PATTERN . exec ( username ) ;
493504 if ( ! username ) {
494505 notice = new MatrixAction (
495506 ActionType . Notice ,
@@ -503,10 +514,11 @@ export class AdminRoomHandler {
503514 `Username is longer than the maximum permitted by the bridge (${ SANE_USERNAME_LENGTH } ).`
504515 ) ;
505516 }
506- else if ( IdentGenerator . sanitiseUsername ( username ) !== username ) {
517+ else if ( invalidChars !== null ) {
507518 notice = new MatrixAction (
508519 ActionType . Notice ,
509- `Username contained invalid characters not supported by IRC.`
520+ "Username contained invalid characters not supported by IRC " +
521+ `(${ JSON . stringify ( invalidChars . join ( "" ) ) } ).`
510522 ) ;
511523 }
512524 else {
0 commit comments