Skip to content

Commit

Permalink
fix(backend): getApTypeでエラーを投げないように (misskey-dev#14361)
Browse files Browse the repository at this point in the history
cherry picked from commit 93fc06d

Co-authored-by: かっこかり <[email protected]>
  • Loading branch information
kakkokari-gtyih authored and u1-liquid committed Dec 24, 2024
1 parent 21e3b04 commit c909c00
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 17 deletions.
12 changes: 6 additions & 6 deletions packages/backend/src/core/activitypub/ApInboxService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ export class ApInboxService {

if (isFollow(object)) return await this.acceptFollow(actor, object);

return `skip: Unknown Accept type: ${getApType(object)}`;
return `skip: Unknown Accept type: ${getApType(object) ?? 'undefined'}}`;
}

@bindThis
Expand Down Expand Up @@ -279,7 +279,7 @@ export class ApInboxService {

if (isPost(target)) await this.announceNote(actor, activity, target);

return `skip: unknown object type ${getApType(target)}`;
return `skip: unknown object type ${getApType(target) ?? 'undefined'}}`;
}

@bindThis
Expand Down Expand Up @@ -399,7 +399,7 @@ export class ApInboxService {
if (isPost(object)) {
return await this.createNote(resolver, actor, object, false, activity, additionalCc);
} else {
return `skip: Unknown type ${getApType(object)}`;
return `skip: Unknown type ${getApType(object) ?? 'undefined'}}`;
}
}

Expand Down Expand Up @@ -586,7 +586,7 @@ export class ApInboxService {

if (isFollow(object)) return await this.rejectFollow(actor, object);

return `skip: Unknown Reject type: ${getApType(object)}`;
return `skip: Unknown Reject type: ${getApType(object) ?? 'undefined'}}`;
}

@bindThis
Expand Down Expand Up @@ -657,7 +657,7 @@ export class ApInboxService {
if (isAnnounce(object)) return await this.undoAnnounce(actor, object);
if (isAccept(object)) return await this.undoAccept(actor, object);

return `skip: unknown object type ${getApType(object)}`;
return `skip: unknown object type ${getApType(object) ?? 'undefined'}}`;
}

@bindThis
Expand Down Expand Up @@ -809,7 +809,7 @@ export class ApInboxService {
unlock();
}
} else {
return `skip: Unknown type: ${getApType(object)}`;
return `skip: Unknown type: ${getApType(object) ?? 'undefined'}}`;
}
}

Expand Down
5 changes: 3 additions & 2 deletions packages/backend/src/core/activitypub/models/ApNoteService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,10 @@ export class ApNoteService {
@bindThis
public validateNote(object: IObject, uri: string): Error | null {
const expectHost = this.utilityService.extractDbHost(uri);
const apType = getApType(object);

if (!validPost.includes(getApType(object))) {
return new IdentifiableError('d450b8a9-48e4-4dab-ae36-f4db763fda7c', `invalid Note: invalid object type ${getApType(object)}`);
if (apType == null || !validPost.includes(apType)) {
return new IdentifiableError('d450b8a9-48e4-4dab-ae36-f4db763fda7c', `invalid Note: invalid object type ${apType ?? 'undefined'}`);
}

if (object.id && this.utilityService.extractDbHost(object.id) !== expectHost) {
Expand Down
32 changes: 23 additions & 9 deletions packages/backend/src/core/activitypub/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,14 @@ export function getApId(value: string | IObject): string {

/**
* Get ActivityStreams Object type
*
* タイプ判定ができなかった場合に、あえてエラーではなくnullを返すようにしている。
* 詳細: https://github.com/misskey-dev/misskey/issues/14239
*/
export function getApType(value: IObject): string {
export function getApType(value: IObject): string | null {
if (typeof value.type === 'string') return value.type;
if (Array.isArray(value.type) && typeof value.type[0] === 'string') return value.type[0];
throw new Error('cannot detect type');
return null;
}

export function getOneApHrefNullable(value: ApObject | undefined): string | undefined {
Expand Down Expand Up @@ -110,8 +113,10 @@ export interface IOrderedCollection extends IObject {

export const validPost = ['Note', 'Question', 'Article', 'Audio', 'Document', 'Image', 'Page', 'Video', 'Event'];

export const isPost = (object: IObject): object is IPost =>
validPost.includes(getApType(object));
export const isPost = (object: IObject): object is IPost => {
const type = getApType(object);
return type != null && validPost.includes(type);
};

export interface IPost extends IObject {
type: 'Note' | 'Question' | 'Article' | 'Audio' | 'Document' | 'Image' | 'Page' | 'Video' | 'Event';
Expand Down Expand Up @@ -158,8 +163,10 @@ export const isTombstone = (object: IObject): object is ITombstone =>

export const validActor = ['Person', 'Service', 'Group', 'Organization', 'Application'];

export const isActor = (object: IObject): object is IActor =>
validActor.includes(getApType(object));
export const isActor = (object: IObject): object is IActor => {
const type = getApType(object);
return type != null && validActor.includes(type);
};

export interface IActor extends IObject {
type: 'Person' | 'Service' | 'Organization' | 'Group' | 'Application';
Expand Down Expand Up @@ -242,12 +249,16 @@ export interface IKey extends IObject {
publicKeyPem: string | Buffer;
}

export const validDocumentTypes = ['Audio', 'Document', 'Image', 'Page', 'Video'];

export interface IApDocument extends IObject {
type: 'Audio' | 'Document' | 'Image' | 'Page' | 'Video';
}

export const isDocument = (object: IObject): object is IApDocument =>
['Audio', 'Document', 'Image', 'Page', 'Video'].includes(getApType(object));
export const isDocument = (object: IObject): object is IApDocument => {
const type = getApType(object);
return type != null && validDocumentTypes.includes(type);
};

export interface IApImage extends IApDocument {
type: 'Image';
Expand Down Expand Up @@ -325,7 +336,10 @@ export const isAccept = (object: IObject): object is IAccept => getApType(object
export const isReject = (object: IObject): object is IReject => getApType(object) === 'Reject';
export const isAdd = (object: IObject): object is IAdd => getApType(object) === 'Add';
export const isRemove = (object: IObject): object is IRemove => getApType(object) === 'Remove';
export const isLike = (object: IObject): object is ILike => getApType(object) === 'Like' || getApType(object) === 'EmojiReaction' || getApType(object) === 'EmojiReact';
export const isLike = (object: IObject): object is ILike => {
const type = getApType(object);
return type != null && ['Like', 'EmojiReaction', 'EmojiReact'].includes(type);
};
export const isAnnounce = (object: IObject): object is IAnnounce => getApType(object) === 'Announce';
export const isBlock = (object: IObject): object is IBlock => getApType(object) === 'Block';
export const isFlag = (object: IObject): object is IFlag => getApType(object) === 'Flag';
Expand Down

0 comments on commit c909c00

Please sign in to comment.