Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix automod url regex and logic #25

Merged
merged 5 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 14 additions & 19 deletions a.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,31 @@
const urlRegex =
/https?:\/\/(www\.)?([-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b)([-a-zA-Z0-9()@:%_\+.~#?&//=]*)/;
/https?:\/\/(www\.)?([-a-zA-Z0-9]{1,63}\.)*([-a-zA-Z0-9]{1,63}\.[a-zA-Z]{2,6})([-a-zA-Z0-9()@:%_\+.~#?&\/\/=]*)/;

const allowedDomains: string[] = [
'hypixel.net',
'*.hypixel.net',
'discord.com',
'*.discord.com',
'kath.lol',
'*.kathund.wtf',
'kathund.wtf',
'kath.lol',
'hypixel-api-reborn.github.io'
];

function matchWildcard(domain: string, pattern: string): boolean {
const regexPattern = pattern.replace(/[-\/\\^$+?.()|[\]{}]/g, '\\$&').replace(/\*/g, '.*');
const regex = new RegExp(`^${regexPattern}$`);
return regex.test(domain);
}

function isUrlAllowed(url: string): boolean {
const isValidUrl = urlRegex.test(url);
if (!isValidUrl) {
return false;
}

if (!isValidUrl) return false;
const match = url.match(urlRegex);
const domain = match ? match[2] : null;
if (!domain) {
if (!match) return false;
const domain: string = match[3];

if (allowedDomains.some((pattern) => pattern === domain) && !match[2]) {
return true;
} else if (!allowedDomains.some((pattern) => pattern === domain)) {
return (
allowedDomains.some((pattern) => pattern === `*.${domain}`) ||
allowedDomains.some((pattern) => pattern === match[2] + domain)
);
} else {
return false;
}

return allowedDomains.some((pattern) => matchWildcard(domain, pattern));
}

const testUrls = [
Expand Down
29 changes: 15 additions & 14 deletions src/events/messageCreate.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,33 @@
import { ChannelType, GuildMemberRoleManager, Message, TextChannel, Webhook, WebhookType } from 'discord.js';
import { HypixelAPIKeyRegex, IPAddressPattern, URLRegex, DiscordInviteRegex } from '../utils/regex';
import { autoModBypassRole } from '../../config.json';
import Infraction from '../utils/Infraction';
import { DiscordInviteRegex, HypixelAPIKeyRegex, IPAddressPattern, URLRegex } from '../utils/regex';

const allowedDomains: string[] = [
'hypixel.net',
'*.hypixel.net',
'discord.com',
'*.discord.com',
'kath.lol',
'*.kathund.wtf',
'kathund.wtf',
'kath.lol',
'hypixel-api-reborn.github.io'
];

function matchWildcard(domain: string, pattern: string): boolean {
const regexPattern = pattern.replace(/[-\/\\^$+?.()|[\]{}]/g, '\\$&').replace(/\*/g, '.*');
const regex = new RegExp(`^${regexPattern}$`);
return regex.test(domain);
}

function isUrlAllowed(url: string): boolean {
const isValidUrl = URLRegex.test(url);
if (!isValidUrl) return false;
const match = url.match(URLRegex);
const domain = match ? match[2] : null;
if (!domain) return false;
return !allowedDomains.some((pattern) => matchWildcard(domain, pattern));
if (!match) return false;
const domain: string = match[3];

if (allowedDomains.some((pattern) => pattern === domain) && !match[2]) {
return true;
} else if (!allowedDomains.some((pattern) => pattern === domain)) {
return (
allowedDomains.some((pattern) => pattern === `*.${domain}`) ||
allowedDomains.some((pattern) => pattern === match[2] + domain)
);
} else {

Check warning on line 28 in src/events/messageCreate.ts

View workflow job for this annotation

GitHub Actions / check linting (eslint)

Unnecessary 'else' after 'return'
return false;
}
}

async function getWebhook(
Expand Down
2 changes: 1 addition & 1 deletion src/utils/regex.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export const HypixelAPIKeyRegex = /[a-zA-Z0-9]{8}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{12}/g;
export const IPAddressPattern = /((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.|$)){4}/g;
export const URLRegex =
/https?:\/\/(www\.)?([-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b)([-a-zA-Z0-9()@:%_\+.~#?&//=]*)/;
/https?:\/\/(www\.)?([-a-zA-Z0-9]{1,63}\.)*([-a-zA-Z0-9]{1,63}\.[a-zA-Z]{2,6})([-a-zA-Z0-9()@:%_\+.~#?&\/\/=]*)/;
export const DiscordInviteRegex = /(?:https:\/\/)?discord\.gg\/[a-zA-Z0-9]+/g;
Loading