javascript - Socket.io sanitize incoming data (xss) - Stack Overflow

im using socket.io in expressjs 3. And i want to sanitize ining messages with express-validator. I have

im using socket.io in expressjs 3. And i want to sanitize ining messages with express-validator. I have this code:

var expressValidator = require('express-validator')
, sanitize = require('express-validator').sanitize;

socket.on('chat', function (data) {
    io.sockets.in('test').emit('chat', {
            user: sh.session.user,
            message: data.message,
            time: new Date()
    });
});

how do i use sanitize(data.message).xss? Because this does not work.

im using socket.io in expressjs 3. And i want to sanitize ining messages with express-validator. I have this code:

var expressValidator = require('express-validator')
, sanitize = require('express-validator').sanitize;

socket.on('chat', function (data) {
    io.sockets.in('test').emit('chat', {
            user: sh.session.user,
            message: data.message,
            time: new Date()
    });
});

how do i use sanitize(data.message).xss? Because this does not work.

Share Improve this question asked Jun 2, 2012 at 12:18 georgesampergeorgesamper 5,1795 gold badges44 silver badges60 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 10

In this case you want to use validator instead of express-validator. First install it thru npm:

npm install validator

Then use it pretty much the same way:

var sanitize = require('validator').sanitize;

// later on
message = sanitize(data.message).xss()

The reason for this is because express-validator is used for when you are dealing with an HTTP request that went thru expressjs. In the case of Websockets, you are not going thru expressjs, but rather just listening on the same port as it. So express-validator is not actually "present" in the context of your Websocket's data event.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744311986a4567993.html

相关推荐

  • javascript - Socket.io sanitize incoming data (xss) - Stack Overflow

    im using socket.io in expressjs 3. And i want to sanitize ining messages with express-validator. I have

    7天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信