fix(terminal): WS close reason 按 UTF-8 字节截断,避免超 123 字节抛错

ws 用 Buffer.byteLength 校验 close reason 的 123 字节上限(ws/lib/sender.js),
原 slice(0, 100) 按 UTF-16 code unit 截断,非 ASCII shell 名可达 316 字节
(实测 200 个 CJK 字符),ws.close() 会抛 RangeError 顶掉原始错误。
改为按字节截断且不切碎 code point,并补回归测试。
This commit is contained in:
Menghuan1918
2026-09-08 22:20:15 +08:00
parent e847771954
commit 81919516d8
2 changed files with 34 additions and 4 deletions
+13
View File
@@ -963,6 +963,19 @@ describe('side card settings routes', () => {
expect(wsCloseReasonOf('plain')).toBe('plain')
})
it('caps the close reason by UTF-8 bytes so the ws 123-byte limit holds', () => {
// 200 CJK characters are ~600 bytes: a character-count slice would still
// overflow the cap `ws` enforces with Buffer.byteLength.
const reason = wsCloseReasonOf(new SidebarError(
'shell-not-found',
'shell executable not found',
400,
{ shell: `/bin/${'终'.repeat(200)}` },
))
expect(reason.startsWith('shell-not-found:')).toBe(true)
expect(Buffer.byteLength(reason)).toBeLessThanOrEqual(123)
})
it('reads the resolved prefs and writes a patch through the seam', async () => {
const route = mountWithSettings(createFakeSettings())
const read = await invoke(route, 'settings.get', {})