From c7dc059116888120751aee45787e85d6bbd972e7 Mon Sep 17 00:00:00 2001 From: Laura Hausmann Date: Sun, 22 Oct 2023 20:10:27 +0200 Subject: [PATCH] [backend] Cleaner workaround for GoToSocial federation with authorized fetch --- .../src/remote/activitypub/db-resolver.ts | 19 +------------------ .../src/remote/activitypub/models/person.ts | 16 ++++++++++++++-- 2 files changed, 15 insertions(+), 20 deletions(-) diff --git a/packages/backend/src/remote/activitypub/db-resolver.ts b/packages/backend/src/remote/activitypub/db-resolver.ts index 02b0c2cd5..d8aa3b706 100644 --- a/packages/backend/src/remote/activitypub/db-resolver.ts +++ b/packages/backend/src/remote/activitypub/db-resolver.ts @@ -19,9 +19,6 @@ import type { IObject } from "./type.js"; import { getApId } from "./type.js"; import { resolvePerson, updatePerson } from "./models/person.js"; import {redisClient, subscriber} from "@/db/redis.js"; -import { remoteLogger } from "@/remote/logger.js"; - -const logger = remoteLogger.createSubLogger("db-resolver"); const publicKeyCache = new Cache("publicKey", 60 * 30); const publicKeyByUserIdCache = new Cache( @@ -191,21 +188,7 @@ export default class DbResolver { user: CacheableRemoteUser; key: UserPublickey | null; } | null> { - let user: CacheableRemoteUser; - - try { - user = (await resolvePerson(uri)) as CacheableRemoteUser; - } - catch (e: any) { - // Bypass GoToSocial issue #1186 (ref: https://github.com/superseriousbusiness/gotosocial/issues/1186) - if (e.message === 'invalid Actor: wrong inbox' && uri.match(/https?:\/\/[a-zA-Z0-9-.]+\/users\/[a-zA-Z0-9_]+\/main-key$/)) { - logger.warn(`Failed to resolve ${uri}, re-attempting without trailing /main-key`); - user = (await resolvePerson(uri.substring(0, uri.length - 9))) as CacheableRemoteUser; - } - else { - throw e; - } - } + const user = (await resolvePerson(uri)) as CacheableRemoteUser; if (user == null) return null; diff --git a/packages/backend/src/remote/activitypub/models/person.ts b/packages/backend/src/remote/activitypub/models/person.ts index 0fdbbcb81..1fb0f52cd 100644 --- a/packages/backend/src/remote/activitypub/models/person.ts +++ b/packages/backend/src/remote/activitypub/models/person.ts @@ -183,9 +183,21 @@ export async function createPerson( if (resolver == null) resolver = new Resolver(); - const object = (await resolver.resolve(uri)) as any; + let object = (await resolver.resolve(uri)) as any; - const person = validateActor(object, uri); + let person: IActor; + try { + person = validateActor(object, uri); + } + catch (e: any) { + if (typeof object.publicKey?.owner !== 'string') + throw e; + + // Work around GoToSocial issue #1186 (ref: https://github.com/superseriousbusiness/gotosocial/issues/1186) + logger.info(`Received stub actor, re-resolving with key owner uri: ${object.publicKey.owner}`); + object = (await resolver.resolve(object.publicKey.owner)) as any; + person = validateActor(object, uri); + } logger.info(`Creating the Person: ${person.id}`);