Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

want to send javascript code to socket.io server

I want to send a javascript code to the socket.io server so that server broadcast to the clients and that code get executed .

what i tried i make a json variable like this .and send that via socket.io

 var sent={
          'code': function(){
           console.log('javascript code');
          }

        };

socket.send(sent); 

when i check at server the message comes is {} and same as it is to the other client .

what is wrong in this code , how should i send javascript code ?

like image 764
XMen Avatar asked Jun 16 '26 18:06

XMen


1 Answers

Do not do this. You will open all listening clients to being hacked.

that said...

...you could follow pimbdb and pass the function in as a string, then use eval on the receiving end to execute it:

// on the sending client
var sent = {
    "code": "function() { /* do something not evil */ }"
}
socket.send(sent);

// on the receiving client
socket.on('message', function(data) {
    if (data.code) eval(data.code); // and pray.
});

Again, don't. Instead, pass some non-executable data that can then be interpreted in a limited number of non-malicious ways.

EDIT: Apologies, interpreted that as if you were executing code on the server. But client<-> client is XSS-prone, still.

like image 186
Stoive Avatar answered Jun 18 '26 06:06

Stoive