61 lines
1.8 KiB
TypeScript
61 lines
1.8 KiB
TypeScript
import { generateCSPHeader } from '../config/csp';
|
|
|
|
// CSP 위반 인터페이스 정의
|
|
interface CSPViolationData {
|
|
documentURI: string;
|
|
blockedURI: string;
|
|
violatedDirective: string;
|
|
originalPolicy: string;
|
|
sourceFile?: string;
|
|
lineNumber?: number;
|
|
columnNumber?: number;
|
|
}
|
|
|
|
// CSP 헤더를 설정하는 유틸리티 함수 (img-src만)
|
|
export function setCSPHeaders(response: Response): Response {
|
|
const environment = process.env.NODE_ENV as 'development' | 'production';
|
|
const cspValue = generateCSPHeader(environment);
|
|
|
|
response.headers.set('Content-Security-Policy', cspValue);
|
|
|
|
return response;
|
|
}
|
|
|
|
// CSP 위반을 로깅하는 함수
|
|
export function logCSPViolation(violation: CSPViolationData): void {
|
|
console.warn('CSP Violation:', {
|
|
documentURI: violation.documentURI,
|
|
blockedURI: violation.blockedURI,
|
|
violatedDirective: violation.violatedDirective,
|
|
originalPolicy: violation.originalPolicy,
|
|
timestamp: new Date().toISOString()
|
|
});
|
|
}
|
|
|
|
// img-src 정책을 검증하는 함수
|
|
export function validateImgSrcPolicy(policy: string): boolean {
|
|
return policy.includes('img-src');
|
|
}
|
|
|
|
// 개발 환경에서 CSP를 비활성화하는 함수 (디버깅용)
|
|
export function disableCSPForDevelopment(): boolean {
|
|
return process.env.NODE_ENV === 'development' &&
|
|
process.env.DISABLE_CSP === 'true';
|
|
}
|
|
|
|
// img-src에 도메인 추가하는 함수
|
|
export function addDomainToImgSrc(
|
|
currentPolicy: string,
|
|
domain: string
|
|
): string {
|
|
if (currentPolicy.includes('img-src')) {
|
|
// 기존 img-src에 도메인 추가
|
|
return currentPolicy.replace(
|
|
/img-src ([^;]+)/,
|
|
`img-src $1 ${domain}`
|
|
);
|
|
} else {
|
|
// img-src가 없으면 새로 생성
|
|
return `${currentPolicy}; img-src 'self' ${domain};`;
|
|
}
|
|
}
|