faye/faye-websocket-node

WebSocket frame length too large

Closed this issue · 2 comments

It seems that the maxLength option does not work correctly when used with the permessage-deflate extension.
The following example generates an error stating that the frame length is too large.

'use strict';

var deflate = require('permessage-deflate')
  , Faye = require('faye-websocket')
  , crypto = require('crypto')
  , http = require('http');


var size = 1024 * 1024 * 10;
var buffer = crypto.pseudoRandomBytes(size);

var server = http.createServer();

server.on('upgrade', function upgrade(req, socket, head) {
  if (!Faye.isWebSocket(req)) return socket.destroy();

  var ws = new Faye(req, socket, head, null, {
    extensions: [ deflate ], maxLength: size
  });

  ws.on('open', function open() {
    ws.on('message', function echo(event) {
      ws.send(event.data);
    });
    ws.on('error', function error(err) {
      console.error(err.message);
    });
    ws.on('close', function close(event) {
      console.log('close', event.code, event.reason);
      ws = null;
    });
  });
});

server.listen(function listening() {
  var ws = new Faye.Client(
    'ws://localhost:' + server.address().port,
    null,
    { extensions: [ deflate ], maxLength: size }
  );

  ws.on('open', function open() {
    ws.send(buffer);
  });
  ws.on('message', function message(event) {
    console.log(event.data.length);
  });
});

If the permessage-deflate extension is removed, the example works as expected.

Maybe I reported it in the wrong repo. https://github.com/faye/websocket-driver-node is probably the correct one.

This is working correctly. A buffer of random noise will not compress well, and running it through DEFLATE will only add data for the compression trees to the front of the message. That 10485760-byte random blob becomes a 10488961-byte DEFLATE message, which is larger than the limit set by the server.