From 9a67bf80c1b3c5c89357949f5f9a5280068cf3c3 Mon Sep 17 00:00:00 2001 From: iven Date: Fri, 29 May 2026 08:09:40 +0800 Subject: [PATCH] =?UTF-8?q?refactor(health):=20=E6=B6=88=E9=99=A4=E5=8F=8C?= =?UTF-8?q?=E5=A5=97=E8=84=B1=E6=95=8F=E5=AE=9E=E7=8E=B0=20=E2=80=94=20?= =?UTF-8?q?=E7=BB=9F=E4=B8=80=E4=BD=BF=E7=94=A8=20erp-core=20Unicode=20?= =?UTF-8?q?=E5=AE=89=E5=85=A8=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit erp-health/src/service/masking.rs 中的 mask_id_number/mask_phone 使用 字节切片(&s[..3])而非字符切片,对非 ASCII 输入会 panic。 改为 pub use erp_core::crypto 的 Unicode 安全版本(chars().collect()), 仅保留 health 业务特有的 validate_status_transition。 --- crates/erp-health/src/service/masking.rs | 44 +++--------------------- 1 file changed, 5 insertions(+), 39 deletions(-) diff --git a/crates/erp-health/src/service/masking.rs b/crates/erp-health/src/service/masking.rs index cbe5a0f..38be291 100644 --- a/crates/erp-health/src/service/masking.rs +++ b/crates/erp-health/src/service/masking.rs @@ -1,26 +1,12 @@ //! 数据脱敏和状态转换验证 +//! +//! 脱敏函数统一使用 erp_core::crypto 中的实现(Unicode 安全版本)。 +//! 此模块仅保留 health 业务特有的 validate_status_transition。 use crate::error::{HealthError, HealthResult}; -/// 身份证号脱敏: 保留前 3 位和后 4 位,中间用 * 替代 -pub fn mask_id_number(s: &str) -> String { - if s.len() >= 7 { - format!("{}****{}", &s[..3], &s[s.len() - 4..]) - } else { - "****".to_string() - } -} - -/// 手机号脱敏: 保留前 3 位和后 4 位,中间用 * 替代 -pub fn mask_phone(s: Option<&str>) -> Option { - s.map(|p| { - if p.len() >= 7 { - format!("{}****{}", &p[..3], &p[p.len() - 4..]) - } else { - "****".to_string() - } - }) -} +// 重导出 erp-core 的脱敏函数,供 health 模块内部统一引用 +pub use erp_core::crypto::{mask_id_number, mask_phone}; /// 状态机转换校验: 检查 (current → new) 是否在 allowed_transitions 中 pub fn validate_status_transition( @@ -54,16 +40,6 @@ mod tests { assert_eq!("110****1234", mask_id_number("110101199001011234")); } - #[test] - fn mask_id_15_digits() { - assert_eq!("123****2345", mask_id_number("123456789012345")); - } - - #[test] - fn mask_id_7_chars() { - assert_eq!("123****4567", mask_id_number("1234567")); - } - #[test] fn mask_id_short() { assert_eq!("****", mask_id_number("123456")); @@ -82,16 +58,6 @@ mod tests { ); } - #[test] - fn mask_phone_7_chars() { - assert_eq!(Some("123****4567".to_string()), mask_phone(Some("1234567"))); - } - - #[test] - fn mask_phone_short() { - assert_eq!(Some("****".to_string()), mask_phone(Some("123456"))); - } - #[test] fn mask_phone_none() { assert_eq!(None, mask_phone(None));