monkeeShark/packages/backend/test/unit/activitypub.ts

512 lines
15 KiB
TypeScript
Raw Normal View History

/*
* SPDX-FileCopyrightText: syuilo and other misskey contributors
* SPDX-License-Identifier: AGPL-3.0-only
*/
process.env.NODE_ENV = 'test';
import * as assert from 'assert';
import { Test } from '@nestjs/testing';
import { jest } from '@jest/globals';
import { ApImageService } from '@/core/activitypub/models/ApImageService.js';
import { ApNoteService } from '@/core/activitypub/models/ApNoteService.js';
import { ApPersonService } from '@/core/activitypub/models/ApPersonService.js';
import { ApRendererService } from '@/core/activitypub/ApRendererService.js';
import { GlobalModule } from '@/GlobalModule.js';
import { CoreModule } from '@/core/CoreModule.js';
import { FederatedInstanceService } from '@/core/FederatedInstanceService.js';
import { LoggerService } from '@/core/LoggerService.js';
import type { IActivity, IApDocument, IActor, ICollection, IObject, IOrderedCollection, IOrderedCollectionPage, IPost } from '@/core/activitypub/type.js';
import { MiMeta, MiNote } from '@/models/_.js';
import { secureRndstr } from '@/misc/secure-rndstr.js';
import { DownloadService } from '@/core/DownloadService.js';
import { MetaService } from '@/core/MetaService.js';
2023-09-05 08:02:14 +00:00
import type { MiRemoteUser } from '@/models/entities/User.js';
import { MockResolver } from '../misc/mock-resolver.js';
2023-02-19 08:49:18 +00:00
const host = 'https://host1.test';
type NonTransientIActor = IActor & { id: string };
type NonTransientIPost = IPost & { id: string };
2023-07-09 20:39:02 +00:00
type NonTransientICollection = ICollection & { id: string };
type NonTransientIOrderedCollection = IOrderedCollection & { id: string };
type NonTransientIOrderedCollectionPage = IOrderedCollectionPage & { id: string };
2023-07-09 21:34:07 +00:00
/**
* Use when the order of the array is not definitive
*/
function deepSortedEqual<T extends unknown[]>(array1: unknown[], array2: T): asserts array1 is T {
return assert.deepStrictEqual(array1.sort(), array2.sort());
}
function createRandomActor({ actorHost = host } = {}): NonTransientIActor {
const preferredUsername = secureRndstr(8);
const actorId = `${actorHost}/users/${preferredUsername.toLowerCase()}`;
2023-02-19 08:49:18 +00:00
return {
'@context': 'https://www.w3.org/ns/activitystreams',
id: actorId,
type: 'Person',
preferredUsername,
inbox: `${actorId}/inbox`,
outbox: `${actorId}/outbox`,
};
}
function createRandomNote(actor: NonTransientIActor): NonTransientIPost {
const id = secureRndstr(8);
const noteId = `${new URL(actor.id).origin}/notes/${id}`;
return {
id: noteId,
type: 'Note',
attributedTo: actor.id,
content: 'test test foo',
};
}
function createRandomNotes(actor: NonTransientIActor, length: number): NonTransientIPost[] {
return new Array(length).fill(null).map(() => createRandomNote(actor));
}
2023-07-09 20:39:02 +00:00
function createRandomFeaturedCollection(actor: NonTransientIActor, length: number): NonTransientICollection {
const items = createRandomNotes(actor, length);
return {
'@context': 'https://www.w3.org/ns/activitystreams',
type: 'Collection',
id: actor.outbox as string,
totalItems: items.length,
items,
};
}
2023-07-09 21:34:07 +00:00
function createRandomActivities(actor: NonTransientIActor, type: string, length: number): IActivity[] {
return new Array(length).fill(null).map((): IActivity => {
2023-07-09 20:39:02 +00:00
const note = createRandomNote(actor);
return {
2023-07-09 21:34:07 +00:00
type,
id: `${note.id}/activity`,
actor,
object: note,
};
});
}
2023-07-09 20:39:02 +00:00
function createRandomNonPagedOutbox(actor: NonTransientIActor, length: number): NonTransientIOrderedCollection {
2023-07-09 21:34:07 +00:00
const orderedItems = createRandomActivities(actor, 'Create', length);
return {
'@context': 'https://www.w3.org/ns/activitystreams',
type: 'OrderedCollection',
id: actor.outbox as string,
totalItems: orderedItems.length,
orderedItems,
};
}
2023-07-09 20:39:02 +00:00
function createRandomOutboxPage(actor: NonTransientIActor, id: string, length: number): NonTransientIOrderedCollectionPage {
2023-07-09 21:34:07 +00:00
const orderedItems = createRandomActivities(actor, 'Create', length);
return {
'@context': 'https://www.w3.org/ns/activitystreams',
type: 'OrderedCollectionPage',
id,
totalItems: orderedItems.length,
orderedItems,
};
}
2023-07-09 20:39:02 +00:00
function createRandomPagedOutbox(actor: NonTransientIActor): NonTransientIOrderedCollection {
return {
'@context': 'https://www.w3.org/ns/activitystreams',
type: 'OrderedCollection',
id: actor.outbox as string,
totalItems: 10,
first: `${actor.outbox}?first`,
2023-07-09 20:36:41 +00:00
};
}
async function createRandomRemoteUser(
resolver: MockResolver,
personService: ApPersonService,
2023-09-05 08:02:14 +00:00
): Promise<MiRemoteUser> {
const actor = createRandomActor();
resolver.register(actor.id, actor);
return await personService.createPerson(actor.id, resolver);
}
describe('ActivityPub', () => {
let imageService: ApImageService;
let noteService: ApNoteService;
let personService: ApPersonService;
let rendererService: ApRendererService;
let resolver: MockResolver;
const metaInitial = {
cacheRemoteFiles: true,
cacheRemoteSensitiveFiles: true,
blockedHosts: [] as string[],
sensitiveWords: [] as string[],
2023-09-05 08:02:14 +00:00
} as MiMeta;
let meta = metaInitial;
2023-07-08 13:40:48 +00:00
beforeAll(async () => {
const app = await Test.createTestingModule({
imports: [GlobalModule, CoreModule],
})
.overrideProvider(DownloadService).useValue({
async downloadUrl(): Promise<{ filename: string }> {
return {
filename: 'dummy.tmp',
};
},
})
.overrideProvider(MetaService).useValue({
2023-09-05 08:02:14 +00:00
async fetch(): Promise<MiMeta> {
return meta;
},
}).compile();
await app.init();
app.enableShutdownHooks();
noteService = app.get<ApNoteService>(ApNoteService);
personService = app.get<ApPersonService>(ApPersonService);
rendererService = app.get<ApRendererService>(ApRendererService);
imageService = app.get<ApImageService>(ApImageService);
resolver = new MockResolver(await app.resolve<LoggerService>(LoggerService));
// Prevent ApPersonService from fetching instance, as it causes Jest import-after-test error
const federatedInstanceService = app.get<FederatedInstanceService>(FederatedInstanceService);
jest.spyOn(federatedInstanceService, 'fetch').mockImplementation(() => new Promise(() => { }));
});
2023-07-08 13:40:48 +00:00
beforeEach(() => {
resolver.clear();
});
describe('Parse minimum object', () => {
2023-02-19 08:49:18 +00:00
const actor = createRandomActor();
const post = {
'@context': 'https://www.w3.org/ns/activitystreams',
id: `${host}/users/${secureRndstr(8)}`,
type: 'Note',
attributedTo: actor.id,
to: 'https://www.w3.org/ns/activitystreams#Public',
content: 'あ',
};
2023-02-02 09:18:25 +00:00
test('Minimum Actor', async () => {
resolver.register(actor.id, actor);
const user = await personService.createPerson(actor.id, resolver);
assert.deepStrictEqual(user.uri, actor.id);
assert.deepStrictEqual(user.username, actor.preferredUsername);
assert.deepStrictEqual(user.inbox, actor.inbox);
});
2023-02-02 09:18:25 +00:00
test('Minimum Note', async () => {
resolver.register(actor.id, actor);
resolver.register(post.id, post);
const note = await noteService.createNote(post.id, resolver, true);
assert.deepStrictEqual(note?.uri, post.id);
2022-05-21 13:21:41 +00:00
assert.deepStrictEqual(note.visibility, 'public');
assert.deepStrictEqual(note.text, post.content);
});
});
2021-08-17 08:25:19 +00:00
2023-02-19 08:49:18 +00:00
describe('Name field', () => {
test('Truncate long name', async () => {
const actor = {
...createRandomActor(),
name: secureRndstr(129),
2023-02-19 08:49:18 +00:00
};
2021-08-17 08:25:19 +00:00
resolver.register(actor.id, actor);
2021-08-17 08:25:19 +00:00
2023-02-19 08:49:18 +00:00
const user = await personService.createPerson(actor.id, resolver);
assert.deepStrictEqual(user.name, actor.name.slice(0, 128));
});
test('Normalize empty name', async () => {
const actor = {
...createRandomActor(),
name: '',
};
2021-08-17 08:25:19 +00:00
resolver.register(actor.id, actor);
2021-08-17 08:25:19 +00:00
const user = await personService.createPerson(actor.id, resolver);
2021-08-17 08:25:19 +00:00
2023-02-19 08:49:18 +00:00
assert.strictEqual(user.name, null);
2021-08-17 08:25:19 +00:00
});
});
describe('Renderer', () => {
test('Render an announce with visibility: followers', () => {
2023-07-15 14:06:36 +00:00
rendererService.renderAnnounce('hoge', {
createdAt: new Date(0),
visibility: 'followers',
2023-09-05 08:02:14 +00:00
} as MiNote);
});
});
describe('Featured', () => {
test('Fetch featured notes from IActor', async () => {
const actor = createRandomActor();
actor.featured = `${actor.id}/collections/featured`;
const featured = createRandomFeaturedCollection(actor, 5);
resolver.register(actor.id, actor);
resolver.register(actor.featured, featured);
await personService.createPerson(actor.id, resolver);
// All notes in `featured` are same-origin, no need to fetch notes again
2023-07-09 21:34:07 +00:00
deepSortedEqual(resolver.remoteGetTrials(), [actor.id, actor.featured, actor.outbox]);
// Created notes without resolving anything
for (const item of featured.items as IPost[]) {
const note = await noteService.fetchNote(item);
assert.ok(note);
assert.strictEqual(note.text, 'test test foo');
assert.strictEqual(note.uri, item.id);
}
});
test('Fetch featured notes from IActor pointing to another remote server', async () => {
const actor1 = createRandomActor();
actor1.featured = `${actor1.id}/collections/featured`;
const actor2 = createRandomActor({ actorHost: 'https://host2.test' });
const actor2Note = createRandomNote(actor2);
const featured = createRandomFeaturedCollection(actor1, 0);
(featured.items as IPost[]).push({
...actor2Note,
content: 'test test bar', // fraud!
});
resolver.register(actor1.id, actor1);
resolver.register(actor1.featured, featured);
resolver.register(actor2.id, actor2);
resolver.register(actor2Note.id, actor2Note);
await personService.createPerson(actor1.id, resolver);
// actor2Note is from a different server and needs to be fetched again
2023-07-09 21:34:07 +00:00
deepSortedEqual(
resolver.remoteGetTrials(),
2023-07-09 21:34:07 +00:00
[actor1.id, actor1.featured, actor1.outbox, actor2Note.id, actor2.id, actor2.outbox],
);
const note = await noteService.fetchNote(actor2Note.id);
assert.ok(note);
// Reflects the original content instead of the fraud
assert.strictEqual(note.text, 'test test foo');
assert.strictEqual(note.uri, actor2Note.id);
});
test('Fetch a note that is a featured note of the attributed actor', async () => {
const actor = createRandomActor();
actor.featured = `${actor.id}/collections/featured`;
const featured = createRandomFeaturedCollection(actor, 5);
const firstNote = (featured.items as NonTransientIPost[])[0];
resolver.register(actor.id, actor);
resolver.register(actor.featured, featured);
resolver.register(firstNote.id, firstNote);
const note = await noteService.createNote(firstNote.id as string, resolver);
assert.strictEqual(note?.uri, firstNote.id);
});
2023-07-09 20:36:07 +00:00
});
describe('Outbox', () => {
test('Fetch non-paged outbox from IActor', async () => {
const actor = createRandomActor();
const outbox = createRandomNonPagedOutbox(actor, 10);
resolver.register(actor.id, actor);
resolver.register(actor.outbox as string, outbox);
await personService.createPerson(actor.id, resolver);
2023-07-09 21:34:07 +00:00
deepSortedEqual(
resolver.remoteGetTrials(),
[actor.id, actor.outbox],
);
for (const item of outbox.orderedItems as IActivity[]) {
const note = await noteService.fetchNote(item.object);
assert.ok(note);
assert.strictEqual(note.text, 'test test foo');
assert.strictEqual(note.uri, (item.object as IObject).id);
}
});
test('Fetch paged outbox from IActor', async () => {
const actor = createRandomActor();
const outbox = createRandomPagedOutbox(actor);
2023-07-09 20:36:27 +00:00
const page = createRandomOutboxPage(actor, outbox.id, 10);
resolver.register(actor.id, actor);
resolver.register(actor.outbox as string, outbox);
resolver.register(outbox.first as string, page);
await personService.createPerson(actor.id, resolver);
2023-07-09 21:34:07 +00:00
deepSortedEqual(
resolver.remoteGetTrials(),
[actor.id, actor.outbox, outbox.first],
);
for (const item of page.orderedItems as IActivity[]) {
const note = await noteService.fetchNote(item.object);
assert.ok(note);
assert.strictEqual(note.text, 'test test foo');
assert.strictEqual(note.uri, (item.object as IObject).id);
}
});
test('Fetch only the first 20 items', async () => {
const actor = createRandomActor();
const outbox = createRandomNonPagedOutbox(actor, 200);
resolver.register(actor.id, actor);
resolver.register(actor.outbox as string, outbox);
await personService.createPerson(actor.id, resolver);
2023-07-09 21:34:07 +00:00
const items = outbox.orderedItems as IActivity[];
deepSortedEqual(
resolver.remoteGetTrials(),
[actor.id, actor.outbox],
);
2023-07-08 13:40:48 +00:00
assert.ok(await noteService.fetchNote(items[19].object));
assert.ok(!await noteService.fetchNote(items[20].object));
2023-07-09 20:36:07 +00:00
});
2023-07-09 21:34:07 +00:00
test('Perform only Create activities', async () => {
const actor = createRandomActor();
const outbox = createRandomNonPagedOutbox(actor, 0);
outbox.orderedItems = createRandomActivities(actor, 'Announce', 10);
resolver.register(actor.id, actor);
resolver.register(actor.outbox as string, outbox);
await personService.createPerson(actor.id, resolver);
deepSortedEqual(
resolver.remoteGetTrials(),
[actor.id, actor.outbox],
);
for (const item of outbox.orderedItems as IActivity[]) {
const note = await noteService.fetchNote(item.object);
assert.ok(!note);
}
});
});
describe('Images', () => {
test('Create images', async () => {
const imageObject: IApDocument = {
type: 'Document',
mediaType: 'image/png',
url: 'http://host1.test/foo.png',
name: '',
};
const driveFile = await imageService.createImage(
await createRandomRemoteUser(resolver, personService),
imageObject,
);
assert.ok(!driveFile.isLink);
const sensitiveImageObject: IApDocument = {
type: 'Document',
mediaType: 'image/png',
url: 'http://host1.test/bar.png',
name: '',
sensitive: true,
};
const sensitiveDriveFile = await imageService.createImage(
await createRandomRemoteUser(resolver, personService),
sensitiveImageObject,
);
assert.ok(!sensitiveDriveFile.isLink);
});
test('cacheRemoteFiles=false disables caching', async () => {
meta = { ...metaInitial, cacheRemoteFiles: false };
const imageObject: IApDocument = {
type: 'Document',
mediaType: 'image/png',
url: 'http://host1.test/foo.png',
name: '',
};
const driveFile = await imageService.createImage(
await createRandomRemoteUser(resolver, personService),
imageObject,
);
assert.ok(driveFile.isLink);
const sensitiveImageObject: IApDocument = {
type: 'Document',
mediaType: 'image/png',
url: 'http://host1.test/bar.png',
name: '',
sensitive: true,
};
const sensitiveDriveFile = await imageService.createImage(
await createRandomRemoteUser(resolver, personService),
sensitiveImageObject,
);
assert.ok(sensitiveDriveFile.isLink);
});
test('cacheRemoteSensitiveFiles=false only affects sensitive files', async () => {
meta = { ...metaInitial, cacheRemoteSensitiveFiles: false };
const imageObject: IApDocument = {
type: 'Document',
mediaType: 'image/png',
url: 'http://host1.test/foo.png',
name: '',
};
const driveFile = await imageService.createImage(
await createRandomRemoteUser(resolver, personService),
imageObject,
);
assert.ok(!driveFile.isLink);
const sensitiveImageObject: IApDocument = {
type: 'Document',
mediaType: 'image/png',
url: 'http://host1.test/bar.png',
name: '',
sensitive: true,
};
const sensitiveDriveFile = await imageService.createImage(
await createRandomRemoteUser(resolver, personService),
sensitiveImageObject,
);
assert.ok(sensitiveDriveFile.isLink);
});
});
});