diff options
Diffstat (limited to 'lib/app.ts')
| -rw-r--r-- | lib/app.ts | 37 |
1 files changed, 30 insertions, 7 deletions
@@ -22,14 +22,37 @@ const app = fastify({ const reqLogger = pino(pino.destination("req")); const resLogger = pino(pino.destination("res")); -// Never log plaintext passwords. Redact password fields from request bodies -// before they reach the request log. -const REDACT_FIELDS = ["password", "confirm", "secret", "otpsecret"]; -const redactBody = (body: any) => { - if (!body || typeof body !== "object") return body; +// Never log plaintext credentials. This MUST recurse: the previous version +// only redacted top-level keys, but POST /register posts {user:{password}} — +// one level down — so every registration wrote its plaintext password to the +// request log, and `pin`/`newpin` were not on the list at all (POST /pin, +// POST /user and POST /take all carry one). Anything nested in an array is +// covered too. Depth is bounded so a pathological body can't spin here. +const REDACT_FIELDS = new Set([ + "password", + "newpassword", + "confirm", + "pin", + "newpin", + "secret", + "otpsecret", + "nsec", + "seed", + "privkey", + "mnemonic", +]); +const redactBody = (body: any, depth = 0): any => { + if (!body || typeof body !== "object" || depth > 6) return body; + if (Array.isArray(body)) return body.map((v) => redactBody(v, depth + 1)); const copy: any = { ...body }; - for (const field of REDACT_FIELDS) - if (field in copy) copy[field] = "[redacted]"; + for (const k of Object.keys(copy)) { + if (REDACT_FIELDS.has(k)) { + if (copy[k] !== undefined && copy[k] !== null && copy[k] !== "") + copy[k] = "[redacted]"; + } else if (copy[k] && typeof copy[k] === "object") { + copy[k] = redactBody(copy[k], depth + 1); + } + } return copy; }; |
