Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Root nsp middleware always called before sub nsp middleware authorization #1888

Closed
peteruithoven opened this issue Nov 24, 2014 · 6 comments
Closed

Comments

@peteruithoven
Copy link

It's now possible to define middleware per namespace, but when this is used for authorization there is an issue. The issue is that the root namespace authorization is always called first, when this fails the namespace specific authorization isn't called. This makes it "impossible" to send the client an error from the namespace it was connecting to.
Not sure what would be the best solution here...

var express = require('express');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var socketClient = require('socket.io-client');
var debug = require('debug')('nspAuthorization');
var port = 5000;

// SERVER
http.listen(port, function(){
  debug('server listening on *:'+port);
});
var rootNSP = io.of('/');
var subNSP = io.of('/namespace');
rootNSP.use(authorization); // when enabled, the user won't get a error
subNSP.use(authorization);
function authorization(socket, next) {
  debug('authorization: to nsp: ',socket.nsp.name);
  var query = socket.handshake.query;
  if (query.secret === 'abc') {
    debug("  valid secret");
    next();
  } else{
    debug("  invalid secret");
    next(new Error('invalid secret'));
  }
}

// CLIENT
var url = 'http://localhost:'+port;
debug('namespace: connecting');
var socket = socketClient.connect(url+'/namespace?secret=fake');
socket.on('error',function(err) { // not received when root authorization middleware is enabled
  debug('namespace: error: ',arguments);
});
socket.on('connection',function() { 
  debug('namespace: connected');
});
@peteruithoven
Copy link
Author

Currently when a client connects it always (also) joins the root namespace. This is convenient in some cases, but it shouldn't be able to block connecting to the target namespace right?

@darrachequesne
Copy link
Member

That issue was closed automatically. Please check if your issue is fixed with the latest release, and reopen if needed (with a fiddle reproducing the issue if possible).

@murrayju
Copy link

This is still very much an issue in v2.1. Just like @peteruithoven, I need to have authentication on the default namespace as well as custom namespaces. When the client connects to a custom namespace with invalid credentials, they never get the error event (or any event for that matter, it just hangs).

Can we reopen this?

@darrachequesne
Copy link
Member

@murrayju thanks for bringing that to my attention! Could you please give a look at socketio/socket.io-client#1202?

@softawarriors
Copy link

softawarriors commented Jul 2, 2018

@darrachequesne There i faced a similar issue. I am using Socket Manager at client side for creating a namespace socket connection. On server side i used namespace middleware for authentication. It works fine for the first time, when close the socket(in my case single socket open at client side, closing that close the manager as well), connecting the manager and socket again, keeps the manager in 'opening' state.

If I remove the namespace middleware and apply it to the default namespace, all works fine, manager and socket reconnects back with authentication.

Any idea?

@darrachequesne
Copy link
Member

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants