BREAKING: server: remove wildcard blocking and instead block subdomains #269
5 changed files with 34 additions and 31 deletions
30
packages/backend/src/misc/should-block-instance.ts
Normal file
30
packages/backend/src/misc/should-block-instance.ts
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
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.
|
||||||
|
*
|
||||||
|
* @param host punycoded instance host
|
||||||
|
* @param meta a Promise contatining the information from the meta table (optional)
|
||||||
|
* @returns whether the given host should be blocked
|
||||||
|
*/
|
||||||
|
|
||||||
|
export async function shouldBlockInstance(host: string, meta: Promise<Meta> = fetchMeta()): Promise<boolean> {
|
||||||
|
const { blockedHosts } = await meta;
|
||||||
|
return blockedHosts.some(blockedHost => matchHost(host, blockedHost));
|
||||||
|
}
|
|
@ -2,39 +2,12 @@ import { db } from '@/db/postgre.js';
|
||||||
import { fetchMeta } from '@/misc/fetch-meta.js';
|
import { fetchMeta } from '@/misc/fetch-meta.js';
|
||||||
import { Instance } from '@/models/entities/instance.js';
|
import { Instance } from '@/models/entities/instance.js';
|
||||||
import { DAY } from '@/const.js';
|
import { DAY } from '@/const.js';
|
||||||
import { Meta } from '@/models/entities/meta.js';
|
import { shouldBlockInstance } from '@/misc/should-block-instance.js';
|
||||||
|
|
||||||
// Threshold from last contact after which an instance will be considered
|
// Threshold from last contact after which an instance will be considered
|
||||||
// "dead" and should no longer get activities delivered to it.
|
// "dead" and should no longer get activities delivered to it.
|
||||||
const deadThreshold = 7 * DAY;
|
const deadThreshold = 7 * DAY;
|
||||||
|
|
||||||
/**
|
|
||||||
* 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.
|
|
||||||
*
|
|
||||||
* @param host punycoded instance host
|
|
||||||
* @param meta a Promise contatining the information from the meta table (oprional)
|
|
||||||
* @returns whether the given host should be blocked
|
|
||||||
*/
|
|
||||||
export async function shouldBlockInstance(host: string, meta: Promise<Meta> = fetchMeta()): Promise<boolean> {
|
|
||||||
const { blockedHosts } = await meta;
|
|
||||||
return blockedHosts.some(blockedHost => matchHost(host, blockedHost));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the subset of hosts which should be skipped.
|
* Returns the subset of hosts which should be skipped.
|
||||||
*
|
*
|
||||||
|
|
|
@ -16,7 +16,7 @@ import { StatusError } from '@/misc/fetch.js';
|
||||||
import { CacheableRemoteUser } from '@/models/entities/user.js';
|
import { CacheableRemoteUser } from '@/models/entities/user.js';
|
||||||
import { UserPublickey } from '@/models/entities/user-publickey.js';
|
import { UserPublickey } from '@/models/entities/user-publickey.js';
|
||||||
import { InboxJobData } from '@/queue/types.js';
|
import { InboxJobData } from '@/queue/types.js';
|
||||||
import { shouldBlockInstance } from '@/misc/skipped-instances.js';
|
import { shouldBlockInstance } from '@/misc/should-block-instance.js';
|
||||||
|
|
||||||
const logger = new Logger('inbox');
|
const logger = new Logger('inbox');
|
||||||
|
|
||||||
|
|
|
@ -11,7 +11,7 @@ import renderQuestion from '@/remote/activitypub/renderer/question.js';
|
||||||
import renderCreate from '@/remote/activitypub/renderer/create.js';
|
import renderCreate from '@/remote/activitypub/renderer/create.js';
|
||||||
import { renderActivity } from '@/remote/activitypub/renderer/index.js';
|
import { renderActivity } from '@/remote/activitypub/renderer/index.js';
|
||||||
import renderFollow from '@/remote/activitypub/renderer/follow.js';
|
import renderFollow from '@/remote/activitypub/renderer/follow.js';
|
||||||
import { shouldBlockInstance } from '@/misc/skipped-instances.js';
|
import { shouldBlockInstance } from '@/misc/should-block-instance.js';
|
||||||
import { signedGet } from './request.js';
|
import { signedGet } from './request.js';
|
||||||
import { IObject, isCollectionOrOrderedCollection, ICollection, IOrderedCollection } from './type.js';
|
import { IObject, isCollectionOrOrderedCollection, ICollection, IOrderedCollection } from './type.js';
|
||||||
import { parseUri } from './db-resolver.js';
|
import { parseUri } from './db-resolver.js';
|
||||||
|
|
|
@ -9,7 +9,7 @@ import { CacheableLocalUser, User } from '@/models/entities/user.js';
|
||||||
import { isActor, isPost, getApId } from '@/remote/activitypub/type.js';
|
import { isActor, isPost, getApId } from '@/remote/activitypub/type.js';
|
||||||
import { SchemaType } from '@/misc/schema.js';
|
import { SchemaType } from '@/misc/schema.js';
|
||||||
import { HOUR } from '@/const.js';
|
import { HOUR } from '@/const.js';
|
||||||
import { shouldBlockInstance } from '@/misc/skipped-instances.js';
|
import { shouldBlockInstance } from '@/misc/should-block-instance.js';
|
||||||
import define from '../../define.js';
|
import define from '../../define.js';
|
||||||
import { ApiError } from '../../error.js';
|
import { ApiError } from '../../error.js';
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue