From 45d1d8ceec6a5b791d5faadfe20aed89150d42e5 Mon Sep 17 00:00:00 2001 From: Omer Cohen <639682+omercnet@users.noreply.github.com> Date: Sat, 21 Nov 2026 11:50:00 +0000 Subject: [PATCH 1/4] feat(logger-json): JSON logger with a fail-closed secret scrubber --- AGENTS.md | 3 +- bun.lock | 36 +++ packages/adapters/logger-json/package.json | 19 ++ packages/adapters/logger-json/src/index.ts | 72 ++++++ packages/adapters/logger-json/src/scrub.ts | 239 ++++++++++++++++++ .../adapters/logger-json/test/limits.test.ts | 239 ++++++++++++++++++ .../adapters/logger-json/test/logger.test.ts | 64 +++++ .../logger-json/test/redaction.test.ts | 191 ++++++++++++++ packages/adapters/logger-json/tsconfig.json | 17 ++ tools/licence-report/lock-licences.json | 52 ++++ tsconfig.json | 3 + 11 files changed, 934 insertions(+), 1 deletion(-) create mode 100644 packages/adapters/logger-json/package.json create mode 100644 packages/adapters/logger-json/src/index.ts create mode 100644 packages/adapters/logger-json/src/scrub.ts create mode 100644 packages/adapters/logger-json/test/limits.test.ts create mode 100644 packages/adapters/logger-json/test/logger.test.ts create mode 100644 packages/adapters/logger-json/test/redaction.test.ts create mode 100644 packages/adapters/logger-json/tsconfig.json diff --git a/AGENTS.md b/AGENTS.md index eae2fd1..7c873ad 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -84,7 +84,8 @@ stand in this document. There is no project OMP configuration. authoriser in `src/authorize`, tests in `test/`. - Adapters (database, auth, storage, and so on) live in their own packages under `packages/adapters/*`; `config-env` (reads the environment into the typed - config, the only place that reads `process.env`) exists, the others arrive later. Apps live in `apps/*`: `apps/web` + config, the only place that reads `process.env`) and `logger-json` (JSON logs with a + secret scrubber that fails closed) exist, the others arrive later. Apps live in `apps/*`: `apps/web` (the web client) and `packages/api-client` exist today as empty placeholders. - `tools/ci`: repository checks, with their own tests (`bun run test:tools`). - `.github/workflows`: CI. Change it only on purpose. diff --git a/bun.lock b/bun.lock index ad2a5c7..b360060 100644 --- a/bun.lock +++ b/bun.lock @@ -24,6 +24,14 @@ "zod": "4.6.7", }, }, + "packages/adapters/logger-json": { + "name": "@tektum/grimoire-adapter-logger-json", + "version": "0.0.0", + "dependencies": { + "@tektum/grimoire-core": "*", + "pino": "10.4.0", + }, + }, "packages/api-client": { "name": "@tektum/grimoire-api-client", "version": "0.0.0", @@ -85,6 +93,8 @@ "@oxc-project/types": ["@oxc-project/types@0.156.0", "", {}, "sha512-1Duyg48TdJNdyhEB8Xn1L36bT9Pv6GUtlKjHxOys8xkaF/rQq3XJE+bAurSK/AgIz5mGFsb77cn9Pb20+WCScw=="], + "@pinojs/redact": ["@pinojs/redact@0.4.0", "", {}, "sha512-k2ENnmBugE/rzQfEcdWHcCY+/FM3VLzH9cYEsbdsoqrvzAKRhUZeRNhAZvB8OitQJ1TBed3yqWtdjzS6wJKBwg=="], + "@rolldown/binding-android-arm-eabi": ["@rolldown/binding-android-arm-eabi@1.2.19", "", { "os": "android", "cpu": "arm" }, "sha512-e+Ebe0WT+9tbXmNKPIiC8YrWAHYwug3m6i4p3rZGGulfZri1jtI4HslT+OZnFf/OIHowWF71DTlHwXBP2SiA7A=="], "@rolldown/binding-android-arm64": ["@rolldown/binding-android-arm64@1.2.19", "", { "os": "android", "cpu": "arm64" }, "sha512-hOombrTJ+szt2ciEzS7oCA78lMPBgF5UMQWCJIyNfnrI7GLl8HYoFqCnpL2gcPOaXMqryGCU5ExdQKyDDcC+yA=="], @@ -121,6 +131,8 @@ "@tektum/grimoire-adapter-config-env": ["@tektum/grimoire-adapter-config-env@workspace:packages/adapters/config-env"], + "@tektum/grimoire-adapter-logger-json": ["@tektum/grimoire-adapter-logger-json@workspace:packages/adapters/logger-json"], + "@tektum/grimoire-api-client": ["@tektum/grimoire-api-client@workspace:packages/api-client"], "@tektum/grimoire-core": ["@tektum/grimoire-core@workspace:packages/core"], @@ -173,6 +185,8 @@ "assertion-error": ["assertion-error@2.0.1", "", {}, "sha512-Izi8RQcffqCeNVgFigKli1ssklIbpHnCYc6AknXGYoB6grJqyeby7jv12JUQgmTAnIDnbck1uxksT4dzN3PWBA=="], + "atomic-sleep": ["atomic-sleep@1.0.0", "", {}, "sha512-kNOjDqAh7px0XWNI+4QbzoiR/nTkHAWNud2uvnJquD1/x5a7EQZMJT0AczqK0Qn67oY/TTQ1LbUKajZpp3I9tQ=="], + "balanced-match": ["balanced-match@4.0.4", "", {}, "sha512-BLrgEcRTwX2o6gGxGOCNyMvGSp35YofuYzw9h1IMTRmKqttAZZVU67bdb9Pr2vUHA8+j3i2tJfjO6C6+4myGTA=="], "brace-expansion": ["brace-expansion@5.0.13", "", { "dependencies": { "balanced-match": "^4.0.2" } }, "sha512-Vh3dmFbc4wZeox7M9I/0L9GfFd83Su4+yCck/OF9Gi/P/dAHn0RT7X4K9R7gMNMhDck3DvhR6QXsnQdjjgwNvQ=="], @@ -293,6 +307,8 @@ "obug": ["obug@3.0.0", "", {}, "sha512-5vvB5+W7ePv+p3uqxi+RcW1XAzLW0/hxt3/4X4Lc4qHudzOhmBiBwOY6DRob4WnanAEGvNcLjF+KNOufrUoEQw=="], + "on-exit-leak-free": ["on-exit-leak-free@2.1.2", "", {}, "sha512-0eJJY6hXLGf1udHwfNftBqH+g73EU4B504nZeKpz1sYRKafAghwxEJunB2O7rDZkL4PGfsMVnTXZ2EjibbqcsA=="], + "optionator": ["optionator@0.9.4", "", { "dependencies": { "deep-is": "^0.1.3", "fast-levenshtein": "^2.0.6", "levn": "^0.4.1", "prelude-ls": "^1.2.1", "type-check": "^0.4.0", "word-wrap": "^1.2.5" } }, "sha512-6IpQ7mKUxRcZNLIObR0hz7lxsapSSIYNZJwXPGeF0mTVqGKFIXj1DQcMoT22S3ROcLyY/rz0PWaWZ9ayWmad9g=="], "p-limit": ["p-limit@3.1.0", "", { "dependencies": { "yocto-queue": "^0.1.0" } }, "sha512-TYOanM3wGwNGsZN2cVTYPArw454xnXj5qmWF1bEoAc4+cU/ol7GVh7odevjp1FNHduHc3KZMcFduxU5Xc6uJRQ=="], @@ -307,26 +323,46 @@ "picomatch": ["picomatch@4.0.7", "", {}, "sha512-qcJu88Q2IWqJsDD529JKMdwGm/dvInW4HvQnRwiH9JtihJvzGOscDtHE3x1pBKeUOTysQ8kVmLnJ2kJu7yhcGA=="], + "pino": ["pino@10.4.0", "", { "dependencies": { "@pinojs/redact": "^0.4.0", "atomic-sleep": "^1.0.0", "on-exit-leak-free": "^2.1.0", "pino-abstract-transport": "^3.0.0", "pino-std-serializers": "^7.0.0", "process-warning": "^5.0.0", "quick-format-unescaped": "^4.0.3", "real-require": "^1.0.0", "safe-stable-stringify": "^2.3.1", "sonic-boom": "^4.0.1", "thread-stream": "^4.0.0" }, "bin": "bin.js" }, "sha512-bk1ZMTwG/Vymx+zPoa28MhNzucFvXJWp3csrSuxuHPSMeUettaCoQ5Jvg07mtC4JGNpcfTwz41oVCnNgBK9+vA=="], + + "pino-abstract-transport": ["pino-abstract-transport@3.0.0", "", { "dependencies": { "split2": "^4.0.0" } }, "sha512-wlfUczU+n7Hy/Ha5j9a/gZNy7We5+cXp8YL+X+PG8S0KXxw7n/JXA3c46Y0zQznIJ83URJiwy7Lh56WLokNuxg=="], + + "pino-std-serializers": ["pino-std-serializers@7.1.0", "", {}, "sha512-BndPH67/JxGExRgiX1dX0w1FvZck5Wa4aal9198SrRhZjH3GxKQUKIBnYJTdj2HDN3UQAS06HlfcSbQj2OHmaw=="], + "postcss": ["postcss@8.5.33", "", { "dependencies": { "nanoid": "^3.3.20", "picocolors": "^1.1.1", "source-map-js": "^1.2.2" } }, "sha512-RVHEU9/Pp1Fnjr/MpAJsZoADukCKhfIi15OunEYiGktIkYLkPVvC1sioWolGDS7XByMTx9m/IGbro0Js13zhnQ=="], "prelude-ls": ["prelude-ls@1.2.1", "", {}, "sha512-vkcDPrRZo1QZLbn5RLGPpg/WmIQ65qoWWhcGKf/b5eplkkarX0m9z8ppCat4mlOqUsWpyNuYgO3VRyrYHSzX5g=="], + "process-warning": ["process-warning@5.1.1", "", {}, "sha512-oCkNVj4OKnKEcocVBcDfiIugN1/cE6rgfT3nCtzeqZPA8+rIxMaQ0xXzIkGP8Gj2qUaP1UR7uhJ1VD8lUCpzCg=="], + "punycode": ["punycode@2.3.1", "", {}, "sha512-vYt7UD1U9Wg6138shLtLOvdAu+8DsC/ilFtEVHcH+wydcSpNE20AfSOduf6MkRFahL5FY7X1oU7nKVZFtfq8Fg=="], "qified": ["qified@0.10.1", "", { "dependencies": { "hookified": "^2.1.1" } }, "sha512-+Owyggi9IxT1ePKGafcI87ubSmxol6smwJ+RAHDQlx9+9cPwFWDiKFFCPuWhr9ignlGpZ9vDQLw67N4dcTVFEA=="], + "quick-format-unescaped": ["quick-format-unescaped@4.0.4", "", {}, "sha512-tYC1Q1hgyRuHgloV/YXs2w15unPVh8qfu/qCTfhTYamaw7fyhumKa2yGpdSo87vY32rIclj+4fWYQXUMs9EHvg=="], + + "real-require": ["real-require@1.0.0", "", {}, "sha512-P4nbQYQfePJxRSmY+v/KINxVucm4NF3p3s7pJveMTtom52FR4YGltUQLB8idDXwDDWW+eYrWDFbuzUnjoWHF7g=="], + "rolldown": ["rolldown@1.2.19", "", { "dependencies": { "@oxc-project/types": "=0.156.0", "@rolldown/pluginutils": "^1.0.0" }, "optionalDependencies": { "@rolldown/binding-android-arm-eabi": "1.2.19", "@rolldown/binding-android-arm64": "1.2.19", "@rolldown/binding-darwin-arm64": "1.2.19", "@rolldown/binding-darwin-x64": "1.2.19", "@rolldown/binding-freebsd-x64": "1.2.19", "@rolldown/binding-linux-arm-gnueabihf": "1.2.19", "@rolldown/binding-linux-arm64-gnu": "1.2.19", "@rolldown/binding-linux-arm64-musl": "1.2.19", "@rolldown/binding-linux-ppc64-gnu": "1.2.19", "@rolldown/binding-linux-s390x-gnu": "1.2.19", "@rolldown/binding-linux-x64-gnu": "1.2.19", "@rolldown/binding-linux-x64-musl": "1.2.19", "@rolldown/binding-openharmony-arm64": "1.2.19", "@rolldown/binding-wasm32-wasi": "1.2.19", "@rolldown/binding-win32-arm64-msvc": "1.2.19", "@rolldown/binding-win32-x64-msvc": "1.2.19" }, "bin": "bin/cli.mjs" }, "sha512-QIiV1J9d0pzN4jeeTH4sjPy0AZ6cAy2cG0ExeoE1+U8Rvj+hR3gYkokx0YbeUNGlZqiQJJ2utcETe0oKXwzsuA=="], + "safe-stable-stringify": ["safe-stable-stringify@2.5.0", "", {}, "sha512-b3rppTKm9T+PsVCBEOUR46GWI7fdOs00VKZ1+9c1EWDaDMvjQc6tUwuFyIprgGgTcWoVHSKrU8H31ZHA2e0RHA=="], + "semver": ["semver@7.8.6", "", { "bin": "bin/semver.js" }, "sha512-9lpSSU4Mly3mrtruwLs1eLK6+gzbzqeBFwRNjIjE+9LuDpWdYctrQlQ3ej1Ilgi7gknpsTRnPoLP8DpCHqHIJw=="], "shebang-command": ["shebang-command@2.0.0", "", { "dependencies": { "shebang-regex": "^3.0.0" } }, "sha512-kHxr2zZpYtdmrN1qDjrrX/Z1rR1kG8Dx+gkpK1G4eXmvXswmcE1hTWBWYUzlraYw1/yZp6YuDY77YtvbN0dmDA=="], "shebang-regex": ["shebang-regex@3.0.0", "", {}, "sha512-7++dFhtcx3353uBaq8DDR4NuxBetBzC7ZQOhmTQInHEd6bSrXdiEyzCvG07Z44UYdLShWUyXt5M/yhz8ekcb1A=="], + "sonic-boom": ["sonic-boom@4.2.1", "", { "dependencies": { "atomic-sleep": "^1.0.0" } }, "sha512-w6AxtubXa2wTXAUsZMMWERrsIRAdrK0Sc+FUytWvYAhBJLyuI4llrMIC1DtlNSdI99EI86KZum2MMq3EAZlF9Q=="], + "source-map-js": ["source-map-js@1.2.4", "", {}, "sha512-J7ZjMvHd9O1fcT/5V09QWaaSl2BbWx3F6O08spde82Namf9UB0GxRVIPiNbVsdKkoizHiSpTodo3vp2bAy91MA=="], + "split2": ["split2@4.2.0", "", {}, "sha512-UcjcJOWknrNkF6PLX83qcHM6KHgVKNkV62Y8a5uYDVv9ydGQVwAHMKqHdJje1VTWpljG0WYpCDhrCdAOYH4TWg=="], + "std-env": ["std-env@4.3.0", "", {}, "sha512-OtU/EgQ1kIm5KwqQpBC6ZEMXrZRui11w8zgfTWp8cdO9B8OaPsbA8bTHO2P+HNo1VlUTGMVBwPhydu6poeXiag=="], + "thread-stream": ["thread-stream@4.2.1", "", { "dependencies": { "real-require": "^1.0.0" } }, "sha512-Stg6edJXC/6/xqGwSXwFokrUhChJ18amEjpKH89WrQQ0oVFDF+pUUcTpiwWhFUrE89Vf16FBOlwhaw+lescxMQ=="], + "tinybench": ["tinybench@6.2.4", "", {}, "sha512-SQ52jEncXxbMCzJuY2A/jGIbbAu6NYsBWW7grz2pNE+EnzQdoGaoBfpk4qC/opE7qiUcGmGgaGIz5EoHdS1O9w=="], "tinyexec": ["tinyexec@1.3.1", "", {}, "sha512-GCvB3aoys96IuDFBMcTB46JOR6mdMtAToqwiW8JlWhsoh1mhHi/xn9ss/Dg7N555GiJyEt2qzoG/NHCwM6h1EA=="], diff --git a/packages/adapters/logger-json/package.json b/packages/adapters/logger-json/package.json new file mode 100644 index 0000000..2d55d20 --- /dev/null +++ b/packages/adapters/logger-json/package.json @@ -0,0 +1,19 @@ +{ + "name": "@tektum/grimoire-adapter-logger-json", + "version": "0.0.0", + "private": true, + "license": "Apache-2.0", + "type": "module", + "main": "./dist/index.js", + "types": "./dist/index.d.ts", + "exports": { + ".": { + "types": "./dist/index.d.ts", + "default": "./dist/index.js" + } + }, + "dependencies": { + "@tektum/grimoire-core": "*", + "pino": "10.4.0" + } +} diff --git a/packages/adapters/logger-json/src/index.ts b/packages/adapters/logger-json/src/index.ts new file mode 100644 index 0000000..088758d --- /dev/null +++ b/packages/adapters/logger-json/src/index.ts @@ -0,0 +1,72 @@ +import { pino } from 'pino'; +import type { Logger as PinoLogger } from 'pino'; +import type { Clock } from '@tektum/grimoire-core/ports/clock'; +import type { Logger, LogLevel } from '@tektum/grimoire-core/ports/logger'; +import { redactString, scrubFields } from './scrub.js'; + +export { redactString } from './scrub.js'; + +export interface JsonLoggerOptions { + readonly level?: LogLevel; + /** Receives pino's output (one JSON line ending in a newline). Defaults to standard output. */ + readonly destination?: { write(line: string): unknown }; + readonly clock?: Clock; + readonly fields?: Record; +} + +// First layer: pino's own path redaction for the known secret names (one level deep). +const SECRET_NAMES = ['token', 'password', 'passwd', 'pwd', 'secret', 'authorization', 'cookie', 'apikey', 'api_key', 'access_key', 'secret_key', 'session', 'credential', 'credentials']; +const REDACT_PATHS = SECRET_NAMES.flatMap((n) => [n, `*.${n}`]); + +interface TsState { value: string } + +function wrap(p: PinoLogger, ts: TsState, now: () => string | undefined): Logger { + const emit = (level: LogLevel, event: string, fields?: Record): void => { + try { + const t = now(); + ts.value = t ?? new Date().toISOString(); + const clean = scrubFields(fields); + if (t === undefined) clean['clockFallback'] = true; + p[level](clean, redactString(String(event))); + } catch { + // The logger never throws into its caller. + } + }; + return { + debug: (e, f) => emit('debug', e, f), + info: (e, f) => emit('info', e, f), + warn: (e, f) => emit('warn', e, f), + error: (e, f) => emit('error', e, f), + child: (fields) => { + try { + return wrap(p.child(scrubFields(fields)), ts, now); + } catch { + return wrap(p, ts, now); + } + }, + }; +} + +export function createJsonLogger(opts: JsonLoggerOptions = {}): Logger { + const clock = opts.clock; + // The clock is read once per event; an unusable reading falls back to real time and the event + // carries `clockFallback: true`. + const now = (): string | undefined => { + try { + return new Date(clock ? clock.now() : Date.now()).toISOString(); + } catch { + return undefined; + } + }; + const ts: TsState = { value: new Date().toISOString() }; + const options = { + level: opts.level ?? 'info', + messageKey: 'event', + base: null, + timestamp: (): string => `,"ts":"${ts.value}"`, + formatters: { level: (label: string) => ({ level: label }) }, + redact: { paths: REDACT_PATHS, censor: '[redacted]' }, + }; + const root = opts.destination ? pino(options, opts.destination) : pino(options); + return wrap(opts.fields ? root.child(scrubFields(opts.fields)) : root, ts, now); +} diff --git a/packages/adapters/logger-json/src/scrub.ts b/packages/adapters/logger-json/src/scrub.ts new file mode 100644 index 0000000..402ad0a --- /dev/null +++ b/packages/adapters/logger-json/src/scrub.ts @@ -0,0 +1,239 @@ +/* + * Why this exists next to pino: the research (docs/research/12-reference-architectures.md, Part 3) + * measured that pino's path-based `redact` misses keys nested deeper than its wildcards reach, + * URL query strings, and connection strings inside messages, error messages and stacks. + * This scrubber is applied to every value before it reaches pino. It fails closed: when a string + * is ambiguous, more is removed, never less. It also flattens values pino cannot serialise safely + * (getters, proxies, Map, Set, Date, Error, bigint, functions) into plain data. + */ +export const REDACTED = '[redacted]'; +export const UNSERIALISABLE = '[unserialisable]'; +export const MAX_STRING_CHARS = 2048; +const MAX_DEPTH = 8; +const MAX_ITEMS = 200; +const MAX_NODES = 2000; +const TRUNCATED = '[truncated]'; + +// Fail-closed key rule. The key is normalised two ways. (1) Joined: lower case, letters only; it is +// secret when it CONTAINS a strong word (so `token2`, `tokenizer`, `secretary`, `sessionless` are +// redacted; accepted over-redaction). (2) Segments: split on any non-alphanumeric, camelCase and +// letter/digit boundaries; secret when a segment EQUALS a short word (so `key2`, `sort_key` are +// redacted but `keyboard`, `turkey`, `bypass`, `author` are not). `code` is deliberately absent: +// it would hide error codes. A key longer than MAX_KEY_CHARS is always treated as secret. +const STRONG_WORDS = [ + 'password', 'passwd', 'pwd', 'passphrase', 'secret', 'token', 'jwt', 'bearer', 'authorization', 'cookie', + 'session', 'credential', 'signature', 'apikey', 'privatekey', 'accesskey', 'secretkey', 'signingkey', + 'encryptionkey', 'sshkey', 'connectionstring', 'databaseurl', 'dsn', 'csrf', 'xsrf', 'mnemonic', +]; +const SHORT_WORDS = new Set([ + 'pass', 'auth', 'sid', 'sig', 'otp', 'pin', 'salt', 'key', 'query', 'search', 'nonce', 'hmac', 'pem', 'seed', 'cert', +]); +export const MAX_KEY_CHARS = 256; +export function isSecretKey(key: string): boolean { + if (key.length > MAX_KEY_CHARS) return true; + const joined = key.toLowerCase().replace(/[^a-z]/g, ''); + if (STRONG_WORDS.some((w) => joined.includes(w))) return true; + const spaced = key + .replace(/([a-z])([A-Z])/g, '$1 $2') + .replace(/([a-zA-Z])(\d)/g, '$1 $2') + .replace(/(\d)([a-zA-Z])/g, '$1 $2'); + return spaced.toLowerCase().split(/[^a-z0-9]+/).some((x) => SHORT_WORDS.has(x)); +} + +/** Text for a key: scrubbed, and cut (with a suffix) when over-long. */ +function keyText(key: string): string { + return key.length > MAX_KEY_CHARS + ? `${redactString(key.slice(0, MAX_KEY_CHARS))}…[truncated ${key.length - MAX_KEY_CHARS} chars]` + : redactString(key); +} + +const SECRET_WORD = + /password|passwd|pwd|secret|token|api[-_ ]?key|access[-_ ]?key|session|credential|authorization|cookie|private|passphrase|signature|auth|\bkey\b|\bsid\b/i; + +const isKeyChar = (c: string): boolean => /[\w-]/.test(c); + +/** key=value / key: value; the value of a secret-looking key runs to the next ; or newline. Linear. */ +function cutKeyValues(s: string): string { + let out = ''; + let pos = 0; // everything before pos is already emitted + for (let i = 0; i < s.length; i++) { + const c = s[i]; + if (c !== '=' && c !== ':') continue; + let b = i; + while (b > pos && s[b - 1] === ' ') b--; + let a = b; + while (a > pos && isKeyChar(s[a - 1] ?? '')) a--; + if (a === b || !SECRET_WORD.test(s.slice(a, b))) continue; + let end = i + 1; + while (end < s.length && s[end] !== ';' && s[end] !== '\n') end++; + out += `${s.slice(pos, a)}${s.slice(a, b)}=${REDACTED}`; + pos = end; + i = end - 1; + } + return out + s.slice(pos); +} + +/** Per whitespace-delimited token: cut at a fragment, or at a query (a URL's query to the end). */ +function cutQueriesAndFragments(s: string): string { + let out = ''; + let pos = 0; + const re = /\S+/g; + for (let m = re.exec(s); m !== null; m = re.exec(s)) { + out += s.slice(pos, m.index); + pos = re.lastIndex; + const tok = m[0]; + const q = tok.indexOf('?'); + const h = tok.indexOf('#', 1); + if (h > 0 && (q < 0 || h < q)) { + out += `${tok.slice(0, h)}#${REDACTED}`; + } else if (q >= 0) { + out += `${tok.slice(0, q)}?${REDACTED}`; + const scheme = tok.indexOf('://'); + if (scheme >= 0 && scheme < q) return out; // a URL's query may contain spaces: drop the rest + } else { + out += tok; + } + } + return out + s.slice(pos); +} + +/** + * Removes userinfo, key=value secrets, bearer/basic credentials, JWTs, query strings, fragments. + * Input is capped first; every step is a single linear pass. + */ +export function redactString(input: string): string { + let s = input; + let truncated = 0; + if (s.length > MAX_STRING_CHARS) { + truncated = s.length - MAX_STRING_CHARS; + s = s.slice(0, MAX_STRING_CHARS); + // the cut may fall inside a secret: drop the trailing partial token + let e = s.length; + while (e > 0 && !/\s/.test(s[e - 1] ?? '')) e--; + s = s.slice(0, e); + } + // userinfo: from the first :// to the LAST @ (the password may contain @ / : ? # or spaces) + const scheme = s.indexOf('://'); + const at = s.lastIndexOf('@'); + if (scheme >= 0 && at > scheme) s = `${s.slice(0, scheme + 3)}${REDACTED}${s.slice(at)}`; + s = cutKeyValues(s); + s = s.replace(/\b(bearer|basic)\s+[^\s;,]+/gi, `$1 ${REDACTED}`); + // JWT-shaped: a run of word, dot and dash characters that contains eyJ and a dot + s = s.replace(/[\w.-]+/g, (run) => (run.includes('eyJ') && run.includes('.') ? REDACTED : run)); + s = cutQueriesAndFragments(s); + return truncated > 0 ? `${s}…[truncated ${truncated} chars]` : s; +} + +interface Budget { nodes: number; readonly seen: WeakSet } + +function safeGet(obj: object, key: string): unknown { + try { + return (obj as Record)[key]; + } catch { + return UNSERIALISABLE; + } +} + +/** A placeholder for anything that carries binary data (Buffer, typed arrays, Blob, ...). */ +function binaryPlaceholder(value: object): string | undefined { + if (ArrayBuffer.isView(value) || value instanceof ArrayBuffer || value instanceof SharedArrayBuffer) { + return `[binary ${value.byteLength} bytes]`; + } + if (value instanceof Blob) return `[binary ${value.size} bytes]`; + const o = value as Record; + if (typeof o['arrayBuffer'] === 'function' || typeof o['byteLength'] === 'number' || 'buffer' in o) { + const n = typeof o['byteLength'] === 'number' ? o['byteLength'] : o['size']; + return `[binary ${typeof n === 'number' ? n : '?'} bytes]`; + } + return undefined; +} + +/** + * Map keys. Strings are kept (and scrubbed later). Primitives are written with their type so that + * the number 1 and the string '1' stay distinct: `[number 1]`, `[boolean true]`, `[bigint 5]`. + * Other keys become a placeholder numbered in order of appearance within the Map, so they never + * overwrite each other: `[binary 24 bytes #1]`, `[non-string #2]`. + */ +function mapKey(k: unknown, counter: { n: number }): string { + if (typeof k === 'string') return k; + if (typeof k === 'number' || typeof k === 'boolean' || typeof k === 'bigint') return `[${typeof k} ${String(k)}]`; + const label = typeof k === 'object' && k !== null ? (binaryPlaceholder(k) ?? '[non-string]') : '[non-string]'; + return `${label.slice(0, -1)} #${++counter.n}]`; +} + +function scrub(value: unknown, depth: number, b: Budget): unknown { + try { + if (typeof value === 'string') return redactString(value); + if (typeof value === 'bigint') return value.toString(); + if (typeof value === 'function' || typeof value === 'symbol') return UNSERIALISABLE; + if (value === null || typeof value !== 'object') return value; + if (depth >= MAX_DEPTH || b.seen.has(value) || ++b.nodes > MAX_NODES) return TRUNCATED; + b.seen.add(value); + try { + return scrubObject(value, depth, b); + } finally { + b.seen.delete(value); // only ancestors count as cycles; a shared reference is logged each time + } + } catch { + return UNSERIALISABLE; + } +} + +function scrubObject(value: object, depth: number, b: Budget): unknown { + const binary = binaryPlaceholder(value); + if (binary !== undefined) return binary; + if (value instanceof Date) { + return Number.isNaN(value.getTime()) ? '[invalid-date]' : value.toISOString(); + } + if (value instanceof Error) { + return { + name: scrub(value.name, depth + 1, b), + message: scrub(value.message, depth + 1, b), + stack: scrub(value.stack, depth + 1, b), + cause: value.cause === undefined ? undefined : scrub(value.cause, depth + 1, b), + }; + } + if (Array.isArray(value) || value instanceof Set) { + const items: unknown[] = []; + const source: Iterable = + value instanceof Set + ? value + : Array.from({ length: Math.min(value.length, MAX_ITEMS + 1) }, (_, i) => safeGet(value, String(i))); + let n = 0; + for (const v of source) { + if (++n > MAX_ITEMS) { items.push(TRUNCATED); break; } + items.push(scrub(v, depth + 1, b)); + } + return items; + } + const entries: [unknown, unknown][] = []; + let total: number; + if (value instanceof Map) { + total = value.size; + for (const kv of value) { + if (entries.length >= MAX_ITEMS) break; + entries.push(kv); + } + } else { + const keys = Object.keys(value); + total = keys.length; + for (const k of keys.slice(0, MAX_ITEMS)) entries.push([k, safeGet(value, k)]); + } + const out: Record = Object.create(null) as Record; + const counter = { n: 0 }; + for (const [rawKey, v] of entries) { + const k = typeof rawKey === 'string' && !(value instanceof Map) ? rawKey : mapKey(rawKey, counter); + out[keyText(k)] = isSecretKey(k) ? REDACTED : scrub(v, depth + 1, b); + } + if (total > MAX_ITEMS) out[TRUNCATED] = `${total - MAX_ITEMS} more`; + return out; +} + +/** Scrub a field bag; reserved output keys are dropped. Never throws. */ +export function scrubFields(fields: Record | undefined): Record { + const out = scrub(fields ?? {}, 0, { nodes: 0, seen: new WeakSet() }); + if (typeof out !== 'object' || out === null || Array.isArray(out)) return { fields: UNSERIALISABLE }; + const clean: Record = {}; + for (const [k, v] of Object.entries(out)) if (k !== 'ts' && k !== 'level' && k !== 'event') clean[k] = v; + return clean; +} diff --git a/packages/adapters/logger-json/test/limits.test.ts b/packages/adapters/logger-json/test/limits.test.ts new file mode 100644 index 0000000..f061a3d --- /dev/null +++ b/packages/adapters/logger-json/test/limits.test.ts @@ -0,0 +1,239 @@ +import { describe, expect, it } from 'vitest'; +import { createJsonLogger } from '../src/index.js'; +import { MAX_STRING_CHARS, isSecretKey, redactString } from '../src/scrub.js'; + +const S = 'LEAKME'; +const MB = 1_000_000; + +function run(fields: Record): string { + const lines: string[] = []; + createJsonLogger({ level: 'debug', destination: { write: (l) => lines.push(l.trimEnd()) }, clock: { now: () => 0 } }).info('ev', fields); + expect(lines).toHaveLength(1); + JSON.parse(lines[0] ?? ''); + return lines[0] ?? ''; +} + +const shapes: readonly (readonly [string, string])[] = [ + ['all x', 'x'.repeat(MB)], + ['all ?', '?'.repeat(MB)], + ['all #', '#'.repeat(MB)], + ['all @', '@'.repeat(MB)], + ['a= repeated', 'a='.repeat(MB / 2)], + [':// repeated', '://'.repeat(MB / 3)], + ['alternating ?#', '?#'.repeat(MB / 2)], + ['run of %', '%'.repeat(MB)], + ['base64-like run', 'QUJDREVGR0hJSktMTU5PUFFSU1RVVldYWVo0123456789+/'.repeat(MB / 48)], + ['run of spaces', ' '.repeat(MB)], + ['token= repeated', 'token='.repeat(MB / 6)], + ['eyJ repeated', 'eyJ'.repeat(MB / 3)], + ['bearer repeated', 'bearer '.repeat(MB / 7)], +]; + +describe('scrubbing is linear and bounded (1 MB inputs)', () => { + it.each(shapes)('%s finishes in under 50 ms', (_n, value) => { + const t0 = performance.now(); + redactString(value); + run({ v: value, nested: { v: [value] } }); + expect(performance.now() - t0).toBeLessThan(50); + }); + + it('truncates long strings with a suffix and never keeps a secret past the cap', () => { + const out = redactString(`${'word '.repeat(MAX_STRING_CHARS)}${S}`); + expect(out).not.toContain(S); + expect(out).toMatch(/…\[truncated \d+ chars\]$/); + expect(out.length).toBeLessThan(MAX_STRING_CHARS + 60); + expect(run({ v: `${'word '.repeat(MB / 5)}postgres://u:${S}@h/db` })).not.toContain(S); + }); + + it('drops a trailing partial token cut by the cap', () => { + const out = redactString(`${'a '.repeat(MAX_STRING_CHARS / 2 - 1)}postgres://u:${S}`); + expect(out).not.toContain('postgres'); + }); + + it('caps depth, items and nodes', () => { + const o = JSON.parse(run({ list: Array.from({ length: 1000 }, (_, i) => i) })) as { list: unknown[] }; + expect(o.list).toHaveLength(201); + expect(o.list[200]).toBe('[truncated]'); + const wide = Object.fromEntries(Array.from({ length: 1000 }, (_, i) => [`k${i}`, i])); + const wideOut = (JSON.parse(run({ wide })) as { wide: Record }).wide; + expect(Object.keys(wideOut)).toHaveLength(201); + expect(wideOut['[truncated]']).toBe('800 more'); + const bigMap = new Map(Array.from({ length: 300 }, (_, i) => [`m${i}`, i] as const)); + expect((JSON.parse(run({ bigMap })) as { bigMap: Record }).bigMap['[truncated]']).toBe('100 more'); + const many = Array.from({ length: 150 }, () => Array.from({ length: 150 }, () => ({ a: 1 }))); + expect(run({ many }).length).toBeLessThan(200_000); + let deep: unknown = S; + for (let i = 0; i < 30; i++) deep = { n: deep }; + expect(run({ deep })).not.toContain(S); + }); +}); + +const secretKeys = [ + 'private_key', 'privateKey', 'passphrase', 'pass-phrase', 'jwt', 'bearer', 'Bearer', 'auth', 'x-auth', 'sid', + 'signature', 'otp', 'pin', 'key', 'salt', 'ssh_key', 'signing_key', 'encryption_key', 'sort_key', 'primaryKey', + 'token', 'PASSWORD', 'pass_word', 'apiKey', 'x-api-key', 'Set-Cookie', 'clientSecret', 'refreshToken', + 'sessionId', 'authToken', 'x-auth-token', 'credentials', 'connectionString', 'database_url', 'dsn', 'query', +]; +const visibleKeys = ['keyboard', 'monkey', 'passenger', 'author', 'authority', 'pinned', 'signal', 'code', 'status', 'event2', 'page', 'turkey', 'hockey', 'compass', 'bypass']; +// over-redacted on purpose: the joined key contains a strong word +const overRedacted = ['sessionless', 'tokenizer', 'secretary']; + +describe('key-name rule', () => { + it.each(secretKeys)('redacts %s', (k) => { + expect(isSecretKey(k)).toBe(true); + expect(run({ [k]: S })).not.toContain(S); + }); + it.each(visibleKeys)('keeps %s visible', (k) => { + expect(isSecretKey(k)).toBe(false); + expect(run({ [k]: 'visible-value' })).toContain('visible-value'); + }); +}); + +const strong = ['password', 'passwd', 'pwd', 'passphrase', 'secret', 'token', 'jwt', 'bearer', 'authorization', 'cookie', 'session', 'credential', 'credentials', 'signature', 'apikey', 'privatekey', 'accesskey', 'secretkey', 'signingkey', 'encryptionkey', 'sshkey', 'connectionstring', 'databaseurl', 'dsn', 'csrf', 'xsrf', 'mnemonic']; +const short = ['pass', 'auth', 'sid', 'sig', 'otp', 'pin', 'salt', 'key', 'query', 'search', 'nonce', 'hmac', 'pem', 'seed', 'cert']; +const cap = (w: string): string => w.charAt(0).toUpperCase() + w.slice(1); +const forms = (w: string): string[] => [`${w}2`, `2${w}`, `${w}_2`, `${w}2x`, `x${w}7`, `${w}-3`, `my_${w}9`, `my${cap(w)}9`, `${w}.4`, `${w.toUpperCase()}5`]; + +describe('key rule with digits (K1)', () => { + it.each(strong)('strong word %s with digit prefixes and suffixes', (w) => { + for (const k of forms(w)) { + expect(isSecretKey(k), k).toBe(true); + expect(run({ [k]: S })).not.toContain(S); + } + }); + it.each(short)('short word %s with digit prefixes and suffixes', (w) => { + for (const k of [`${w}2`, `2${w}`, `${w}_2`, `x_${w}7`, `${w}-3`, `my_${w}9`, `my${cap(w)}9`, `${w}.4`, `${w.toUpperCase()}5`]) { + expect(isSecretKey(k), k).toBe(true); + expect(run({ [k]: S })).not.toContain(S); + } + }); + it.each(overRedacted)('%s is redacted by the rule', (k) => { + expect(isSecretKey(k)).toBe(true); + }); +}); + +describe('long keys (K3)', () => { + it.each([255, 256, 257, 300, 1000])('a %i-character key ending in a secret word is redacted and truncated', (n) => { + for (const k of ['k'.repeat(n) + '_token', 'k'.repeat(n) + 'Password']) { + const line = run({ [k]: S }); + expect(line).not.toContain(S); + expect(isSecretKey(k)).toBe(true); + } + const line = run({ ['k'.repeat(1000) + '_x']: S }); + expect(line).toMatch(/…\[truncated \d+ chars\]/); + expect(line.length).toBeLessThan(600); + }); +}); + +describe('Map keys (K2)', () => { + const conn = Buffer.from(`postgres://u:${S}@h/db`); + const kinds: readonly (readonly [string, unknown])[] = [ + ['Buffer', conn], + ['Uint8Array', new Uint8Array(conn)], + ['Int8Array', new Int8Array(conn)], + ['Uint8ClampedArray', new Uint8ClampedArray(conn)], + ['Uint16Array', new Uint16Array(4)], + ['Int16Array', new Int16Array(4)], + ['Uint32Array', new Uint32Array(4)], + ['Int32Array', new Int32Array(4)], + ['Float32Array', new Float32Array(4)], + ['Float64Array', new Float64Array(4)], + ['BigInt64Array', new BigInt64Array(2)], + ['BigUint64Array', new BigUint64Array(2)], + ['DataView', new DataView(new ArrayBuffer(24))], + ['object', { toString: () => `postgres://u:${S}@h/db`, x: S }], + ['symbol', Symbol(S)], + ['function', function leakme() { return S; }], + ['Blob', new Blob([conn])], + ]; + it.each(kinds)('%s as a Map key never leaks', (_n, key) => { + const line = run({ m: new Map([['k', 1], [key, 2]]) }); + expect(line).not.toContain(S); + expect(line).not.toMatch(/\d+,\d+,\d+/); + }); + it('writes number and boolean keys as text and binary keys as placeholders', () => { + const o = JSON.parse(run({ m: new Map([[1, 'a'], [true, 'b'], [new Uint8Array(24), 'c']]) })) as { m: Record }; + expect(o.m).toEqual({ '[number 1]': 'a', '[boolean true]': 'b', '[binary 24 bytes #1]': 'c' }); + }); +}); + +describe('Map key scheme', () => { + it('keeps distinct non-string keys distinct and the number 1 apart from the string 1', () => { + const m = new Map([[1, 'num'], ['1', 'str'], [{ a: 1 }, 'o1'], [{ b: 2 }, 'o2'], [Symbol('s'), 's'], + [new Uint8Array(3), 'b1'], [new Uint8Array(3), 'b2'], [1n, 'big']]); + const o = JSON.parse(run({ m })) as { m: Record }; + expect(o.m).toEqual({ + '[number 1]': 'num', '1': 'str', '[non-string #1]': 'o1', '[non-string #2]': 'o2', '[non-string #3]': 's', + '[binary 3 bytes #4]': 'b1', '[binary 3 bytes #5]': 'b2', '[bigint 1]': 'big', + }); + }); +}); + +describe('over-long keys without a secret word', () => { + const key = (n: number): string => 'k'.repeat(n); + it('257 k: value redacted, key written truncated with the suffix', () => { + expect(isSecretKey(key(257))).toBe(true); + const line = run({ [key(257)]: 'visible-value' }); + expect(line).not.toContain('visible-value'); + expect(line).toContain(`${key(256)}…[truncated 1 chars]`); + }); + it.each([255, 256])('%i k: value stays visible', (n) => { + expect(isSecretKey(key(n))).toBe(false); + const line = run({ [key(n)]: 'visible-value' }); + expect(line).toContain('visible-value'); + expect(line).not.toContain('truncated'); + }); +}); + +describe('truncation marker (S1) and shared references (S3)', () => { + it('marks an object cut at 200 entries', () => { + const o = JSON.parse(run({ w: Object.fromEntries(Array.from({ length: 301 }, (_, i) => [`k${i}`, i])) })) as { w: Record }; + expect(o.w['[truncated]']).toBe('101 more'); + }); + it('logs a shared (non-circular) reference every time', () => { + const v = { c: 1 }; + const line = run({ a: { b: [v, { c: v }] } }); + expect(line).not.toContain('truncated'); + }); +}); + +describe('Blob and binary-like objects', () => { + it('Blob and File are placeholders', () => { + expect(run({ b: new Blob(['abc']) })).toContain('"b":"[binary 3 bytes]"'); + expect(run({ f: new File(['abcd'], 'x.txt') })).toContain('"f":"[binary 4 bytes]"'); + }); + it('objects exposing arrayBuffer, byteLength or buffer are placeholders', () => { + expect(run({ o: { arrayBuffer: () => 1, size: 7 } })).toContain('[binary 7 bytes]'); + expect(run({ o: { byteLength: 9, data: S } })).not.toContain(S); + expect(run({ o: { buffer: Buffer.from(S) } })).not.toContain(S); + }); +}); + +describe('clock fallback (S4)', () => { + it.each([[() => NaN], [() => { throw new Error('c'); }]])('marks events with clockFallback', (now) => { + const lines: string[] = []; + createJsonLogger({ destination: { write: (l) => lines.push(l) }, clock: { now } }).info('e'); + const o = JSON.parse(lines[0] ?? '') as Record; + expect(o['clockFallback']).toBe(true); + expect(Number.isNaN(Date.parse(String(o['ts'])))).toBe(false); + }); +}); + +describe('binary values', () => { + const conn = `postgres://u:${S}@h/db`; + const values: readonly (readonly [string, unknown, number])[] = [ + ['Buffer', Buffer.from(conn), conn.length], + ['Uint8Array', new Uint8Array(Buffer.from(conn)), conn.length], + ['Float64Array', new Float64Array(3), 24], + ['ArrayBuffer', new ArrayBuffer(24), 24], + ['SharedArrayBuffer', new SharedArrayBuffer(24), 24], + ['DataView', new DataView(new ArrayBuffer(24)), 24], + ]; + it.each(values)('%s is a placeholder', (_n, v, bytes) => { + const line = run({ b: v, nested: { x: [v] } }); + expect(line).not.toContain(S); + expect(line).not.toMatch(/\[\d+,\d+/); + expect(line).not.toContain('"0":'); + expect(line).toContain(`[binary ${bytes} bytes]`); + }); +}); diff --git a/packages/adapters/logger-json/test/logger.test.ts b/packages/adapters/logger-json/test/logger.test.ts new file mode 100644 index 0000000..6667d3b --- /dev/null +++ b/packages/adapters/logger-json/test/logger.test.ts @@ -0,0 +1,64 @@ +import { describe, expect, it } from 'vitest'; +import { FakeClock } from '../../../core/src/testing/fake-clock.js'; +import { createJsonLogger } from '../src/index.js'; + +function capture(level?: 'debug' | 'info' | 'warn' | 'error') { + const lines: string[] = []; + const clock = new FakeClock(Date.UTC(2026, 10, 20)); + const log = createJsonLogger({ destination: { write: (l) => lines.push(l.trimEnd()) }, clock, ...(level ? { level } : {}) }); + return { lines, log, clock }; +} + +describe('logger-json', () => { + it('writes one parseable JSON line with ts, level and event', () => { + const { lines, log } = capture(); + log.info('connect', { page: 'p1', bytes: 5 }); + log.warn('refused', { reason: 'rate' }); + expect(lines).toHaveLength(2); + for (const l of lines) { + expect(l).not.toContain('\n'); + const o = JSON.parse(l) as Record; + expect(typeof o['ts']).toBe('string'); + expect(['info', 'warn']).toContain(o['level']); + expect(typeof o['event']).toBe('string'); + } + expect(JSON.parse(lines[0] ?? '')).toEqual({ page: 'p1', bytes: 5, ts: '2026-11-20T00:00:00.000Z', level: 'info', event: 'connect' }); + }); + + it('never writes a token field or a URL query string', () => { + const { lines, log } = capture(); + log.info('connect', { + token: 'abc.secret.jwt', + nested: { Password: 'pw-123', authToken: 't2', list: ['ok'] }, + url: 'wss://host/collab?token=abc.secret.jwt&x=1', + dsn: 'postgres://u:dbpass-99@h/db', + note: 'GET /p?token=zzz-leak failed', + }); + const out = lines.join('\n'); + for (const secret of ['abc.secret.jwt', 'pw-123', 't2', 'dbpass-99', 'zzz-leak', 'x=1']) { + expect(out).not.toContain(secret); + } + expect(out).toContain('wss://host/collab'); + expect(out).toContain('"ok"'); + expect(JSON.parse(lines[0] ?? '')).toMatchObject({ ts: expect.any(String), level: 'info', event: 'connect' }); + }); + + it('filters below the level and merges child fields', () => { + const { lines, log } = capture('warn'); + log.info('dropped'); + log.child({ role: 'api' }).error('boom', { err: new Error('failed at /x?token=q1') }); + expect(lines).toHaveLength(1); + expect(lines[0]).toContain('"role":"api"'); + expect(lines[0]).not.toContain('q1'); + }); + + it('does not let fields override ts, level or event, and survives cycles', () => { + const { lines, log } = capture(); + const cyc: Record = {}; + cyc['self'] = cyc; + log.info('real', { level: 'fake', event: 'fake', ts: 'x', cyc }); + const o = JSON.parse(lines[0] ?? '') as Record; + expect(o).toMatchObject({ level: 'info', event: 'real' }); + expect(o['ts']).not.toBe('x'); + }); +}); diff --git a/packages/adapters/logger-json/test/redaction.test.ts b/packages/adapters/logger-json/test/redaction.test.ts new file mode 100644 index 0000000..ec07996 --- /dev/null +++ b/packages/adapters/logger-json/test/redaction.test.ts @@ -0,0 +1,191 @@ +import { describe, expect, it } from 'vitest'; +import { createJsonLogger } from '../src/index.js'; + +const S = 'LEAKME'; + +function run(fields: Record, clockNow: () => number = () => 0, event = 'ev'): string[] { + const lines: string[] = []; + const log = createJsonLogger({ level: 'debug', destination: { write: (l) => lines.push(l.trimEnd()) }, clock: { now: clockNow } }); + log.info(event, fields); + return lines; +} + +function expectSafe(lines: string[], marker = S): void { + expect(lines).toHaveLength(1); + const line = lines[0] ?? ''; + expect(line).not.toContain(marker); + expect(line).not.toContain('\n'); + const o = JSON.parse(line) as Record; + expect(['ts', 'level', 'event'].every((k) => k in o)).toBe(true); +} + +const stringCases: readonly (readonly [string, string])[] = [ + ['url with bare query', `https://h/p?${S}`], + ['host/path with bare query', `h.com/p?${S}`], + ['password containing @', `postgres://u:p@ss${S}@h/db`], + ['password containing /', `postgres://u:p/${S}@h/db`], + ['password containing :', `postgres://u:a:${S}@h/db`], + ['password containing ?', `postgres://u:a?${S}@h/db`], + ['password containing #', `postgres://u:a#${S}@h/db`], + ['password containing spaces', `postgres://u:a ${S} b@h/db`], + ['userinfo without password', `https://${S}@h/db`], + ['connection text Password=', `Server=h;Password=${S}`], + ['Password= upper case spaced', `Server=h; PASSWORD = ${S};Db=x`], + ['pwd=', `host=h;pwd=${S}`], + ['secret=', `secret=${S}`], + ['token=', `a=1;token=${S}`], + ['apikey=', `apikey=${S}`], + ['api_key:', `API_KEY: ${S}`], + ['Authorization Bearer', `Authorization: Bearer ${S}`], + ['authorization lower no spaces', `authorization:bearer ${S}`], + ['Basic header', `Authorization: Basic ${S}`], + ['bare Basic', `sent Basic ${S} to server`], + ['bare bearer opaque', `got bearer ${S}${S}${S}`], + ['JWT', `eyJ${S}.eyJ${S}.sig${S}`], + ['JWT in sentence', `token was eyJhbGci.eyJ${S}.abc end`], + ['fragment', `https://h/p#${S}`], + ['fragment on path', `/cb#access_${S}`], + ['query with ? only in value', `GET /p?a=b?${S} failed`], + ['query without =', `/p?${S}`], + ['query with spaces after URL', `https://h/p?a=1 ${S}`], + ['sentence with url query', `see http://h/a?x=${S}&y=1 ok`], + ['Set-Cookie text', `Set-Cookie: session=${S}; Path=/`], +]; + +describe('string redaction fails closed', () => { + it.each(stringCases)('%s', (_name, value) => { + expectSafe(run({ v: value })); + expectSafe(run({ deep: { list: [value, { x: [value] }] } })); + expectSafe(run({}, () => 0, `ev ${value}`)); + expectSafe(run({ err: new Error(`failed: ${value}`) })); + expectSafe(run({ err: new Error('outer', { cause: new Error(value) }) })); + }); + + it('removes secrets from error stacks', () => { + const e = new Error('x'); + e.stack = `Error: x\n at connect (postgres://u:${S}@h/db)`; + expectSafe(run({ e })); + }); + + it('keeps harmless text', () => { + const line = run({ page: 'p1', note: 'saved 5 bytes' })[0] ?? ''; + expect(line).toContain('saved 5 bytes'); + }); +}); + +const keyNames = [ + 'token', 'Token', 'TOKEN', 'authToken', 'x-auth-token', 'password', 'Password', 'passwd', 'pwd', 'secret', + 'client_secret', 'authorization', 'Authorization', 'cookie', 'set-cookie', 'Set_Cookie', 'apikey', 'apiKey', + 'api_key', 'api-key', 'access_key', 'accessKey', 'secret_key', 'session', 'sessionId', 'credential', 'credentials', +]; + +describe('secret-named keys', () => { + it.each(keyNames)('redacts the value of %s at any depth', (key) => { + for (const value of [S, { inner: S }, [S], 42 as unknown]) { + const marker = typeof value === 'number' ? '42' : S; + expectSafe(run({ [key]: value }), marker); + expectSafe(run({ a: { b: [{ [key]: value }] } }), marker); + } + }); +}); + +describe('the logger never throws', () => { + const cases: readonly (readonly [string, () => Record, () => number])[] = [ + ['throwing getter', () => ({ get g(): string { throw new Error('boom'); } }), () => 0], + ['proxy ownKeys throws', () => ({ p: new Proxy({}, { ownKeys() { throw new Error('p'); } }) }), () => 0], + ['proxy get throws', () => ({ p: new Proxy({ a: 1 }, { get() { throw new Error('p'); } }) }), () => 0], + ['top-level proxy', () => new Proxy({}, { ownKeys() { throw new Error('p'); } }), () => 0], + ['clock NaN', () => ({ a: 1 }), () => NaN], + ['clock Infinity', () => ({ a: 1 }), () => Infinity], + ['clock throws', () => ({ a: 1 }), () => { throw new Error('c'); }], + ['toJSON throws', () => ({ t: { toJSON() { throw new Error('t'); } } }), () => 0], + ['symbol keys and values', () => ({ [Symbol('s')]: 1, v: Symbol('x') }), () => 0], + ['functions', () => ({ f: () => 1 }), () => 0], + ['bigint', () => ({ b: 10n }), () => 0], + ['circular', () => { const c: Record = {}; c['c'] = c; return c; }, () => 0], + ['invalid date', () => ({ d: new Date(NaN) }), () => 0], + ]; + it.each(cases)('%s', (_n, make, now) => { + const lines = run(make(), now); + expect(lines).toHaveLength(1); + const o = JSON.parse(lines[0] ?? '') as Record; + expect(Number.isNaN(Date.parse(String(o['ts'])))).toBe(false); + expect(o['level']).toBe('info'); + expect(o['event']).toBe('ev'); + }); + + it('writes placeholders for unserialisable parts', () => { + const o = JSON.parse(run({ f: () => 1, get g(): string { throw new Error('x'); } })[0] ?? '') as Record; + expect(o['f']).toBe('[unserialisable]'); + expect(o['g']).toBe('[unserialisable]'); + }); + + it('does not throw when the writer throws', () => { + const log = createJsonLogger({ destination: { write: () => { throw new Error('w'); } } }); + expect(() => log.error('e')).not.toThrow(); + }); + + it('logs dates as ISO strings and Map and Set as data', () => { + const o = JSON.parse(run({ d: new Date(0), m: new Map([['a', 1]]), s: new Set([1, 2]) })[0] ?? '') as Record; + expect(o['d']).toBe('1970-01-01T00:00:00.000Z'); + expect(o['m']).toEqual({ a: 1 }); + expect(o['s']).toEqual([1, 2]); + }); + + it('redacts secret-named Map keys', () => { + expectSafe(run({ m: new Map([['password', S]]) })); + }); +}); + +describe('property: a random marker never survives', () => { + let seed = 12345; + const rnd = (n: number): number => { + seed = (Math.imul(seed, 1664525) + 1013904223) >>> 0; + return seed % n; + }; + const pick = (xs: readonly T[]): T => xs[rnd(xs.length)] as T; + const marker = (): string => `MK${rnd(1e9).toString(36)}${rnd(1e9).toString(36)}Z`; + const pwChars = ['@', '/', ':', '?', '#', ' ', 'a', 'B', '9', '!', ';', '=']; + const pw = (m: string): string => { + const parts = [m]; + for (let i = rnd(4); i > 0; i--) parts.push(pick(pwChars)); + return parts.join(''); + }; + const secretKeys = ['token', 'Password', 'x-api-key', 'client_secret', 'Set-Cookie', 'sessionId', 'Authorization', 'token2', '2password', 'key_7', 'otp3']; + + const stringWith = (m: string): string => + pick<() => string>([ + () => `${pick(['http', 'https', 'postgres', 'wss'])}://u:${pw(m)}@h:5/db`, + () => `${pick(['https://h/p', 'h.com/x', '/rel'])}?${pick(['', 'a=', 'token='])}${m}${pick(['', '&y=1'])}`, + () => `https://h/p#${m}`, + () => `Server=h;${pick(['Password', 'pwd', 'SECRET', 'Token', 'ApiKey'])}${pick(['=', ' = ', ': '])}${m};x=1`, + () => `Authorization: ${pick(['Bearer', 'Basic', 'bearer'])} ${m}`, + () => `eyJ${m}.eyJ${m}.${m}`, + ])(); + + const nested = (m: string, depth: number): unknown => { + if (depth === 0) return pick<() => unknown>([ + () => stringWith(m), + () => new Error(stringWith(m)), + () => ({ [pick(secretKeys)]: m }), + () => ({ ['k'.repeat(257 + rnd(50))]: m }), // secret only through the long-key rule + ])(); + return pick<() => unknown>([ + () => ({ [pick(['a', 'b', 'c'])]: nested(m, depth - 1), z: 1 }), + () => [nested(m, depth - 1), 'x'], + () => new Map([['k', nested(m, depth - 1)]]), + () => new Map([[Buffer.from(`postgres://u:${m}@h/db`), nested(m, depth - 1)]]), + () => new Map([[{ toString: () => m, secret: m }, nested(m, depth - 1)], [new Uint16Array([rnd(60000)]), 1]]), + () => new Map([[Symbol(m), m.length > 0 ? 'ok' : 'ok']]), + () => new Set([nested(m, depth - 1)]), + ])(); + }; + + it('holds over 500 random structures', () => { + for (let i = 0; i < 500; i++) { + const m = marker(); + const lines = run({ root: nested(m, rnd(4)) }, () => 0, `ev ${stringWith(m)}`); + expectSafe(lines, m); + } + }); +}); diff --git a/packages/adapters/logger-json/tsconfig.json b/packages/adapters/logger-json/tsconfig.json new file mode 100644 index 0000000..45f00ef --- /dev/null +++ b/packages/adapters/logger-json/tsconfig.json @@ -0,0 +1,17 @@ +{ + "extends": "../../../tsconfig.base.json", + "compilerOptions": { + "composite": true, + "rootDir": "src", + "outDir": "dist", + "tsBuildInfoFile": "dist/tsconfig.tsbuildinfo" + }, + "include": [ + "src" + ], + "references": [ + { + "path": "../../core" + } + ] +} diff --git a/tools/licence-report/lock-licences.json b/tools/licence-report/lock-licences.json index c6b5764..38318a2 100644 --- a/tools/licence-report/lock-licences.json +++ b/tools/licence-report/lock-licences.json @@ -99,6 +99,10 @@ "licence": "MIT", "integrity": "sha512-1Duyg48TdJNdyhEB8Xn1L36bT9Pv6GUtlKjHxOys8xkaF/rQq3XJE+bAurSK/AgIz5mGFsb77cn9Pb20+WCScw==" }, + "@pinojs/redact@0.4.0": { + "licence": "MIT", + "integrity": "sha512-k2ENnmBugE/rzQfEcdWHcCY+/FM3VLzH9cYEsbdsoqrvzAKRhUZeRNhAZvB8OitQJ1TBed3yqWtdjzS6wJKBwg==" + }, "@rolldown/binding-android-arm-eabi@1.2.19": { "licence": "MIT", "integrity": "sha512-e+Ebe0WT+9tbXmNKPIiC8YrWAHYwug3m6i4p3rZGGulfZri1jtI4HslT+OZnFf/OIHowWF71DTlHwXBP2SiA7A==" @@ -259,6 +263,10 @@ "licence": "MIT", "integrity": "sha512-Izi8RQcffqCeNVgFigKli1ssklIbpHnCYc6AknXGYoB6grJqyeby7jv12JUQgmTAnIDnbck1uxksT4dzN3PWBA==" }, + "atomic-sleep@1.0.0": { + "licence": "MIT", + "integrity": "sha512-kNOjDqAh7px0XWNI+4QbzoiR/nTkHAWNud2uvnJquD1/x5a7EQZMJT0AczqK0Qn67oY/TTQ1LbUKajZpp3I9tQ==" + }, "balanced-match@4.0.4": { "licence": "MIT", "integrity": "sha512-BLrgEcRTwX2o6gGxGOCNyMvGSp35YofuYzw9h1IMTRmKqttAZZVU67bdb9Pr2vUHA8+j3i2tJfjO6C6+4myGTA==" @@ -511,6 +519,10 @@ "licence": "MIT", "integrity": "sha512-5vvB5+W7ePv+p3uqxi+RcW1XAzLW0/hxt3/4X4Lc4qHudzOhmBiBwOY6DRob4WnanAEGvNcLjF+KNOufrUoEQw==" }, + "on-exit-leak-free@2.1.2": { + "licence": "MIT", + "integrity": "sha512-0eJJY6hXLGf1udHwfNftBqH+g73EU4B504nZeKpz1sYRKafAghwxEJunB2O7rDZkL4PGfsMVnTXZ2EjibbqcsA==" + }, "optionator@0.9.4": { "licence": "MIT", "integrity": "sha512-6IpQ7mKUxRcZNLIObR0hz7lxsapSSIYNZJwXPGeF0mTVqGKFIXj1DQcMoT22S3ROcLyY/rz0PWaWZ9ayWmad9g==" @@ -539,6 +551,18 @@ "licence": "MIT", "integrity": "sha512-qcJu88Q2IWqJsDD529JKMdwGm/dvInW4HvQnRwiH9JtihJvzGOscDtHE3x1pBKeUOTysQ8kVmLnJ2kJu7yhcGA==" }, + "pino-abstract-transport@3.0.0": { + "licence": "MIT", + "integrity": "sha512-wlfUczU+n7Hy/Ha5j9a/gZNy7We5+cXp8YL+X+PG8S0KXxw7n/JXA3c46Y0zQznIJ83URJiwy7Lh56WLokNuxg==" + }, + "pino-std-serializers@7.1.0": { + "licence": "MIT", + "integrity": "sha512-BndPH67/JxGExRgiX1dX0w1FvZck5Wa4aal9198SrRhZjH3GxKQUKIBnYJTdj2HDN3UQAS06HlfcSbQj2OHmaw==" + }, + "pino@10.4.0": { + "licence": "MIT", + "integrity": "sha512-bk1ZMTwG/Vymx+zPoa28MhNzucFvXJWp3csrSuxuHPSMeUettaCoQ5Jvg07mtC4JGNpcfTwz41oVCnNgBK9+vA==" + }, "postcss@8.5.33": { "licence": "MIT", "integrity": "sha512-RVHEU9/Pp1Fnjr/MpAJsZoADukCKhfIi15OunEYiGktIkYLkPVvC1sioWolGDS7XByMTx9m/IGbro0Js13zhnQ==" @@ -547,6 +571,10 @@ "licence": "MIT", "integrity": "sha512-vkcDPrRZo1QZLbn5RLGPpg/WmIQ65qoWWhcGKf/b5eplkkarX0m9z8ppCat4mlOqUsWpyNuYgO3VRyrYHSzX5g==" }, + "process-warning@5.1.1": { + "licence": "MIT", + "integrity": "sha512-oCkNVj4OKnKEcocVBcDfiIugN1/cE6rgfT3nCtzeqZPA8+rIxMaQ0xXzIkGP8Gj2qUaP1UR7uhJ1VD8lUCpzCg==" + }, "punycode@2.3.1": { "licence": "MIT", "integrity": "sha512-vYt7UD1U9Wg6138shLtLOvdAu+8DsC/ilFtEVHcH+wydcSpNE20AfSOduf6MkRFahL5FY7X1oU7nKVZFtfq8Fg==" @@ -555,10 +583,22 @@ "licence": "MIT", "integrity": "sha512-+Owyggi9IxT1ePKGafcI87ubSmxol6smwJ+RAHDQlx9+9cPwFWDiKFFCPuWhr9ignlGpZ9vDQLw67N4dcTVFEA==" }, + "quick-format-unescaped@4.0.4": { + "licence": "MIT", + "integrity": "sha512-tYC1Q1hgyRuHgloV/YXs2w15unPVh8qfu/qCTfhTYamaw7fyhumKa2yGpdSo87vY32rIclj+4fWYQXUMs9EHvg==" + }, + "real-require@1.0.0": { + "licence": "MIT", + "integrity": "sha512-P4nbQYQfePJxRSmY+v/KINxVucm4NF3p3s7pJveMTtom52FR4YGltUQLB8idDXwDDWW+eYrWDFbuzUnjoWHF7g==" + }, "rolldown@1.2.19": { "licence": "MIT", "integrity": "sha512-QIiV1J9d0pzN4jeeTH4sjPy0AZ6cAy2cG0ExeoE1+U8Rvj+hR3gYkokx0YbeUNGlZqiQJJ2utcETe0oKXwzsuA==" }, + "safe-stable-stringify@2.5.0": { + "licence": "MIT", + "integrity": "sha512-b3rppTKm9T+PsVCBEOUR46GWI7fdOs00VKZ1+9c1EWDaDMvjQc6tUwuFyIprgGgTcWoVHSKrU8H31ZHA2e0RHA==" + }, "semver@7.8.6": { "licence": "ISC", "integrity": "sha512-9lpSSU4Mly3mrtruwLs1eLK6+gzbzqeBFwRNjIjE+9LuDpWdYctrQlQ3ej1Ilgi7gknpsTRnPoLP8DpCHqHIJw==" @@ -571,14 +611,26 @@ "licence": "MIT", "integrity": "sha512-7++dFhtcx3353uBaq8DDR4NuxBetBzC7ZQOhmTQInHEd6bSrXdiEyzCvG07Z44UYdLShWUyXt5M/yhz8ekcb1A==" }, + "sonic-boom@4.2.1": { + "licence": "MIT", + "integrity": "sha512-w6AxtubXa2wTXAUsZMMWERrsIRAdrK0Sc+FUytWvYAhBJLyuI4llrMIC1DtlNSdI99EI86KZum2MMq3EAZlF9Q==" + }, "source-map-js@1.2.4": { "licence": "BSD-3-Clause", "integrity": "sha512-J7ZjMvHd9O1fcT/5V09QWaaSl2BbWx3F6O08spde82Namf9UB0GxRVIPiNbVsdKkoizHiSpTodo3vp2bAy91MA==" }, + "split2@4.2.0": { + "licence": "ISC", + "integrity": "sha512-UcjcJOWknrNkF6PLX83qcHM6KHgVKNkV62Y8a5uYDVv9ydGQVwAHMKqHdJje1VTWpljG0WYpCDhrCdAOYH4TWg==" + }, "std-env@4.3.0": { "licence": "MIT", "integrity": "sha512-OtU/EgQ1kIm5KwqQpBC6ZEMXrZRui11w8zgfTWp8cdO9B8OaPsbA8bTHO2P+HNo1VlUTGMVBwPhydu6poeXiag==" }, + "thread-stream@4.2.1": { + "licence": "MIT", + "integrity": "sha512-Stg6edJXC/6/xqGwSXwFokrUhChJ18amEjpKH89WrQQ0oVFDF+pUUcTpiwWhFUrE89Vf16FBOlwhaw+lescxMQ==" + }, "tinybench@6.2.4": { "licence": "MIT", "integrity": "sha512-SQ52jEncXxbMCzJuY2A/jGIbbAu6NYsBWW7grz2pNE+EnzQdoGaoBfpk4qC/opE7qiUcGmGgaGIz5EoHdS1O9w==" diff --git a/tsconfig.json b/tsconfig.json index 065e010..79c2210 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -13,6 +13,9 @@ { "path": "./packages/adapters/config-env" }, + { + "path": "./packages/adapters/logger-json" + }, { "path": "./packages/core/tsconfig.test.json" } From 6e32302db4467bc6a81a85561e7719d08324ce2a Mon Sep 17 00:00:00 2001 From: Omer Cohen <639682+omercnet@users.noreply.github.com> Date: Sat, 21 Nov 2026 12:46:22 +0000 Subject: [PATCH 2/4] fix(logger-json): redact quoted keys and judge key=value pairs by the structured key rule --- packages/adapters/logger-json/src/scrub.ts | 29 ++++++++++++++----- .../logger-json/test/redaction.test.ts | 22 ++++++++++++++ 2 files changed, 43 insertions(+), 8 deletions(-) diff --git a/packages/adapters/logger-json/src/scrub.ts b/packages/adapters/logger-json/src/scrub.ts index 402ad0a..8f3c6c2 100644 --- a/packages/adapters/logger-json/src/scrub.ts +++ b/packages/adapters/logger-json/src/scrub.ts @@ -47,12 +47,15 @@ function keyText(key: string): string { : redactString(key); } -const SECRET_WORD = - /password|passwd|pwd|secret|token|api[-_ ]?key|access[-_ ]?key|session|credential|authorization|cookie|private|passphrase|signature|auth|\bkey\b|\bsid\b/i; - const isKeyChar = (c: string): boolean => /[\w-]/.test(c); -/** key=value / key: value; the value of a secret-looking key runs to the next ; or newline. Linear. */ +const isQuote = (c: string | undefined): boolean => c === '"' || c === "'"; + +/** + * key=value / key: value, also with a quoted key ("password": "x", 'token' = 'x'), judged by the same rule as a structured field + * (isSecretKey). The value of a secret-looking key runs to the closing quote when it is quoted, otherwise to the next ; or newline. + * Linear. + */ function cutKeyValues(s: string): string { let out = ''; let pos = 0; // everything before pos is already emitted @@ -61,12 +64,22 @@ function cutKeyValues(s: string): string { if (c !== '=' && c !== ':') continue; let b = i; while (b > pos && s[b - 1] === ' ') b--; + const closing = b > pos && isQuote(s[b - 1]) ? s[b - 1] : ''; + if (closing !== '') b--; let a = b; while (a > pos && isKeyChar(s[a - 1] ?? '')) a--; - if (a === b || !SECRET_WORD.test(s.slice(a, b))) continue; - let end = i + 1; - while (end < s.length && s[end] !== ';' && s[end] !== '\n') end++; - out += `${s.slice(pos, a)}${s.slice(a, b)}=${REDACTED}`; + if (a === b || !isSecretKey(s.slice(a, b))) continue; + let start = i + 1; + while (start < s.length && s[start] === ' ') start++; + const q = isQuote(s[start]) ? (s[start] ?? '') : ''; + let end = start; + if (q !== '') { + end = s.indexOf(q, start + 1); + end = end === -1 ? s.length : end + 1; + } else { + while (end < s.length && s[end] !== ';' && s[end] !== '\n') end++; + } + out += `${s.slice(pos, a)}${s.slice(a, b)}${closing}=${REDACTED}`; pos = end; i = end - 1; } diff --git a/packages/adapters/logger-json/test/redaction.test.ts b/packages/adapters/logger-json/test/redaction.test.ts index ec07996..b14930a 100644 --- a/packages/adapters/logger-json/test/redaction.test.ts +++ b/packages/adapters/logger-json/test/redaction.test.ts @@ -1,5 +1,6 @@ import { describe, expect, it } from 'vitest'; import { createJsonLogger } from '../src/index.js'; +import { redactString } from '../src/scrub.js'; const S = 'LEAKME'; @@ -189,3 +190,24 @@ describe('property: a random marker never survives', () => { } }); }); + +describe('secrets inside strings, whatever the key quoting or the key word', () => { + it('redacts the values of quoted keys (embedded JSON and object reprs)', () => { + for (const text of ['upstream failed: {"password":"hunter2","token":"abc"}', "failed: {'password': 'hunter2'}", 'x {"api_key" : "hunter2"}']) { + const out = redactString(text); + expect(out, text).not.toContain('hunter2'); + expect(out, text).not.toContain('abc'); + } + }); + + it('uses the key rule of the structured path for key=value pairs (otp, pin, salt, nonce ...)', () => { + for (const word of ['otp', 'pin', 'salt', 'nonce', 'hmac', 'seed', 'pass', 'csrf']) { + expect(redactString(`${word}=LEAKME`), word).not.toContain('LEAKME'); + expect(redactString(`${word}: LEAKME`), word).not.toContain('LEAKME'); + } + }); + + it('keeps the text around a quoted secret readable', () => { + expect(redactString('failed: {"password":"hunter2","user":"ann"}')).toContain('"user":"ann"'); + }); +}); From d3a780cc684a7d19769524553285e570781581d4 Mon Sep 17 00:00:00 2001 From: Omer Cohen <639682+omercnet@users.noreply.github.com> Date: Sat, 21 Nov 2026 12:55:05 +0000 Subject: [PATCH 3/4] fix(logger-json): skip escaped quotes in quoted values; only real binary values count as binary --- packages/adapters/logger-json/src/scrub.ts | 7 ++++--- .../adapters/logger-json/test/redaction.test.ts | 14 ++++++++++++++ 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/packages/adapters/logger-json/src/scrub.ts b/packages/adapters/logger-json/src/scrub.ts index 8f3c6c2..200dd34 100644 --- a/packages/adapters/logger-json/src/scrub.ts +++ b/packages/adapters/logger-json/src/scrub.ts @@ -74,8 +74,9 @@ function cutKeyValues(s: string): string { const q = isQuote(s[start]) ? (s[start] ?? '') : ''; let end = start; if (q !== '') { - end = s.indexOf(q, start + 1); - end = end === -1 ? s.length : end + 1; + end = start + 1; // up to the next unescaped matching quote; none means the rest of the text + while (end < s.length && s[end] !== q) end += s[end] === '\\' ? 2 : 1; + end = Math.min(end + 1, s.length); } else { while (end < s.length && s[end] !== ';' && s[end] !== '\n') end++; } @@ -154,7 +155,7 @@ function binaryPlaceholder(value: object): string | undefined { } if (value instanceof Blob) return `[binary ${value.size} bytes]`; const o = value as Record; - if (typeof o['arrayBuffer'] === 'function' || typeof o['byteLength'] === 'number' || 'buffer' in o) { + if (typeof o['arrayBuffer'] === 'function' || typeof o['byteLength'] === 'number' || o['buffer'] instanceof ArrayBuffer || o['buffer'] instanceof SharedArrayBuffer) { const n = typeof o['byteLength'] === 'number' ? o['byteLength'] : o['size']; return `[binary ${typeof n === 'number' ? n : '?'} bytes]`; } diff --git a/packages/adapters/logger-json/test/redaction.test.ts b/packages/adapters/logger-json/test/redaction.test.ts index b14930a..c85cdff 100644 --- a/packages/adapters/logger-json/test/redaction.test.ts +++ b/packages/adapters/logger-json/test/redaction.test.ts @@ -210,4 +210,18 @@ describe('secrets inside strings, whatever the key quoting or the key word', () it('keeps the text around a quoted secret readable', () => { expect(redactString('failed: {"password":"hunter2","user":"ann"}')).toContain('"user":"ann"'); }); + + it('a quoted value ends at the next unescaped quote, and runs to the end when there is none', () => { + for (const text of ['x {"password":"ab\\"cd"} y', "x {'token': 'ab\\'cd'} y", 'x password="ab\\"cd" y']) { + const out = redactString(text); + expect(out, text).not.toContain('cd'); + expect(out, text).not.toContain('ab'); + } + expect(redactString('x password="never closed LEAKME')).not.toContain('LEAKME'); + }); + + it('an object that merely has a buffer key keeps its other fields', () => { + const lines = run({ id: 'keep-me', buffer: 'not binary' }); + expect(lines[0]).toContain('keep-me'); + }); }); From 4ef15fc4ef122687769878c07f5653082d097654 Mon Sep 17 00:00:00 2001 From: Omer Cohen <639682+omercnet@users.noreply.github.com> Date: Sat, 21 Nov 2026 12:57:04 +0000 Subject: [PATCH 4/4] docs(logger-json): state the scrubber's limits and pin them with tests --- packages/adapters/logger-json/src/scrub.ts | 6 ++++++ .../adapters/logger-json/test/redaction.test.ts | 13 +++++++++++++ 2 files changed, 19 insertions(+) diff --git a/packages/adapters/logger-json/src/scrub.ts b/packages/adapters/logger-json/src/scrub.ts index 200dd34..df4f21a 100644 --- a/packages/adapters/logger-json/src/scrub.ts +++ b/packages/adapters/logger-json/src/scrub.ts @@ -5,6 +5,12 @@ * This scrubber is applied to every value before it reaches pino. It fails closed: when a string * is ambiguous, more is removed, never less. It also flattens values pino cannot serialise safely * (getters, proxies, Map, Set, Date, Error, bigint, functions) into plain data. + * + * Recognised in free text: URLs with credentials (scheme://user:password@host, including PostgreSQL URLs), query strings and fragments, + * key=value and "key": "value" pairs whose key looks secret, bearer and basic credentials, and JWTs. This is a best-effort safety net, not a + * guarantee: the rule is that code never logs a secret, and named fields are redacted by name. Known limits (pinned by tests named + * "known limit"): an unquoted value ends at a ; so a value containing one is cut there, and scheme-less credentials such as + * user:secret@tcp(host:3306)/db are not recognised. */ export const REDACTED = '[redacted]'; export const UNSERIALISABLE = '[unserialisable]'; diff --git a/packages/adapters/logger-json/test/redaction.test.ts b/packages/adapters/logger-json/test/redaction.test.ts index c85cdff..9101660 100644 --- a/packages/adapters/logger-json/test/redaction.test.ts +++ b/packages/adapters/logger-json/test/redaction.test.ts @@ -225,3 +225,16 @@ describe('secrets inside strings, whatever the key quoting or the key word', () expect(lines[0]).toContain('keep-me'); }); }); + +describe('known limits of the best-effort scrubber (changing one of these is a decision, not a fix)', () => { + it('known limit: an unquoted value ends at a semicolon, so the rest of a value that contains one stays', () => { + const out = redactString('password=abc;def'); + expect(out).not.toContain('abc'); + expect(out).toContain('def'); + }); + + it('known limit: scheme-less credentials (user:secret@tcp(host)/db) are not recognised, unlike URLs with a scheme', () => { + expect(redactString('root:Sup3rSecret@tcp(10.0.0.5:3306)/app')).toContain('Sup3rSecret'); + expect(redactString('postgres://root:Sup3rSecret@10.0.0.5:5432/app')).not.toContain('Sup3rSecret'); + }); +});