From a143a5cc481f840658ac4630bfe47c6d35535b94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B4=AA=20=E6=B0=91=E6=86=99=20=28Hong=20Minhee=29?= Date: Wed, 5 Jun 2024 21:07:45 +0000 Subject: [PATCH] fix(backend): handle corner case where a user has multiple pubkeys MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 一人のユーザが複数の公開鍵を持つコーナーケースを処理 --- .../activitypub/models/ApPersonService.ts | 22 ++++++++++++------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/packages/backend/src/core/activitypub/models/ApPersonService.ts b/packages/backend/src/core/activitypub/models/ApPersonService.ts index 744b1ea68376..cd06e8dd63cf 100644 --- a/packages/backend/src/core/activitypub/models/ApPersonService.ts +++ b/packages/backend/src/core/activitypub/models/ApPersonService.ts @@ -183,13 +183,16 @@ export class ApPersonService implements OnModuleInit { } if (x.publicKey) { - if (typeof x.publicKey.id !== 'string') { - throw new Error('invalid Actor: publicKey.id is not a string'); - } + const publicKeys = Array.isArray(x.publicKey) ? x.publicKey : [x.publicKey]; + for (const publicKey of publicKeys) { + if (typeof publicKey.id !== 'string') { + throw new Error('invalid Actor: publicKey.id is not a string'); + } - const publicKeyIdHost = this.punyHost(x.publicKey.id); - if (publicKeyIdHost !== expectHost) { - throw new Error('invalid Actor: publicKey.id has different host'); + const publicKeyIdHost = this.punyHost(publicKey.id); + if (publicKeyIdHost !== expectHost) { + throw new Error('invalid Actor: publicKey.id has different host'); + } } } @@ -356,10 +359,13 @@ export class ApPersonService implements OnModuleInit { })); if (person.publicKey) { + // TODO: 一人のユーザが複数の公開鍵を持っている場合があるので、MiUserPublicKeyの主キーもuserIdから(userId, keyId)に変更する必要があるかも…? + // As a user can have multiple public keys, it might be better to change MiUserPublicKey's primary key from userId to (userId, keyId). + const publicKey = Array.isArray(person.publicKey) ? person.publicKey[0] : person.publicKey; await transactionalEntityManager.save(new MiUserPublickey({ userId: user.id, - keyId: person.publicKey.id, - keyPem: person.publicKey.publicKeyPem, + keyId: publicKey.id, + keyPem: publicKey.publicKeyPem, })); } });