目标
递归遍历 JSON 结构,命中敏感键集合的非对象值替换为 ***,数组与嵌套对象逐层处理,原始对象绝不被修改。
要点
键名匹配是白名单驱动——域特有的敏感键应显式加入名单而非按值猜;demo 对含 password 与 apiToken 的载荷输出 ***。
提示
藏在无辜键名下的秘密不会被基于名字的规则捕获。
验证
node –test 验证嵌套数组、null 与原对象不变三个性质;CLI 脱敏示例载荷。 复现命令:
node --test mask.test.mjs
node mask.mjs payload.json
预期输出要点:Given a payload with password and apiToken fields, the CLI prints the same JSON with those values replaced by the string ***.
实测证据:Node 22.23.2: node –test -> 3 passed; CLI masked password and nested apiToken values as *** while keeping user and roles intact.