Skip to content

Commit

Permalink
内部のメディアプロキシ・サマリープロキシの動作を変更 (#363)
Browse files Browse the repository at this point in the history
* production環境では本体メディアプロキシを起動しない

* 本体サマリープロキシを削除

* メディアプロキシのoriginパラメータを削除

* production環境では/proxy以下へのリクエストに404を返すように

* remove summaly error test
  • Loading branch information
adzukimame authored Dec 11, 2024
1 parent d59d8a2 commit e06fd64
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 52 deletions.
2 changes: 1 addition & 1 deletion packages/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
"@fastify/static": "8.0.1",
"@fastify/view": "10.0.1",
"@misskey-dev/sharp-read-bmp": "1.2.0",
"@misskey-dev/summaly": "5.1.0",
"@nestjs/common": "10.4.4",
"@nestjs/core": "10.4.4",
"@nestjs/testing": "10.4.4",
Expand Down Expand Up @@ -141,6 +140,7 @@
},
"devDependencies": {
"@jest/globals": "29.7.0",
"@misskey-dev/summaly": "5.1.0",
"@nestjs/platform-express": "10.4.4",
"@simplewebauthn/types": "10.0.0",
"@swc/cli": "0.3.12",
Expand Down
25 changes: 14 additions & 11 deletions packages/backend/src/server/FileServerService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,19 @@ export class FileServerService {
done();
});

fastify.get<{
Params: { url: string; };
Querystring: { url?: string; };
}>('/proxy/:url*', async (request, reply) => {
return await this.proxyHandler(request, reply)
.catch(err => this.errorHandler(request, reply, err));
});
if (process.env.NODE_ENV === 'production') {
fastify.get('/proxy/*', async (request, reply) => {
return reply.status(404).send();
});
} else {
fastify.get<{
Params: { url: string; };
Querystring: { url?: string; };
}>('/proxy/:url*', async (request, reply) => {
return await this.proxyHandler(request, reply)
.catch(err => this.errorHandler(request, reply, err));
});
}

done();
}
Expand Down Expand Up @@ -298,10 +304,7 @@ export class FileServerService {
return;
}

// アバタークロップなど、どうしてもオリジンである必要がある場合
const mustOrigin = 'origin' in request.query;

if (this.config.externalMediaProxyEnabled && !mustOrigin) {
if (this.config.externalMediaProxyEnabled) {
// 外部のメディアプロキシが有効なら、そちらにリダイレクト

reply.header('Cache-Control', 'public, max-age=259200'); // 3 days
Expand Down
28 changes: 3 additions & 25 deletions packages/backend/src/server/web/UrlPreviewService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
*/

import { Inject, Injectable } from '@nestjs/common';
import { summaly } from '@misskey-dev/summaly';
import { SummalyResult } from '@misskey-dev/summaly/built/summary.js';
import { DI } from '@/di-symbols.js';
import type { Config } from '@/config.js';
import { HttpRequestService } from '@/core/HttpRequestService.js';
Expand All @@ -15,6 +13,7 @@ import { LoggerService } from '@/core/LoggerService.js';
import { bindThis } from '@/decorators.js';
import { ApiError } from '@/server/api/error.js';
import { MiMeta } from '@/models/Meta.js';
import type { SummalyResult } from '@misskey-dev/summaly/built/summary.js';
import type { FastifyRequest, FastifyReply } from 'fastify';

@Injectable()
Expand Down Expand Up @@ -63,7 +62,7 @@ export class UrlPreviewService {
return;
}

if (!this.meta.urlPreviewEnabled) {
if (!this.meta.urlPreviewEnabled || this.meta.urlPreviewSummaryProxyUrl === null) {
reply.code(403);
return {
error: new ApiError({
Expand All @@ -79,9 +78,7 @@ export class UrlPreviewService {
: `Getting preview of ${url}@${lang} ...`);

try {
const summary = this.meta.urlPreviewSummaryProxyUrl
? await this.fetchSummaryFromProxy(url, this.meta, lang)
: await this.fetchSummary(url, this.meta, lang);
const summary = await this.fetchSummaryFromProxy(url, this.meta, lang);

this.logger.succ(`Got preview of ${url}: ${summary.title}`);

Expand Down Expand Up @@ -115,25 +112,6 @@ export class UrlPreviewService {
}
}

private fetchSummary(url: string, meta: MiMeta, lang?: string): Promise<SummalyResult> {
const agent = this.config.proxy
? {
http: this.httpRequestService.httpAgent,
https: this.httpRequestService.httpsAgent,
}
: undefined;

return summaly(url, {
followRedirects: false,
lang: lang ?? 'ja-JP',
agent: agent,
userAgent: meta.urlPreviewUserAgent ?? undefined,
operationTimeout: meta.urlPreviewTimeout,
contentLengthLimit: meta.urlPreviewMaximumContentLength,
contentLengthRequired: meta.urlPreviewRequireContentLength,
});
}

private fetchSummaryFromProxy(url: string, meta: MiMeta, lang?: string): Promise<SummalyResult> {
const proxy = meta.urlPreviewSummaryProxyUrl!;
const queryStr = query({
Expand Down
8 changes: 0 additions & 8 deletions packages/backend/test/e2e/endpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1043,14 +1043,6 @@ describe('Endpoints', () => {
});
});

describe('URL preview', () => {
test('Error from summaly becomes HTTP 422', async () => {
const res = await simpleGet('/url?url=https://e:xample.com');
assert.strictEqual(res.status, 422);
assert.strictEqual(res.body.error.code, 'URL_PREVIEW_FAILED');
});
});

describe('パーソナルメモ機能のテスト', () => {
test('他者に関するメモを更新できる', async () => {
const memo = '10月まで低浮上とのこと。';
Expand Down
1 change: 0 additions & 1 deletion packages/frontend/src/components/global/MkCustomEmoji.vue
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ const url = computed(() => {
: getProxiedImageUrl(
rawUrl.value,
props.useOriginalSize ? undefined : 'emoji',
false,
true,
);
return defaultStore.reactiveState.disableShowingAnimatedImages.value && !props.forceShowingAnimatedImages
Expand Down
5 changes: 2 additions & 3 deletions packages/frontend/src/scripts/media-proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,21 @@ import { query } from '@/scripts/url.js';
import { url } from '@/config.js';
import { instance } from '@/instance.js';

export function getProxiedImageUrl(imageUrl: string, type?: 'preview' | 'emoji' | 'avatar', mustOrigin = false, noFallback = false): string {
export function getProxiedImageUrl(imageUrl: string, type?: 'preview' | 'emoji' | 'avatar', noFallback = false): string {
const localProxy = `${url}/proxy`;

if (imageUrl.startsWith(instance.mediaProxy + '/') || imageUrl.startsWith('/proxy/') || imageUrl.startsWith(localProxy + '/')) {
// もう既にproxyっぽそうだったらurlを取り出す
imageUrl = (new URL(imageUrl)).searchParams.get('url') ?? imageUrl;
}

return `${mustOrigin ? localProxy : instance.mediaProxy}/${
return `${instance.mediaProxy}/${
type === 'preview' ? 'preview.webp'
: 'image.webp'
}?${query({
url: imageUrl,
...(!noFallback ? { 'fallback': '1' } : {}),
...(type ? { [type]: '1' } : {}),
...(mustOrigin ? { origin: '1' } : {}),
})}`;
}

Expand Down
6 changes: 3 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit e06fd64

Please sign in to comment.