socket.io-client icon indicating copy to clipboard operation
socket.io-client copied to clipboard

Bad handshake method (only when using extra headers!)

Open JohnCoding94 opened this issue 9 years ago • 13 comments

You want to:

  • [x] report a bug
  • [ ] request a feature

Current behaviour

I get a 400 with the message {"code":2,"message":"Bad handshake method"} when trying to connect my server with an extra header (works fine if i don't try to add it).

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

I try to connect my server with the following configuration:

  const options = {
    transportOptions: {
      polling: {
        extraHeaders: {
          Authorization: "Bearer test"
        }
      }
    }
  };

I also noticed that an OPTIONS request is made when i add this extra header (instead of a GET when i don't add it)

Expected behaviour

A get GET request (to preserve cookies) with the extra headers that don't break the handshake.

Setup

  • OS: Mac
  • browser: Chrome 58
  • socket.io version: 2.0.3

JohnCoding94 avatar Jun 23 '17 10:06 JohnCoding94

Same Issue, here.. totally works with the nodejs version of socket.io-client though not the browser

  • OS: Win
  • browser: Chrome 59
  • socket.io version: 2.0.3

ghostfreak3000 avatar Jul 19 '17 06:07 ghostfreak3000

Hi! I think you have to add the proper headers on the server-side (it seems you're in a CORS situation):

const io = require('socket.io')(3000, {
  handlePreflightRequest: function (req, res) {
    var headers = {
      'Access-Control-Allow-Headers': 'Content-Type, Authorization',
      'Access-Control-Allow-Origin': 'http://localhost:3001',
      'Access-Control-Allow-Credentials': true
    };
    res.writeHead(200, headers);
    res.end();
  }
});

I added an example there: https://github.com/darrachequesne/socket.io-fiddle/tree/extra-headers

darrachequesne avatar Aug 30 '17 11:08 darrachequesne

@darrachequesne how to specify multiple domains for origin, following ways are not working:

  1. 'Access-Control-Allow-Origin': 'http://localhost:3001, http://localhost:3002'
  2. 'Access-Control-Allow-Origin': ['http://localhost:3001, http://localhost:3002']
  3. 'Access-Control-Allow-Origin': 'http://localhost:3001 http://localhost:3002'

DaVincii avatar Sep 28 '17 07:09 DaVincii

@DaVincii you cannot use multiple domains, you need to read the client header origin and check agains your whitelist.

see this and this on stackoveflow

stalinb87 avatar Sep 28 '17 14:09 stalinb87

@DaVincii i defined as null, but gets freed to any domain.

const headers = { 'Access-Control-Allow-Headers': 'Content-Type, Authorization', 'Access-Control-Allow-Origin': null, 'Access-Control-Allow-Credentials': true };

roberto-souza-dbc avatar Oct 03 '17 19:10 roberto-souza-dbc

For CORS Use { handlePreflightRequest: function (req, res) { var headers = { 'Access-Control-Allow-Headers': 'Content-Type, Authorization', 'Access-Control-Allow-Origin': req.headers.origin , 'Access-Control-Allow-Credentials': true }; res.writeHead(200, headers); res.end(); } }

kiran-varghese avatar Mar 19 '18 09:03 kiran-varghese

For CORS Use { handlePreflightRequest: function (req, res) { var headers = { 'Access-Control-Allow-Headers': 'Content-Type, Authorization', 'Access-Control-Allow-Origin': req.headers.origin , 'Access-Control-Allow-Credentials': true }; res.writeHead(200, headers); res.end(); } }

'Access-Control-Allow-Origin': req.headers.origin This just saved my day! When I used: 'Access-Control-Allow-Origin': '', It would give me the following error: "The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '' when the request's credentials mode is 'include'. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute."

moreirasantos avatar Jan 24 '19 09:01 moreirasantos

This does not seem to work anymore with the latest socket.io. Typescript does not like it. Anyway to fix? image

SupertigerDev avatar Jun 16 '20 09:06 SupertigerDev

This does not seem to work anymore with the latest socket.io. Typescript does not like it. Anyway to fix?

@supertiger1234 handlePreflightRequest has a different interface in the latest version

Screenshot 2020-07-20 at 20 16 00

So in your case it should be:

...
handlePreflightRequest: function(server, req, res) {
  var headers = {
    "Access-Control-Allow-Headers": "Content-Type, Authorization",
    "Access-Control-Allow-Origin": config.allowedOrigins,
    "Access-Control-Allow-Credentials": true,
  };

  res.writeHead(200, headers);
  res.end();
...

pixochi avatar Jul 20 '20 18:07 pixochi

@supertiger1234 handlePreflightRequest has a different interface in the latest version

⚠️ If you install socket.io right now from npm you will have an old version, but if you install typings (via @types) it will have new typings! Which can be super misleading.

pA1nD avatar Sep 22 '20 22:09 pA1nD

How do I install the latest socket io?

SupertigerDev avatar Sep 23 '20 06:09 SupertigerDev

For future reference: this issue also occurs when using a post-3.0 server with a pre-3.0 client. This may be caused by a misconfigured cache that makes the clients use an older version of socket.io.js .

lovasoa avatar Nov 17 '20 10:11 lovasoa

For future readers, please see:

  • CORS documentation for v2: https://socket.io/docs/v2/handling-cors/
  • CORS documentation for v3: https://socket.io/docs/v3/handling-cors/

darrachequesne avatar Nov 25 '20 15:11 darrachequesne