Skip to content

Module crashes in Node if File or Blob global is defined #2871

@sgress454

Description

@sgress454

Current behaviour

If a global variable named File or Blob is defined in an app using Socket.io, the app will crash due to a failed instanceof check in the has-binary dependency.

Steps to reproduce (if the current behaviour is a bug)

Create a file test.js with:

// Set a global var named File
File = {foo: 'bar'};

// Create a new socket.io server.
var server = require('http').createServer();
var io = require('socket.io')(server);

// When a socket connects to the server, add an 'event' handler to the socket.
io.on('connect', function(client){
  // When the `event` message is received, quit.
  client.on('event', function(data, cb){
    process.exit(0);
  });
});

// Start the server.
server.listen(3000);

// Connect a client socket using socket.io-client.
var socket = require('socket.io-client')('http://localhost:3000');
socket.on('connect', function(){
  socket.emit('event');
});

Expected behaviour

This script should run and exit cleanly with node test.js. Instead, the following error is thrown:

TypeError: Expecting a function in instanceof check, but got #<Object>
    at _hasBinary (/Users/scottmac/Development/_sandbox/testglobalfile/node_modules/socket.io-client/node_modules/has-binary/index.js:31:41)
    at hasBinary (/Users/scottmac/Development/_sandbox/testglobalfile/node_modules/socket.io-client/node_modules/has-binary/index.js:58:10)
    at Socket.emit (/Users/scottmac/Development/_sandbox/testglobalfile/node_modules/socket.io-client/lib/socket.js:142:7)
...

The issue is that the has-binary dependency includes this code:

(global.Blob && obj instanceof Blob) ||
(global.File && obj instanceof File)

without checking whether Blob or File are functions.

Setup

  • socket.io version: 1.7.3

Other information (e.g. stacktraces, related issues, suggestions how to fix)

I know this isn't an issue directly in socket.io, but @rauchg maintains has-binary on NPM and there's no Github repo for it as far as I can tell, so this is the best place I could think of to post. Happy to do a PR if someone can point me at where I should post it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions