[보안/인증] - 모든 player/queue API 라우트에 세션 가드 추가 (이전: /api/servers 만 보호) - NextAuth 환경변수 부팅 시점 검증, NEXTAUTH_SECRET 명시 - next.config.ts CSP/보안 헤더 추가, 잘못된 allowedDevOrigins 제거 - Redis 호스트 하드코딩 IP 제거(필수 env 로 강제) [안정성] - 봇 RPC 패턴(@/lib/api) 공용화: crypto.randomUUID requestId, JSON.parse 안전, EXPIRE 자동, 폴링 백오프 - SSE(@/lib/sse) 공용화: subscriber error 처리, JSON.parse 안전, 30초 keep-alive, abort/에러 정리 - pause API 양 끝(boolean) 정상화: 프론트 String() 캐스트 + 백엔드 .trim().toLowerCase() 비교 제거 - 봇 RedisClient: isPaused/index/seek/volume falsy 거부 → typeof 검사로 교체(0/false 정상 허용) [타입/품질] - next-auth 모듈 보강 → session.user.id, session.accessToken 타입 안전 - DiscordServer/Track/SearchTrack 공용 타입 도입, 컴포넌트 any 제거 - BigInt permissions 안전 검증(타입 가드) - Logger: NODE_ENV 게이트, error → stderr, ISO 기반 안전 timestamp - tsconfig target → ES2020 (BigInt 리터럴) [취약점] - next 16.2.2 → 16.2.4 (DoS/postcss XSS 패치) Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
59 lines
2.2 KiB
TypeScript
59 lines
2.2 KiB
TypeScript
import { NextResponse } from "next/server";
|
|
import { Redis } from "@/lib/Redis";
|
|
import { Logger } from "@/lib/Logger";
|
|
import { requireSession } from "@/lib/api";
|
|
|
|
interface DiscordGuild {
|
|
id: string;
|
|
name: string;
|
|
icon: string | null;
|
|
owner: boolean;
|
|
permissions: string;
|
|
}
|
|
|
|
export async function GET() {
|
|
const sessionResult = await requireSession();
|
|
if (!sessionResult.ok) return sessionResult.response;
|
|
|
|
const accessToken = sessionResult.session.accessToken;
|
|
if (!accessToken) {
|
|
return NextResponse.json({ success: false, error: "Discord 액세스 토큰이 없습니다." }, { status: 401 });
|
|
}
|
|
|
|
try {
|
|
// 1. 디스코드 API에서 유저가 속한 서버 목록 가져오기
|
|
const userGuildsRes = await fetch("https://discord.com/api/users/@me/guilds", {
|
|
headers: { Authorization: `Bearer ${accessToken}` },
|
|
});
|
|
if (!userGuildsRes.ok) {
|
|
Logger.warn(`Discord guilds API ${userGuildsRes.status} ${userGuildsRes.statusText}`);
|
|
return NextResponse.json(
|
|
{ success: false, error: "Discord 서버 목록을 가져오지 못했습니다." },
|
|
{ status: 502 },
|
|
);
|
|
}
|
|
const userGuildsRaw: unknown = await userGuildsRes.json();
|
|
const userGuilds: DiscordGuild[] = Array.isArray(userGuildsRaw) ? (userGuildsRaw as DiscordGuild[]) : [];
|
|
|
|
// 2. Redis에서 봇이 속한 서버 목록(화이트리스트) 가져오기
|
|
const botGuildsData = await Redis.get("bot-guilds");
|
|
let botGuildIds: string[] = [];
|
|
if (botGuildsData) {
|
|
try {
|
|
const parsed = JSON.parse(botGuildsData);
|
|
if (Array.isArray(parsed)) botGuildIds = parsed.filter((v): v is string => typeof v === "string");
|
|
} catch {
|
|
Logger.warn("Redis bot-guilds JSON 파싱 실패");
|
|
}
|
|
}
|
|
|
|
// 3. 🌟 두 목록을 비교해서 봇이 있는 서버만 필터링!
|
|
const filteredGuilds = userGuilds.filter((guild) => botGuildIds.includes(guild.id));
|
|
|
|
return NextResponse.json(filteredGuilds);
|
|
} catch (error) {
|
|
Logger.error(`서버 필터링 에러: ${error instanceof Error ? error.message : String(error)}`);
|
|
return NextResponse.json({ success: false, error: "서버 목록을 가져오지 못했습니다." }, { status: 500 });
|
|
}
|
|
}
|