Skip to main content
Menu
Advisories

@logtape/logtape, @logtape/pretty: unsanitized control characters in log message and category enable log forgery and terminal injection

GHSA-HP6W-C2CH-G44WPublished Updated GitHub

Description

@logtape/logtape and @logtape/pretty concatenate a log record's even-index message parts (including a whole plain-string message argument) and the record's category segments into the rendered output with no control-character or ANSI-escape sanitization. An attacker who controls a logged string containing terminal escape sequences (ESC / C0 / OSC / cursor / screen-control) has those bytes rendered verbatim to the operator's TTY, enabling ANSI/terminal injection (CWE-150): screen clears, cursor repositioning to overwrite prior log lines with forged entries, hidden text, and operator deception during log triage. The same gap passes interior newlines through, so an attacker-controlled string can also emit a complete, syntactically valid forged log record (CWE-117); this variant needs no terminal and affects file sinks and line-oriented log collectors too. The existing advisory GHSA-8h6h-x5pq-56fq only covers the syslog sink and does not touch the built-in text formatters.

Note that the injection channel is the message position only. In LogTape's documented usage, untrusted data goes into a value position, either ` logger.info... ${value} or logger.info("... {value}", { value }), and value-position arguments *are* escaped (inspect in pretty, stringifyWithoutCycles in core). The vulnerable pattern is logger.info(untrustedString)`, which passes the whole string through as message part 0.

Root Cause

In packages/logtape/src/formatter.ts:93-109 (renderMessageParts(), used by getTextFormatter() and therefore by defaultTextFormatter and getAnsiColorFormatter()), even-index message parts are appended raw:

message += (i % 2 === 0)
  ? msgParts[i] as string          // RAW
  : valueRenderer(msgParts[i]);    // escaped

The same shape appears in packages/logtape/src/formatter.ts:1505 (defaultConsoleFormatter()) and in packages/pretty/src/formatter.ts:1002-1004:

for (let i = 0; i < record.message.length; i++) {
  if (i % 2 === 0) message += record.message[i];   // RAW
  else { const value = record.message[i]; /* inspect-quoted */ }
}

Category segments are equally unsanitized: they are joined and emitted raw at packages/logtape/src/formatter.ts:746-748 and passed through truncateCategory() at packages/pretty/src/formatter.ts:987.

In @logtape/pretty a stripAnsi helper exists but is applied only to display-width math (formatter.ts:1110/1139), never to emitted output; and its regex /\x1b\[[0-9;]*m/g (wcwidth.ts:14) matches only SGR color sequences — not \x1b[2J (clear screen), cursor moves, OSC title, or C0 controls. @logtape/logtape has no equivalent helper at all. The console sink strips only a trailing \r?\n$ (sink.ts:376); interior ESC/\n pass through unchanged.

Impact

ANSI/terminal-control injection (CWE-150) on the operator's terminal: clear the screen, reposition the cursor to overwrite previously-printed log lines with forged entries, hide text, or spoof a benign-looking success line — deceiving an operator triaging logs on a TTY. Because the injection channel is the log message content (not an HTTP header), the runtime HTTP-parser rejection that blocks header-based control-char vectors does not apply.

Separately, newline pass-through yields log record forgery (CWE-117) with no terminal involved. Under defaultTextFormatter the forged line is byte-for-byte indistinguishable from a genuine record, so it deceives log viewers, grep-based triage, and any collector that splits on newlines. @logtape/pretty is partially mitigated here by accident: wrapText() indents continuation lines, so a forged line appears indented rather than flush left.

Proof of Concept

logger.info("\x1b[1A\x1b[2K\x1b[32m[INF] auth: login ok")

Under @logtape/pretty with colors enabled (default on a TTY), or under defaultTextFormatter/ansiColorFormatter from @logtape/logtape, this is emitted verbatim: on a live terminal it moves the cursor up one line, clears it, and prints a green forged success entry over a real log line.

The newline variant, against defaultTextFormatter:

logger.info("ok\n2026-09-22 08:00:00.000 +00:00 [INF] app: payment refunded to attacker")

The category variant, where a category segment rather than the message carries the payload:

getLogger(["app", untrustedSegment]).info("hello")

Attack Chain

  1. Entry: logger.info(userInput) under @logtape/logtape's defaultTextFormatter/ansiColorFormatter or @logtape/pretty (colors on, default when TTY), where userInput is attacker-influenced and contains ESC or \n (raw ESC survives JSON.parse, proven). Guard: the value renderers escape value-position (odd-index) args only. Bypass proof: logtape/src/formatter.ts:93-109 and pretty/src/formatter.ts:1002-1004 append even-index parts (incl. the whole plain-string message at index 0) raw.
  2. Sanitization check: does any sanitizer run on emitted text? Guard: stripAnsi exists in @logtape/pretty. Bypass proof: applied only to display-width math (pretty/src/formatter.ts:1110/1139), never to emitted output; regex /\x1b\[[0-9;]*m/g (wcwidth.ts:14) matches only SGR color sequences — not cursor/screen/OSC/C0. @logtape/logtape has no sanitizer at all.
  3. Render: string written to console sink → TTY interprets raw ESC; or written to a file sink / collector, where the interior \n starts a forged record. Guard: console sink strips only a trailing \r?\n$ (sink.ts:376); interior ESC/\n pass. Bypass proof: confirmed by code.
  4. Impact: ANSI/terminal-control injection (screen clear, cursor reposition to overwrite prior lines, hidden text) and log record forgery — operator deception.

Bypass Evidence

Verified on main at 2.3.6 (96f14d97). Even-index concatenation is raw in both packages (logtape/src/formatter.ts:93-109, pretty/src/formatter.ts:1002-1004); the only ANSI sanitizer's regex matches SGR colors exclusively (pretty/src/wcwidth.ts:14) and is never applied to output. Observed formatter output for message "\x1b[1A\x1b[2K\x1b[32m[INF] auth: login ok":

pretty  message:  "... \u001b[2m\u001b[38;2;148;163;184m\u001b[1A\u001b[2K\u001b[32m[INF] auth: login ok\u001b[0m\n"
text    message:  "2026-09-22 ... [INF] app: \u001b[1A\u001b[2K\u001b[32m[INF] auth: login ok\n"
text    category: "2026-09-22 ... [INF] app·\u001b[1A\u001b[2K\u001b[32m[INF] auth: login ok: hi\n"
text    value:    "2026-09-22 ... [INF] app: x: \"\\x1b[1A\\x1b[2K\\x1b[32m[INF] auth: login ok\"\n"   (escaped, not vulnerable)

And for the newline payload under defaultTextFormatter:

"2026-09-22 08:34:48.491 +00:00 [INF] app: ok\n2026-09-22 08:00:00.000 +00:00 [INF] app: payment refunded to attacker\n"

Channel = message content, so HTTP-parser rejection does not apply.

Affected Versions

@logtape/logtape and @logtape/pretty <= 2.3.6 (present on main at 2.3.6; no post-tag fix). Earlier 0.x/1.x releases carry the same code shape.

Suggested Fix

Escape or strip C0 control characters (including \n and \r) and non-SGR ANSI sequences (CSI cursor/screen ops, OSC, other ESC introducers) from message-position parts and from category segments, in both @logtape/logtape's built-in text formatters and @logtape/pretty, rather than only in the pretty package. Extending stripAnsi alone is insufficient on two counts: it lives in @logtape/pretty only, and its regex covers SGR colors exclusively.

Because some applications deliberately log pre-colored strings or captured subprocess output, the sanitizer should preserve SGR sequences by default and be opt-out via formatter options.

--- Reported by zx (Jace) — GitHub: @manus-use Revised by the maintainer to correct the affected-package scope and to add the category and newline-forgery variants.

Affected versions

References

GitHub · dahlia/logtape