From 41548fc0bdc74d1670e5cae068b28e69e633141d Mon Sep 17 00:00:00 2001 From: Kagami Sascha Rosylight Date: Sat, 21 Dec 2024 19:51:16 +0100 Subject: [PATCH] refactor(backend): replace `punyHost` with `new URL().host` --- packages/backend/src/core/UtilityService.ts | 9 +-------- .../src/core/activitypub/ApRequestService.ts | 2 +- .../src/core/activitypub/ApResolverService.ts | 2 +- .../core/activitypub/models/ApNoteService.ts | 2 +- .../activitypub/models/ApPersonService.ts | 20 +++++++++---------- 5 files changed, 14 insertions(+), 21 deletions(-) diff --git a/packages/backend/src/core/UtilityService.ts b/packages/backend/src/core/UtilityService.ts index 9a2ba72ed3..7f1a59401c 100644 --- a/packages/backend/src/core/UtilityService.ts +++ b/packages/backend/src/core/UtilityService.ts @@ -36,7 +36,7 @@ export class UtilityService { @bindThis public isUriLocal(uri: string): boolean { - return this.punyHost(uri) === this.toPuny(this.config.host); + return new URL(uri).host === this.toPuny(this.config.host); } @bindThis @@ -115,13 +115,6 @@ export class UtilityService { return toASCII(host.toLowerCase()); } - @bindThis - public punyHost(url: string): string { - const urlObj = new URL(url); - const host = `${this.toPuny(urlObj.hostname)}${urlObj.port.length > 0 ? ':' + urlObj.port : ''}`; - return host; - } - @bindThis public isFederationAllowedHost(host: string): boolean { if (this.meta.federation === 'none') return false; diff --git a/packages/backend/src/core/activitypub/ApRequestService.ts b/packages/backend/src/core/activitypub/ApRequestService.ts index 8c3b7295e4..0783e226c2 100644 --- a/packages/backend/src/core/activitypub/ApRequestService.ts +++ b/packages/backend/src/core/activitypub/ApRequestService.ts @@ -242,7 +242,7 @@ export class ApRequestService { const alternate = document.querySelector('head > link[rel="alternate"][type="application/activity+json"]'); if (alternate) { const href = alternate.getAttribute('href'); - if (href && this.utilityService.punyHost(url) === this.utilityService.punyHost(href)) { + if (href && new URL(url).host === new URL(href).host) { return await this.signedGet(href, user, false); } } diff --git a/packages/backend/src/core/activitypub/ApResolverService.ts b/packages/backend/src/core/activitypub/ApResolverService.ts index 52cc569140..8c5aec3b9a 100644 --- a/packages/backend/src/core/activitypub/ApResolverService.ts +++ b/packages/backend/src/core/activitypub/ApResolverService.ts @@ -127,7 +127,7 @@ export class Resolver { throw new IdentifiableError('ad2dc287-75c1-44c4-839d-3d2e64576675', 'invalid AP object: missing id'); } - if (this.utilityService.punyHost(object.id) !== this.utilityService.punyHost(value)) { + if (new URL(object.id).host !== new URL(value).host) { throw new IdentifiableError('fd93c2fa-69a8-440f-880b-bf178e0ec877', `invalid AP object ${value}: id ${object.id} has different host`); } diff --git a/packages/backend/src/core/activitypub/models/ApNoteService.ts b/packages/backend/src/core/activitypub/models/ApNoteService.ts index eb2e771a38..2072e2d14c 100644 --- a/packages/backend/src/core/activitypub/models/ApNoteService.ts +++ b/packages/backend/src/core/activitypub/models/ApNoteService.ts @@ -159,7 +159,7 @@ export class ApNoteService { throw new Error('unexpected schema of note url: ' + url); } - if (this.utilityService.punyHost(url) !== this.utilityService.punyHost(note.id)) { + if (new URL(url).host !== new URL(note.id).host) { throw new Error(`note url & uri host mismatch: note url: ${url}, note uri: ${note.id}`); } } diff --git a/packages/backend/src/core/activitypub/models/ApPersonService.ts b/packages/backend/src/core/activitypub/models/ApPersonService.ts index 8590861ca0..13d4e44ec8 100644 --- a/packages/backend/src/core/activitypub/models/ApPersonService.ts +++ b/packages/backend/src/core/activitypub/models/ApPersonService.ts @@ -136,7 +136,7 @@ export class ApPersonService implements OnModuleInit { */ @bindThis private validateActor(x: IObject, uri: string): IActor { - const expectHost = this.utilityService.punyHost(uri); + const expectHost = new URL(uri).host; if (!isActor(x)) { throw new Error(`invalid Actor type '${x.type}'`); @@ -150,14 +150,14 @@ export class ApPersonService implements OnModuleInit { throw new Error('invalid Actor: wrong inbox'); } - if (this.utilityService.punyHost(x.inbox) !== expectHost) { + if (new URL(x.inbox).host !== expectHost) { throw new Error('invalid Actor: inbox has different host'); } const sharedInboxObject = x.sharedInbox ?? (x.endpoints ? x.endpoints.sharedInbox : undefined); if (sharedInboxObject != null) { const sharedInbox = getApId(sharedInboxObject); - if (!(typeof sharedInbox === 'string' && sharedInbox.length > 0 && this.utilityService.punyHost(sharedInbox) === expectHost)) { + if (!(typeof sharedInbox === 'string' && sharedInbox.length > 0 && new URL(sharedInbox).host === expectHost)) { throw new Error('invalid Actor: wrong shared inbox'); } } @@ -167,7 +167,7 @@ export class ApPersonService implements OnModuleInit { if (xCollection != null) { const collectionUri = getApId(xCollection); if (typeof collectionUri === 'string' && collectionUri.length > 0) { - if (this.utilityService.punyHost(collectionUri) !== expectHost) { + if (new URL(collectionUri).host !== expectHost) { throw new Error(`invalid Actor: ${collection} has different host`); } } else if (collectionUri != null) { @@ -199,7 +199,7 @@ export class ApPersonService implements OnModuleInit { x.summary = truncate(x.summary, summaryLength); } - const idHost = this.utilityService.punyHost(x.id); + const idHost = new URL(x.id).host; if (idHost !== expectHost) { throw new Error('invalid Actor: id has different host'); } @@ -209,7 +209,7 @@ export class ApPersonService implements OnModuleInit { throw new Error('invalid Actor: publicKey.id is not a string'); } - const publicKeyIdHost = this.utilityService.punyHost(x.publicKey.id); + const publicKeyIdHost = new URL(x.publicKey.id).host; if (publicKeyIdHost !== expectHost) { throw new Error('invalid Actor: publicKey.id has different host'); } @@ -257,7 +257,7 @@ export class ApPersonService implements OnModuleInit { if (Array.isArray(img)) { img = img.find(item => item && item.url) ?? null; } - + // if we have an explicitly missing image, return an // explicitly-null set of values if ((img == null) || (typeof img === 'object' && img.url == null)) { @@ -300,7 +300,7 @@ export class ApPersonService implements OnModuleInit { public async createPerson(uri: string, resolver?: Resolver): Promise { if (typeof uri !== 'string') throw new Error('uri is not string'); - const host = this.utilityService.punyHost(uri); + const host = new URL(uri).host; if (host === this.utilityService.toPuny(this.config.host)) { throw new StatusError('cannot resolve local user', 400, 'cannot resolve local user'); } @@ -349,7 +349,7 @@ export class ApPersonService implements OnModuleInit { throw new Error('unexpected schema of person url: ' + url); } - if (this.utilityService.punyHost(url) !== this.utilityService.punyHost(person.id)) { + if (new URL(url).host !== new URL(person.id).host) { throw new Error(`person url <> uri host mismatch: ${url} <> ${person.id}`); } } @@ -552,7 +552,7 @@ export class ApPersonService implements OnModuleInit { throw new Error('unexpected schema of person url: ' + url); } - if (this.utilityService.punyHost(url) !== this.utilityService.punyHost(person.id)) { + if (new URL(url).host !== new URL(person.id).host) { throw new Error(`person url <> uri host mismatch: ${url} <> ${person.id}`); } }