NEW326651
WebSocket: after a compressed message that ends with a final (BFINAL) deflate block, the next message closes the connection
https://bugs.webkit.org/show_bug.cgi?id=326651
Summary WebSocket: after a compressed message that ends with a final (BFINAL) deflate...
reinder
Reported 2026-10-07 01:09:55 PDT
SUMMARY When a server compresses a WebSocket message the way RFC 7692 section 7.2.3.4 shows (a DEFLATE block with BFINAL set, followed by one 0x00 byte), Safari delivers that message just fine. But as soon as the next message arrives, Safari closes the connection. The server gets a close frame with code 1002 (protocol error), the page gets a close event with code 1006, and the next message is never delivered. Chrome and Firefox handle the same bytes without problems. WHY IT MATTERS Bun's WebSocket server compresses small messages exactly this way when compression is on (perMessageDeflate: true). So any Bun server that sends a small compressed message followed by anything else loses its Safari users. We found it because a document in our app never loaded on an iPhone or in Safari on the Mac, while it opened instantly in Chrome. STEPS TO REPRODUCE 1. Save the file below as server.js. It has no dependencies. 2. Run node server.js. 3. Open http://localhost:8080 in Safari. The page opens two WebSockets one after the other. Each one gets the same message, "Hello", compressed in one of the two ways RFC 7692 shows, then a plain "World", then a normal close. Expected (what Chrome and Firefox show): normal: received "Hello" normal: received "World" normal: closed with code 1000 (ok) bfinal: received "Hello" bfinal: received "World" bfinal: closed with code 1000 (ok) Actual (Safari): normal: received "Hello" normal: received "World" normal: closed with code 1000 (ok) bfinal: received "Hello" bfinal: closed with code 1006 (connection lost) The two "Hello" payloads are the RFC's own examples: - section 7.2.3.1: f2 48 cd c9 c9 07 00 (BFINAL not set), which works - section 7.2.3.4: f3 48 cd c9 c9 07 00 00 (BFINAL set, plus the extra 0x00), which breaks the connection on the next message If nothing comes after the BFINAL message, Safari doesn't drop the connection, but it stops responding: it never answers the server's close frame. ENVIRONMENT - Safari 26.6.2 on macOS 26.6 - Safari 26.6.1 on iPhone 14 Pro MY GUESS It looks like Safari's decompressor stays in a "stream ended" state after the BFINAL block and treats the next message as an error, instead of starting fresh for each message the way the RFC describes. SERVER.JS // Safari drops a WebSocket after a permessage-deflate message that uses a BFINAL block (RFC 7692, section 7.2.3.4). // Run: node server.js, then open http://localhost:8080 in Safari. const http = require('node:http'); const crypto = require('node:crypto'); // "Hello", compressed the two ways RFC 7692 shows (sections 7.2.3.1 and 7.2.3.4). const NORMAL = Buffer.from('f248cdc9c90700', 'hex'); // one DEFLATE block, BFINAL = 0 const BFINAL = Buffer.from('f348cdc9c90700' + '00', 'hex'); // DEFLATE block with BFINAL = 1, plus the extra 0x00 const page = `<!doctype html><meta name="viewport" content="width=device-width"> <pre id="log" style="font-size:16px"></pre> <script> function log(line) { document.getElementById('log').textContent += line + '\\n'; } function run(kind) { return new Promise((done) => { const ws = new WebSocket('ws://' + location.host + '/' + kind); ws.onmessage = (e) => log(kind + ': received "' + e.data + '"'); ws.onclose = (e) => { log(kind + ': closed with code ' + e.code + (e.code === 1000 ? ' (ok)' : ' (connection lost)')); done(); }; }); } run('normal').then(() => run('bfinal')); </script>`; const server = http.createServer((req, res) => { res.setHeader('Content-Type', 'text/html'); res.end(page); }); server.on('upgrade', (req, socket) => { const accept = crypto.createHash('sha1').update(req.headers['sec-websocket-key'] + '258EAFA5-E914-47DA-95CA-C5AB0DC85B11').digest('base64'); socket.write( 'HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\n' + `Sec-WebSocket-Accept: ${accept}\r\n` + 'Sec-WebSocket-Extensions: permessage-deflate; server_no_context_takeover; client_no_context_takeover\r\n\r\n', ); const payload = req.url === '/bfinal' ? BFINAL : NORMAL; // FIN + RSV1 (compressed) + text frame, then the payload length and the payload. socket.write(Buffer.concat([Buffer.from([0xc1, payload.length]), payload])); // Then a plain, uncompressed "World", and a normal close. setTimeout(() => socket.write(Buffer.concat([Buffer.from([0x81, 5]), Buffer.from('World')])), 200); setTimeout(() => socket.end(Buffer.from([0x88, 0x02, 0x03, 0xe8])), 500); socket.on('error', () => {}); }); server.listen(8080, () => console.log('Open http://localhost:8080 in Safari'));
Attachments
Radar WebKit Bug Importer
Comment 1 2026-10-14 01:10:13 PDT
Note You need to log in before you can comment on or make changes to this bug.