Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

address review (codex): escape a raw control char even when a backslash precedes it

[._.]/ Adam Eivy41b1679bb61f
diff --git a/server/lib/jsonExtract.js b/server/lib/jsonExtract.js
index 5b7c362b3c..ea27977583 100644
--- a/server/lib/jsonExtract.js
+++ b/server/lib/jsonExtract.js
@@ -138,10 +138,14 @@ function escapeControlCharsInStrings(input) {
for (let i = 0; i < input.length; i += 1) {
const ch = input[i];
if (inString) {
- if (escaped) { out += ch; escaped = false; continue; }
- if (ch === '\\') { out += ch; escaped = true; continue; }
- if (ch === '"') { out += ch; inString = false; continue; }
const code = input.charCodeAt(i);
+ // A raw control char is ALWAYS illegal inside a JSON string — it can
+ // never be the target of a valid `\`-escape (escape targets are the
+ // printable set `"\/bfnrtu`). So escape it BEFORE consulting `escaped`:
+ // a control char that follows a backslash means that backslash was a
+ // dangling/invalid escape, and copying the control char through (the old
+ // `escaped` branch) left the string unparseable. Resetting `escaped`
+ // consumes any pending (spurious) escape.
if (code <= 0x1f) {
if (ch === '\n') out += '\\n';
else if (ch === '\r') out += '\\r';
@@ -149,8 +153,12 @@ function escapeControlCharsInStrings(input) {
else if (ch === '\b') out += '\\b';
else if (ch === '\f') out += '\\f';
else out += `\\u${code.toString(16).padStart(4, '0')}`;
+ escaped = false;
continue;
}
+ if (escaped) { out += ch; escaped = false; continue; }
+ if (ch === '\\') { out += ch; escaped = true; continue; }
+ if (ch === '"') { out += ch; inString = false; continue; }
out += ch;
continue;
}
diff --git a/server/lib/jsonExtract.test.js b/server/lib/jsonExtract.test.js
index 8534c702f6..699e9b958f 100644
--- a/server/lib/jsonExtract.test.js
+++ b/server/lib/jsonExtract.test.js
@@ -99,6 +99,17 @@ describe('jsonExtract.tryParseWithRepair', () => {
});
});
+ it('escapes a raw control char even when a backslash immediately precedes it', () => {
+ // A dangling backslash followed by a literal newline (double corruption:
+ // an invalid \-escape AND a raw control char). The control char must still
+ // be escaped so the block parses, rather than being copied through as an
+ // "escaped" char. `\\` + raw LF → parseable `\\` + `\n`.
+ const bad = '{"body":"line\\\nnext"}';
+ const r = tryParseWithRepair(bad);
+ expect(r.error).toBeUndefined();
+ expect(typeof r.value.body).toBe('string');
+ });
+
it('leaves structural whitespace + already-escaped sequences untouched while escaping a raw control char', () => {
// Force the repair to actually run: one string has a RAW newline (invalid),
// while the object also has structural inter-token newlines and another