mirror of
https://github.com/kirameki-cafe/Yumi.git
synced 2026-09-13 10:49:20 +00:00
Added safety user input checks
This commit is contained in:
+41
-1
@@ -7,7 +7,7 @@ import Prisma from "../providers/Prisma";
|
|||||||
import osuAPI from "../providers/osuAPI";
|
import osuAPI from "../providers/osuAPI";
|
||||||
import { countryCodeEmoji, emojiCountryCode } from "country-code-emoji";
|
import { countryCodeEmoji, emojiCountryCode } from "country-code-emoji";
|
||||||
import countryLookup from "country-code-lookup";
|
import countryLookup from "country-code-lookup";
|
||||||
import { Beatmap } from "node-osu";
|
import validator from 'validator';
|
||||||
|
|
||||||
const EMBEDS = {
|
const EMBEDS = {
|
||||||
osu_INFO:(data: Message | Interaction) => {
|
osu_INFO:(data: Message | Interaction) => {
|
||||||
@@ -39,6 +39,18 @@ const EMBEDS = {
|
|||||||
user: (data instanceof Interaction) ? data.user : data.author
|
user: (data instanceof Interaction) ? data.user : data.author
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
INVALID_USER_MENTIONED: (data: Message | Interaction) => {
|
||||||
|
return makeErrorEmbed ({
|
||||||
|
title: `Not a valid osu username or id`,
|
||||||
|
user: (data instanceof Interaction) ? data.user : data.author
|
||||||
|
});
|
||||||
|
},
|
||||||
|
INVALID_BEATMAP_ID_MENTIONED: (data: Message | Interaction) => {
|
||||||
|
return makeErrorEmbed ({
|
||||||
|
title: `Not a valid osu beatmap id`,
|
||||||
|
user: (data instanceof Interaction) ? data.user : data.author
|
||||||
|
});
|
||||||
|
},
|
||||||
NO_BEATMAP_FOUND: (data: Message | Interaction) => {
|
NO_BEATMAP_FOUND: (data: Message | Interaction) => {
|
||||||
return makeErrorEmbed ({
|
return makeErrorEmbed ({
|
||||||
title: `That beatmap doesn't exists on osu!`,
|
title: `That beatmap doesn't exists on osu!`,
|
||||||
@@ -95,6 +107,10 @@ export default class osu {
|
|||||||
else if(data instanceof CommandInteraction)
|
else if(data instanceof CommandInteraction)
|
||||||
user = data.options.getString('user');
|
user = data.options.getString('user');
|
||||||
|
|
||||||
|
|
||||||
|
if(!validator.isNumeric(user) && !this.validate_osu_username(user))
|
||||||
|
return await sendMessageOrInteractionResponse(data, { embeds: [EMBEDS.INVALID_USER_MENTIONED(data)] });
|
||||||
|
|
||||||
let result = await osuAPI.client.getUser({ u: user });
|
let result = await osuAPI.client.getUser({ u: user });
|
||||||
|
|
||||||
if(result instanceof Array && result.length === 0)
|
if(result instanceof Array && result.length === 0)
|
||||||
@@ -219,6 +235,10 @@ export default class osu {
|
|||||||
else if(data instanceof CommandInteraction)
|
else if(data instanceof CommandInteraction)
|
||||||
beatmap = data.options.getString('beatmap');
|
beatmap = data.options.getString('beatmap');
|
||||||
|
|
||||||
|
|
||||||
|
if(!validator.isNumeric(beatmap))
|
||||||
|
return await sendMessageOrInteractionResponse(data, { embeds: [EMBEDS.INVALID_BEATMAP_ID_MENTIONED(data)] });
|
||||||
|
|
||||||
let result = await osuAPI.client.getBeatmaps({ b: beatmap });
|
let result = await osuAPI.client.getBeatmaps({ b: beatmap });
|
||||||
|
|
||||||
if(result instanceof Array && result.length === 0)
|
if(result instanceof Array && result.length === 0)
|
||||||
@@ -405,4 +425,24 @@ export default class osu {
|
|||||||
return x;
|
return x;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// Criteria from https://github.com/ppy/osu-web/blob/9de00a0b874c56893d98261d558d78d76259d81b/app/Libraries/UsernameValidation.php
|
||||||
|
private validate_osu_username(username: string) {
|
||||||
|
|
||||||
|
//username_no_spaces
|
||||||
|
if (username.startsWith(' ') || username.endsWith(' ')) return false;
|
||||||
|
|
||||||
|
//username_too_short
|
||||||
|
if (username.length < 3) return false;
|
||||||
|
|
||||||
|
//username_too_long
|
||||||
|
if (username.length > 15) return false;
|
||||||
|
|
||||||
|
//username_invalid_characters
|
||||||
|
if (username.includes(' ') || !(/^[A-Za-z0-9-\[\]_ ]+$/.test(username))) return false;
|
||||||
|
|
||||||
|
//username_no_space_userscore_mix
|
||||||
|
if (username.includes('_') && username.includes(' ')) return false;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user