-
Notifications
You must be signed in to change notification settings - Fork 2
feat: rate limiting + suspicious link filtering (#39) #93
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d01bb82
feat(moderation): add rate limiter with sliding window (issue #39)
BillChirico d1c3959
feat(moderation): add link filter with phishing detection (issue #39)
BillChirico 3062e40
feat(events): wire rate limit + link filter into messageCreate (issue…
BillChirico 07fdabf
fix: check MODERATE_MEMBERS permission before timeout
BillChirico c1bd8ce
style: fix biome lint and formatting issues
BillChirico e52101b
fix: resolve review comments for rate limiting and link filtering
BillChirico File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,176 @@ | ||
| /** | ||
| * Link Filter Module | ||
| * Extracts URLs from messages and checks against a configurable domain blocklist. | ||
| * Also detects phishing TLD patterns (.xyz with suspicious keywords). | ||
| */ | ||
|
|
||
| import { EmbedBuilder } from 'discord.js'; | ||
| import { warn } from '../logger.js'; | ||
| import { isExempt } from '../utils/modExempt.js'; | ||
| import { safeSend } from '../utils/safeSend.js'; | ||
| import { sanitizeMentions } from '../utils/sanitizeMentions.js'; | ||
|
|
||
| /** | ||
| * Regex to extract URLs from message content. | ||
| * Matches http/https URLs and bare domain.tld patterns. | ||
| */ | ||
| const URL_REGEX = | ||
| /https?:\/\/(?:www\.)?([a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?(?:\.[a-z]{2,})+)(\/[^\s]*)?|(?:^|\s)(?:www\.)?([a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?(?:\.[a-z]{2,})+)(\/[^\s]*)?/gi; | ||
|
|
||
| /** | ||
| * Phishing TLD patterns: .xyz links whose path/subdomain contains scam keywords. | ||
| * Catches "discord-nitro-free.xyz", "free-nitro.xyz/claim", etc. | ||
| */ | ||
| const PHISHING_PATTERNS = [ | ||
| // .xyz domains with suspicious keywords anywhere in the URL | ||
| /(?:discord|nitro|free|gift|giveaway|steam|crypto|nft|airdrop)[a-z0-9\-_.]*\.xyz(?:\/[^\s]*)?/i, | ||
| // Any .xyz URL that contains those keywords in the path | ||
| /[a-z0-9\-_.]+\.xyz\/[^\s]*(?:discord|nitro|free|gift|steam|crypto)[^\s]*/i, | ||
| // Common phishing subdomains regardless of TLD | ||
| /(?:discord-nitro|discordnitro|free-nitro|steamgift)\.[a-z]{2,}(?:\/[^\s]*)?/i, | ||
| ]; | ||
|
|
||
| /** | ||
| * Normalize a domain entry from the blocklist. | ||
| * Lowercases the value and strips a leading "www." so that blocklist entries | ||
| * are comparable to the already-normalized hostnames extracted by extractUrls(). | ||
| * | ||
| * @param {string} domain | ||
| * @returns {string} | ||
| */ | ||
| function normalizeBlockedDomain(domain) { | ||
| return domain.toLowerCase().replace(/^www\./, ''); | ||
| } | ||
|
|
||
| /** | ||
| * Extract all hostnames/domains from a message string. | ||
| * @param {string} content | ||
| * @returns {{ hostname: string, fullUrl: string }[]} | ||
| */ | ||
| export function extractUrls(content) { | ||
| const results = []; | ||
| const seen = new Set(); | ||
| let match; | ||
| const regex = new RegExp(URL_REGEX.source, URL_REGEX.flags); | ||
|
|
||
| for (match = regex.exec(content); match; match = regex.exec(content)) { | ||
| // Group 1: hostname from http(s):// URL, Group 3: bare domain | ||
| const hostname = (match[1] || match[3] || '').toLowerCase().replace(/^www\./, ''); | ||
| const fullUrl = match[0].trim(); | ||
|
|
||
| if (hostname && !seen.has(hostname)) { | ||
| seen.add(hostname); | ||
| results.push({ hostname, fullUrl }); | ||
| } | ||
| } | ||
|
|
||
| return results; | ||
| } | ||
|
|
||
| /** | ||
| * Check whether the content contains any phishing TLD patterns. | ||
| * @param {string} content | ||
| * @returns {string|null} matched pattern string or null | ||
| */ | ||
| export function matchPhishingPattern(content) { | ||
| for (const pattern of PHISHING_PATTERNS) { | ||
| const m = content.match(pattern); | ||
| if (m) return m[0]; | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| /** | ||
| * Alert the mod channel about a blocked link. | ||
| * @param {import('discord.js').Message} message | ||
| * @param {Object} config | ||
| * @param {string} matchedDomain | ||
| * @param {string} reason - 'blocklist' | 'phishing' | ||
| */ | ||
| async function alertModChannel(message, config, matchedDomain, reason) { | ||
| const alertChannelId = config.moderation?.alertChannelId; | ||
| if (!alertChannelId) return; | ||
|
|
||
| const alertChannel = await message.client.channels.fetch(alertChannelId).catch(() => null); | ||
| if (!alertChannel) return; | ||
|
|
||
| const embed = new EmbedBuilder() | ||
| .setColor(0xed4245) | ||
| .setTitle( | ||
| `🔗 Suspicious Link ${reason === 'phishing' ? '(Phishing Pattern)' : '(Blocklisted Domain)'} Detected`, | ||
| ) | ||
| .addFields( | ||
| { | ||
| name: 'User', | ||
| value: `<@${message.author.id}> (${sanitizeMentions(message.author.tag)})`, | ||
| inline: true, | ||
| }, | ||
| { name: 'Channel', value: `<#${message.channel.id}>`, inline: true }, | ||
| { name: 'Matched', value: `\`${matchedDomain}\``, inline: true }, | ||
| { name: 'Content', value: sanitizeMentions(message.content.slice(0, 1000)) || '*empty*' }, | ||
| ) | ||
| .setTimestamp(); | ||
|
|
||
| await safeSend(alertChannel, { embeds: [embed] }).catch(() => {}); | ||
| } | ||
|
|
||
| /** | ||
| * Check whether a message contains blocked or suspicious links. | ||
| * Deletes the message and alerts the mod channel if a match is found. | ||
| * | ||
| * @param {import('discord.js').Message} message - Discord message object | ||
| * @param {Object} config - Bot config (merged guild config) | ||
| * @returns {Promise<{ blocked: boolean, domain?: string }>} | ||
| */ | ||
| export async function checkLinks(message, config) { | ||
| const lfConfig = config.moderation?.linkFilter ?? {}; | ||
|
|
||
| if (!lfConfig.enabled) return { blocked: false }; | ||
| if (isExempt(message, config)) return { blocked: false }; | ||
|
|
||
| const content = message.content; | ||
| if (!content) return { blocked: false }; | ||
|
|
||
| // 1. Check phishing patterns first (fast regex, no list lookup needed) | ||
| const phishingMatch = matchPhishingPattern(content); | ||
| if (phishingMatch) { | ||
| warn('Link filter: phishing pattern detected', { | ||
| userId: message.author.id, | ||
| channelId: message.channel.id, | ||
| match: phishingMatch, | ||
| }); | ||
| await message.delete().catch(() => {}); | ||
| await alertModChannel(message, config, phishingMatch, 'phishing'); | ||
| return { blocked: true, domain: phishingMatch }; | ||
| } | ||
|
|
||
| // 2. Check extracted URLs against the configurable domain blocklist. | ||
| // Normalize each blocklist entry (lowercase, strip www.) so that | ||
| // mixed-case or www-prefixed config entries match correctly. | ||
| const rawBlockedDomains = lfConfig.blockedDomains ?? []; | ||
| if (rawBlockedDomains.length === 0) return { blocked: false }; | ||
|
|
||
| const blockedDomains = rawBlockedDomains.map(normalizeBlockedDomain); | ||
|
|
||
| const urls = extractUrls(content); | ||
| for (const { hostname, fullUrl } of urls) { | ||
| // Exact match or subdomain match (e.g. "evil.com" also catches "sub.evil.com") | ||
| const matched = blockedDomains.find( | ||
| (blocked) => hostname === blocked || hostname.endsWith(`.${blocked}`), | ||
| ); | ||
|
|
||
| if (matched) { | ||
| warn('Link filter: blocked domain detected', { | ||
| userId: message.author.id, | ||
| channelId: message.channel.id, | ||
| hostname, | ||
| blockedRule: matched, | ||
| }); | ||
| await message.delete().catch(() => {}); | ||
| await alertModChannel(message, config, hostname || fullUrl, 'blocklist'); | ||
| return { blocked: true, domain: matched }; | ||
| } | ||
| } | ||
|
|
||
| return { blocked: false }; | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.