CSP 허용 후보 표시 추가

This commit is contained in:
2026-07-29 16:12:56 +09:00
parent c4493bb7b2
commit 9113711a8a
13 changed files with 215 additions and 17 deletions

View File

@@ -1,4 +1,5 @@
import { getPostgresClient } from './postgres-client.js'
import { createCspAllowlistCandidates } from '../../lib/csp-policy-suggestions.js'
/**
* CSP 위반을 원본 없이 정규화된 차원별로 집계한다.
@@ -70,7 +71,7 @@ const normalizeCspViolationRow = (row) => {
/**
* 관리자 CSP 관찰용 최근 위반 집계 요약을 조회한다.
* @param {{ days?: number, limit?: number }} [options] - 조회 옵션
* @returns {Promise<{ days: number, totalViolations: number, totalRows: number, recentViolations: number, latestSeenAt: string | null, topViolations: Array<Object> }>} CSP 위반 요약
* @returns {Promise<{ days: number, totalViolations: number, totalRows: number, recentViolations: number, latestSeenAt: string | null, topViolations: Array<Object>, allowlistCandidates: Array<Object> }>} CSP 위반 요약
*/
export const getCspViolationSummary = async (options = {}) => {
const sql = getPostgresClient()
@@ -84,7 +85,8 @@ export const getCspViolationSummary = async (options = {}) => {
totalRows: 0,
recentViolations: 0,
latestSeenAt: null,
topViolations: []
topViolations: [],
allowlistCandidates: []
}
}
@@ -97,7 +99,7 @@ export const getCspViolationSummary = async (options = {}) => {
FROM csp_violation_aggregates
`
const topRows = await sql`
const recentRows = await sql`
SELECT
policy_version,
document_path,
@@ -110,8 +112,9 @@ export const getCspViolationSummary = async (options = {}) => {
FROM csp_violation_aggregates
WHERE last_seen_at >= now() - (${days}::int * interval '1 day')
ORDER BY violation_count DESC, last_seen_at DESC
LIMIT ${limit}
LIMIT 200
`
const topViolations = recentRows.slice(0, limit).map(normalizeCspViolationRow)
return {
days,
@@ -119,6 +122,10 @@ export const getCspViolationSummary = async (options = {}) => {
totalRows: Number(summary?.total_rows || 0),
recentViolations: Number(summary?.recent_violations || 0),
latestSeenAt: summary?.latest_seen_at || null,
topViolations: topRows.map(normalizeCspViolationRow)
topViolations,
allowlistCandidates: createCspAllowlistCandidates(
recentRows.map(normalizeCspViolationRow),
{ limit: 6 }
)
}
}