Files
SupplierSystem/migrations/20260924_redact_operation_logs_secrets.sql
Ubuntu deb3e72750
CI / server typecheck + tests (push) Canceled after 0s
CI / web typecheck + tests + miniprogram syntax (push) Canceled after 0s
CI / eslint (观察项,C7 骨架) (push) Canceled after 0s
CI / npm audit (critical 硬拦) (push) Canceled after 0s
chore: 以 AI 生鲜配送系统 当前版本作为 SupplierSystem 起点
来源仓库:jxc/ai-fresh-delivery
来源提交:f0b1a180e2deb7e4673b9d1c8542619f7bc0f73d
快照树:  09f679334ff38f0ff25e907961c94a8223bcaedd(与来源 HEAD 工作树逐字节一致,3801 个文件)

按约定本仓库不带任何历史提交:来源仓库的历史中含真实凭据
(生产 SSH 私钥 deploy/id_rsa_tencent、server/.env、.env.tencent/.env.production/.env.gitea、
deploy/小程序AppSecret.txt,其中 MP_APPSECRET / S3 AccessKey 经哈希比对与线上同值),
整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。
供应商系统后续开发自本提交开始。
2026-10-08 17:35:43 +08:00

81 lines
5.8 KiB
SQL
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
-- 审计日志存量明文密钥脱敏(H1,2026-09-24):
-- operation_logs 含 apiKeys 的行共 52 行,其中**8 行是真密钥**:
-- 5 行 sk-…(2× DeepSeek / 3× TokenHub)+ 2 行 apikey-… + 1 行 ark-…(Volcengine/Ark 形态)。
-- 后 3 行报告的 sk- 扫描没覆盖,2026-09-24 自查(大小写不敏感 + "敏感键有非空未打码值")才发现。
-- 本迁移把命中密钥形态的内容替换为 ***,**只 UPDATE、不 DELETE、不改表结构**。
--
-- 设计:docs/design/20260924-audit-redaction-hardening.md §5
-- 平台冻结:operation_logs 只允许"按保留期删除",本迁移不删任何行。
--
-- ⚠️ 执行顺序:**先轮换密钥,再跑本迁移**(脱敏不能挽回已泄露的值)。
-- ⚠️ 回滚:内容替换不可逆 → 迁移前 pg_dump -t operation_logs 备份可核对;断言失败即中止(fail-closed)。
-- 幂等:按"命中才更新"构造,重复执行第二次为 0 行。
--
-- 实现注意(自查踩过的坑,全部经 Postgres 实测修正):
-- ① 匹配必须大小写不敏感(~* / gi)——字段名实际是 apiKeys(大写 K);
-- ② 替换链不写进 format() 模板(模板里 % 要写成 %%,且手工数括号必错)→ 由脚本生成嵌套
-- regexp_replace(括号配平 + 交替组已断言),再用 %s 注入;
-- ③ 键名规则的**交替组必须单独括起**:("(A|B|C)"\s*:\s*)"[^"]*";
-- 写成 ("A|B|C"\s*:\s*) 时 \s*:\s* 只绑最后一个分支 → 开引号被吃 → 非法 JSON(实测)。
DO $$
DECLARE
sch text;
updated_rows int;
hit_before int;
hit_after int;
key_pattern CONSTANT text := $kp$(api_?keys?|apiKey|apiKeys|private_?key|privateKey|access_?key|accessKey|accessKeyId|llm_?key|secret|secretKey|appSecret|client_?secret|token|authToken|sessionToken|refresh_?token|idToken|authorization|cookie|signature|credential|credentials|passwd|password)"\s*:\s*"[^"*]$kp$;
content_pattern CONSTANT text := $cp$(sk-|apikey-|ark-)[A-Za-z0-9_-]{16,}|Bearer\s+[A-Za-z0-9._~+/-]{20,}=*|AKIA[0-9A-Z]{16}|eyJ[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{5,}|postgres(ql)?://[^"\s]+:[^"@\s]+@|(q-)?signature=[A-Za-z0-9%._-]{16,}|-----BEGIN [A-Z ]*PRIVATE KEY-----$cp$;
new_expr CONSTANT text := $new$regexp_replace(regexp_replace(regexp_replace(regexp_replace(regexp_replace(regexp_replace(regexp_replace(regexp_replace(t.new_value::text, '("(api_?keys?|apiKey|apiKeys|private_?key|privateKey|access_?key|accessKey|accessKeyId|llm_?key|secret|secretKey|appSecret|client_?secret|token|authToken|sessionToken|refresh_?token|idToken|authorization|cookie|signature|credential|credentials|passwd|password)"\s*:\s*)"[^"]*"', '\1"***"', 'gi'), '(sk-|apikey-|ark-)[A-Za-z0-9_-]{16,}', '***', 'gi'), 'AKIA[0-9A-Z]{16}', '***', 'g'), 'Bearer\s+[A-Za-z0-9._~+/-]{20,}=*', 'Bearer ***', 'g'), 'eyJ[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{5,}', '***', 'g'), 'postgres(ql)?://[^"\s]+:[^"@\s]+@', 'postgresql://***:***@', 'gi'), '(q-)?signature=[A-Za-z0-9%._-]{16,}', 'signature=***', 'gi'), '-----BEGIN [A-Z ]*PRIVATE KEY-----', '-----BEGIN PRIVATE KEY [REDACTED]-----', 'g')::jsonb$new$;
old_expr CONSTANT text := $old$regexp_replace(regexp_replace(regexp_replace(regexp_replace(regexp_replace(regexp_replace(regexp_replace(regexp_replace(t.old_value::text, '("(api_?keys?|apiKey|apiKeys|private_?key|privateKey|access_?key|accessKey|accessKeyId|llm_?key|secret|secretKey|appSecret|client_?secret|token|authToken|sessionToken|refresh_?token|idToken|authorization|cookie|signature|credential|credentials|passwd|password)"\s*:\s*)"[^"]*"', '\1"***"', 'gi'), '(sk-|apikey-|ark-)[A-Za-z0-9_-]{16,}', '***', 'gi'), 'AKIA[0-9A-Z]{16}', '***', 'g'), 'Bearer\s+[A-Za-z0-9._~+/-]{20,}=*', 'Bearer ***', 'g'), 'eyJ[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{5,}', '***', 'g'), 'postgres(ql)?://[^"\s]+:[^"@\s]+@', 'postgresql://***:***@', 'gi'), '(q-)?signature=[A-Za-z0-9%._-]{16,}', 'signature=***', 'gi'), '-----BEGIN [A-Z ]*PRIVATE KEY-----', '-----BEGIN PRIVATE KEY [REDACTED]-----', 'g')::jsonb$old$;
BEGIN
FOR sch IN
SELECT nspname FROM pg_namespace
WHERE nspname = 'public' OR nspname = 'legacy_public' OR nspname = 'tenant_template' OR nspname LIKE 'tenant_%'
ORDER BY 1
LOOP
IF NOT EXISTS (
SELECT 1 FROM information_schema.tables
WHERE table_schema = sch AND table_name = 'operation_logs'
) THEN
CONTINUE;
END IF;
EXECUTE format(
'SELECT count(*) FROM %I.operation_logs
WHERE new_value::text ~* %L OR old_value::text ~* %L
OR new_value::text ~* %L OR old_value::text ~* %L',
sch, key_pattern, key_pattern, content_pattern, content_pattern
) INTO hit_before;
IF hit_before = 0 THEN
RAISE NOTICE '[%] 无需脱敏(0 行命中)', sch;
CONTINUE;
END IF;
EXECUTE format(
'UPDATE %I.operation_logs AS t SET
new_value = CASE WHEN t.new_value IS NULL THEN NULL ELSE %s END,
old_value = CASE WHEN t.old_value IS NULL THEN NULL ELSE %s END
WHERE new_value::text ~* %L OR old_value::text ~* %L
OR new_value::text ~* %L OR old_value::text ~* %L',
sch, new_expr, old_expr, key_pattern, key_pattern, content_pattern, content_pattern
);
GET DIAGNOSTICS updated_rows = ROW_COUNT;
EXECUTE format(
'SELECT count(*) FROM %I.operation_logs
WHERE new_value::text ~* %L OR old_value::text ~* %L
OR new_value::text ~* %L OR old_value::text ~* %L',
sch, key_pattern, key_pattern, content_pattern, content_pattern
) INTO hit_after;
IF hit_after > 0 THEN
RAISE EXCEPTION '[%] 脱敏后仍有 % 行命中密钥形态,已中止(请人工核对)', sch, hit_after;
END IF;
RAISE NOTICE '[%] 审计日志脱敏完成:更新 % 行(迁移前命中 %)', sch, updated_rows, hit_before;
END LOOP;
END $$;