The log injection bug behind one unescaped newline
My logger claimed it blocked newline injection. It only stripped carriage returns. Here is the real log injection bug and the fix I tested.
14 posts
My logger claimed it blocked newline injection. It only stripped carriage returns. Here is the real log injection bug and the fix I tested.
INCR then PEXPIRE is the standard Redis rate limit. I tested what happens when the second one fails, and found a key that never expires.
I tested pino's sonic-boom write stream: it does not buffer log lines by default, and a full buffer drops lines while write() still returns true.
A window error listener ignores every failed image, script and stylesheet. One argument fixes it, then feeds your logger events with no error, no message.
I hand-rolled an OTLP log exporter and tested it. Bare int64 numbers, ms times 1e6 timestamps, wrong severity and base64 trace IDs all still return 200.
One BigInt makes JSON.stringify throw, so your logger drops every other field too. What I measured, and a safe() helper that keeps Dates and Errors intact.
Batched logs sit in memory until a timer fires. Serverless freezes your function the moment it responds. Here is what I measured, and the one-line fix.
Everyone says console.time() breaks under concurrency because the second call overwrites the first. I tested it in Node, Bun and Deno, and that is not what happens. The second timer is thrown away, the fast request reports the slow request's duration, and the slow request reports nothing at all.
Structured logging only pays off if the data field has a stable shape you can query on. Nothing enforces that by default. Here's how to catch drift with schema validation that never drops the log line, using the Standard Schema spec so it works with Zod, Valibot, or nothing at all.
I set LOG_LEVEL to turn on debug logs for one feature. Nothing appeared. The rules were spelled correctly — they were just in the wrong order. Here is what I measured, and the rule that tells you which order is right.
window.onerror is the answer everyone gives. It never fires for a broken image, and the next library that loads can delete yours. Here is what to use instead, on the client and on the server, tested in a real browser.
A crash at chunks/page.js:1:518 tells you nothing. Source maps turn it back into checkout.ts:3:49. Here's how the lookup works, and the one case where it hands you a real-looking line number that is completely wrong.
Error objects have non-enumerable fields, so a logger that just JSON.stringify's them prints an empty object. Here's why, and a serializer that captures message, stack, cause chains, and AggregateErrors properly.
The problem with console.log in the browser, why a naive 'POST logs to the server' endpoint is a security hole, and how @developerehsan/nextjs-logger solves both with one import.