actual username validation lol

This commit is contained in:
Elijah R 2024-07-12 21:20:37 -04:00
parent 276ea9208a
commit e90c8cd36c
2 changed files with 21 additions and 2 deletions

View file

@ -102,7 +102,18 @@ export class Client extends EventEmitter {
this.socket.close(); this.socket.close();
return; return;
} }
let username = htmlentities.encode(joinMsg.data.username); let username = joinMsg.data.username.trim();
if (!validateUsername(username)) {
let msg: MSAgentErrorMessage = {
op: MSAgentProtocolMessageType.Error,
data: {
error: "Usernames can contain only numbers, letters, spaces, dashes, underscores, and dots, and it must be between 3 and 20 characters."
}
};
await this.send(msg);
this.socket.close();
return;
}
if (this.room.config.bannedWords.some(w => username.indexOf(w) !== -1)) { if (this.room.config.bannedWords.some(w => username.indexOf(w) !== -1)) {
this.socket.close(); this.socket.close();
return; return;
@ -215,3 +226,11 @@ export class Client extends EventEmitter {
} }
} }
} }
function validateUsername(username: string) {
return (
username.length >= 3 &&
username.length <= 20 &&
/^[a-zA-Z0-9\ \-\_\.]+$/.test(username)
);
}

View file

@ -1,4 +1,4 @@
export const Config = { export const Config = {
// The server address for the webapp to connect to. The below default is the same address the webapp is hosted at. // The server address for the webapp to connect to. The below default is the same address the webapp is hosted at.
serverAddress: `${window.location.protocol}//${window.location.host}` serverAddress: `http://127.0.0.1:3000`
} }