Skip to content

Commit

Permalink
enhance(backend): headタグ内にrel=alternateの指定のあるlinkタグがある場合、記述されたURLを参照し…
Browse files Browse the repository at this point in the history
…て照会できるように (misskey-dev#14371)

cherry picked from commit 9fbc1b7

Co-authored-by: syuilo <[email protected]>
Co-authored-by: taichan <[email protected]>
  • Loading branch information
2 people authored and u1-liquid committed Dec 24, 2024
1 parent c909c00 commit edf94b5
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions packages/backend/src/core/activitypub/ApRequestService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import * as crypto from 'node:crypto';
import { URL } from 'node:url';
import { Inject, Injectable } from '@nestjs/common';
import { JSDOM } from 'jsdom';
import { DI } from '@/di-symbols.js';
import type { Config } from '@/config.js';
import type { MiUser } from '@/models/User.js';
Expand Down Expand Up @@ -179,7 +180,8 @@ export class ApRequestService {
* @param url URL to fetch
*/
@bindThis
public async signedGet(url: string, user: { id: MiUser['id'] }): Promise<unknown> {
public async signedGet(url: string, user: { id: MiUser['id'] }, followAlternate?: boolean): Promise<unknown> {
const _followAlternate = followAlternate ?? true;
const keypair = await this.userKeypairService.getUserKeypair(user.id);

const req = ApRequestCreator.createSignedGet({
Expand All @@ -197,9 +199,25 @@ export class ApRequestService {
headers: req.request.headers,
}, {
throwErrorWhenResponseNotOk: true,
validators: [validateContentTypeSetAsActivityPub],
});

//#region リクエスト先がhtmlかつactivity+jsonへのalternate linkタグがあるとき
if (res.headers.get('Content-type')?.startsWith('text/html;') && _followAlternate) {
const html = await res.text();
const fragment = JSDOM.fragment(html);

const alternate = fragment.querySelector('head > link[rel="alternate"][type="application/activity+json"]');
if (alternate) {
const href = alternate.getAttribute('href');
if (href) {
return await this.signedGet(href, user, false);
}
}
}
//#endregion

validateContentTypeSetAsActivityPub(res);

return await res.json();
}
}

0 comments on commit edf94b5

Please sign in to comment.