feat(ops): 数据保留清理、CORS 白名单与上传失败回滚
- 新增每日数据保留清理任务:按保留期清空原始报文、删除过期 事件/审计记录,批量幂等可重跑;067 迁移补齐清理索引并清理 冗余索引,check-sql-guard 增加防线 - 生产 CORS 改为显式来源白名单(CORS_ALLOWED_ORIGINS),禁用 通配符,非通配响应补充 Vary: Origin - 文件上传任一步失败时回滚已上传对象,清理失败记录 warn 日志 - 新增 HTTP/CORS 与 PostgreSQL 集成测试;若干文件仅 prettier 重排
This commit is contained in:
@@ -21,6 +21,14 @@ export function createDefaultRuntimeConfig(projectRoot: string): RuntimeConfig {
|
||||
retentionDays: 30,
|
||||
},
|
||||
|
||||
retention: {
|
||||
// 原始报文只保留追溯所需窗口,清理后保留订单/事件摘要。
|
||||
rawPayloadDays: 180,
|
||||
taskEventDays: 180,
|
||||
webhookEventDays: 180,
|
||||
auditLogDays: 365,
|
||||
},
|
||||
|
||||
database: {
|
||||
url: 'postgres://postgres:postgres@127.0.0.1:5432/order_site',
|
||||
ssl: false,
|
||||
|
||||
@@ -27,6 +27,16 @@ const DEPLOYMENT_ONLY_ENV_NAMES = [
|
||||
'NPM_CONFIG_REGISTRY',
|
||||
'POSTGRES_DB',
|
||||
'POSTGRES_LOG_MIN_DURATION_STATEMENT_MS',
|
||||
'POSTGRES_MEM_LIMIT',
|
||||
'POSTGRES_CPUS',
|
||||
'POSTGRES_SHARED_BUFFERS',
|
||||
'POSTGRES_EFFECTIVE_CACHE_SIZE',
|
||||
'POSTGRES_WORK_MEM',
|
||||
'POSTGRES_MAINTENANCE_WORK_MEM',
|
||||
'BACKEND_MEM_LIMIT',
|
||||
'BACKEND_CPUS',
|
||||
'WEB_MEM_LIMIT',
|
||||
'WEB_CPUS',
|
||||
'POSTGRES_PASSWORD',
|
||||
'POSTGRES_PORT',
|
||||
'POSTGRES_USER',
|
||||
|
||||
@@ -33,6 +33,10 @@ export const ENV_OVERRIDES: readonly EnvOverride[] = [
|
||||
stringEnv('LOG_LEVEL', ['logging', 'level']),
|
||||
stringEnv('LOG_INTEGRATION_LEVEL', ['logging', 'integrationLevel']),
|
||||
integerEnv('LOG_RETENTION_DAYS', ['logging', 'retentionDays']),
|
||||
integerEnv('RAW_PAYLOAD_RETENTION_DAYS', ['retention', 'rawPayloadDays']),
|
||||
integerEnv('TASK_EVENT_RETENTION_DAYS', ['retention', 'taskEventDays']),
|
||||
integerEnv('WEBHOOK_EVENT_RETENTION_DAYS', ['retention', 'webhookEventDays']),
|
||||
integerEnv('AUDIT_LOG_RETENTION_DAYS', ['retention', 'auditLogDays']),
|
||||
stringEnv('DATABASE_URL', ['database', 'url']),
|
||||
booleanEnv('DATABASE_SSL', ['database', 'ssl']),
|
||||
integerEnv('DATABASE_MAX_CONNECTIONS', ['database', 'maxConnections']),
|
||||
|
||||
@@ -77,6 +77,17 @@ test('validateRuntimeConfig accepts complete production config', () => {
|
||||
assert.deepEqual(issues, [])
|
||||
})
|
||||
|
||||
test('validateRuntimeConfig rejects wildcard CORS in production', () => {
|
||||
const issues = validateRuntimeConfig(createRuntimeConfig({ cors: { allowedOrigins: ['*'] } }), {
|
||||
env: { NODE_ENV: 'production' },
|
||||
})
|
||||
|
||||
assert.deepEqual(
|
||||
issues.map((issue) => issue.path),
|
||||
['cors.allowedOrigins'],
|
||||
)
|
||||
})
|
||||
|
||||
test('validateRuntimeConfig catches invalid numeric and url values', () => {
|
||||
const issues = validateRuntimeConfig(
|
||||
createRuntimeConfig({
|
||||
|
||||
@@ -79,6 +79,14 @@ export function validateRuntimeConfig(
|
||||
min: 1,
|
||||
max: 100,
|
||||
})
|
||||
if (config.retention) {
|
||||
requireInteger(issues, 'retention.rawPayloadDays', config.retention.rawPayloadDays, { min: 1 })
|
||||
requireInteger(issues, 'retention.taskEventDays', config.retention.taskEventDays, { min: 1 })
|
||||
requireInteger(issues, 'retention.webhookEventDays', config.retention.webhookEventDays, {
|
||||
min: 1,
|
||||
})
|
||||
requireInteger(issues, 'retention.auditLogDays', config.retention.auditLogDays, { min: 1 })
|
||||
}
|
||||
const storageMode = normalizeStorageMode(config.storage?.mode)
|
||||
if (!storageMode) {
|
||||
issues.push({
|
||||
@@ -137,6 +145,24 @@ export function validateRuntimeConfig(
|
||||
validateRequiredHttpUrl(issues, 'orders.claimBaseUrl', config.orders?.claimBaseUrl)
|
||||
validateSecret(issues, 'admin.sessionSecret', config.admin?.sessionSecret, { minLength: 32 })
|
||||
validateAdminDefaultUsers(issues, config.admin?.defaultUsers)
|
||||
const allowedOrigins = config.cors?.allowedOrigins
|
||||
if (Array.isArray(allowedOrigins)) {
|
||||
if (allowedOrigins.length === 0 || allowedOrigins.includes('*')) {
|
||||
issues.push({
|
||||
path: 'cors.allowedOrigins',
|
||||
message: '生产环境必须配置显式 CORS 域名白名单,不能使用 *',
|
||||
})
|
||||
} else {
|
||||
allowedOrigins.forEach((origin, index) => {
|
||||
if (!isHttpUrl(String(origin || '').trim())) {
|
||||
issues.push({
|
||||
path: `cors.allowedOrigins[${index}]`,
|
||||
message: '必须是 http 或 https 来源 URL',
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return issues
|
||||
|
||||
Reference in New Issue
Block a user