Repository navigation
fix: escape newlines and backslashes in logfmt values - #9
Conversation
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]>
|
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:
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 ( The fix: line breaks (and backslashes) are now stored as See it yourself: on 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
left a comment
There was a problem hiding this comment.
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 --noEmitclean.- 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/\tsequences re-decode on read (the PR acknowledges this; we should pick an explicit policy rather than silence) → tracked in #14. - Defense-in-depth: validate
tsshape 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.
|
🎉 This PR is included in version 0.2.3 🎉 The release is available on: Your semantic-release bot 📦🚀 |
Follow-up to what I sent on Discord.
Problem
quote()insrc/logfmt.tsonly escaped". Each record is one line, so:"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 frommemory_importfiles or auto-capture.C:\tools\) swallowed its closing quote and the content was lost.The same
formatLinewrites the evidence log, so evidence lines had the same issue.Fix
\,\",\n,\rand\t, andparseLineunescapes them.C:\Program Files) read back unchanged. One edge remains: an old quoted value containing a literal\n,\t,\ror\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 onmain.bun testpasses (164 pass, 4 skip) andtsc --noEmitis clean. No platform-specific code; LF line endings.🤖 Generated with Claude Code