bambu-printer-mcp 1.1.94 → 1.1.96
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CONTRIBUTORS.md +1 -0
- package/README.md +16 -4
- package/dist/cad/contracts.d.ts +10 -0
- package/dist/cad/contracts.js +32 -1
- package/dist/cad-engine.js +40 -3
- package/dist/counterpart/diagnostics.d.ts +163 -0
- package/dist/counterpart/diagnostics.js +816 -0
- package/dist/counterpart/info.js +1 -0
- package/dist/counterpart/remote.js +1 -0
- package/dist/counterpart/schemas.d.ts +6 -0
- package/dist/counterpart/schemas.js +1 -0
- package/dist/fusion-mcp-bridge.d.ts +13 -0
- package/dist/fusion-mcp-bridge.js +35 -4
- package/dist/index.js +101 -3
- package/dist/tool-annotations.js +1 -0
- package/package.json +2 -2
- package/src/cad/contracts.ts +36 -1
- package/src/cad/examples/desk_clamp_hook.py +32 -0
- package/src/cad/examples/desk_edge_cable_holder.py +24 -0
- package/src/cad/examples/phone_stand.py +26 -0
- package/src/cad/geometry.py +52 -2
- package/src/cad/purpose.py +492 -0
- package/src/cad/worker.py +11 -0
- package/src/cad-engine.ts +42 -3
- package/src/counterpart/diagnostics.ts +775 -0
- package/src/counterpart/info.ts +1 -0
- package/src/counterpart/remote.ts +1 -0
- package/src/counterpart/schemas.ts +3 -0
- package/src/fusion-mcp-bridge.ts +34 -4
- package/src/index.ts +84 -3
- package/src/tool-annotations.ts +1 -0
|
@@ -0,0 +1,775 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* counterpart_diagnostics (counterpart/1): this server's own diagnostics bundle for a tester's bug report. The client
|
|
3
|
+
* adds the files it returns to its own export and needs to know no log path here. Every byte is redacted on the way
|
|
4
|
+
* out, the tail of each source is kept, and the bundle (file names, contents and notes) never exceeds max_bytes.
|
|
5
|
+
* A missing source is listed in notes, never an error. Local only: it reads this server's own files and memory and
|
|
6
|
+
* sends nothing to a printer.
|
|
7
|
+
*
|
|
8
|
+
* This file is shared verbatim between bambu-printer-mcp and mcp-3D-printer-server; each server lists its own sources.
|
|
9
|
+
*/
|
|
10
|
+
import fs from "node:fs";
|
|
11
|
+
import os from "node:os";
|
|
12
|
+
import path from "node:path";
|
|
13
|
+
|
|
14
|
+
export const DIAGNOSTICS_DEFAULT_BYTES = 4_194_304;
|
|
15
|
+
export const DIAGNOSTICS_MAX_BYTES = 8_388_608;
|
|
16
|
+
|
|
17
|
+
export type DiagnosticsFile = { name: string; content: string; truncated: boolean };
|
|
18
|
+
export type DiagnosticsResult = { package: string; version: string; files: DiagnosticsFile[]; notes: string[] };
|
|
19
|
+
|
|
20
|
+
/** A file on disk, whose tail goes in, or text the server composes. `missing` is the note when the file is absent. */
|
|
21
|
+
export type DiagnosticsSource = { name: string; file: string; missing?: string } | { name: string; text: string };
|
|
22
|
+
|
|
23
|
+
export const DIAGNOSTICS_INPUT_PROPERTIES = {
|
|
24
|
+
printer: { type: "string", description: "Only this configured printer (its serial number or host, as this server's other tools take them). Default: every configured printer." },
|
|
25
|
+
max_bytes: { type: "integer", minimum: 1, maximum: DIAGNOSTICS_MAX_BYTES, description: `Most bytes the whole bundle may hold (default ${DIAGNOSTICS_DEFAULT_BYTES}, at most ${DIAGNOSTICS_MAX_BYTES}).` },
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
export const DIAGNOSTICS_OUTPUT_SCHEMA = {
|
|
29
|
+
type: "object" as const,
|
|
30
|
+
properties: {
|
|
31
|
+
package: { type: "string", description: "This server's npm package name." },
|
|
32
|
+
version: { type: "string", description: "This server's npm package version." },
|
|
33
|
+
files: {
|
|
34
|
+
type: "array",
|
|
35
|
+
items: {
|
|
36
|
+
type: "object",
|
|
37
|
+
properties: {
|
|
38
|
+
name: { type: "string", description: "Relative path for the file in the client's export; never absolute, never '..'." },
|
|
39
|
+
content: { type: "string", description: "UTF-8 text, redacted." },
|
|
40
|
+
truncated: { type: "boolean", description: "Only the tail of the source is here." },
|
|
41
|
+
},
|
|
42
|
+
required: ["name", "content", "truncated"],
|
|
43
|
+
},
|
|
44
|
+
},
|
|
45
|
+
notes: { type: "array", items: { type: "string" }, description: "What is missing or left out, and why." },
|
|
46
|
+
},
|
|
47
|
+
required: ["package", "version", "files", "notes"],
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
/** The max_bytes a call asks for, checked. */
|
|
51
|
+
export function diagnosticsBudget(value: unknown): number {
|
|
52
|
+
if (value === undefined) return DIAGNOSTICS_DEFAULT_BYTES;
|
|
53
|
+
if (typeof value !== "number" || !Number.isInteger(value) || value < 1 || value > DIAGNOSTICS_MAX_BYTES) {
|
|
54
|
+
throw new Error(`max_bytes must be an integer from 1 to ${DIAGNOSTICS_MAX_BYTES}.`);
|
|
55
|
+
}
|
|
56
|
+
return value;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// ---------------------------------------------------------------------------------------------------------------------
|
|
60
|
+
// What this process said and which tools failed, kept in memory for the bundle (raw; redacted only on the way out).
|
|
61
|
+
|
|
62
|
+
const LINE_LIMIT = 4000;
|
|
63
|
+
const SERVER_LOG_LINES = 2000;
|
|
64
|
+
const TOOL_ERROR_LINES = 200;
|
|
65
|
+
const serverLog: string[] = [];
|
|
66
|
+
const toolErrors: string[] = [];
|
|
67
|
+
/** Secret values this process has seen in tool arguments, so they are redacted even where nothing names them. */
|
|
68
|
+
const seenSecrets = new Set<string>();
|
|
69
|
+
const SEEN_SECRET_LIMIT = 200;
|
|
70
|
+
|
|
71
|
+
function remember(lines: string[], limit: number, line: string): void {
|
|
72
|
+
// Known secrets go before the cut, so one straddling the cut leaves no part of itself.
|
|
73
|
+
const clean = removeKnownSecrets(line);
|
|
74
|
+
lines.push(clean.length > LINE_LIMIT ? clean.slice(0, LINE_LIMIT) + " [line cut]" : clean);
|
|
75
|
+
if (lines.length > limit) lines.splice(0, lines.length - limit);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** A logged value as text, redacted by its structure first when it is an object (redactValue). */
|
|
79
|
+
function describe(value: unknown): string {
|
|
80
|
+
if (value instanceof Error) return value.stack || `${value.name}: ${value.message}`;
|
|
81
|
+
if (typeof value === "string") return value;
|
|
82
|
+
try { return JSON.stringify(redactValue(value)) ?? String(value); } catch { return "[a value that could not be read safely]"; }
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
let captured = false;
|
|
86
|
+
/**
|
|
87
|
+
* Keeps what this server writes through console.error and console.warn (its stderr diagnostics: MQTT, connection,
|
|
88
|
+
* bridge and slicer messages) in memory as well. Writing to stderr is unchanged. Idempotent.
|
|
89
|
+
*/
|
|
90
|
+
export function captureServerLog(): void {
|
|
91
|
+
if (captured) return;
|
|
92
|
+
captured = true;
|
|
93
|
+
for (const level of ["error", "warn"] as const) {
|
|
94
|
+
const original = console[level].bind(console);
|
|
95
|
+
console[level] = (...args: unknown[]) => {
|
|
96
|
+
try { remember(serverLog, SERVER_LOG_LINES, `${new Date().toISOString()} ${level} ${args.map(describe).join(" ")}`); } catch { /* diagnostics only */ }
|
|
97
|
+
original(...args);
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** Records a failed tool call: its name and error, not its arguments. */
|
|
103
|
+
export function recordToolError(tool: string, error: unknown): void {
|
|
104
|
+
remember(toolErrors, TOOL_ERROR_LINES, `${new Date().toISOString()} ${tool}: ${describe(error)}`);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** Remembers the values of a tool call's secret-named arguments (access codes, API keys, tokens) for redaction. */
|
|
108
|
+
export function rememberSecretArguments(args: unknown): void {
|
|
109
|
+
const visit = (value: unknown, key: string, depth: number): void => {
|
|
110
|
+
if (depth > 32 || value === null || value === undefined) return;
|
|
111
|
+
if (typeof value === "string" || typeof value === "number") {
|
|
112
|
+
const text = String(value).trim();
|
|
113
|
+
if (isSecretName(key) && text.length >= MIN_SECRET_LENGTH && !PLAIN_VALUE.test(text) && seenSecrets.size < SEEN_SECRET_LIMIT) seenSecrets.add(text);
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
// An array's elements are held under the key that owns the array ({tokens: ["…"]}).
|
|
117
|
+
if (Array.isArray(value)) { for (const item of value) visit(item, key, depth + 1); return; }
|
|
118
|
+
if (typeof value === "object") for (const [k, v] of Object.entries(value as Record<string, unknown>)) visit(v, k, depth + 1);
|
|
119
|
+
};
|
|
120
|
+
visit(args, "", 0);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export const serverLogText = () => serverLog.join("\n") + (serverLog.length ? "\n" : "");
|
|
124
|
+
export const toolErrorsText = () => toolErrors.join("\n") + (toolErrors.length ? "\n" : "");
|
|
125
|
+
|
|
126
|
+
/** For tests: forgets what was recorded. */
|
|
127
|
+
export function resetDiagnosticsMemory(): void {
|
|
128
|
+
serverLog.length = 0;
|
|
129
|
+
toolErrors.length = 0;
|
|
130
|
+
seenSecrets.clear();
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// ---------------------------------------------------------------------------------------------------------------------
|
|
134
|
+
// Redaction.
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Whether a name says its value is a secret. The name is split into words at _ . - spaces and camelCase
|
|
138
|
+
* ("sessionId", "SCAN_TO_MAKE_AI_KEY", "X-Api-Key"); \uXXXX escapes are read first ("\u0070assword").
|
|
139
|
+
* STRONG words count inside a longer word too ("mytoken", "apikey"); WHOLE words only alone ("PAIRING_PIN", not
|
|
140
|
+
* "spinner"); "code" only after a word that makes it one (access, lan, pair, pairing, auth, verification, security).
|
|
141
|
+
*/
|
|
142
|
+
const STRONG_WORDS = /pass(?:word|wd|code|phrase|key)|secret|token|apikey|accesscode|accesskey|authkey|privatekey|cookie|session|credential|signature|bearer|e-?mail|username|userinfo|login|ticket|authoriz|(?:lan|pair|pairing|device|unlock|auth|verification|security|otp)code/;
|
|
143
|
+
const WHOLE_WORDS = new Set(["key", "keys", "pin", "otp", "pwd", "pass", "passwd", "auth", "account", "sid"]);
|
|
144
|
+
const CODE_OWNERS = new Set(["access", "lan", "pair", "pairing", "auth", "verification", "security", "otp", "device", "unlock"]);
|
|
145
|
+
/** Text with \\uXXXX, \\xXX and %XX escapes read, so "\\x70assword" and "pass%77ord" are seen as "password". */
|
|
146
|
+
export function decodeEscapes(text: string): string {
|
|
147
|
+
return text.replace(/\\{1,2}u\{([0-9a-fA-F]{1,6})\}|\\{1,2}u([0-9a-fA-F]{4})|\\{1,2}x([0-9a-fA-F]{2})|%([0-9a-fA-F]{2})|&#[xX]([0-9a-fA-F]{1,6});?|&#(\d{1,7});?|\\\//g,
|
|
148
|
+
(match, brace, u, x, p, hexEntity, decEntity) => {
|
|
149
|
+
if (match.endsWith("/") && match.startsWith("\\")) return "/";
|
|
150
|
+
const code = decEntity !== undefined ? parseInt(decEntity, 10) : parseInt(brace ?? u ?? x ?? p ?? hexEntity, 16);
|
|
151
|
+
return code <= 0x10ffff ? String.fromCodePoint(code) : match;
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
export function isSecretName(name: string): boolean {
|
|
155
|
+
const plain = decodeEscapes(name);
|
|
156
|
+
// The whole name first, lowercased and joined: "PaSsWoRd" and "AcCeSsCoDe" are not split into nonsense words.
|
|
157
|
+
if (STRONG_WORDS.test(plain.toLowerCase().replace(/[^a-z0-9]/g, ""))) return true;
|
|
158
|
+
const words = plain.replace(/([a-z0-9])([A-Z])/g, "$1 $2").replace(/([A-Z]+)([A-Z][a-z])/g, "$1 $2")
|
|
159
|
+
.toLowerCase().split(/[^a-z0-9]+/).filter(Boolean);
|
|
160
|
+
return words.some((word, i) => STRONG_WORDS.test(word) || WHOLE_WORDS.has(word) || (word === "code" && i > 0 && CODE_OWNERS.has(words[i - 1]))
|
|
161
|
+
|| (i > 0 && STRONG_WORDS.test(words[i - 1] + word)));
|
|
162
|
+
}
|
|
163
|
+
/** A name in key=value or JSON: letters, digits, _ . - and \uXXXX escapes, starting a word. */
|
|
164
|
+
// Only a name with a word that can make it a secret is taken (the lookahead), so "https://…?token=…" is read from "token".
|
|
165
|
+
const NAME_BODY = String.raw`(?=(?:[A-Za-z0-9_.-]|\\{1,2}u[0-9A-Fa-f]{4}){0,96}?(?:pass|pwd|secret|token|key|cookie|session|credential|signature|bearer|mail|user|login|ticket|auth|pin|otp|code|account|sid|\\{1,2}u))(?:[A-Za-z0-9_.-]|\\{1,2}u[0-9A-Fa-f]{4}){1,96}`;
|
|
166
|
+
const NAME = String.raw`(?<![A-Za-z0-9_.\\-])${NAME_BODY}`;
|
|
167
|
+
// Every pattern below starts at a word's start and bounds its runs, so a long log line costs linear time.
|
|
168
|
+
/** Values too common to replace wherever they appear. */
|
|
169
|
+
const PLAIN_VALUE = /^(?:true|false|yes|no|null|none|undefined|on|off)$/i;
|
|
170
|
+
const MIN_SECRET_LENGTH = 5;
|
|
171
|
+
const MARK = "[redacted]";
|
|
172
|
+
/**
|
|
173
|
+
* An empty value, one already replaced, or a bare true/false/null says only whether it is set ("accessCodeSet": true):
|
|
174
|
+
* it stays. Only as the whole value: "[set]hunter2" and "true-secret" are secrets.
|
|
175
|
+
*/
|
|
176
|
+
const SAFE_VALUE = /^(?:(?:\\?["'])?(?:\[set\]|\[redacted(?:-[a-z]+)?\])?(?:\\?["'])?|true|false|null)$/;
|
|
177
|
+
const AUTH_SCHEME = /^(?:\\?["'])?(?:bearer|basic|digest|token|negotiate|ntlm)$/i;
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* key=value, key: value, "key": "value", \"key\":\"value\", ?key=value&. Each name's value is read ahead without being taken,
|
|
181
|
+
* so a name that is not a secret ("https:", "?code=1") leaves what follows it to be read again ("#id_token=…").
|
|
182
|
+
*/
|
|
183
|
+
const KEY_VALUE = new RegExp(String.raw`(\\?["']?)(${NAME})(\\?["']?)(\s*[:=]\s*)(?=(\\"(?:(?!\\")[^\n]){0,4096}\\"|"(?:[^"\\\n]|\\.){0,4096}"|'[^'\n]{0,4096}'|["']?(?:\[[a-z-]{1,16}\])?[^\s,;&}\])"'\\]*))`, "gi");
|
|
184
|
+
/** Where the JSON object or array starting at `start` ends (after its closing bracket), or its line's end if it is cut. */
|
|
185
|
+
function structureEnd(text: string, start: number): number {
|
|
186
|
+
let depth = 0, inString = false;
|
|
187
|
+
const limit = Math.min(text.length, start + 65536);
|
|
188
|
+
for (let i = start; i < limit; i++) {
|
|
189
|
+
const c = text[i];
|
|
190
|
+
if (inString) { if (c === "\\") i++; else if (c === '"') inString = false; continue; }
|
|
191
|
+
if (c === '"') inString = true;
|
|
192
|
+
else if (c === "{" || c === "[") depth++;
|
|
193
|
+
else if (c === "}" || c === "]") { if (--depth === 0) return i + 1; }
|
|
194
|
+
}
|
|
195
|
+
const line = text.indexOf("\n", start);
|
|
196
|
+
return line < 0 || line > limit ? limit : line;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
const PAIR_NAME = /"(?:name|key|env|variable|var|field|id)"\s*:\s*"([^"\\\n]{1,96})"/gi;
|
|
200
|
+
const PAIR_VALUE = /("(?:value|val|secret|data)"\s*:\s*)("(?:[^"\\\n]|\\.){0,65536}"|[^\s,}\]]{1,4096})/gi;
|
|
201
|
+
/**
|
|
202
|
+
* {"name":"BAMBU_ACCESS_CODE","scope":"lan","value":"…"}: in each flat JSON object (no object inside it), when a
|
|
203
|
+
* name/key field names a secret, its value fields go, wherever they are in the object and in either order.
|
|
204
|
+
*/
|
|
205
|
+
function redactNamedValues(text: string): string {
|
|
206
|
+
return text.replace(/\{[^{}]{0,16384}\}/g, object => {
|
|
207
|
+
const names = [...object.matchAll(PAIR_NAME)].map(m => m[1]);
|
|
208
|
+
if (!names.some(isSecretName)) return object;
|
|
209
|
+
return object.replace(PAIR_VALUE, (m, head, value) => SAFE_VALUE.test(value) ? m : `${head}"${MARK}"`);
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/** Where the quoted string starting at `start` ends (after its closing quote), or its line's end if it is cut. */
|
|
214
|
+
function stringEnd(text: string, start: number, quote: string): number {
|
|
215
|
+
const limit = Math.min(text.length, start + 1_048_576);
|
|
216
|
+
for (let i = start + quote.length; i < limit; i++) {
|
|
217
|
+
if (text.startsWith(quote, i)) return i + quote.length;
|
|
218
|
+
if (text[i] === "\\" && quote === '"') i++;
|
|
219
|
+
else if (text[i] === "\n") return i;
|
|
220
|
+
}
|
|
221
|
+
return limit;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/** A bare "code" whose value is a random run of mixed case and digits (a pairing code), unlike a printer's error code. */
|
|
225
|
+
const randomCode = (key: string, value: string) => /(?:^|[^A-Za-z])code$/i.test(key.replace(/["'\\]/g, "")) && (() => {
|
|
226
|
+
const plain = value.replace(/^\\?["']|\\?["']$/g, "");
|
|
227
|
+
return /^[A-Za-z0-9_-]{16,256}$/.test(plain) && /[a-z]/.test(plain) && /[A-Z]/.test(plain) && /\d/.test(plain);
|
|
228
|
+
})();
|
|
229
|
+
|
|
230
|
+
function redactKeyValues(text: string): string {
|
|
231
|
+
let out = "", last = 0;
|
|
232
|
+
KEY_VALUE.lastIndex = 0;
|
|
233
|
+
for (let m = KEY_VALUE.exec(text); m; m = KEY_VALUE.exec(text)) {
|
|
234
|
+
const [head, q1, key, q2, sep] = m;
|
|
235
|
+
let value = m[5];
|
|
236
|
+
const start = m.index + head.length;
|
|
237
|
+
// An object or array under a secret's name ({"password":{"value":…}}, {"tokens":[…]}) goes whole, to its closing bracket.
|
|
238
|
+
// Only after a quoted JSON key: token=[set]hunter2 is a value, not an array.
|
|
239
|
+
if (q2 && (text[start] === "{" || text[start] === "[") && isSecretName(key)) value = text.slice(start, structureEnd(text, start));
|
|
240
|
+
// A quoted value longer than the lookahead reads goes to its closing quote, not to its first space.
|
|
241
|
+
else if (value?.startsWith('"') || value?.startsWith('\\"')) {
|
|
242
|
+
const quote = value.startsWith('"') ? '"' : '\\"';
|
|
243
|
+
if (value.length <= quote.length || !value.endsWith(quote)) value = text.slice(start, stringEnd(text, start, quote));
|
|
244
|
+
}
|
|
245
|
+
// An empty value, a safe one, or an authorization scheme ("authorization: bearer …", replaced after the scheme) stays.
|
|
246
|
+
if (!(isSecretName(key) || randomCode(key, value)) || !value || SAFE_VALUE.test(value) || AUTH_SCHEME.test(value)) continue;
|
|
247
|
+
// A bare number under a measure's name (tokenCount: 5, sessionTimeoutSec=30) is not a secret.
|
|
248
|
+
if (/^-?\d+(?:\.\d+)?$/.test(value) && MEASURE_NAME.test(key.replace(/["'\\]/g, ""))) continue;
|
|
249
|
+
const quote = value.startsWith('\\"') ? '\\"' : value.startsWith('"') ? '"' : value.startsWith("'") ? "'" : "";
|
|
250
|
+
// A JSON key's bare value (a number) is replaced by a string, so the JSON still parses.
|
|
251
|
+
const json = quote ? quote : q2 === '"' ? '"' : q2 === '\\"' ? '\\"' : "";
|
|
252
|
+
out += text.slice(last, m.index) + `${q1}${key}${q2}${sep}${json}${MARK}${json}`;
|
|
253
|
+
last = KEY_VALUE.lastIndex = m.index + head.length + value.length;
|
|
254
|
+
}
|
|
255
|
+
return out + text.slice(last);
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
const RULES: Array<[RegExp, string | ((...m: string[]) => string)] | ((text: string) => string)> = [
|
|
259
|
+
// PEM blocks of every type (private keys, certificates, requests), whole.
|
|
260
|
+
(text: string) => removePem(text),
|
|
261
|
+
// Cookie headers: every cookie on the line.
|
|
262
|
+
[/\b((?:set-)?cookie\s*[:=]\s*)[^\r\n]*/gi, `$1${MARK}`],
|
|
263
|
+
// An Authorization or Proxy-Authorization header line goes whole, past its scheme (a Digest username goes with it).
|
|
264
|
+
[/(?<![\w"'\\-])((?:proxy-)?authorization[ \t]*:[ \t]*)(?:(bearer|basic|digest|negotiate|ntlm|token)[ \t]+)?[^\r\n]*/gi,
|
|
265
|
+
(_m, head, scheme) => `${head}${scheme ? `${scheme} ` : ""}${MARK}`],
|
|
266
|
+
// A secret named by a JSON object's name and given in its value: {"name":"BAMBU_ACCESS_CODE","value":"…"}, either order.
|
|
267
|
+
redactNamedValues,
|
|
268
|
+
// Digest credentials written bare (Digest username="…", response="…"): the whole list, to the line's end.
|
|
269
|
+
[/\b(Digest)([ \t]+)(?=[A-Za-z_-]{1,32}[ \t]*=)[^\r\n]*/gi, `$1$2${MARK}`],
|
|
270
|
+
// Two-word labels: "access code: 12345678", "pairing code = …".
|
|
271
|
+
[/\b((?:access|lan|pair|pairing|auth|verification|security|otp)[ \t]+code)([ \t]*[:=][ \t]*)(?!\[redacted|\[set\])("[^"\n]{1,4096}"|'[^'\n]{1,4096}'|[^\s,;&}\])"']+)/gi,
|
|
272
|
+
(_m, label, sep, value) => `${label}${sep}${value.startsWith('"') ? `"${MARK}"` : value.startsWith("'") ? `'${MARK}'` : MARK}`],
|
|
273
|
+
// Secrets in XML and plists: <password>…</password>, <key>apiToken</key><string>…</string>.
|
|
274
|
+
[/(<key>([^<]{1,96})<\/key>\s*<(string|data|integer)>)([^<]{1,65536})(<\/\3>)/g,
|
|
275
|
+
(m, head, name, _tag, value, tail) => isSecretName(name) ? `${head}${MARK}${tail}` : m],
|
|
276
|
+
[/<([A-Za-z_][A-Za-z0-9_.:-]{0,95})((?:\s[^<>]{0,1024})?)>([^<]{1,65536})<\/\1>/g,
|
|
277
|
+
(m, tag, attributes, value) => tag !== "key" && isSecretName(tag) && !SAFE_VALUE.test(value) ? `<${tag}${attributes}>${MARK}</${tag}>` : m],
|
|
278
|
+
// Authorization schemes, in any case.
|
|
279
|
+
[/\b(Bearer|Basic|Digest|Token|Negotiate|NTLM)(\s+)[A-Za-z0-9._~+/=-]{4,}/gi, `$1$2${MARK}`],
|
|
280
|
+
// user:password@ in URLs.
|
|
281
|
+
[/(?<![A-Za-z0-9+.-])([a-z][a-z0-9+.-]{0,30}:\/\/)[^\s/@:]{1,256}(?::[^\s/@]{0,256})?@/gi, `$1${MARK}@`],
|
|
282
|
+
// key=value, key: value, "key": "value", \"key\":\"value\", ?key=value& (redactKeyValues).
|
|
283
|
+
redactKeyValues,
|
|
284
|
+
// --api-key VALUE, -password VALUE.
|
|
285
|
+
[new RegExp(String.raw`(\s--?)(${NAME_BODY})(\s+)(?!--?\w)(\S+)`, "gi"),
|
|
286
|
+
(match, dash, key, space, value) => isSecretName(key) && !SAFE_VALUE.test(value) ? `${dash}${key}${space}${MARK}` : match],
|
|
287
|
+
// Email addresses with a URL-encoded @ (name%40example.com).
|
|
288
|
+
[/(?<![A-Za-z0-9._%+-])[A-Za-z0-9._+-]{1,64}%40[A-Za-z0-9-]{1,63}(?:\.[A-Za-z0-9-]{1,63}){0,8}\.[A-Za-z]{2,24}(?![A-Za-z0-9-])/gi, "[redacted-email]"],
|
|
289
|
+
// Email addresses (account logins).
|
|
290
|
+
[/(?<![A-Za-z0-9._%+-])[A-Za-z0-9._%+-]{1,64}@[A-Za-z0-9-]{1,63}(?:\.[A-Za-z0-9-]{1,63}){0,8}\.[A-Za-z]{2,24}(?![A-Za-z0-9-])/g, "[redacted-email]"],
|
|
291
|
+
// JSON Web Tokens.
|
|
292
|
+
[/\beyJ[A-Za-z0-9_-]{5,}\.[A-Za-z0-9_-]{5,}(?:\.[A-Za-z0-9_-]*)?/g, "[redacted-token]"],
|
|
293
|
+
// Tokens with a known prefix, bare: GitHub, AWS access key ids, Google API keys, sk-/pk- keys, Slack, Hugging Face.
|
|
294
|
+
[/\b(?:gh[pousr]_[A-Za-z0-9]{20,255}|github_pat_[A-Za-z0-9_]{20,255}|(?:AKIA|ASIA)[A-Z0-9]{16}|AIza[A-Za-z0-9_-]{30,64}|(?:sk|pk|rk)-[A-Za-z0-9_-]{16,256}|xox[abposr]-[A-Za-z0-9-]{10,256}|hf_[A-Za-z0-9]{20,255})(?![A-Za-z0-9_-])/g, "[redacted-token]"],
|
|
295
|
+
// An AWS secret access key, bare: 40 characters of mixed case, digits and / or +, not a path.
|
|
296
|
+
[/(?<![A-Za-z0-9/+=._-])[A-Za-z0-9][A-Za-z0-9/+]{39}(?![A-Za-z0-9/+=._-])/g,
|
|
297
|
+
run => /[/+]/.test(run) && /[a-z]/.test(run) && /[A-Z]/.test(run) && /\d/.test(run) && (run.match(/[/+]/g) ?? []).length <= 4 && !/\/\//.test(run) ? "[redacted-token]" : run],
|
|
298
|
+
// Home folders: other people's names in /Users/<name>, /home/<name>, C:\Users\<name> and ~name, slashes escaped or not.
|
|
299
|
+
[/((?:^|[^A-Za-z0-9_.-])(?:\\{0,2}\/)(?:Users|home)\\{0,2}\/)(?!Shared\b|\[user\])([^\\/\s"'<>:*?|]{1,64})/g, "$1[user]"],
|
|
300
|
+
[/(\b[A-Za-z]:(?:\\{1,2}|\/)Users(?:\\{1,2}|\/))(?!Public\b|\[user\])([^\\/\s"'<>:*?|]{1,64})/gi, "$1[user]"],
|
|
301
|
+
[/(?<![A-Za-z0-9_~])~(?!\[user\])[A-Za-z][A-Za-z0-9._-]{0,31}(?=\\{0,2}\/)/g, "~[user]"],
|
|
302
|
+
];
|
|
303
|
+
|
|
304
|
+
/** Long mixed-case-and-digit runs (API keys, session tokens) and 32-digit uppercase hex keys; lowercase hashes stay. */
|
|
305
|
+
function redactTokens(text: string): string {
|
|
306
|
+
return text.replace(/[A-Za-z0-9_+=-]{32,1024}(?!\.[A-Za-z][A-Za-z0-9]{1,5}\b)/g, run =>
|
|
307
|
+
(/[a-z]/.test(run) && /[A-Z]/.test(run) && /\d/.test(run)) || /^[0-9A-F]{32,1024}$/.test(run) && /[A-F]/.test(run) && /\d/.test(run)
|
|
308
|
+
? "[redacted-token]" : run);
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
/** The values of this environment's secret-named variables. */
|
|
312
|
+
export function environmentSecrets(env: NodeJS.ProcessEnv = process.env): string[] {
|
|
313
|
+
return Object.entries(env).filter(([name, value]) => isSecretName(name) && typeof value === "string")
|
|
314
|
+
.map(([, value]) => value!.trim()).filter(value => value.length >= MIN_SECRET_LENGTH && !PLAIN_VALUE.test(value));
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
const escapeRegExp = (text: string) => text.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
318
|
+
|
|
319
|
+
/** The forms a known secret can take in a log: as is, URL-encoded, JSON-escaped, and base64 at any byte offset. */
|
|
320
|
+
function encodedForms(secret: string): string[] {
|
|
321
|
+
const forms = new Set([secret, encodeURIComponent(secret), JSON.stringify(secret).slice(1, -1)]);
|
|
322
|
+
const bytes = Buffer.from(secret, "utf8");
|
|
323
|
+
for (const before of [0, 1, 2]) {
|
|
324
|
+
// The base64 characters that only this secret's bytes decide, wherever it sits in an encoded run ("bblp:<code>").
|
|
325
|
+
const encoded = Buffer.concat([Buffer.alloc(before), bytes]).toString("base64");
|
|
326
|
+
const start = before ? 4 : 0, end = Math.floor((before + bytes.length) / 3) * 4;
|
|
327
|
+
if (end - start >= 4) forms.add(encoded.slice(start, end));
|
|
328
|
+
if (!before) { forms.add(encoded); forms.add(encoded.replace(/=+$/, "")); forms.add(encoded.replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "")); }
|
|
329
|
+
}
|
|
330
|
+
return [...forms].filter(form => form.length >= 4);
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
/** Base64 characters around a removed core: the group before and after it mix the secret's bytes with its neighbours'. */
|
|
334
|
+
const B64_EDGE = /[A-Za-z0-9+/_-]{1,4}\[redacted\][A-Za-z0-9+/_-]{0,4}={0,2}|\[redacted\][A-Za-z0-9+/_-]{1,4}={0,2}/g;
|
|
335
|
+
|
|
336
|
+
/** text with every known secret (configured, in this environment, or seen in tool arguments) removed in each encoded form. */
|
|
337
|
+
export function removeKnownSecrets(text: string, secrets: Iterable<string> = []): string {
|
|
338
|
+
const known = [...new Set([...secrets, ...environmentSecrets(), ...seenSecrets])].filter(value => value.length >= MIN_SECRET_LENGTH);
|
|
339
|
+
if (!known.length) return text.replace(/\u0000/g, "");
|
|
340
|
+
let out = text.replace(/\u0000/g, "");
|
|
341
|
+
// Other encoders' spellings (form-url `+`, lowercase %-hex, Go's \u003c, a lone \u escape, \/, HTML entities): a copy
|
|
342
|
+
// of each line is decoded, and a line whose decoded copy holds a known secret is withheld whole.
|
|
343
|
+
out = out.split("\n").map(line => {
|
|
344
|
+
if (!/[%+\\&]/.test(line)) return line;
|
|
345
|
+
const decoded = decodeEscapes(line);
|
|
346
|
+
const spaced = decodeEscapes(line.replace(/\+/g, " "));
|
|
347
|
+
return known.some(value => decoded.includes(value) || spaced.includes(value)) ? (LINE_PREFIX.exec(line)?.[0] ?? "") + withheldMarker(line) : line;
|
|
348
|
+
}).join("\n");
|
|
349
|
+
for (const value of known) {
|
|
350
|
+
const plain = encodedForms(value).filter(form => form.length >= MIN_SECRET_LENGTH || /^[A-Za-z0-9+/_-]+$/.test(form));
|
|
351
|
+
for (const form of plain.sort((a, b) => b.length - a.length)) out = out.split(form).join(MARK);
|
|
352
|
+
}
|
|
353
|
+
// A base64 core leaves the mixed groups either side of it; they go too (a small over-redaction of innocent base64).
|
|
354
|
+
return out.replace(B64_EDGE, MARK);
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
/**
|
|
358
|
+
* Words that make a line suspect, looked for in the line lowercased, its escapes read and everything but letters and
|
|
359
|
+
* digits removed (so "pass word", "Pa_Ss-WoRd", "\x70assword" and "pass%77ord" all read "password"); WORDS only as
|
|
360
|
+
* a whole word of the line ("mqtt pass=…", not "compass").
|
|
361
|
+
*/
|
|
362
|
+
const LINE_SECRETS = /pass(?:word|wd|code|phrase|key)|secret|token|apikey|accesscode|accesskey|(?:lan|pair|pairing|device|unlock|auth|verification|security|otp)code|authoriz|authkey|cookie|session|credential|privatekey|signature|bearer|userinfo|ticket/;
|
|
363
|
+
const LINE_WORDS = new Set(["pass", "passwd", "pwd", "pin", "otp", "auth", "sid", "key", "keys", "digest", "negotiate", "ntlm"]);
|
|
364
|
+
/** A name whose bare number is a measure, not a secret: tokenCount, sessionTimeoutSec, passwordMinLength. */
|
|
365
|
+
const MEASURE_NAME = /(?:count|counts|total|num|number|sec|secs|seconds|ms|millis|minutes|length|len|size|bytes|timeout|ttl|max|min|limit|retries|attempts|index|level|version)$/i;
|
|
366
|
+
const NUMBER = /^-?\d+(?:\.\d+)?$/;
|
|
367
|
+
/** A value that says nothing secret: a marker, empty, a boolean, a firmware version or a printer error (HMS) code. */
|
|
368
|
+
const SAFE_SHAPE = String.raw`\[set\]|\[redacted(?:-[a-z]+)?\]|true|false|null|none|""|''|\d{1,3}(?:\.\d{1,3}){2,3}|[0-9A-Fa-f]{4}(?:[-_][0-9A-Fa-f]{4}){1,3}`;
|
|
369
|
+
const SAFE_PAIR = new RegExp(String.raw`^[\s"'{\[,]*([A-Za-z0-9_.\-]{1,96})["']?\s*[:=]\s*["']?(${SAFE_SHAPE}|-?\d+(?:\.\d+)?)["']?[\s,}\]]*$`);
|
|
370
|
+
/** A log line's leading time and level ("2026-11-14T10:00:00.000Z error"), kept on a withheld line. */
|
|
371
|
+
const LINE_PREFIX = /^(?:\[?\d{4}-\d\d-\d\d[T ][\d:.,]+(?:Z|[+-]\d\d:?\d\d)?\]?\s+(?:\[?(?:error|warn(?:ing)?|info|debug|trace|fatal|notice)\]?:?\s+)?|\[?(?:error|warn(?:ing)?|info|debug|trace|fatal|notice)\]?:?\s+)/i;
|
|
372
|
+
export const WITHHELD = "[line withheld: may contain a secret]";
|
|
373
|
+
|
|
374
|
+
/**
|
|
375
|
+
* Reasons a withheld line names, from this fixed list only: each is a canonical phrase found in the line read
|
|
376
|
+
* lowercased, its escapes read and everything but letters and digits made one space. The marker carries these strings
|
|
377
|
+
* and never any of the line's own text, so it cannot leak: "[line withheld: may contain a secret · mentions: token
|
|
378
|
+
* expired]". Generic auth states, no printer or service wording.
|
|
379
|
+
*/
|
|
380
|
+
const REASONS: Array<[string, RegExp]> = [
|
|
381
|
+
["failed to authenticate", /\bfailed to authenticate\b/],
|
|
382
|
+
["authentication failed", /\b(?:authentication|auth|login) failed\b/],
|
|
383
|
+
["login expired", /\b(?:login|session|sign in) (?:has |is )?expired\b/],
|
|
384
|
+
["login time limit", /\blogin\b.*\btime limit\b/],
|
|
385
|
+
["token expired", /\btoken (?:has |is )?expired\b|\bexpired token\b/],
|
|
386
|
+
["token refresh failed", /\btoken refresh failed\b|\brefresh token failed\b|\bfailed to refresh (?:the )?token\b/],
|
|
387
|
+
["token invalid", /\btoken (?:is )?invalid\b|\binvalid token\b/],
|
|
388
|
+
["invalid credentials", /\binvalid credentials\b/],
|
|
389
|
+
["unauthorized", /\bunauthori[sz]ed\b/],
|
|
390
|
+
["401", /\b401\b/],
|
|
391
|
+
["access code wrong", /\baccess code (?:is |was )?(?:wrong|incorrect|invalid)\b|\b(?:wrong|incorrect|invalid) access code\b/],
|
|
392
|
+
["access code rejected", /\baccess code (?:was )?rejected\b/],
|
|
393
|
+
["password wrong", /\bpassword (?:is |was )?(?:wrong|incorrect|invalid)\b|\b(?:wrong|incorrect|invalid) password\b/],
|
|
394
|
+
["password rejected", /\bpassword (?:was )?rejected\b/],
|
|
395
|
+
["not signed in", /\bnot (?:signed|logged) in\b/],
|
|
396
|
+
["claude auth login", /\bclaude auth login\b/],
|
|
397
|
+
["auth login", /\bauth login\b/],
|
|
398
|
+
["sign in again", /\b(?:sign|log) in again\b/],
|
|
399
|
+
["setup-token", /\bsetup token\b/],
|
|
400
|
+
["permission denied", /\b(?:permission|access) denied\b/],
|
|
401
|
+
];
|
|
402
|
+
/** The canonical phrases of REASONS, in order: all a withheld marker may name. */
|
|
403
|
+
export const WITHHELD_REASONS = REASONS.map(([phrase]) => phrase);
|
|
404
|
+
|
|
405
|
+
/** The withheld marker for these lines: WITHHELD, naming the REASONS they mention ("claude auth login" over "auth login"). */
|
|
406
|
+
export function withheldMarker(lines: string): string {
|
|
407
|
+
const plain = ` ${decodeEscapes(lines).toLowerCase().replace(/[^a-z0-9]+/g, " ")} `;
|
|
408
|
+
const found = REASONS.filter(([, pattern]) => pattern.test(plain)).map(([phrase]) => phrase);
|
|
409
|
+
const tags = found.filter(tag => !(tag === "auth login" && found.includes("claude auth login")));
|
|
410
|
+
return tags.length ? `${WITHHELD.slice(0, -1)} · mentions: ${tags.join(", ")}]` : WITHHELD;
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
/** Whether a line may carry a secret: a secret word anywhere in it, in any spelling or encoding. */
|
|
414
|
+
export function suspectLine(line: string): boolean {
|
|
415
|
+
const plain = decodeEscapes(line).toLowerCase();
|
|
416
|
+
if (LINE_SECRETS.test(plain.replace(/[^a-z0-9]/g, ""))) return true;
|
|
417
|
+
return plain.split(/[^a-z0-9]+/).some(word => LINE_WORDS.has(word));
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
/** Quotes and brackets left open so far, read on through a line (a value that goes on to the next line). */
|
|
421
|
+
type Open = { depth: number; quote: string };
|
|
422
|
+
/**
|
|
423
|
+
* The quotes and brackets a line leaves open. On a suspect line a single quote counts too, opening only where a value
|
|
424
|
+
* starts (`password='…`), not inside a word (`don't`).
|
|
425
|
+
*/
|
|
426
|
+
function readOpen(state: Open, line: string, single = false): Open {
|
|
427
|
+
let { depth, quote } = state;
|
|
428
|
+
for (let i = 0; i < line.length; i++) {
|
|
429
|
+
const c = line[i];
|
|
430
|
+
if (quote) { if (c === "\\") i++; else if (c === quote) quote = ""; continue; }
|
|
431
|
+
if (c === '"') quote = c;
|
|
432
|
+
else if (single && c === "'" && !/[A-Za-z0-9]/.test(line[i - 1] ?? "")) quote = c;
|
|
433
|
+
else if (c === "{" || c === "[") depth++;
|
|
434
|
+
else if (c === "}" || c === "]") depth = Math.max(0, depth - 1);
|
|
435
|
+
}
|
|
436
|
+
return { depth, quote };
|
|
437
|
+
}
|
|
438
|
+
const isOpen = (open: Open) => open.depth > 0 || open.quote !== "";
|
|
439
|
+
|
|
440
|
+
/** Whether a suspect line is only a name and a value that says nothing secret, closing all it opens ("accessCodeSet": true). */
|
|
441
|
+
function safeLine(line: string): boolean {
|
|
442
|
+
const body = line.replace(LINE_PREFIX, "");
|
|
443
|
+
const m = SAFE_PAIR.exec(body);
|
|
444
|
+
if (!m || isOpen(readOpen({ depth: 0, quote: "" }, body, true))) return false;
|
|
445
|
+
return !NUMBER.test(m[2]) || MEASURE_NAME.test(m[1]);
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
/** Most lines a heuristic continuation (indented lines, a YAML block, a single quote) takes, so it stays bounded. */
|
|
449
|
+
const CONTINUATION_LIMIT = 200;
|
|
450
|
+
const indent = (line: string) => /^[ \t]*/.exec(line)![0].length;
|
|
451
|
+
|
|
452
|
+
/**
|
|
453
|
+
* How many lines after lines[i] (a suspect line) continue it: while a double quote or a bracket it opened is verifiably
|
|
454
|
+
* still open, however many; lines indented deeper than it, a YAML block scalar it starts (blank lines included), or
|
|
455
|
+
* a single-quoted value, up to CONTINUATION_LIMIT; a multipart part named like a secret, to its next boundary; and
|
|
456
|
+
* an Authorization or Bearer line that ends mid-token, its wrapped next line.
|
|
457
|
+
*/
|
|
458
|
+
function continuation(lines: string[], i: number): number {
|
|
459
|
+
const line = lines[i];
|
|
460
|
+
if (/^\s*content-disposition\s*:/i.test(line)) {
|
|
461
|
+
let j = i + 1;
|
|
462
|
+
while (j < lines.length && !/^--\S/.test(lines[j])) j++;
|
|
463
|
+
return j - i - 1;
|
|
464
|
+
}
|
|
465
|
+
let open = readOpen({ depth: 0, quote: "" }, line, true);
|
|
466
|
+
const block = /[:=]\s*[>|][-+]?\d?\s*$/.test(line), base = indent(line);
|
|
467
|
+
let taken = 0, heuristic = 0;
|
|
468
|
+
while (i + taken + 1 < lines.length) {
|
|
469
|
+
const next = lines[i + taken + 1];
|
|
470
|
+
const verified = open.depth > 0 || open.quote === '"';
|
|
471
|
+
if (!verified) {
|
|
472
|
+
const deeper = /\S/.test(next) && indent(next) > base && !LINE_PREFIX.test(next.trimStart());
|
|
473
|
+
const inBlock = block && (!/\S/.test(next) || indent(next) > base);
|
|
474
|
+
if (!(open.quote === "'" || deeper || inBlock) || ++heuristic > CONTINUATION_LIMIT) break;
|
|
475
|
+
}
|
|
476
|
+
open = readOpen(open, next, true);
|
|
477
|
+
taken++;
|
|
478
|
+
}
|
|
479
|
+
// A blank line closing a YAML block is not part of it.
|
|
480
|
+
while (block && taken > 0 && !/\S/.test(lines[i + taken])) taken--;
|
|
481
|
+
if (taken === 0 && /\b(?:authorization|bearer)\b/i.test(line) && /[A-Za-z0-9._~+/=-]$/.test(line)) {
|
|
482
|
+
const next = lines[i + 1];
|
|
483
|
+
if (next !== undefined && /^[A-Za-z0-9._~+/=-]/.test(next) && !LINE_PREFIX.test(next)) return 1;
|
|
484
|
+
}
|
|
485
|
+
return taken;
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
/**
|
|
489
|
+
* Free text with each suspect line withheld whole, with the lines that continue it; its time and level stay. A suspect
|
|
490
|
+
* line that is only a name and a safe value stays, but what continues it is withheld all the same.
|
|
491
|
+
*/
|
|
492
|
+
export function withholdSuspectLines(text: string): string {
|
|
493
|
+
const lines = text.split("\n"), out: string[] = [];
|
|
494
|
+
for (let i = 0; i < lines.length; i++) {
|
|
495
|
+
const line = lines[i];
|
|
496
|
+
if (!suspectLine(line)) { out.push(line); continue; }
|
|
497
|
+
const taken = continuation(lines, i);
|
|
498
|
+
if (safeLine(line)) {
|
|
499
|
+
out.push(line);
|
|
500
|
+
if (taken) out.push(withheldMarker(lines.slice(i + 1, i + 1 + taken).join("\n")));
|
|
501
|
+
} else {
|
|
502
|
+
out.push((LINE_PREFIX.exec(line)?.[0] ?? "") + withheldMarker(lines.slice(i, i + 1 + taken).join("\n")));
|
|
503
|
+
}
|
|
504
|
+
i += taken;
|
|
505
|
+
}
|
|
506
|
+
return out.join("\n");
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
/**
|
|
510
|
+
* text with every secret this module can recognise replaced, failing closed: known secret values in every encoded
|
|
511
|
+
* form; then the named patterns (values of secret-named keys, cookies, authorization headers, URL credentials,
|
|
512
|
+
* emails, JWTs, bare tokens, PEM blocks, other people's home folders); then every line that still names a secret
|
|
513
|
+
* withheld whole. Serial numbers, hosts, error codes and lowercase hashes stay.
|
|
514
|
+
*/
|
|
515
|
+
export function redactText(text: string, secrets: Iterable<string> = []): string {
|
|
516
|
+
// Lines are judged as written (a pattern's replacement could close a quote the secret left open); PEM blocks span
|
|
517
|
+
// lines with no secret word in them, so they go first.
|
|
518
|
+
const known = [...secrets];
|
|
519
|
+
// A withheld marker is fixed text: the pattern rules never rewrite it ("token expired" is a reason, not a credential).
|
|
520
|
+
const markers: string[] = [];
|
|
521
|
+
const held = withholdSuspectLines(removePem(removeKnownSecrets(text.replace(/\u0001/g, ""), known)))
|
|
522
|
+
.replace(/\[line withheld: may contain a secret[^\]\n]*\]/g, marker => `\u0001${markers.push(marker) - 1}\u0001`);
|
|
523
|
+
return redactPatterns(held, known).replace(/\u0001(\d+)\u0001/g, (_, n) => markers[Number(n)]);
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
const PEM = /-----BEGIN ([A-Z0-9 ]{1,64})-----[\s\S]*?(?:-----END \1-----|$)/g;
|
|
527
|
+
const removePem = (text: string) => text.replace(PEM, MARK);
|
|
528
|
+
|
|
529
|
+
/** The pattern rules alone (redactText without withholding lines): what each named form becomes. */
|
|
530
|
+
export function redactPatterns(text: string, secrets: Iterable<string> = []): string {
|
|
531
|
+
let out = removeKnownSecrets(text, secrets);
|
|
532
|
+
// This server's own home folder reads ~ (also with JSON's escaped slashes and Windows' doubled backslashes).
|
|
533
|
+
const home = os.homedir();
|
|
534
|
+
if (home && home.length > 1) {
|
|
535
|
+
for (const form of new Set([home, home.replace(/\//g, "\\/"), home.replace(/\\/g, "\\\\")])) {
|
|
536
|
+
out = out.replace(new RegExp(escapeRegExp(form) + "(?![A-Za-z0-9._-])", "g"), "~");
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
for (const rule of RULES) out = typeof rule === "function" ? rule(out) : out.replace(rule[0], rule[1] as any);
|
|
540
|
+
return redactTokens(out);
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
/**
|
|
544
|
+
* A logged or composed value with its secrets removed, by its structure: a secret-named key's value goes (a bare
|
|
545
|
+
* measure such as tokenCount stays); an object where any field names a secret ({"name":"BAMBU_ACCESS_CODE", …})
|
|
546
|
+
* loses its other scalars; a string holding JSON is read and walked; any other string is redacted as text, one
|
|
547
|
+
* value per line, so a string that names a secret is withheld.
|
|
548
|
+
*/
|
|
549
|
+
export function redactValue(value: unknown, secrets: Iterable<string> = [], depth = 0): unknown {
|
|
550
|
+
const known = [...secrets];
|
|
551
|
+
if (depth > 32) return WITHHELD;
|
|
552
|
+
if (typeof value === "string") {
|
|
553
|
+
const t = value.trim();
|
|
554
|
+
if ((t.startsWith("{") && t.endsWith("}")) || (t.startsWith("[") && t.endsWith("]"))) {
|
|
555
|
+
try { return JSON.stringify(redactValue(JSON.parse(t), known, depth + 1)); } catch { /* not JSON */ }
|
|
556
|
+
}
|
|
557
|
+
return redactText(value, known);
|
|
558
|
+
}
|
|
559
|
+
return redactStructure(value, known, depth);
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
/** Fields that hold a setting's name, whose secret-sounding value is a name, not a secret: {"name":"BAMBU_TOKEN"}. */
|
|
563
|
+
/** Fields that hold a setting's name, in the order one is chosen as an object's name: {"name":"BAMBU_TOKEN"}. */
|
|
564
|
+
const NAME_FIELDS = ["name", "key", "env", "envname", "env_name", "variable", "var", "field", "setting", "setting_name", "settingname", "property", "label", "id"];
|
|
565
|
+
/** A name as people write one: an identifier, or a few words ("Access Code"). */
|
|
566
|
+
const LABEL = /^[A-Za-z_][\w.\-]*(?: [\w.\-]+){0,3}$/;
|
|
567
|
+
const safeScalar = (v: unknown) => typeof v === "boolean" || v === null || v === undefined || v === "" || v === "[set]";
|
|
568
|
+
|
|
569
|
+
function redactStructure(value: unknown, known: string[], depth: number): unknown {
|
|
570
|
+
if (Array.isArray(value)) {
|
|
571
|
+
// A pair, as Object.entries writes one: ["BAMBU_TOKEN", "…"]. Its second item goes when the first names a secret.
|
|
572
|
+
if (value.length === 2 && typeof value[0] === "string" && value[0].length <= 96 && isSecretName(value[0])) {
|
|
573
|
+
return [LABEL.test(value[0]) ? value[0] : redactValue(value[0], known, depth + 1), safeScalar(value[1]) ? value[1] : MARK];
|
|
574
|
+
}
|
|
575
|
+
return value.map(item => redactValue(item, known, depth + 1));
|
|
576
|
+
}
|
|
577
|
+
if (value && typeof value === "object") {
|
|
578
|
+
const entries = Object.entries(value as Record<string, unknown>);
|
|
579
|
+
// The one field naming this object: the first name field, in NAME_FIELDS' order, whose whole string names a secret.
|
|
580
|
+
const candidates = entries.filter(([k, v]) => NAME_FIELDS.includes(k.toLowerCase()) && typeof v === "string" && v.length <= 96 && isSecretName(v));
|
|
581
|
+
const chosen = candidates.sort(([a], [b]) => NAME_FIELDS.indexOf(a.toLowerCase()) - NAME_FIELDS.indexOf(b.toLowerCase()))[0]?.[0];
|
|
582
|
+
return Object.fromEntries(entries.map(([k, v]) => {
|
|
583
|
+
// The chosen name field keeps its name ({"name":"BAMBU_TOKEN"}, {"name":"Access Code"}).
|
|
584
|
+
if (k === chosen) return [k, LABEL.test(v as string) ? v : redactValue(v, known, depth + 1)];
|
|
585
|
+
if (isSecretName(k)) return [k, safeScalar(v) || (typeof v === "number" && MEASURE_NAME.test(k)) ? v : MARK];
|
|
586
|
+
// An object that names a secret ({"name":"BAMBU_ACCESS_CODE","note":…,"value":…}) loses every other value.
|
|
587
|
+
if (chosen !== undefined) return [k, safeScalar(v) ? v : MARK];
|
|
588
|
+
return [k, redactValue(v, known, depth + 1)];
|
|
589
|
+
}));
|
|
590
|
+
}
|
|
591
|
+
return value;
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
/** A JSON file's text with its values redacted by structure (it stays valid JSON); other text as free text. */
|
|
595
|
+
export function redactJsonText(text: string, secrets: Iterable<string> = []): string {
|
|
596
|
+
try { return JSON.stringify(redactValue(JSON.parse(text), secrets), null, 2) + "\n"; } catch { return redactText(text, secrets); }
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
// ---------------------------------------------------------------------------------------------------------------------
|
|
600
|
+
// The bundle.
|
|
601
|
+
|
|
602
|
+
const byteLength = (text: string) => Buffer.byteLength(text, "utf8");
|
|
603
|
+
|
|
604
|
+
/** The last `bytes` of text, starting at a whole line where one starts inside it. */
|
|
605
|
+
export function tailText(text: string, bytes: number): { text: string; cut: boolean } {
|
|
606
|
+
const buffer = Buffer.from(text, "utf8");
|
|
607
|
+
if (buffer.length <= bytes) return { text, cut: false };
|
|
608
|
+
if (bytes <= 0) return { text: "", cut: true };
|
|
609
|
+
let tail = buffer.subarray(buffer.length - bytes).toString("utf8").replace(/^�+/, "");
|
|
610
|
+
const newline = tail.indexOf("\n");
|
|
611
|
+
if (newline >= 0 && newline < tail.length - 1) tail = tail.slice(newline + 1);
|
|
612
|
+
while (byteLength(tail) > bytes) tail = tail.slice(1);
|
|
613
|
+
return { text: tail, cut: true };
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
/** The tail of a file, at most `bytes` read, from its first whole line. */
|
|
617
|
+
function readTail(file: string, bytes: number): { text: string; cut: boolean } {
|
|
618
|
+
const fd = fs.openSync(file, "r");
|
|
619
|
+
try {
|
|
620
|
+
const size = fs.fstatSync(fd).size;
|
|
621
|
+
const length = Math.min(size, bytes);
|
|
622
|
+
const buffer = Buffer.alloc(length);
|
|
623
|
+
let read = 0;
|
|
624
|
+
while (read < length) {
|
|
625
|
+
const n = fs.readSync(fd, buffer, read, length - read, size - length + read);
|
|
626
|
+
if (n <= 0) break;
|
|
627
|
+
read += n;
|
|
628
|
+
}
|
|
629
|
+
let text = buffer.subarray(0, read).toString("utf8");
|
|
630
|
+
if (length < size) {
|
|
631
|
+
// The first line may start mid-secret ("…ode=12345678"): drop it.
|
|
632
|
+
const newline = text.indexOf("\n");
|
|
633
|
+
text = newline >= 0 ? text.slice(newline + 1) : "";
|
|
634
|
+
}
|
|
635
|
+
return { text, cut: length < size };
|
|
636
|
+
} finally {
|
|
637
|
+
fs.closeSync(fd);
|
|
638
|
+
}
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
function checkName(name: string): void {
|
|
642
|
+
const parts = name.split(/[\\/]/);
|
|
643
|
+
if (!name || path.isAbsolute(name) || /^[A-Za-z]:/.test(name) || parts.some(part => part === ".." || part === "" || part === ".")) {
|
|
644
|
+
throw new Error(`Diagnostics file name ${JSON.stringify(name)} is not a plain relative path.`);
|
|
645
|
+
}
|
|
646
|
+
}
|
|
647
|
+
|
|
648
|
+
/**
|
|
649
|
+
* The bundle from sources, in their order: each file's tail, redacted. Small sources go in whole; the rest share what
|
|
650
|
+
* is left equally, keeping their newest lines. Names, contents and notes together stay within maxBytes.
|
|
651
|
+
*/
|
|
652
|
+
export function buildDiagnostics(options: { package: string; version: string; sources: DiagnosticsSource[]; notes?: string[]; fixedNotes?: string[]; maxBytes?: number; secrets?: string[] }): DiagnosticsResult {
|
|
653
|
+
const maxBytes = diagnosticsBudget(options.maxBytes);
|
|
654
|
+
// Notes are redacted as any text is; fixedNotes are this server's own sentences built only from constants (setting
|
|
655
|
+
// names it reads), whose secret words name settings: the patterns and known secrets go, the words stay.
|
|
656
|
+
const notes = [...(options.fixedNotes ?? []).map(note => redactPatterns(note, options.secrets)), ...(options.notes ?? []).map(note => redactText(note, options.secrets))];
|
|
657
|
+
const redact = (name: string, text: string) => name.endsWith(".json") ? redactJsonText(text, options.secrets) : redactText(text, options.secrets);
|
|
658
|
+
const entries: Array<{ name: string; text: string; cut: boolean }> = [];
|
|
659
|
+
for (const source of options.sources) {
|
|
660
|
+
checkName(source.name);
|
|
661
|
+
if ("text" in source) {
|
|
662
|
+
entries.push({ name: source.name, text: redact(source.name, source.text), cut: false });
|
|
663
|
+
continue;
|
|
664
|
+
}
|
|
665
|
+
let read: { text: string; cut: boolean };
|
|
666
|
+
try {
|
|
667
|
+
if (!fs.statSync(source.file).isFile()) throw Object.assign(new Error("not a file"), { code: "ENOTFILE" });
|
|
668
|
+
read = readTail(source.file, maxBytes);
|
|
669
|
+
} catch (error) {
|
|
670
|
+
const code = (error as NodeJS.ErrnoException)?.code;
|
|
671
|
+
notes.push(redactText(source.missing && code === "ENOENT" ? source.missing
|
|
672
|
+
: `${source.name}: not included (${code === "ENOENT" ? "no such file" : code ?? (error instanceof Error ? error.message : String(error))}).`, options.secrets));
|
|
673
|
+
continue;
|
|
674
|
+
}
|
|
675
|
+
entries.push({ name: source.name, text: redact(source.name, read.text), cut: read.cut });
|
|
676
|
+
}
|
|
677
|
+
|
|
678
|
+
// Notes first: the client shows them even when nothing else fits.
|
|
679
|
+
const kept: string[] = [];
|
|
680
|
+
let left = maxBytes;
|
|
681
|
+
for (const note of notes) {
|
|
682
|
+
const size = byteLength(note);
|
|
683
|
+
if (size > left) { left = 0; break; }
|
|
684
|
+
kept.push(note);
|
|
685
|
+
left -= size;
|
|
686
|
+
}
|
|
687
|
+
for (const entry of entries) left -= byteLength(entry.name);
|
|
688
|
+
left = Math.max(0, left);
|
|
689
|
+
|
|
690
|
+
const allowed = new Map<number, number>();
|
|
691
|
+
const order = entries.map((entry, index) => ({ index, size: byteLength(entry.text) })).sort((a, b) => a.size - b.size);
|
|
692
|
+
order.forEach(({ index, size }, position) => {
|
|
693
|
+
const share = Math.floor(left / (order.length - position));
|
|
694
|
+
const take = Math.min(size, share);
|
|
695
|
+
allowed.set(index, take);
|
|
696
|
+
left -= take;
|
|
697
|
+
});
|
|
698
|
+
|
|
699
|
+
const files: DiagnosticsFile[] = [];
|
|
700
|
+
let total = kept.reduce((sum, note) => sum + byteLength(note), 0);
|
|
701
|
+
entries.forEach((entry, index) => {
|
|
702
|
+
const fitted = tailText(entry.text, allowed.get(index) ?? 0);
|
|
703
|
+
const size = byteLength(entry.name) + byteLength(fitted.text);
|
|
704
|
+
if (total + size > maxBytes) return;
|
|
705
|
+
total += size;
|
|
706
|
+
files.push({ name: entry.name, content: fitted.text, truncated: entry.cut || fitted.cut });
|
|
707
|
+
});
|
|
708
|
+
return { package: options.package, version: options.version, files, notes: kept };
|
|
709
|
+
}
|
|
710
|
+
|
|
711
|
+
/**
|
|
712
|
+
* Settings whose values are known not to be secret (paths, hosts, ports, modes, models, timeouts). Any other setting
|
|
713
|
+
* shows only whether it is set: commands and their arguments, URLs and anything else can carry a credential.
|
|
714
|
+
*/
|
|
715
|
+
export const SAFE_SETTINGS = new Set(`BAMBU_CONFIRMATION_TIMEOUT_MS BAMBU_CONTROL_REPLY_MS BAMBU_DEFAULT_CONNECTION_MODE BAMBU_DEFAULT_CONNECTION_TYPE
|
|
716
|
+
BAMBU_DISPATCH_CHECK_MS BAMBU_EMERGENCY_MQTT_TIMEOUT_MS BAMBU_MODEL BAMBU_NATIVE_ALLOW_UNPINNED_PLUGIN BAMBU_NATIVE_CERT_DIR
|
|
717
|
+
BAMBU_NATIVE_CERT_FILE BAMBU_NATIVE_CONFIG_DIR BAMBU_NATIVE_HELPER BAMBU_NATIVE_HOME BAMBU_NATIVE_PLUGIN BAMBU_NATIVE_TRY_EMMC_PRINT
|
|
718
|
+
BAMBU_NETWORK_CONFIG_DIR BAMBU_NETWORK_COUNTRY_CODE BAMBU_NETWORK_PLUGIN_DIR BAMBU_NOZZLE_TYPE BAMBU_NOZZLE_VOLUME_TYPE BAMBU_PRINTER_HOST
|
|
719
|
+
BAMBU_PRINTER_MODEL BAMBU_PRINTER_SERIAL BAMBU_PROFILES_ROOT BAMBU_RESUME_STATE_DIR BAMBU_SERIAL BAMBU_SLICER_PROFILE
|
|
720
|
+
BAMBU_SLICER_PROFILE_DIRS BAMBU_STOP_ACK_MS BAMBU_STUDIO_CONFIG BAMBU_STUDIO_PATH BAMBU_TEMPLATE_3MF_PATH BAMBU_TEMPLATE_DIR BED_TYPE
|
|
721
|
+
BLENDER_MCP_TIMEOUT_MS CAD_DATA_DIR CAD_OUTPUT_DIRS FILAMENT_PROFILE FULU_ORCA_PATH FULU_ORCA_PLUGIN_DIR FUSION_MCP_HOST FUSION_MCP_PORT
|
|
722
|
+
FUSION_MCP_TIMEOUT_MS MCP_HTTP_HOST MCP_HTTP_JSON_RESPONSE MCP_HTTP_PATH MCP_HTTP_PORT MCP_HTTP_STATEFUL MCP_TRANSPORT NOZZLE_DIAMETER
|
|
723
|
+
ORCASLICER_BAMBULAB_PATH ORCASLICER_BAMBULAB_PLUGIN_DIR ORCA_SLICER_BAMBULAB_PATH PJARCZAK_BAMBU_COUNTRY_CODE PJARCZAK_BAMBU_PLUGIN_DIR
|
|
724
|
+
PJARCZAK_EXPECTED_BAMBU_NETWORK_VERSION PJARCZAK_MAC_RUNTIME_DIR PRINTER_HOST PRINTER_PORT PRINTER_TYPE PRINT_CONFIRMATION_TIMEOUT_MS
|
|
725
|
+
REPETIER_SLUG SLICER_API_MODE SLICER_API_TIMEOUT_MS SLICER_FILAMENT_PROFILE SLICER_PATH SLICER_PROFILE SLICER_STALL_SECONDS
|
|
726
|
+
SLICER_TIMEOUT_MS SLICER_TYPE TEMP_DIR`.split(/\s+/));
|
|
727
|
+
|
|
728
|
+
/** The environment settings whose names match, sorted: a known non-secret one with its value, any other only "[set]" (or ""). */
|
|
729
|
+
export function settingsSnapshot(names: RegExp, env: NodeJS.ProcessEnv = process.env): Record<string, string> {
|
|
730
|
+
return Object.fromEntries(Object.entries(env)
|
|
731
|
+
.filter((entry): entry is [string, string] => names.test(entry[0]) && typeof entry[1] === "string")
|
|
732
|
+
.sort(([a], [b]) => a.localeCompare(b))
|
|
733
|
+
.map(([name, value]) => [name, SAFE_SETTINGS.has(name) || !value ? value : "[set]"]));
|
|
734
|
+
}
|
|
735
|
+
|
|
736
|
+
/** The text of a JSON value for a bundle file. */
|
|
737
|
+
export const jsonText = (value: unknown) => JSON.stringify(value, null, 2) + "\n";
|
|
738
|
+
|
|
739
|
+
/** The newest `count` *.log files directly in each folder, newest first; absent folders are skipped. */
|
|
740
|
+
export function newestLogs(folders: string[], count: number, exclude: string[] = []): string[] {
|
|
741
|
+
const found: Array<{ file: string; mtime: number }> = [];
|
|
742
|
+
for (const folder of folders) {
|
|
743
|
+
let names: string[];
|
|
744
|
+
try { names = fs.readdirSync(folder); } catch { continue; }
|
|
745
|
+
for (const name of names) {
|
|
746
|
+
if (!/\.log(?:\.\d+)?$/i.test(name) || exclude.includes(name)) continue;
|
|
747
|
+
const file = path.join(folder, name);
|
|
748
|
+
try {
|
|
749
|
+
const stat = fs.statSync(file);
|
|
750
|
+
if (stat.isFile()) found.push({ file, mtime: stat.mtimeMs });
|
|
751
|
+
} catch { /* gone */ }
|
|
752
|
+
}
|
|
753
|
+
}
|
|
754
|
+
return found.sort((a, b) => b.mtime - a.mtime).slice(0, count).map(entry => entry.file);
|
|
755
|
+
}
|
|
756
|
+
|
|
757
|
+
/**
|
|
758
|
+
* The version of the app an executable belongs to, read without running it: a macOS bundle's Info.plist
|
|
759
|
+
* (CFBundleShortVersionString, else CFBundleVersion), else a version in the file's own name (AppImage). Undefined
|
|
760
|
+
* when neither says.
|
|
761
|
+
*/
|
|
762
|
+
export function appVersion(executable: string | undefined): string | undefined {
|
|
763
|
+
if (!executable) return undefined;
|
|
764
|
+
const bundle = /^(.*?\.app)(?:[\\/]|$)/i.exec(executable)?.[1];
|
|
765
|
+
if (bundle) {
|
|
766
|
+
try {
|
|
767
|
+
const plist = fs.readFileSync(path.join(bundle, "Contents", "Info.plist"), "utf8");
|
|
768
|
+
for (const key of ["CFBundleShortVersionString", "CFBundleVersion"]) {
|
|
769
|
+
const value = new RegExp(`<key>${key}</key>\\s*<string>([^<]+)</string>`).exec(plist)?.[1]?.trim();
|
|
770
|
+
if (value) return value;
|
|
771
|
+
}
|
|
772
|
+
} catch { /* not readable */ }
|
|
773
|
+
}
|
|
774
|
+
return /(?:^|[_\-\sv])(\d+\.\d+\.\d+(?:\.\d+)?)(?=[_\-.\s]|$)/i.exec(path.basename(executable))?.[1];
|
|
775
|
+
}
|