server: make more places use shouldBlockInstance

This commit is contained in:
Norm 2022-12-03 06:39:37 -05:00
parent 1839efe409
commit e3701b21db
Signed by: norm
GPG key ID: 7123E30E441E80DE
3 changed files with 9 additions and 12 deletions

View file

@ -1,13 +1,12 @@
import { db } from '@/db/postgre.js'; import { db } from '@/db/postgre.js';
import { Instance } from '@/models/entities/instance.js'; import { Instance } from '@/models/entities/instance.js';
import { Packed } from '@/misc/schema.js'; import { Packed } from '@/misc/schema.js';
import { fetchMeta } from '@/misc/fetch-meta.js'; import { shouldBlockInstance } from '@/misc/should-block-instance.js';
export const InstanceRepository = db.getRepository(Instance).extend({ export const InstanceRepository = db.getRepository(Instance).extend({
async pack( async pack(
instance: Instance, instance: Instance,
): Promise<Packed<'FederationInstance'>> { ): Promise<Packed<'FederationInstance'>> {
const meta = await fetchMeta();
return { return {
id: instance.id, id: instance.id,
caughtAt: instance.caughtAt.toISOString(), caughtAt: instance.caughtAt.toISOString(),
@ -20,7 +19,7 @@ export const InstanceRepository = db.getRepository(Instance).extend({
lastCommunicatedAt: instance.lastCommunicatedAt.toISOString(), lastCommunicatedAt: instance.lastCommunicatedAt.toISOString(),
isNotResponding: instance.isNotResponding, isNotResponding: instance.isNotResponding,
isSuspended: instance.isSuspended, isSuspended: instance.isSuspended,
isBlocked: meta.blockedHosts.includes(instance.host), isBlocked: await shouldBlockInstance(instance.host),
softwareName: instance.softwareName, softwareName: instance.softwareName,
softwareVersion: instance.softwareVersion, softwareVersion: instance.softwareVersion,
openRegistrations: instance.openRegistrations, openRegistrations: instance.openRegistrations,

View file

@ -1,7 +1,6 @@
import post from '@/services/note/create.js'; import post from '@/services/note/create.js';
import { CacheableRemoteUser } from '@/models/entities/user.js'; import { CacheableRemoteUser } from '@/models/entities/user.js';
import { extractDbHost } from '@/misc/convert-host.js'; import { extractDbHost } from '@/misc/convert-host.js';
import { fetchMeta } from '@/misc/fetch-meta.js';
import { getApLock } from '@/misc/app-lock.js'; import { getApLock } from '@/misc/app-lock.js';
import { StatusError } from '@/misc/fetch.js'; import { StatusError } from '@/misc/fetch.js';
import { Notes } from '@/models/index.js'; import { Notes } from '@/models/index.js';
@ -10,6 +9,7 @@ import { apLogger } from '@/remote/activitypub/logger.js';
import { fetchNote, resolveNote } from '@/remote/activitypub/models/note.js'; import { fetchNote, resolveNote } from '@/remote/activitypub/models/note.js';
import Resolver from '@/remote/activitypub/resolver.js'; import Resolver from '@/remote/activitypub/resolver.js';
import { IAnnounce, getApId } from '@/remote/activitypub/type.js'; import { IAnnounce, getApId } from '@/remote/activitypub/type.js';
import { shouldBlockInstance } from '@/misc/should-block-instance.js';
export default async function(resolver: Resolver, actor: CacheableRemoteUser, activity: IAnnounce, targetUri: string): Promise<void> { export default async function(resolver: Resolver, actor: CacheableRemoteUser, activity: IAnnounce, targetUri: string): Promise<void> {
const uri = getApId(activity); const uri = getApId(activity);
@ -19,8 +19,7 @@ export default async function(resolver: Resolver, actor: CacheableRemoteUser, ac
} }
// Cancel if the announced from host is blocked. // Cancel if the announced from host is blocked.
const meta = await fetchMeta(); if (await shouldBlockInstance(extractDbHost(uri))) return;
if (meta.blockedHosts.includes(extractDbHost(uri))) return;
const unlock = await getApLock(uri); const unlock = await getApLock(uri);

View file

@ -12,11 +12,11 @@ import { Emojis, Polls, MessagingMessages } from '@/models/index.js';
import { Note } from '@/models/entities/note.js'; import { Note } from '@/models/entities/note.js';
import { Emoji } from '@/models/entities/emoji.js'; import { Emoji } from '@/models/entities/emoji.js';
import { genId } from '@/misc/gen-id.js'; import { genId } from '@/misc/gen-id.js';
import { fetchMeta } from '@/misc/fetch-meta.js';
import { getApLock } from '@/misc/app-lock.js'; import { getApLock } from '@/misc/app-lock.js';
import { createMessage } from '@/services/messages/create.js'; import { createMessage } from '@/services/messages/create.js';
import { StatusError } from '@/misc/fetch.js'; import { StatusError } from '@/misc/fetch.js';
import { fromHtml } from '@/mfm/from-html.js'; import { fromHtml } from '@/mfm/from-html.js';
import { shouldBlockInstance } from '@/misc/should-block-instance.js';
import { parseAudience } from '../audience.js'; import { parseAudience } from '../audience.js';
import { IObject, getOneApId, getApId, getOneApHrefNullable, validPost, IPost, isEmoji, getApType } from '../type.js'; import { IObject, getOneApId, getApId, getOneApHrefNullable, validPost, IPost, isEmoji, getApType } from '../type.js';
import DbResolver from '../db-resolver.js'; import DbResolver from '../db-resolver.js';
@ -28,7 +28,7 @@ import { extractApHashtags } from './tag.js';
import { extractPollFromQuestion } from './question.js'; import { extractPollFromQuestion } from './question.js';
import { extractApMentions } from './mention.js'; import { extractApMentions } from './mention.js';
export function validateNote(object: any, uri: string) { export function validateNote(object: any, uri: string): Error | null {
const expectHost = extractDbHost(uri); const expectHost = extractDbHost(uri);
if (object == null) { if (object == null) {
@ -270,14 +270,13 @@ export async function resolveNote(value: string | IObject, resolver?: Resolver):
const uri = typeof value === 'string' ? value : value.id; const uri = typeof value === 'string' ? value : value.id;
if (uri == null) throw new Error('missing uri'); if (uri == null) throw new Error('missing uri');
// ブロックしてたら中断 // Interrupt if blocked.
const meta = await fetchMeta(); if (await shouldBlockInstance(extractDbHost(uri))) throw new StatusError('host blocked', 451, `host ${extractDbHost(uri)} is blocked`);
if (meta.blockedHosts.includes(extractDbHost(uri))) throw new StatusError('host blocked', 451, `host ${extractDbHost(uri)} is blocked`);
const unlock = await getApLock(uri); const unlock = await getApLock(uri);
try { try {
//#region このサーバーに既に登録されていたらそれを返す //#region If already registered on this server, return it.
const exist = await fetchNote(uri); const exist = await fetchNote(uri);
if (exist) { if (exist) {