From 1abb06d2cc3fab6306c5b380132c35d9abcb2c42 Mon Sep 17 00:00:00 2001 From: okayurisotto Date: Sun, 2 Jul 2023 12:23:02 +0900 Subject: [PATCH] refactor(`ApImageService.ts`) --- .../src/core/activitypub/models/ApImageService.ts | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/packages/backend/src/core/activitypub/models/ApImageService.ts b/packages/backend/src/core/activitypub/models/ApImageService.ts index 79c5ae958a..e0e219244f 100644 --- a/packages/backend/src/core/activitypub/models/ApImageService.ts +++ b/packages/backend/src/core/activitypub/models/ApImageService.ts @@ -10,9 +10,10 @@ import { DB_MAX_IMAGE_COMMENT_LENGTH } from '@/const.js'; import { DriveService } from '@/core/DriveService.js'; import type Logger from '@/logger.js'; import { bindThis } from '@/decorators.js'; +import { checkHttps } from '@/misc/check-https.js'; import { ApResolverService } from '../ApResolverService.js'; import { ApLoggerService } from '../ApLoggerService.js'; -import { checkHttps } from '@/misc/check-https.js'; +import type { IObject } from '../type.js'; @Injectable() export class ApImageService { @@ -37,18 +38,22 @@ export class ApImageService { * Imageを作成します。 */ @bindThis - public async createImage(actor: RemoteUser, value: any): Promise { + public async createImage(actor: RemoteUser, value: string | IObject): Promise { // 投稿者が凍結されていたらスキップ if (actor.isSuspended) { throw new Error('actor has been suspended'); } - const image = await this.apResolverService.createResolver().resolve(value) as any; + const image = await this.apResolverService.createResolver().resolve(value); if (image.url == null) { throw new Error('invalid image: url not privided'); } + if (typeof image.url !== 'string') { + throw new Error('invalid image: unexpected type of url: ' + JSON.stringify(image.url)); + } + if (!checkHttps(image.url)) { throw new Error('invalid image: unexpected schema of url: ' + image.url); } @@ -63,7 +68,7 @@ export class ApImageService { uri: image.url, sensitive: image.sensitive, isLink: !instance.cacheRemoteFiles, - comment: truncate(image.name, DB_MAX_IMAGE_COMMENT_LENGTH), + comment: truncate(image.name ?? undefined, DB_MAX_IMAGE_COMMENT_LENGTH), }); if (file.isLink) { @@ -89,7 +94,7 @@ export class ApImageService { * リモートサーバーからフェッチしてMisskeyに登録しそれを返します。 */ @bindThis - public async resolveImage(actor: RemoteUser, value: any): Promise { + public async resolveImage(actor: RemoteUser, value: string | IObject): Promise { // TODO // リモートサーバーからフェッチしてきて登録