fix: 공개 API와 토너먼트의 사용자·장수 권한 경계를 강화한다

This commit is contained in:
2026-10-10 01:56:31 +09:00
parent 5771701007
commit 4e668b9fb6
31 changed files with 889 additions and 238 deletions
+3 -1
View File
@@ -1,6 +1,6 @@
import { readFileSync } from 'node:fs';
import path from 'node:path';
import { parseBooleanWithFallback, parseNumberWithFallback } from '@sammo-ts/common';
import { parseBooleanWithFallback, parseNumberWithFallback, resolveApiAllowedOrigins } from '@sammo-ts/common';
import { resolveFrontendServeMode, type FrontendServeMode } from './orchestrator/frontendArtifactManager.js';
export interface GatewayApiConfig {
@@ -19,6 +19,7 @@ export interface GatewayApiConfig {
kakaoAdminKey?: string;
kakaoRedirectUri: string;
publicBaseUrl: string;
corsAllowedOrigins: string[];
userIconDir: string;
userIconPublicUrl: string;
imageUploadBaseUrl: string;
@@ -129,6 +130,7 @@ export const resolveGatewayApiConfigFromEnv = (env: NodeJS.ProcessEnv = process.
kakaoAdminKey: env.KAKAO_ADMIN_KEY,
kakaoRedirectUri,
publicBaseUrl,
corsAllowedOrigins: resolveApiAllowedOrigins(env.API_ALLOWED_ORIGINS, [publicBaseUrl]),
userIconDir: env.GATEWAY_USER_ICON_DIR ?? 'uploads/user-icons',
userIconPublicUrl: env.GATEWAY_USER_ICON_PUBLIC_URL ?? 'https://sam-image.hided.net/icons',
imageUploadBaseUrl: env.GATEWAY_IMAGE_UPLOAD_URL ?? 'https://sam-image.hided.net',
+3 -2
View File
@@ -1,4 +1,4 @@
import { safeHttpLoggerOptions, safeHttpErrorHandler } from '@sammo-ts/common';
import { safeHttpLoggerOptions, safeHttpErrorHandler, createApiOriginGuard } from '@sammo-ts/common';
import fastify, { type FastifyRequest } from 'fastify';
import cors from '@fastify/cors';
import fastifyStatic from '@fastify/static';
@@ -125,8 +125,9 @@ export const createGatewayApiServer = async () => {
(error) => app.log.error({ err: error }, 'web push delivery failed')
);
app.addHook('onRequest', createApiOriginGuard(config.corsAllowedOrigins));
await app.register(cors, {
origin: true,
origin: config.corsAllowedOrigins,
credentials: true,
});
await fs.mkdir(path.resolve(process.cwd(), config.userIconDir), { recursive: true });
@@ -1,10 +1,11 @@
import { z } from 'zod';
import { decryptGameSessionToken } from '@sammo-ts/common/auth/gameToken';
import fastify, { type FastifyRequest } from 'fastify';
import cors from '@fastify/cors';
import { fastifyTRPCPlugin } from '@trpc/server/adapters/fastify';
import { afterEach, describe, expect, it } from 'vitest';
import { trpcJsonBodyHttpServerOptions } from '@sammo-ts/common';
import { trpcJsonBodyHttpServerOptions, createApiOriginGuard, resolveApiAllowedOrigins } from '@sammo-ts/common';
import type { GatewayPrismaClient } from '@sammo-ts/infra';
import { InMemoryGatewaySessionService } from '../src/auth/inMemorySessionService.js';
@@ -93,6 +94,9 @@ const createHarness = async (adminRoles = ['user', 'admin.users.manage', 'admin.
const targetSession = await sessions.createSession(target);
const flushes: Array<{ userId: string; reason?: string }> = [];
const app = fastify({ logger: false });
const origins = resolveApiAllowedOrigins(undefined, ['https://sam.hided.net/gateway/']);
app.addHook('onRequest', createApiOriginGuard(origins));
await app.register(cors, { origin: origins, credentials: true });
await app.register(fastifyTRPCPlugin, {
prefix: '/trpc',
trpcOptions: {
@@ -176,6 +180,29 @@ const postTrpc = async (
};
describe('admin security over HTTP transport', () => {
it('rejects an untrusted browser origin before a valid session can log out', async () => {
const harness = await createHarness();
for (const origin of ['https://untrusted.example', 'null']) {
const response = await fetch(`${harness.baseUrl}/trpc/auth.logout`, {
method: 'POST',
headers: { 'content-type': 'application/json', 'x-session-token': harness.adminSessionToken, origin },
body: JSON.stringify({ sessionToken: harness.adminSessionToken }),
});
expect(response.status).toBe(403);
expect(response.headers.get('access-control-allow-origin')).toBeNull();
expect((await postTrpc(harness.baseUrl, 'me', null, harness.adminSessionToken)).response.status).toBe(200);
}
const allowed = await fetch(`${harness.baseUrl}/trpc/auth.logout`, {
method: 'OPTIONS',
headers: {
origin: 'https://sam.hided.net',
'access-control-request-method': 'POST',
'access-control-request-headers': 'x-session-token,content-type',
},
});
expect(allowed.status).toBe(204);
expect(allowed.headers.get('access-control-allow-origin')).toBe('https://sam.hided.net');
});
it('protects runtime diagnostics at the HTTP authentication and profile scope boundaries', async () => {
const harness = await createHarness(['admin.profiles.runtime:che:default']);
const input = { profileName: 'che:default' };