109 lines
4.9 KiB
PL/PgSQL
109 lines
4.9 KiB
PL/PgSQL
-- sys_log.params 里的口令脱敏:入库前把 password 类字段的值换成 ***(本轮 P1-2)
|
||
--
|
||
-- 为什么要修在这一层(17:38 真库现采):框架的 com/unisbase/aspect/OperationLogAspect 把方法入参
|
||
-- 整个交给注入的 ObjectMapper 序列化进 sys_log.params,口令字段一点没脱敏(框架那边只有一个
|
||
-- unisbase.log.enabled 开关,javap 过 LogProperties,没有字段白名单可配)。现采数字:sys_log 共
|
||
-- 15813 行,含 password 类键的 256 行,其中值仍是明文的 64 行,全部来自「用户管理」模块 ——
|
||
-- 上一轮手工洗掉的 29 行之所以现在长出 64 行,就是因为写侧没人管:改一次口令就多一条明文。
|
||
-- 所以一次性洗数据不够,必须把"入库前替换"装到库上;框架侧的字段白名单已作为建议回报。
|
||
--
|
||
-- 口径:只替换值,键名与其余字段原样保留(形如 "oldPassword":"***"),排查时仍看得出请求带了哪几个字段;
|
||
-- 匹配键名里含 password 的(大小写不敏感,覆盖 oldPassword / newPassword / password / passwordConfirmation)。
|
||
-- params 为 null 不动,已经是 *** 的值不动。
|
||
--
|
||
-- 验收(跑完必须为 0):
|
||
-- select count(*) from sys_log
|
||
-- where params ~* '"[a-z0-9_]*password[a-z0-9_]*"[[:space:]]*:[[:space:]]*"[^*"]{3,}"';
|
||
-- 再改一次口令,新写的行必须直接是 ***(证明写侧生效,不是靠事后洗)。
|
||
--
|
||
-- 风险与退路:触发器对所有写 sys_log 的路径生效(框架与本仓都算)。它只做一次字符串替换、不查表、
|
||
-- 不改类型(params 现列类型 text,已现采确认)。真出现插入异常,退路是文件末尾那句 drop trigger,
|
||
-- 摘掉即回到今天的行为,已写入的数据不会因为装过它而失真。
|
||
--
|
||
-- 执行:psql -f sql/20261119_idp_sys_log_password_mask.sql
|
||
-- 自带 begin/commit;存量掩码不可逆(原值替换即丢),执行前不需要备份。
|
||
|
||
begin;
|
||
|
||
set search_path to public;
|
||
|
||
-- 掩码函数:写侧与存量清洗共用同一份判据,分成两份口径一定漂。
|
||
create or replace function crm_mask_password_values(p_params text)
|
||
returns text
|
||
language plpgsql
|
||
immutable
|
||
as $$
|
||
declare
|
||
v_masked text;
|
||
begin
|
||
if p_params is null or p_params !~* 'password' then
|
||
return p_params;
|
||
end if;
|
||
-- 三种写法都盖:JSON 带引号的值、JSON 不带引号的值、序列化失败退回的 toString 形状(key=value)。
|
||
-- 引号要留着:只把值换成星号、连引号一起吃掉会让 params 不再是合法 JSON,
|
||
-- 下一个想 jsonb 解析它的人会被绊住(本轮现采的第一版就是这么错的)。
|
||
v_masked := regexp_replace(p_params,
|
||
'("[A-Za-z0-9_]*password[A-Za-z0-9_]*")([[:space:]]*:[[:space:]]*)"[^"]*"',
|
||
'\1\2"***"',
|
||
'gi');
|
||
v_masked := regexp_replace(v_masked,
|
||
'("[A-Za-z0-9_]*password[A-Za-z0-9_]*")([[:space:]]*:[[:space:]]*)([^"{,}[:space:]][^,}]*)',
|
||
'\1\2***',
|
||
'gi');
|
||
return regexp_replace(v_masked,
|
||
'([A-Za-z0-9_]*password[A-Za-z0-9_]*)([[:space:]]*=[[:space:]]*)[^,}\]]+',
|
||
'\1\2***',
|
||
'gi');
|
||
end;
|
||
$$;
|
||
|
||
create or replace function crm_mask_sys_log_password()
|
||
returns trigger
|
||
language plpgsql
|
||
as $$
|
||
begin
|
||
new.params := crm_mask_password_values(new.params);
|
||
return new;
|
||
end;
|
||
$$;
|
||
|
||
do $$
|
||
declare
|
||
v_rows integer;
|
||
begin
|
||
if to_regclass('public.sys_log') is null then
|
||
raise notice 'public.sys_log 不存在,本脚本不适用(已整段跳过)';
|
||
return;
|
||
end if;
|
||
|
||
update sys_log
|
||
set params = crm_mask_password_values(params)
|
||
where params ~* '"[a-z0-9_]*password[a-z0-9_]*"[[:space:]]*:';
|
||
get diagnostics v_rows = row_count;
|
||
raise notice 'sys_log 存量口令脱敏:候选 % 行(值已是 *** 的不会被改回去)', v_rows;
|
||
|
||
drop trigger if exists crm_sys_log_mask_password on public.sys_log;
|
||
create trigger crm_sys_log_mask_password
|
||
before insert or update of params on public.sys_log
|
||
for each row execute function crm_mask_sys_log_password();
|
||
raise notice 'sys_log.params 写侧脱敏触发器已安装(crm_sys_log_mask_password)';
|
||
end $$;
|
||
|
||
-- 装完自查:触发器在不在 + 还剩多少明文。
|
||
select tgname as trigger_name, tgenabled as enabled
|
||
from pg_trigger t
|
||
join pg_class c on c.oid = t.tgrelid
|
||
where c.relname = 'sys_log' and not t.tgisinternal;
|
||
|
||
select count(*) as plaintext_password_rows
|
||
from sys_log
|
||
where params ~* '"[a-z0-9_]*password[a-z0-9_]*"[[:space:]]*:[[:space:]]*"[^*"]{3,}"';
|
||
|
||
commit;
|
||
|
||
-- 回滚:
|
||
-- drop trigger if exists crm_sys_log_mask_password on public.sys_log;
|
||
-- drop function if exists crm_mask_sys_log_password();
|
||
-- drop function if exists crm_mask_password_values(text);
|
||
-- 存量掩码不回滚(原值已丢,回滚也造不回来);摘掉触发器即恢复今天"明文照写"的行为。
|