Skip to content

Commit 375b3be

Browse files
committed
fix(room-share): count braces outside JSON strings only
parseRoomShare found the end of the envelope by counting every `{` and `}`, so a brace a person typed in a chat line closed the object early, JSON.parse failed and the whole line was dropped. An open brace in `if (x) { y()`, or a lone `}` at the end of a dict, lost the message. Skip string literals while scanning, with backslash escapes honoured, so the braces JSON.stringify left raw inside `text` stay data. Fixes #2447 Docs-not-needed: no owned path or contract changes; the parser now reads the envelope the envelope itself describes.
1 parent 90a5944 commit 375b3be

5 files changed

Lines changed: 93 additions & 2 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
A chat line that carries a brace — `if (x) { y()` or a lone `}` — reaches the room again instead of being dropped: the room-share parser counts braces outside JSON strings only, so one unbalanced brace no longer ends the envelope early.

‎core/src/shared/room-share.js‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,11 +255,22 @@ export function parseRoomShare(text) {
255255
if (at < 0) return undefined
256256
const start = raw.indexOf('{', at)
257257
if (start < 0) return undefined
258+
// #2447: braces inside a JSON string are the sender's characters, not structure. One unbalanced
259+
// brace in a chat line used to close the envelope early and drop the whole line.
258260
let depth = 0
259261
let end = -1
262+
let inString = false
263+
let escaped = false
260264
for (let i = start; i < raw.length; i += 1) {
261265
const ch = raw[i]
262-
if (ch === '{') depth += 1
266+
if (inString) {
267+
if (escaped) escaped = false
268+
else if (ch === '\\') escaped = true
269+
else if (ch === '"') inString = false
270+
continue
271+
}
272+
if (ch === '"') inString = true
273+
else if (ch === '{') depth += 1
263274
else if (ch === '}') {
264275
depth -= 1
265276
if (depth === 0) { end = i; break }
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// A brace in a chat line is a character someone typed, not structure (#2447): `parseRoomShare`
2+
// finds the end of the JSON envelope by counting braces, and it must not count the ones inside the
3+
// strings `encodeRoomShare` writes — otherwise a single unbalanced brace drops the whole line.
4+
import assert from 'node:assert/strict'
5+
import { test } from 'node:test'
6+
import { encodeRoomShare, parseRoomShare } from '../src/shared/room-share.js'
7+
8+
const line = (text) => encodeRoomShare({ op: 'line', roomId: 'r1', seq: 7, who: 'Alice', text })
9+
10+
test('an open brace in a line does not end the envelope early', () => {
11+
const text = 'if (x) { y()'
12+
const parsed = parseRoomShare(line(text))
13+
assert.ok(parsed, 'the envelope was dropped')
14+
assert.equal(parsed.text, text)
15+
})
16+
17+
test('a close brace in a line does not end the envelope early', () => {
18+
const text = 'so the closing brace is }'
19+
const parsed = parseRoomShare(line(text))
20+
assert.ok(parsed, 'the envelope was dropped')
21+
assert.equal(parsed.text, text)
22+
})
23+
24+
test('a brace inside a quoted word, and an escaped quote, both stay text', () => {
25+
const text = 'he said "{" and then "}" and \\ back'
26+
const parsed = parseRoomShare(line(text))
27+
assert.ok(parsed, 'the envelope was dropped')
28+
assert.equal(parsed.text, text)
29+
})
30+
31+
test('an envelope that really is cut short is still refused (control)', () => {
32+
const text = line('hello there')
33+
assert.equal(parseRoomShare(text.slice(0, text.length - 3)), undefined)
34+
})

‎patches/ds-bot/files/src/shared/room-share.js‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,11 +255,22 @@ export function parseRoomShare(text) {
255255
if (at < 0) return undefined
256256
const start = raw.indexOf('{', at)
257257
if (start < 0) return undefined
258+
// #2447: braces inside a JSON string are the sender's characters, not structure. One unbalanced
259+
// brace in a chat line used to close the envelope early and drop the whole line.
258260
let depth = 0
259261
let end = -1
262+
let inString = false
263+
let escaped = false
260264
for (let i = start; i < raw.length; i += 1) {
261265
const ch = raw[i]
262-
if (ch === '{') depth += 1
266+
if (inString) {
267+
if (escaped) escaped = false
268+
else if (ch === '\\') escaped = true
269+
else if (ch === '"') inString = false
270+
continue
271+
}
272+
if (ch === '"') inString = true
273+
else if (ch === '{') depth += 1
263274
else if (ch === '}') {
264275
depth -= 1
265276
if (depth === 0) { end = i; break }
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// A brace in a chat line is a character someone typed, not structure (#2447): `parseRoomShare`
2+
// finds the end of the JSON envelope by counting braces, and it must not count the ones inside the
3+
// strings `encodeRoomShare` writes — otherwise a single unbalanced brace drops the whole line.
4+
import assert from 'node:assert/strict'
5+
import { test } from 'node:test'
6+
import { encodeRoomShare, parseRoomShare } from '../src/shared/room-share.js'
7+
8+
const line = (text) => encodeRoomShare({ op: 'line', roomId: 'r1', seq: 7, who: 'Alice', text })
9+
10+
test('an open brace in a line does not end the envelope early', () => {
11+
const text = 'if (x) { y()'
12+
const parsed = parseRoomShare(line(text))
13+
assert.ok(parsed, 'the envelope was dropped')
14+
assert.equal(parsed.text, text)
15+
})
16+
17+
test('a close brace in a line does not end the envelope early', () => {
18+
const text = 'so the closing brace is }'
19+
const parsed = parseRoomShare(line(text))
20+
assert.ok(parsed, 'the envelope was dropped')
21+
assert.equal(parsed.text, text)
22+
})
23+
24+
test('a brace inside a quoted word, and an escaped quote, both stay text', () => {
25+
const text = 'he said "{" and then "}" and \\ back'
26+
const parsed = parseRoomShare(line(text))
27+
assert.ok(parsed, 'the envelope was dropped')
28+
assert.equal(parsed.text, text)
29+
})
30+
31+
test('an envelope that really is cut short is still refused (control)', () => {
32+
const text = line('hello there')
33+
assert.equal(parseRoomShare(text.slice(0, text.length - 3)), undefined)
34+
})

0 commit comments

Comments
 (0)