(I had checked the similar topics this/this/this/this, but I couldn't find the exact scenario or example)
Let's say I have a websocket server (MITM and XSS attacks are not in scope) where inside 'open' event, I check if user has authorization/permissions, and if not, I close
the connection. Otherwise, server proceeds to normal connection (subscribe & executing commands from client-side).
const srv = new WebSocket.Server('wss://example.com');
srv.on('connection', WSS => {
if ( ! checkAuth(userToken) ) { // let's assume the logic is working already
WSS.close();
}
else {
WSS.on('message', message => {
// ****** why should I check for authentication here ?? ******
console.log ("executing your command:" + message);
});
WSS.on('close', function close() {
console.log ("closed");
});
}
});
My question is, what are scenarios, where it's needed to check authentication inside further events, as connection is being closed immediately, if user is not authorized.