2021-03-24 11:05:37 +09:00
|
|
|
import { URL } from 'url';
|
2019-03-07 23:07:21 +09:00
|
|
|
import * as Bull from 'bull';
|
2019-02-01 19:59:12 +09:00
|
|
|
import * as httpSignature from 'http-signature';
|
2021-08-19 21:55:45 +09:00
|
|
|
import perform from '@/remote/activitypub/perform';
|
|
|
|
import Logger from '@/services/logger';
|
|
|
|
import { registerOrFetchInstanceDoc } from '@/services/register-or-fetch-instance-doc';
|
|
|
|
import { Instances } from '@/models/index';
|
2022-02-06 00:13:52 +09:00
|
|
|
import { apRequestChart, federationChart, instanceChart } from '@/services/chart/index';
|
2021-08-19 21:55:45 +09:00
|
|
|
import { fetchMeta } from '@/misc/fetch-meta';
|
|
|
|
import { toPuny, extractDbHost } from '@/misc/convert-host';
|
|
|
|
import { getApId } from '@/remote/activitypub/type';
|
|
|
|
import { fetchInstanceMetadata } from '@/services/fetch-instance-metadata';
|
|
|
|
import { InboxJobData } from '../types';
|
|
|
|
import DbResolver from '@/remote/activitypub/db-resolver';
|
|
|
|
import { resolvePerson } from '@/remote/activitypub/models/person';
|
|
|
|
import { LdSignature } from '@/remote/activitypub/misc/ld-signature';
|
2021-10-16 17:16:24 +09:00
|
|
|
import { StatusError } from '@/misc/fetch';
|
2018-04-04 23:12:35 +09:00
|
|
|
|
2019-02-03 18:16:57 +09:00
|
|
|
const logger = new Logger('inbox');
|
2018-04-06 22:40:06 +09:00
|
|
|
|
2018-04-04 23:12:35 +09:00
|
|
|
// ユーザーのinboxにアクティビティが届いた時の処理
|
2020-05-09 08:21:42 +09:00
|
|
|
export default async (job: Bull.Job<InboxJobData>): Promise<string> => {
|
|
|
|
const signature = job.data.signature; // HTTP-signature
|
2018-04-04 23:12:35 +09:00
|
|
|
const activity = job.data.activity;
|
|
|
|
|
2018-04-06 22:40:06 +09:00
|
|
|
//#region Log
|
2021-05-08 18:56:21 +09:00
|
|
|
const info = Object.assign({}, activity) as any;
|
2018-04-06 22:40:06 +09:00
|
|
|
delete info['@context'];
|
2019-02-05 14:04:40 +09:00
|
|
|
logger.debug(JSON.stringify(info, null, 2));
|
2018-04-06 22:40:06 +09:00
|
|
|
//#endregion
|
|
|
|
|
2019-04-18 00:57:06 +09:00
|
|
|
const host = toPuny(new URL(signature.keyId).hostname);
|
2019-02-08 04:26:43 +09:00
|
|
|
|
2019-04-18 00:57:06 +09:00
|
|
|
// ブロックしてたら中断
|
|
|
|
const meta = await fetchMeta();
|
|
|
|
if (meta.blockedHosts.includes(host)) {
|
2020-05-09 08:21:42 +09:00
|
|
|
return `Blocked request: ${host}`;
|
2019-04-18 00:57:06 +09:00
|
|
|
}
|
2019-04-18 00:53:00 +09:00
|
|
|
|
2020-05-09 08:21:42 +09:00
|
|
|
const keyIdLower = signature.keyId.toLowerCase();
|
|
|
|
if (keyIdLower.startsWith('acct:')) {
|
|
|
|
return `Old keyId is no longer supported. ${keyIdLower}`;
|
|
|
|
}
|
2019-04-07 21:50:36 +09:00
|
|
|
|
2021-03-18 11:03:38 +09:00
|
|
|
// TDOO: キャッシュ
|
2020-05-09 08:21:42 +09:00
|
|
|
const dbResolver = new DbResolver();
|
|
|
|
|
|
|
|
// HTTP-Signature keyIdを元にDBから取得
|
|
|
|
let authUser = await dbResolver.getAuthUserFromKeyId(signature.keyId);
|
2019-04-18 00:57:06 +09:00
|
|
|
|
2020-05-09 08:21:42 +09:00
|
|
|
// keyIdでわからなければ、activity.actorを元にDBから取得 || activity.actorを元にリモートから取得
|
|
|
|
if (authUser == null) {
|
2020-07-17 22:47:22 +09:00
|
|
|
try {
|
|
|
|
authUser = await dbResolver.getAuthUserFromApId(getApId(activity.actor));
|
|
|
|
} catch (e) {
|
|
|
|
// 対象が4xxならスキップ
|
2022-02-04 11:10:53 +09:00
|
|
|
if (e instanceof StatusError) {
|
|
|
|
if (e.isClientError) {
|
|
|
|
return `skip: Ignored deleted actors on both ends ${activity.actor} - ${e.statusCode}`;
|
|
|
|
}
|
|
|
|
throw `Error in actor ${activity.actor} - ${e.statusCode || e}`;
|
2020-07-17 22:47:22 +09:00
|
|
|
}
|
|
|
|
}
|
2018-09-01 17:53:38 +09:00
|
|
|
}
|
2018-04-04 23:12:35 +09:00
|
|
|
|
2020-05-09 08:21:42 +09:00
|
|
|
// それでもわからなければ終了
|
|
|
|
if (authUser == null) {
|
|
|
|
return `skip: failed to resolve user`;
|
|
|
|
}
|
|
|
|
|
2021-07-10 23:14:57 +09:00
|
|
|
// publicKey がなくても終了
|
|
|
|
if (authUser.key == null) {
|
|
|
|
return `skip: failed to resolve user publicKey`;
|
|
|
|
}
|
|
|
|
|
2020-05-09 08:21:42 +09:00
|
|
|
// HTTP-Signatureの検証
|
2020-05-10 18:42:31 +09:00
|
|
|
const httpSignatureValidated = httpSignature.verifySignature(signature, authUser.key.keyPem);
|
2020-05-09 08:21:42 +09:00
|
|
|
|
2020-05-10 18:42:31 +09:00
|
|
|
// また、signatureのsignerは、activity.actorと一致する必要がある
|
|
|
|
if (!httpSignatureValidated || authUser.user.uri !== activity.actor) {
|
2020-05-09 08:21:42 +09:00
|
|
|
// 一致しなくても、でもLD-Signatureがありそうならそっちも見る
|
|
|
|
if (activity.signature) {
|
|
|
|
if (activity.signature.type !== 'RsaSignature2017') {
|
|
|
|
return `skip: unsupported LD-signature type ${activity.signature.type}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
// activity.signature.creator: https://example.oom/users/user#main-key
|
|
|
|
// みたいになっててUserを引っ張れば公開キーも入ることを期待する
|
|
|
|
if (activity.signature.creator) {
|
|
|
|
const candicate = activity.signature.creator.replace(/#.*/, '');
|
|
|
|
await resolvePerson(candicate).catch(() => null);
|
|
|
|
}
|
|
|
|
|
|
|
|
// keyIdからLD-Signatureのユーザーを取得
|
|
|
|
authUser = await dbResolver.getAuthUserFromKeyId(activity.signature.creator);
|
|
|
|
if (authUser == null) {
|
|
|
|
return `skip: LD-Signatureのユーザーが取得できませんでした`;
|
|
|
|
}
|
|
|
|
|
2021-07-10 23:14:57 +09:00
|
|
|
if (authUser.key == null) {
|
|
|
|
return `skip: LD-SignatureのユーザーはpublicKeyを持っていませんでした`;
|
|
|
|
}
|
|
|
|
|
2020-05-09 08:21:42 +09:00
|
|
|
// LD-Signature検証
|
|
|
|
const ldSignature = new LdSignature();
|
|
|
|
const verified = await ldSignature.verifyRsaSignature2017(activity, authUser.key.keyPem).catch(() => false);
|
|
|
|
if (!verified) {
|
|
|
|
return `skip: LD-Signatureの検証に失敗しました`;
|
|
|
|
}
|
|
|
|
|
|
|
|
// もう一度actorチェック
|
|
|
|
if (authUser.user.uri !== activity.actor) {
|
|
|
|
return `skip: LD-Signature user(${authUser.user.uri}) !== activity.actor(${activity.actor})`;
|
2018-09-01 17:53:38 +09:00
|
|
|
}
|
2020-05-15 20:50:28 +09:00
|
|
|
|
|
|
|
// ブロックしてたら中断
|
|
|
|
const ldHost = extractDbHost(authUser.user.uri);
|
|
|
|
if (meta.blockedHosts.includes(ldHost)) {
|
|
|
|
return `Blocked request: ${ldHost}`;
|
|
|
|
}
|
2020-05-09 17:08:54 +09:00
|
|
|
} else {
|
2020-06-03 09:12:54 +09:00
|
|
|
return `skip: http-signature verification failed and no LD-Signature. keyId=${signature.keyId}`;
|
2018-04-04 23:12:35 +09:00
|
|
|
}
|
2018-09-01 17:53:38 +09:00
|
|
|
}
|
|
|
|
|
2020-05-09 08:21:42 +09:00
|
|
|
// activity.idがあればホストが署名者のホストであることを確認する
|
|
|
|
if (typeof activity.id === 'string') {
|
|
|
|
const signerHost = extractDbHost(authUser.user.uri!);
|
|
|
|
const activityIdHost = extractDbHost(activity.id);
|
|
|
|
if (signerHost !== activityIdHost) {
|
|
|
|
return `skip: signerHost(${signerHost}) !== activity.id host(${activityIdHost}`;
|
|
|
|
}
|
2018-04-04 23:12:35 +09:00
|
|
|
}
|
2018-11-16 17:04:28 +09:00
|
|
|
|
2019-02-07 16:05:29 +09:00
|
|
|
// Update stats
|
2020-05-09 08:21:42 +09:00
|
|
|
registerOrFetchInstanceDoc(authUser.user.host).then(i => {
|
2019-04-07 21:50:36 +09:00
|
|
|
Instances.update(i.id, {
|
|
|
|
latestRequestReceivedAt: new Date(),
|
|
|
|
lastCommunicatedAt: new Date(),
|
2021-12-09 23:58:30 +09:00
|
|
|
isNotResponding: false,
|
2019-02-07 16:05:29 +09:00
|
|
|
});
|
2019-02-08 16:58:57 +09:00
|
|
|
|
2020-07-26 11:04:07 +09:00
|
|
|
fetchInstanceMetadata(i);
|
2019-11-05 22:14:42 +09:00
|
|
|
|
2019-02-08 16:58:57 +09:00
|
|
|
instanceChart.requestReceived(i.host);
|
2022-02-06 00:13:52 +09:00
|
|
|
apRequestChart.inbox();
|
|
|
|
federationChart.inbox(i.host);
|
2019-02-07 16:05:29 +09:00
|
|
|
});
|
|
|
|
|
2018-04-04 23:12:35 +09:00
|
|
|
// アクティビティを処理
|
2020-05-09 08:21:42 +09:00
|
|
|
await perform(authUser.user, activity);
|
|
|
|
return `ok`;
|
2018-04-04 23:12:35 +09:00
|
|
|
};
|