fix: 서버 전용 키가 브라우저 번들에 들어가지 않게 한다

- vite.config define 의 GEMINI_API_KEY 를 지운다
- API 대시보드의 import.meta.env 동적 조회를 공개 변수 목록으로 바꾼다. 동적 조회 때문에 VITE_ 변수 전체(VITE_CREATOMATE_API_KEY 포함)가 번들에 들어갔다
- 모든 배포 빌드 끝에 서버 전용 키 값이 번들에 있으면 빌드를 실패시킨다

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Haewon KamandClaude Opus 5.5 committed 2026-10-07 14:39:19 +09:00
1 parent 0b729759c3
commit e20a90dc35
3 files changed
+50 -12

No files matched your search

@@ -71,3 +71,35 @@ export function assertPublicBundleClean(outDir: string): void {
);
}
}
// ─── 모든 배포 공통: 서버 전용 비밀값이 번들에 들어갔는지 ─────────────────────────────
// 이름에 KEY·SECRET·TOKEN·PASSWORD·SERVICE_ROLE 이 들어간 환경변수 값을 서버 전용으로 본다.
// 공개를 전제로 만든 값만 예외로 둔다. 값 자체를 대조하므로 키 형식이 바뀌어도 잡는다.
const SECRET_NAME = /(KEY|SECRET|TOKEN|PASSWORD|SERVICE_ROLE)/;
const PUBLIC_BY_DESIGN = new Set(['VITE_SUPABASE_ANON_KEY']);
/** 위반한 환경변수 이름 목록을 돌려준다. 값은 출력하지 않는다. */
export function findSecretValueLeaks(outDir: string, env: Record<string, string>): string[] {
const secrets = Object.entries(env).filter(
([name, value]) => SECRET_NAME.test(name) && !PUBLIC_BY_DESIGN.has(name) && typeof value === 'string' && value.length >= 16,
);
if (!secrets.length) return [];
const leaks = new Set<string>();
for (const file of listFiles(outDir)) {
if (!TEXT_EXT.has(path.extname(file).toLowerCase())) continue;
const text = fs.readFileSync(path.join(outDir, file), 'utf8');
for (const [name, value] of secrets) if (text.includes(value)) leaks.add(`${name} (${file})`);
}
return [...leaks];
}
/** 서버 전용 비밀값이 번들에 있으면 빌드를 멈춘다. 모든 배포(webforai.kr·infinith-demo)에 건다. */
export function assertNoSecretValues(outDir: string, env: Record<string, string>): void {
const leaks = findSecretValueLeaks(outDir, env);
if (leaks.length) {
throw new Error(
`번들 비밀값 검사 실패 (${leaks.length}건). 서버 전용 키가 브라우저 번들에 들어갔습니다. VITE_ 접두사나 vite.config define 을 확인하세요.\n` +
leaks.map((v) => ` - ${v}`).join('\n'),
);
}
}