Skip to content

Commit

Permalink
fixes #46 Sanitize mattermost username to match openai regex.
Browse files Browse the repository at this point in the history
  • Loading branch information
haegelsperger committed Jul 20, 2023
1 parent c6203eb commit dab85a8
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "chatgpt-mattermost-bot",
"version": "2.0.0",
"version": "2.0.1",
"private": true,
"scripts": {
"start": "ts-node ./src/botservice.ts",
Expand Down
11 changes: 10 additions & 1 deletion src/botservice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ if(!global.FormData) {
}

const name = process.env['MATTERMOST_BOTNAME'] || '@chatgpt'
const contextMsgCount = Number(process.env['BOT_CONTEXT_MSG'] ?? 7)
const contextMsgCount = Number(process.env['BOT_CONTEXT_MSG'] ?? 100)

/* List of all registered plugins */
const plugins: PluginBase<any>[] = [
Expand Down Expand Up @@ -185,6 +185,15 @@ async function userIdToName(userId: string): Promise<string> {
} else {
// username not in cache our outdated
username = (await mmClient.getUser(userId)).username

if(!/^[a-zA-Z0-9_-]{1,64}$/.test(username)) {
username = username.replace(/[.@!?]/g,'_').slice(0, 64)
}

if(!/^[a-zA-Z0-9_-]{1,64}$/.test(username)) {
username = [...username.matchAll(/[a-zA-Z0-9_-]/g)].join('').slice(0, 64)
}

usernameCache[userId] = {
username: username,
expireTime: Date.now() + 1000 * 60 * 5
Expand Down

0 comments on commit dab85a8

Please sign in to comment.