JavaScript code substitution on client side

后端 未结 1 1122
谎友^
谎友^ 2021-01-25 00:38

I\'m now learning some new technologies (such as node.js, socket.io, redis etc.) and making some simple test applications to see how it can work.

My question is about se

相关标签:
1条回答
  • 2021-01-25 01:31

    Supposing your variables are protected in closures and that it's not trivial to change them by typing username='root' in the console, a user could simply replace the whole code.

    Everything that happens client side is totally out of your control.

    The good news is that they are solutions not involving a duplicate authentication. Supposing you already authenticate the user in your express application, you can get the session and the user from that.

    See how I do it in my chat server :

    var sessionSockets = new SessionSockets(io, sessionStore, cookieParser);
    sessionSockets.on('connection', function (err, socket, session) {
        function die(err){
            console.log('ERR', err);
            socket.emit('error', err.toString());
            socket.disconnect();
        }
        if (! (session && session.passport && session.passport.user && session.room)) return die ('invalid session');
        var userId = session.passport.user;
        if (!userId) return die('no authenticated user in session');
        ... handling socket for authenticated user
    });
    

    Basically, it uses the session.socket.io module to propagate the session from the standard http requests (authenticated using passport) to the socket.io connection. And everything that isn't supposed to be provided by the user is taken from the session/db/server.

    0 讨论(0)
提交回复
热议问题