Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(backend): replace punyHost with new URL().host #15164

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 1 addition & 8 deletions packages/backend/src/core/UtilityService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 1 addition & 3 deletions packages/backend/src/core/activitypub/ApRequestService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import { DI } from '@/di-symbols.js';
import type { Config } from '@/config.js';
import type { MiUser } from '@/models/User.js';
import { UserKeypairService } from '@/core/UserKeypairService.js';
import { UtilityService } from '@/core/UtilityService.js';
import { HttpRequestService } from '@/core/HttpRequestService.js';
import { LoggerService } from '@/core/LoggerService.js';
import { bindThis } from '@/decorators.js';
Expand Down Expand Up @@ -148,7 +147,6 @@ export class ApRequestService {
private userKeypairService: UserKeypairService,
private httpRequestService: HttpRequestService,
private loggerService: LoggerService,
private utilityService: UtilityService,
) {
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
this.logger = this.loggerService?.getLogger('ap-request'); // なぜか TypeError: Cannot read properties of undefined (reading 'getLogger') と言われる
Expand Down Expand Up @@ -242,7 +240,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);
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/backend/src/core/activitypub/ApResolverService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
}
}
Expand Down
20 changes: 10 additions & 10 deletions packages/backend/src/core/activitypub/models/ApPersonService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}'`);
Expand All @@ -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');
}
}
Expand All @@ -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) {
Expand Down Expand Up @@ -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');
}
Expand All @@ -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');
}
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -300,7 +300,7 @@ export class ApPersonService implements OnModuleInit {
public async createPerson(uri: string, resolver?: Resolver): Promise<MiRemoteUser> {
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');
}
Expand Down Expand Up @@ -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}`);
}
}
Expand Down Expand Up @@ -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}`);
}
}
Expand Down
Loading