summaryrefslogtreecommitdiff
path: root/packages/backend/src/core/HttpRequestService.ts
diff options
context:
space:
mode:
authortamaina <tamaina@hotmail.co.jp>2024-02-17 12:41:19 +0900
committerdakkar <dakkar@thenautilus.net>2024-02-17 13:09:08 +0000
commit0a8ffd9cfa8c374b0dfb4ae3c92a416cb79360b2 (patch)
treeab4857af148a1df731610e4821a42df2ffe83d1b /packages/backend/src/core/HttpRequestService.ts
parentmerge: Fix Note Edits being federated incorrectly (!417) (diff)
downloadsharkey-0a8ffd9cfa8c374b0dfb4ae3c92a416cb79360b2.tar.gz
sharkey-0a8ffd9cfa8c374b0dfb4ae3c92a416cb79360b2.tar.bz2
sharkey-0a8ffd9cfa8c374b0dfb4ae3c92a416cb79360b2.zip
Merge pull request from GHSA-qqrm-9grj-6v32
* maybe ok * fix * test wip * :v: * fix * if (res.ok) * validateContentTypeSetAsJsonLD * 条件を考慮し直す * その他の+json接尾辞が付いているメディアタイプも受け容れる * https://github.com/misskey-dev/misskey-ghsa-qqrm-9grj-6v32/pull/1#discussion_r1490999009 * add `; profile="https://www.w3.org/ns/activitystreams"` * application/ld+json;
Diffstat (limited to 'packages/backend/src/core/HttpRequestService.ts')
-rw-r--r--packages/backend/src/core/HttpRequestService.ts55
1 files changed, 44 insertions, 11 deletions
diff --git a/packages/backend/src/core/HttpRequestService.ts b/packages/backend/src/core/HttpRequestService.ts
index 73bb3dc7e9..1352e137ce 100644
--- a/packages/backend/src/core/HttpRequestService.ts
+++ b/packages/backend/src/core/HttpRequestService.ts
@@ -14,9 +14,16 @@ import { DI } from '@/di-symbols.js';
import type { Config } from '@/config.js';
import { StatusError } from '@/misc/status-error.js';
import { bindThis } from '@/decorators.js';
+import { validateContentTypeSetAsActivityPub } from '@/core/activitypub/misc/validator.js';
+import type { IObject } from '@/core/activitypub/type.js';
import type { Response } from 'node-fetch';
import type { URL } from 'node:url';
+export type HttpRequestSendOptions = {
+ throwErrorWhenResponseNotOk: boolean;
+ validators?: ((res: Response) => void)[];
+};
+
@Injectable()
export class HttpRequestService {
/**
@@ -105,6 +112,23 @@ export class HttpRequestService {
}
@bindThis
+ public async getActivityJson(url: string): Promise<IObject> {
+ const res = await this.send(url, {
+ method: 'GET',
+ headers: {
+ Accept: 'application/activity+json, application/ld+json; profile="https://www.w3.org/ns/activitystreams"',
+ },
+ timeout: 5000,
+ size: 1024 * 256,
+ }, {
+ throwErrorWhenResponseNotOk: true,
+ validators: [validateContentTypeSetAsActivityPub],
+ });
+
+ return await res.json() as IObject;
+ }
+
+ @bindThis
public async getJson<T = unknown>(url: string, accept = 'application/json, */*', headers?: Record<string, string>): Promise<T> {
const res = await this.send(url, {
method: 'GET',
@@ -132,17 +156,20 @@ export class HttpRequestService {
}
@bindThis
- public async send(url: string, args: {
- method?: string,
- body?: string,
- headers?: Record<string, string>,
- timeout?: number,
- size?: number,
- } = {}, extra: {
- throwErrorWhenResponseNotOk: boolean;
- } = {
- throwErrorWhenResponseNotOk: true,
- }): Promise<Response> {
+ public async send(
+ url: string,
+ args: {
+ method?: string,
+ body?: string,
+ headers?: Record<string, string>,
+ timeout?: number,
+ size?: number,
+ } = {},
+ extra: HttpRequestSendOptions = {
+ throwErrorWhenResponseNotOk: true,
+ validators: [],
+ },
+ ): Promise<Response> {
const timeout = args.timeout ?? 5000;
const controller = new AbortController();
@@ -166,6 +193,12 @@ export class HttpRequestService {
throw new StatusError(`${res.status} ${res.statusText}`, res.status, res.statusText);
}
+ if (res.ok) {
+ for (const validator of (extra.validators ?? [])) {
+ validator(res);
+ }
+ }
+
return res;
}
}