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

enhance(backend): 一人のリモートユーザーが複数の公開鍵を持つコーナーケースを処理 #13950

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
22 changes: 14 additions & 8 deletions packages/backend/src/core/activitypub/models/ApPersonService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const publicKeys = Array.isArray(x.publicKey) ? x.publicKey : [x.publicKey];
const publicKeys = toArray(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');
}
}
}

Expand Down Expand Up @@ -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;
Comment on lines 361 to +364
Copy link
Contributor

@tesaguri tesaguri Jul 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

この処理だと配列が空の場合にpublicKeyundefinedになってしまうので、配列からの値の取り出しはifの手前で行うのが確実です。(そもそも公開鍵を持たないのにpublicKeyを指定しているactorという時点で奇妙なので、エッジケースではありますが。)

ついでに前のレビューコメントと同様にmiscの関数を使った方が良いかと思います(要import { toSingle } from '@/misc/prelude/array.js';)。

Suggested change
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;
// 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 = toSingle(person.publicKey);
if (publicKey) {

await transactionalEntityManager.save(new MiUserPublickey({
userId: user.id,
keyId: person.publicKey.id,
keyPem: person.publicKey.publicKeyPem,
keyId: publicKey.id,
keyPem: publicKey.publicKeyPem,
}));
}
});
Expand Down
Loading