BREAKING: server: remove wildcard blocking and instead block subdomains #269

Merged
norm merged 4 commits from subdomain-blocking into main 2022-12-05 17:55:38 +00:00
Showing only changes of commit 52c96e96cf - Show all commits

View file

@ -2,20 +2,6 @@ import { fetchMeta } from '@/misc/fetch-meta.js';
import { Instance } from '@/models/entities/instance.js';
import { Meta } from '@/models/entities/meta.js';
/**
* Returns whether a given host matches a wildcard pattern.
* @param host punycoded instance host
* @param pattern wildcard pattern containing a punycoded instance host
* @returns whether the post matches the pattern
*/
function matchHost(host: Instance['host'], pattern: string): boolean {
// Escape all of the regex special characters. Pattern from:
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#escaping
const escape = (str: string): string => str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
const re = new RegExp('^' + pattern.split('*').map(escape).join('.*') + '$');
return re.test(host);
}
/**
* Returns whether a specific host (punycoded) should be blocked.
*
@ -24,7 +10,7 @@ function matchHost(host: Instance['host'], pattern: string): boolean {
* @returns whether the given host should be blocked
*/
export async function shouldBlockInstance(host: string, meta: Promise<Meta> = fetchMeta()): Promise<boolean> {
export async function shouldBlockInstance(host: Instance['host'], meta: Promise<Meta> = fetchMeta()): Promise<boolean> {
const { blockedHosts } = await meta;
return blockedHosts.some(blockedHost => matchHost(host, blockedHost));
return blockedHosts.some(blockedHost => host === blockedHost || host.endsWith('.' + blockedHost));
}