JavaScript code substitution on client side

Fortis

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 security on a client-side javascript code: for example, i have a chat-server on node.js+express and when a user connects to this chat, server should assign his registred username (authorisation through oldschool php+mysql is used) to his socket. The question is, can user modify his client-side script and connect to chat under different users' names?

Some code is given below:

(server-side part of assigning username, which is just getting the username from client-side call)

// when the client emits 'adduser', this listens and executes
socket.on('adduser', function(username){
    // store the username in the socket session for this client
    socket.username = username;
    // store the room name in the socket session for this client
    socket.room = 'General';
    // add the client's username to the global list
    usernames[username] = username;
    // send client to room 1
    socket.join('General');
    // echo to client they've connected
    socket.emit('updatechat', 'SERVER', 'you have connected to General');
    // echo to room 1 that a person has connected to their room
    socket.broadcast.to('General').emit('updatechat', 'SERVER', username + ' has connected to this room');
    socket.emit('updaterooms', rooms, 'General');
});

(client-side part of sending username to server, it looks like 'var username = "User";' for a particular user)

Yii::$app->view->registerJs('var username = "'.$user->identity->username.'";', yii\web\View::POS_HEAD);

(connect function)

chat.on('connect', function(){
        // call the server-side function 'adduser' and send one parameter (value of prompt)
        chat.emit('adduser', username);
});

SO the question is: can user change (for example, through chrome development tools) his username in line 'var username ...' and connect to chat under the different name?

P.S. this particular situation is just an example, obviously, changed nicknames in chat are not more than a simple joke, but similar situations can appear in other projects...

Denys Séguret

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Testing client side javascript code with Jasmine

From Dev

Visual Studio Code debugging client side JavaScript

From Dev

Monitoring GWT JavaScript/Client-side code

From Dev

How to run code coverage for client side javascript code using grunt

From Dev

JavaScript eval() on client-side for running untrusted code

From Dev

Fido U2F client side javascript source code

From Dev

jade and client side javascript

From Dev

javascript client side security

From Dev

jade and client side javascript

From Dev

Write javascript code that works with client side javascript and server side NodeJs modules

From Dev

Execute code securely on client side

From Dev

View client side Meteor code

From Dev

Call a server side variable on client side in javascript

From Dev

JavaScript array to PNG? - client side

From Dev

javascript client side validation loop

From Dev

JavaScript: Best practices for DOM-reliant code executed both server & client-side?

From Dev

JavaScript: Best practices for DOM-reliant code executed both server & client-side?

From Dev

how to get Current date time of other country in client side code in JavaScript?

From Dev

Access server-side javascript variable for manipulation with client side javascript

From Dev

Need to understand a specific signalr client side code

From Dev

Licensing system for client side code web application

From Dev

On deciding how heavily to use client side code

From Dev

Cannot find variable on client side react code

From Dev

Caching client side code results across pages

From Dev

Meteor build is importing server side code to client

From Dev

meteor one time initialization code on client side

From Dev

Using same rendering code for client side and server side

From Dev

Using same rendering code for client side and server side

From Dev

Validate XML against XSD with JavaScript on client side

Related Related

  1. 1

    Testing client side javascript code with Jasmine

  2. 2

    Visual Studio Code debugging client side JavaScript

  3. 3

    Monitoring GWT JavaScript/Client-side code

  4. 4

    How to run code coverage for client side javascript code using grunt

  5. 5

    JavaScript eval() on client-side for running untrusted code

  6. 6

    Fido U2F client side javascript source code

  7. 7

    jade and client side javascript

  8. 8

    javascript client side security

  9. 9

    jade and client side javascript

  10. 10

    Write javascript code that works with client side javascript and server side NodeJs modules

  11. 11

    Execute code securely on client side

  12. 12

    View client side Meteor code

  13. 13

    Call a server side variable on client side in javascript

  14. 14

    JavaScript array to PNG? - client side

  15. 15

    javascript client side validation loop

  16. 16

    JavaScript: Best practices for DOM-reliant code executed both server & client-side?

  17. 17

    JavaScript: Best practices for DOM-reliant code executed both server & client-side?

  18. 18

    how to get Current date time of other country in client side code in JavaScript?

  19. 19

    Access server-side javascript variable for manipulation with client side javascript

  20. 20

    Need to understand a specific signalr client side code

  21. 21

    Licensing system for client side code web application

  22. 22

    On deciding how heavily to use client side code

  23. 23

    Cannot find variable on client side react code

  24. 24

    Caching client side code results across pages

  25. 25

    Meteor build is importing server side code to client

  26. 26

    meteor one time initialization code on client side

  27. 27

    Using same rendering code for client side and server side

  28. 28

    Using same rendering code for client side and server side

  29. 29

    Validate XML against XSD with JavaScript on client side

HotTag

Archive