fix: recover BPM browsers and harden redaction and Windows supervision

This commit is contained in:
abaishu0730 committed 2026-10-19 08:56:10 +08:00
1 parent acba28c879
commit db98d78f6b
14 files changed
+223 -59

No files matched your search

+44
View File
@@ -0,0 +1,44 @@
'use strict';
const sensitive=/^(?:password|passwd|apikey|accesstoken|refreshtoken|clientsecret|secret|sessioncookie|token|cookie|authorization|密码)$/i;
const isSensitive=key=>sensitive.test(key.replace(/[_\s-]/g,''));
function sanitize(value){
if(Array.isArray(value))return value.map(sanitize);
if(value&&typeof value==='object')return Object.fromEntries(Object.entries(value).map(([key,item])=>[key,isSensitive(key)?'[已隐藏]':sanitize(item)]));
return typeof value==='string'?redactText(jsonSegments(value)):value;
}
// Parse balanced JSON fragments as well as complete documents. Quoted log payloads
// are decoded before traversal, so escaped JSON has the same policy as plain JSON.
function jsonSegments(source){
let output='',cursor=0;
for(let start=0;start<source.length;start++){
if(!['{','[','"'].includes(source[start]))continue;
const stack=[],quoted=source[start]==='"';let inString=false,escaped=false,end=-1;
for(let at=start;at<source.length;at++){
const char=source[at];
if(inString){if(escaped)escaped=false;else if(char==='\\')escaped=true;else if(char==='"'){inString=false;if(quoted){end=at+1;break;}}continue;}
if(char==='"'){inString=true;continue;}
if(char==='{'||char==='[')stack.push(char);
else if(char==='}'||char===']'){if(stack.pop()!==(char==='}'?'{':'['))break;if(!stack.length){end=at+1;break;}}
}
if(end<0)continue;
const fragment=source.slice(start,end);let parsed;
try{parsed=JSON.parse(fragment);}catch{continue;}
const clean=sanitize(parsed),replacement=JSON.stringify(clean);
output+=source.slice(cursor,start)+(JSON.stringify(parsed)===replacement?fragment:replacement);cursor=end;start=end-1;
}
return output+source.slice(cursor);
}
function redact(value,maxLength=2000){
return redactText(jsonSegments(String(value??''))).slice(0,maxLength);
}
function redactText(value){
return value
.replace(/("(?:password|passwd|api[_-]?key|(?:access|refresh)[_-]?token|client[_-]?secret|secret|session[_-]?cookie|token|cookie|authorization|密码)"\s*:\s*)("(?:\\.|[^"\\])*"|[^,}\]\r\n]+)/gi,'$1"[已隐藏]"')
.replace(/\b(Cookie|Authorization)\s*:\s*[^\r\n]+/gi,'$1: [已隐藏]')
.replace(/\bBearer\s+[^\s,;]+/gi,'Bearer [已隐藏]')
.replace(/((?:password|passwd|api[_-]?key|(?:access|refresh)[_-]?token|client[_-]?secret|secret|session[_-]?cookie|token|cookie|authorization|密码)\s*[=:]\s*)([^\r\n,]+)/gi,'$1[已隐藏]')
.replace(/((?:password|passwd|api[_-]?key|(?:access|refresh)[_-]?token|client[_-]?secret|secret|session[_-]?cookie|token|密码)\s*:\s*)([^\s,;]+)/gi,'$1[已隐藏]')
.replace(/\b[A-Za-z]:[\\/][^\r\n"'<>]*/g,'[本机路径已隐藏]')
.replace(/\\\\[^\r\n"'<>]*/g,'[共享路径已隐藏]');
}
module.exports={redact};