Skip to content

Commit 35e2819

Browse files
committed
Cut peer-supplied face-room text on code points, not UTF-16 units
A cap that landed between the halves of a surrogate pair stored a lone high surrogate for a peer's face-room name, role label, platform line, and the two 200-unit failure details, so a remote member could make a label render as a replacement character. All five cuts now share one code-point bound. Fixes #2438 Docs-not-needed: a cut helper inside an existing sanitiser; no subsystem behaviour changes
1 parent 653cc05 commit 35e2819

3 files changed

Lines changed: 78 additions & 11 deletions

File tree

‎changelog.d/2438.fixed.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Face-to-face room names, role labels, platform lines and surface Bot Social's own failure text now cap on character boundaries instead of UTF-16 units, so an emoji at the cut is no longer split into a half character and shown as a replacement character ([#2438](https://github.com/ThinkFlowLab/FlyBot/issues/2438)).

‎plugins/social/src/face-rooms.js‎

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@ const CALL_TIMEOUT_MS = 30_000
2121
const NAME_MAX = 60
2222
const SEEN_KEPT = 200
2323

24+
// Peer text is cut on code points, never on UTF-16 units: a cut between the halves of a surrogate
25+
// pair stores a lone surrogate, which every encoder reads as U+FFFD (#2438). The plugin ships as its
26+
// own package and may not reach into core's shared/text-boundary.js (#155), so the rule is repeated
27+
// here the way social.js's `cutCodePoints` already repeats it.
28+
const cutCodePoints = (value, max) => {
29+
const text = String(value ?? '').toWellFormed()
30+
return text.length <= max ? text : Array.from(text).slice(0, max).join('')
31+
}
32+
2433
export class FaceRoomError extends Error {
2534
constructor(message, code, extra = {}) {
2635
super(message)
@@ -74,12 +83,11 @@ export const localInviteHandle = value => parseInviteLink(value)?.handle ?? ''
7483
// brackets, markup or @, capped. Used for every peer-controlled name and for platform
7584
// system copy that embeds those names.
7685
export function cleanFaceName(value, fallback = 'a remote Bot') {
77-
const text = stripFaceMarkers(String(value ?? ''))
86+
const text = cutCodePoints(stripFaceMarkers(String(value ?? ''))
7887
.replace(/[\u0000-\u001f\u007f-\u009f\u2028\u2029]/g, ' ')
7988
.replace(/[[\]"<>`@]/g, '')
8089
.replace(/\s+/g, ' ')
81-
.trim()
82-
.slice(0, NAME_MAX)
90+
.trim(), NAME_MAX)
8391
return text || fallback
8492
}
8593

@@ -196,7 +204,7 @@ export function mapRegisterError(error, bot) {
196204
if (code === 'http_401' || code === 'http_403' || code === 'unauthorized' || /401|403|unauthori[sz]ed|token/i.test(code + msg)) {
197205
return new FaceRoomError(FRIENDLY.unauthorized, 'unauthorized', { status: 401 })
198206
}
199-
const raw = msg.replace(/^Bot Social\s+\S+:\s*/, '').replace(/\s+/g, ' ').trim().slice(0, 200)
207+
const raw = cutCodePoints(msg.replace(/^Bot Social\s+\S+:\s*/, '').replace(/\s+/g, ' ').trim(), 200)
200208
const detail = raw && !/^[a-z_]+$/.test(raw) ? `: ${raw}` : '. Try again.'
201209
return new FaceRoomError(`Could not register ${name} on Bot Social${detail}`, code || 'register_failed')
202210
}
@@ -221,7 +229,7 @@ export function mapFaceError(data, status) {
221229
? `Too many failed attempts. Try again in ${formatRetryAfter(retryAfter)}.`
222230
: 'Too many failed attempts. Try again later.'
223231
} else if (!message) {
224-
const raw = String(payload.message ?? '').replace(/\s+/g, ' ').trim().slice(0, 200)
232+
const raw = cutCodePoints(String(payload.message ?? '').replace(/\s+/g, ' ').trim(), 200)
225233
message = raw && !/^[a-z_]+$/.test(raw) ? raw : 'Could not join that room. Try another number.'
226234
if (status === 401) message = FRIENDLY.unauthorized
227235
else if (status === 429) message = retryAfter != null
@@ -500,12 +508,11 @@ export function viewFaceMember(member) {
500508
export const FACE_ROLE_MAX = 80
501509
export function cleanFaceRole(value) {
502510
if (typeof value !== 'string') return ''
503-
return stripFaceMarkers(value)
511+
return cutCodePoints(stripFaceMarkers(value)
504512
.replace(/[\u0000-\u001f\u007f-\u009f\u2028\u2029]/g, ' ')
505513
.replace(/[[\]"<>`@]/g, '')
506514
.replace(/\s+/g, ' ')
507-
.trim()
508-
.slice(0, FACE_ROLE_MAX)
515+
.trim(), FACE_ROLE_MAX)
509516
}
510517

511518
// The room lead every home follows (#476). Bot Social sends the creator's pick, else its default
@@ -610,12 +617,11 @@ function sanitizePlatformText(text, type, member, reason) {
610617
if (type === 'member.left' && member) return platformLeaveText(member)
611618
if (type === 'room.started') return platformStartedText(reason)
612619
if (type === 'room.lead_changed') return platformLeadText(member, reason)
613-
const cleaned = String(text ?? '')
620+
const cleaned = cutCodePoints(String(text ?? '')
614621
.replace(/[\u0000-\u001f\u007f-\u009f\u2028\u2029]/g, ' ')
615622
.replace(/[[\]"<>`@]/g, '')
616623
.replace(/\s+/g, ' ')
617-
.trim()
618-
.slice(0, 200)
624+
.trim(), 200)
619625
return cleaned
620626
}
621627

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import assert from 'node:assert/strict'
2+
import { test } from 'node:test'
3+
import { cleanFaceName, cleanFaceRole, mapFaceError, mapRegisterError, viewFaceEvent } from '../src/face-rooms.js'
4+
5+
// Peer-supplied face-room text is capped (#2438). A cap that lands between the halves of a surrogate
6+
// pair stores a lone high surrogate, which every encoder reads as U+FFFD, so each cut must land
7+
// on a code point and each cap must count code points. `\p{Surrogate}` with the `u` flag matches a
8+
// lone surrogate only: a valid pair is one code point outside that range.
9+
const ROCKET = '\u{1F680}'
10+
const REPLACEMENT = String.fromCharCode(0xfffd)
11+
const shapes = value => {
12+
assert.equal(typeof value, 'string')
13+
assert.ok(value.isWellFormed(), `not well formed: ${JSON.stringify(value)}`)
14+
assert.ok(!/\p{Surrogate}/u.test(value), `lone surrogate: ${JSON.stringify(value)}`)
15+
assert.ok(!value.includes(REPLACEMENT), `replacement character: ${JSON.stringify(value)}`)
16+
return value
17+
}
18+
19+
test('a face-room name cut lands on a code point boundary, not inside an emoji', () => {
20+
const stored = shapes(cleanFaceName(`${'a'.repeat(59)}${ROCKET}rest`))
21+
assert.equal(stored, `${'a'.repeat(59)}${ROCKET}`)
22+
})
23+
24+
test('the name cap counts code points, so whole emoji are never cut in half', () => {
25+
const stored = shapes(cleanFaceName(ROCKET.repeat(70)))
26+
assert.equal(Array.from(stored).length, 60)
27+
})
28+
29+
test('a face-room role cut lands on a code point boundary, not inside an emoji', () => {
30+
const stored = shapes(cleanFaceRole(`${'a'.repeat(79)}${ROCKET}rest`))
31+
assert.equal(stored, `${'a'.repeat(79)}${ROCKET}`)
32+
})
33+
34+
test('the role cap counts code points, so whole emoji are never cut in half', () => {
35+
const stored = shapes(cleanFaceRole(ROCKET.repeat(90)))
36+
assert.equal(Array.from(stored).length, 80)
37+
})
38+
39+
test('a platform line cut lands on a code point boundary, not inside an emoji', () => {
40+
const viewed = viewFaceEvent({ type: 'room.pinned', text: `${'a'.repeat(199)}${ROCKET}rest` })
41+
const stored = shapes(viewed.text)
42+
assert.equal(stored, `${'a'.repeat(199)}${ROCKET}`)
43+
})
44+
45+
test('the platform line cap counts code points, so whole emoji are never cut in half', () => {
46+
const viewed = viewFaceEvent({ type: 'room.pinned', text: ROCKET.repeat(250) })
47+
assert.equal(Array.from(shapes(viewed.text)).length, 200)
48+
})
49+
50+
test('a failed registration detail lands on a code point boundary, not inside an emoji', () => {
51+
const error = mapRegisterError({ message: `${'a'.repeat(199)}${ROCKET}rest` }, { name: 'Bolt' })
52+
const stored = shapes(error.message)
53+
assert.ok(stored.endsWith(`${'a'.repeat(199)}${ROCKET}`), stored.slice(-8))
54+
})
55+
56+
test('an unmapped join error message lands on a code point boundary, not inside an emoji', () => {
57+
const error = mapFaceError({ error: { code: 'odd_room_failure', message: `${'a'.repeat(199)}${ROCKET}rest` } }, 400)
58+
const stored = shapes(error.message)
59+
assert.equal(stored, `${'a'.repeat(199)}${ROCKET}`)
60+
})

0 commit comments

Comments
 (0)