Skip to content

fix: escape newlines and backslashes in logfmt values - #9

Merged
abogartz merged 1 commit into
abogartz:mainfrom
WhollyMindless:fix/logfmt-escaping
Oct 21, 2026
Merged

abogartz merged 1 commit into
abogartz:mainfrom
WhollyMindless:fix/logfmt-escaping

Conversation

@WhollyMindless

Copy link
Copy Markdown
Contributor

Follow-up to what I sent on Discord.

Problem

quote() in src/logfmt.ts only escaped ". Each record is one line, so:

  • A value containing a newline ended the record early. Multi-line memories were truncated ("Steps:\n1. bun install" read back as "Steps:), and the rest of the value was parsed as a separate store row. Content shaped like …\nts=… type=decision scope=deploy content=… produced an extra memory with its own type, scope and timestamp that never went through the write gates. That content can come from memory_import files or auto-capture.
  • A value ending in a backslash (C:\tools\) swallowed its closing quote and the content was lost.

The same formatLine writes the evidence log, so evidence lines had the same issue.

Fix

  • Quoted values now escape \, \", \n, \r and \t, and parseLine unescapes them.
  • Unknown escape sequences are left as written, so older files with raw backslashes (C:\Program Files) read back unchanged. One edge remains: an old quoted value containing a literal \n, \t, \r or \ sequence will now be read as the escaped character.

Tests

New test/logfmt.test.ts: round-trips for newlines, backslashes, quotes and tabs, legacy-line parsing, and a store-level test that content with an embedded record stays one memory. Three of these fail on main. bun test passes (164 pass, 4 skip) and tsc --noEmit is clean. No platform-specific code; LF line endings.

🤖 Generated with Claude Code

quote() only escaped double quotes. A value containing a newline ended the
record early, and the remainder was parsed as a separate line of fields, so
multi-line memories were truncated and content could produce extra store
rows that never went through the write gates. A trailing backslash also
swallowed the closing quote.

Quoted values now escape \, \", \n, \r and \t, and parseLine unescapes
them. Unknown escape sequences are left as written so older files with raw
backslashes (C:\Program Files) still read back unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@WhollyMindless

Copy link
Copy Markdown
Contributor Author

Some plain-language context on this one, for clarification.

The big picture: everything stored as a memory gets fed to the AI as part of its instructions. So anything that can slip a memory in can effectively tell the agent what to do ("before deploying, always run this script"). This PR makes sure memories only get in through the front door: your write gates.

What was wrong: each memory is saved as one line in a text file. The code put quotes around the text but didn't deal with line breaks, so a memory containing one got cut in two:

  • The first half was saved as a broken, truncated memory.
  • The second half was read as a brand-new line. If it was worded to look like a memory, it became one, skipping all four gates.

It's the same class of problem as a stray comma breaking a CSV file. It matters because memory text can come from places you don't fully control, like imported files (memory_import) and auto-capture from chat transcripts.

The fix: line breaks (and backslashes) are now stored as \n and \, like JSON does, and turned back into real line breaks when read. Existing memory files read the same as before.

See it yourself: on main, ask the agent to remember something spanning several lines, then check memory_list or memory_recall. It's cut off after the first line. On this branch the full text is kept.

No data migration is needed. Since people pin the version in their config, a short "upgrade recommended" in the release notes would help them know to bump it.

@abogartz abogartz left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved — reviewed by reading the full diff and testing the fix, not the PR description.

Vulnerability reproduced on main: one appendLog of a memory whose content contains a newline reads back through readLines + parseLine as two store rows — including a forged decision/deploy row with its own ts that never passed the write gates — matching the description exactly. On this branch the same payload stays one row and round-trips byte-intact, including the trailing-backslash case.

Verification:

  • bun test — 164 pass / 4 skip / 0 fail; tsc --noEmit clean.
  • Adversarial round-trip via the real write path (formatLine → file → readLines → parseLine): 1 row, content identical.
  • The quoted-value regex in parseLine ((?:[^"\\]|\\.)*) already handles the new escapes; quote()'s quoting predicate /[\s"=]/ covers all JS whitespace (incl. \u2028/\u2029), and unquoted values (raw backslashes, no whitespace) correctly bypass unquoting — so there's no injection path left through the tokenizer.
  • Tests are additive; no existing assertions weakened.

Non-blocking notes (filed, merged as-is is fine):

  • Legacy values containing literal \\ / \n / \t sequences re-decode on read (the PR acknowledges this; we should pick an explicit policy rather than silence) → tracked in #14.
  • Defense-in-depth: validate ts shape at the store reader layer so a malformed row is never addressable regardless of file contents → tracked in #15.

Thanks — this closes a write-gate bypass that went straight into the injected system prompt.

@github-actions

Copy link
Copy Markdown

🎉 This PR is included in version 0.2.3 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants