|
1 | 1 | import { serve } from "bun"; |
2 | | -import { expect, test } from "bun:test"; |
| 2 | +import { expect, setDefaultTimeout, test } from "bun:test"; |
| 3 | + |
| 4 | +// The decompression bomb test needs extra time to compress 150MB of test data |
| 5 | +setDefaultTimeout(30_000); |
3 | 6 |
|
4 | 7 | // Test compressed continuation frames |
5 | 8 | test("WebSocket client handles compressed continuation frames correctly", async () => { |
@@ -198,3 +201,133 @@ test("WebSocket client handles compression errors gracefully", async () => { |
198 | 201 | client.close(); |
199 | 202 | server.stop(); |
200 | 203 | }); |
| 204 | + |
| 205 | +// Test that decompression is limited to prevent decompression bombs |
| 206 | +test("WebSocket client rejects decompression bombs", async () => { |
| 207 | + const net = await import("net"); |
| 208 | + const zlib = await import("zlib"); |
| 209 | + const crypto = await import("crypto"); |
| 210 | + |
| 211 | + // Create a raw TCP server that speaks WebSocket protocol |
| 212 | + const tcpServer = net.createServer(); |
| 213 | + |
| 214 | + const serverReady = new Promise<number>(resolve => { |
| 215 | + tcpServer.listen(0, () => { |
| 216 | + const addr = tcpServer.address(); |
| 217 | + resolve(typeof addr === "object" && addr ? addr.port : 0); |
| 218 | + }); |
| 219 | + }); |
| 220 | + |
| 221 | + const port = await serverReady; |
| 222 | + |
| 223 | + tcpServer.on("connection", socket => { |
| 224 | + let buffer = Buffer.alloc(0); |
| 225 | + |
| 226 | + socket.on("data", data => { |
| 227 | + buffer = Buffer.concat([buffer, data]); |
| 228 | + |
| 229 | + // Look for end of HTTP headers |
| 230 | + const headerEnd = buffer.indexOf("\r\n\r\n"); |
| 231 | + if (headerEnd === -1) return; |
| 232 | + |
| 233 | + const headers = buffer.slice(0, headerEnd).toString(); |
| 234 | + |
| 235 | + // Extract Sec-WebSocket-Key |
| 236 | + const keyMatch = headers.match(/Sec-WebSocket-Key: ([A-Za-z0-9+/=]+)/i); |
| 237 | + if (!keyMatch) { |
| 238 | + socket.end(); |
| 239 | + return; |
| 240 | + } |
| 241 | + |
| 242 | + const key = keyMatch[1]; |
| 243 | + const acceptKey = crypto |
| 244 | + .createHash("sha1") |
| 245 | + .update(key + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11") |
| 246 | + .digest("base64"); |
| 247 | + |
| 248 | + // Send WebSocket upgrade response with permessage-deflate |
| 249 | + socket.write( |
| 250 | + "HTTP/1.1 101 Switching Protocols\r\n" + |
| 251 | + "Upgrade: websocket\r\n" + |
| 252 | + "Connection: Upgrade\r\n" + |
| 253 | + `Sec-WebSocket-Accept: ${acceptKey}\r\n` + |
| 254 | + "Sec-WebSocket-Extensions: permessage-deflate; server_no_context_takeover; client_no_context_takeover\r\n" + |
| 255 | + "\r\n", |
| 256 | + ); |
| 257 | + |
| 258 | + // Create a decompression bomb: 150MB of zeros (exceeds the 128MB limit) |
| 259 | + const uncompressedSize = 150 * 1024 * 1024; |
| 260 | + const payload = Buffer.alloc(uncompressedSize, 0); |
| 261 | + |
| 262 | + // Compress with raw deflate (no header, no trailing bytes that permessage-deflate removes) |
| 263 | + const compressed = zlib.deflateRawSync(payload, { level: 9 }); |
| 264 | + |
| 265 | + // Build WebSocket frame (binary, FIN=1, RSV1=1 for compression) |
| 266 | + // Frame format: FIN(1) RSV1(1) RSV2(0) RSV3(0) Opcode(4) Mask(1) PayloadLen(7) [ExtendedLen] [MaskKey] Payload |
| 267 | + const frameHeader: number[] = []; |
| 268 | + |
| 269 | + // First byte: FIN=1, RSV1=1 (compressed), RSV2=0, RSV3=0, Opcode=2 (binary) |
| 270 | + frameHeader.push(0b11000010); |
| 271 | + |
| 272 | + // Second byte: Mask=0 (server to client), payload length |
| 273 | + if (compressed.length < 126) { |
| 274 | + frameHeader.push(compressed.length); |
| 275 | + } else if (compressed.length < 65536) { |
| 276 | + frameHeader.push(126); |
| 277 | + frameHeader.push((compressed.length >> 8) & 0xff); |
| 278 | + frameHeader.push(compressed.length & 0xff); |
| 279 | + } else { |
| 280 | + frameHeader.push(127); |
| 281 | + // 64-bit length (we only need lower 32 bits for this test) |
| 282 | + frameHeader.push(0, 0, 0, 0); |
| 283 | + frameHeader.push((compressed.length >> 24) & 0xff); |
| 284 | + frameHeader.push((compressed.length >> 16) & 0xff); |
| 285 | + frameHeader.push((compressed.length >> 8) & 0xff); |
| 286 | + frameHeader.push(compressed.length & 0xff); |
| 287 | + } |
| 288 | + |
| 289 | + const frame = Buffer.concat([Buffer.from(frameHeader), compressed]); |
| 290 | + socket.write(frame); |
| 291 | + }); |
| 292 | + }); |
| 293 | + |
| 294 | + let client: WebSocket | null = null; |
| 295 | + let messageReceived = false; |
| 296 | + |
| 297 | + try { |
| 298 | + // Connect with Bun's WebSocket client |
| 299 | + client = new WebSocket(`ws://localhost:${port}`); |
| 300 | + |
| 301 | + const result = await new Promise<{ code: number; reason: string }>(resolve => { |
| 302 | + client!.onopen = () => { |
| 303 | + // Connection opened, waiting for the bomb to be sent |
| 304 | + }; |
| 305 | + |
| 306 | + client!.onmessage = () => { |
| 307 | + // Should NOT receive the message - it should be rejected |
| 308 | + messageReceived = true; |
| 309 | + }; |
| 310 | + |
| 311 | + client!.onerror = () => { |
| 312 | + // Error is expected |
| 313 | + }; |
| 314 | + |
| 315 | + client!.onclose = event => { |
| 316 | + resolve({ |
| 317 | + code: messageReceived ? -1 : event.code, |
| 318 | + reason: messageReceived ? "Message was received but should have been rejected" : event.reason, |
| 319 | + }); |
| 320 | + }; |
| 321 | + }); |
| 322 | + |
| 323 | + // The connection should be closed with code 1009 (Message Too Big) |
| 324 | + expect(result.code).toBe(1009); |
| 325 | + expect(result.reason).toBe("Message too big"); |
| 326 | + } finally { |
| 327 | + // Ensure cleanup happens even on test failure/timeout |
| 328 | + if (client && client.readyState !== WebSocket.CLOSED) { |
| 329 | + client.close(); |
| 330 | + } |
| 331 | + await new Promise<void>(resolve => tcpServer.close(() => resolve())); |
| 332 | + } |
| 333 | +}); |
0 commit comments