The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 06:00 UTC
| 1 | import * as Redacted from "effect/Redacted"; |
| 2 | import * as Stream from "effect/Stream"; |
| 3 | import { isPlainObject } from "./data.ts"; |
| 4 | |
| 5 | /** |
| 6 | * Removing secrets from text that may echo them: API error bodies, Helm |
| 7 | * stderr, command output. Secrets come from the `Redacted` values a caller |
| 8 | * sent; each is matched raw, base64-encoded and JSON-escaped, since those are |
| 9 | * the forms an echo usually takes. |
| 10 | */ |
| 11 | |
| 12 | /** Marker written in place of a secret unless a caller picks another. */ |
| 13 | export const DEFAULT_REDACTION_MARKER = "<redacted>"; |
| 14 | |
| 15 | /** |
| 16 | * Plaintext strings wrapped in {@link Redacted} anywhere in `value`, including |
| 17 | * strings nested inside a redacted object or array. Plain values are ignored. |
| 18 | */ |
| 19 | export const collectRedactedSecrets = (value: unknown): string[] => { |
| 20 | const out: string[] = []; |
| 21 | const visit = (current: unknown, sensitive: boolean): void => { |
| 22 | if (Redacted.isRedacted(current)) { |
| 23 | visit(Redacted.value(current), true); |
| 24 | return; |
| 25 | } |
| 26 | |
| 27 | if (typeof current === "number") { |
| 28 | if (!sensitive) return; |
| 29 | const text = String(current); |
| 30 | if (text.length === 0) return; |
| 31 | out.push(text); |
| 32 | return; |
| 33 | } |
| 34 | |
| 35 | if (typeof current === "string") { |
| 36 | if (!sensitive || current.length === 0) return; |
| 37 | out.push(current); |
| 38 | // Env values are strings, so a redacted object arrives as JSON. |
| 39 | // Parse it so an echoed inner secret still scrubs. |
| 40 | const head = current.charCodeAt(0); |
| 41 | if (head !== 123 && head !== 91) return; |
| 42 | try { |
| 43 | const parsed: unknown = JSON.parse(current); |
| 44 | if (parsed !== null && typeof parsed === "object") visit(parsed, true); |
| 45 | } catch { |
| 46 | // Not JSON. The string itself is the secret. |
| 47 | } |
| 48 | return; |
| 49 | } |
| 50 | |
| 51 | if (Array.isArray(current)) { |
| 52 | for (const item of current) visit(item, sensitive); |
| 53 | return; |
| 54 | } |
| 55 | |
| 56 | if (isPlainObject(current)) { |
| 57 | for (const item of Object.values(current)) visit(item, sensitive); |
| 58 | } |
| 59 | }; |
| 60 | |
| 61 | visit(value, false); |
| 62 | return [...new Set(out)]; |
| 63 | }; |
| 64 | |
| 65 | /** The forms a secret is echoed in: raw, base64, and JSON-escaped. */ |
| 66 | const secretForms = (secret: string): string[] => [ |
| 67 | ...new Set( |
| 68 | [secret, Buffer.from(secret).toString("base64"), JSON.stringify(secret).slice(1, -1)].filter( |
| 69 | (form) => form.length > 0, |
| 70 | ), |
| 71 | ), |
| 72 | ]; |
| 73 | |
| 74 | /** |
| 75 | * A marker that cannot reproduce any secret: `preferred`, unless a secret |
| 76 | * appears inside it, then a private-use character absent from every secret. |
| 77 | */ |
| 78 | export const redactionMarker = ( |
| 79 | secrets: ReadonlyArray<string>, |
| 80 | preferred: string = DEFAULT_REDACTION_MARKER, |
| 81 | ): string => { |
| 82 | if (!secrets.some((secret) => preferred.includes(secret))) return preferred; |
| 83 | for (let codePoint = 0xe000; codePoint <= 0xf8ff; codePoint++) { |
| 84 | const candidate = String.fromCodePoint(codePoint); |
| 85 | if (secrets.every((secret) => !secret.includes(candidate))) return candidate; |
| 86 | } |
| 87 | for (let codePoint = 0xf0000; codePoint <= 0xffffd; codePoint++) { |
| 88 | const candidate = String.fromCodePoint(codePoint); |
| 89 | if (secrets.every((secret) => !secret.includes(candidate))) return candidate; |
| 90 | } |
| 91 | // Only a secret larger than the private-use space gets here. An empty |
| 92 | // replacement is safer than reproducing any part of it. |
| 93 | return ""; |
| 94 | }; |
| 95 | |
| 96 | // Secrets shorter than 4 characters only match whole tokens, so "42" does not |
| 97 | // eat "14293". Longer secrets match anywhere (`printf "prefix%ssuffix"`). |
| 98 | const SHORT_SECRET_LENGTH = 4; |
| 99 | |
| 100 | const replaceForm = (text: string, form: string, anywhere: boolean, marker: string): string => { |
| 101 | if (form.length === 0 || !text.includes(form)) return text; |
| 102 | if (anywhere) return text.replaceAll(form, marker); |
| 103 | const pattern = form.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); |
| 104 | return text.replace(new RegExp(`(?<![A-Za-z0-9_])${pattern}(?![A-Za-z0-9_])`, "g"), marker); |
| 105 | }; |
| 106 | |
| 107 | /** Replace every form of every secret in plain text with `marker`. */ |
| 108 | export const redactText = ( |
| 109 | text: string, |
| 110 | secrets: ReadonlyArray<string>, |
| 111 | marker: string = DEFAULT_REDACTION_MARKER, |
| 112 | ): string => { |
| 113 | if (secrets.length === 0 || text.length === 0) return text; |
| 114 | // One secret can be a prefix of another. Replace the longest form first or |
| 115 | // the shorter one splits the longer one and leaves its suffix behind. |
| 116 | const forms = new Map<string, boolean>(); |
| 117 | for (const secret of secrets) { |
| 118 | const anywhere = secret.length >= SHORT_SECRET_LENGTH; |
| 119 | for (const form of secretForms(secret)) { |
| 120 | forms.set(form, forms.get(form) === true || anywhere); |
| 121 | } |
| 122 | } |
| 123 | const ordered = [...forms.entries()].sort((left, right) => right[0].length - left[0].length); |
| 124 | let out = text; |
| 125 | for (const [form, anywhere] of ordered) out = replaceForm(out, form, anywhere, marker); |
| 126 | return out; |
| 127 | }; |
| 128 | |
| 129 | const redactNode = (value: unknown, secrets: ReadonlyArray<string>, marker: string): unknown => { |
| 130 | if (typeof value === "string") return redactText(value, secrets, marker); |
| 131 | if (typeof value === "number" && secrets.some((secret) => secret === String(value))) { |
| 132 | return marker; |
| 133 | } |
| 134 | if (Array.isArray(value)) return value.map((item) => redactNode(item, secrets, marker)); |
| 135 | if (isPlainObject(value)) { |
| 136 | return Object.fromEntries( |
| 137 | Object.entries(value).map(([key, child]) => [key, redactNode(child, secrets, marker)]), |
| 138 | ); |
| 139 | } |
| 140 | return value; |
| 141 | }; |
| 142 | |
| 143 | /** |
| 144 | * Like {@link redactText}, but a JSON document is redacted leaf by leaf, so a |
| 145 | * secret that only appears JSON-escaped inside a string is still caught. |
| 146 | * Text that is not JSON falls back to {@link redactText}. |
| 147 | */ |
| 148 | export const redactJson = ( |
| 149 | text: string, |
| 150 | secrets: ReadonlyArray<string>, |
| 151 | marker: string = DEFAULT_REDACTION_MARKER, |
| 152 | ): string => { |
| 153 | if (secrets.length === 0 || text.length === 0) return text; |
| 154 | try { |
| 155 | return JSON.stringify(redactNode(JSON.parse(text), secrets, marker)); |
| 156 | } catch { |
| 157 | return redactText(text, secrets, marker); |
| 158 | } |
| 159 | }; |
| 160 | |
| 161 | /** |
| 162 | * Redact a text stream without leaking a secret split across chunks. Every |
| 163 | * form of every secret matches as a substring; the earliest match wins, and |
| 164 | * the longest form wins a tie. A match is only replaced once no form starting |
| 165 | * at or before it can still grow in the next chunk, so the output does not |
| 166 | * depend on where the chunks split. |
| 167 | */ |
| 168 | export const redactStream = <E, R>( |
| 169 | stream: Stream.Stream<string, E, R>, |
| 170 | secrets: ReadonlyArray<string>, |
| 171 | marker: string = DEFAULT_REDACTION_MARKER, |
| 172 | ): Stream.Stream<string, E, R> => { |
| 173 | const forms = [...new Set(secrets.flatMap(secretForms))].sort( |
| 174 | (left, right) => right.length - left.length, |
| 175 | ); |
| 176 | if (forms.length === 0) return stream; |
| 177 | |
| 178 | // Split `text` into redacted output and the pending suffix still held back. |
| 179 | // At the end of the stream nothing can grow, so nothing is held back. |
| 180 | const scan = (text: string, final: boolean): readonly [pending: string, output: string] => { |
| 181 | let remaining = text; |
| 182 | let output = ""; |
| 183 | |
| 184 | while (remaining.length > 0) { |
| 185 | let matchIndex = -1; |
| 186 | let match: string | undefined; |
| 187 | for (const form of forms) { |
| 188 | const index = remaining.indexOf(form); |
| 189 | if ( |
| 190 | index >= 0 && |
| 191 | (matchIndex < 0 || |
| 192 | index < matchIndex || |
| 193 | (index === matchIndex && form.length > (match?.length ?? 0))) |
| 194 | ) { |
| 195 | matchIndex = index; |
| 196 | match = form; |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | // The longest suffix that is a proper prefix of some form. |
| 201 | let suffixLength = 0; |
| 202 | if (!final) { |
| 203 | for (const form of forms) { |
| 204 | const candidateLength = Math.min(form.length - 1, remaining.length); |
| 205 | for (let length = candidateLength; length > suffixLength; length--) { |
| 206 | if (form.startsWith(remaining.slice(-length))) { |
| 207 | suffixLength = length; |
| 208 | break; |
| 209 | } |
| 210 | } |
| 211 | } |
| 212 | } |
| 213 | const pendingIndex = remaining.length - suffixLength; |
| 214 | |
| 215 | // A form that may still complete at or before the match would win |
| 216 | // over it, so wait for more text before deciding. |
| 217 | if (match !== undefined && (suffixLength === 0 || matchIndex < pendingIndex)) { |
| 218 | output += remaining.slice(0, matchIndex) + marker; |
| 219 | remaining = remaining.slice(matchIndex + match.length); |
| 220 | continue; |
| 221 | } |
| 222 | |
| 223 | output += remaining.slice(0, pendingIndex); |
| 224 | remaining = remaining.slice(pendingIndex); |
| 225 | break; |
| 226 | } |
| 227 | |
| 228 | return [remaining, output] as const; |
| 229 | }; |
| 230 | |
| 231 | return stream.pipe( |
| 232 | Stream.mapAccum( |
| 233 | () => "", |
| 234 | (pending, chunk) => { |
| 235 | const [remaining, output] = scan(pending + chunk, false); |
| 236 | return [remaining, output.length === 0 ? [] : [output]] as const; |
| 237 | }, |
| 238 | { |
| 239 | onHalt: (pending) => { |
| 240 | const [, output] = scan(pending, true); |
| 241 | return output.length === 0 ? [] : [output]; |
| 242 | }, |
| 243 | }, |
| 244 | ), |
| 245 | ); |
| 246 | }; |