gpt4 book ai didi

javascript - 我用sockets.io做了一个简单的聊天室,如何防止XSS攻击?

转载 作者:行者123 更新时间:2023-12-02 18:44:50 25 4
gpt4 key购买 nike

正如标题所说,我用sockets.io制作了一个简单的聊天室,唯一的问题是我没有xss保护,而且我的好友不断将无限循环作为用户名,所以你可以想象这会变得多么糟糕:P。这是我的 app.js

/**
* Module dependencies.
*/

var express = require('express')
, routes = require('./routes')
, user = require('./routes/user')
, http = require('http')
, path = require('path');

var app = express();

// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));

// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}

app.get('/', routes.index);
app.get('/users', user.list);

var server = http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
var io = require('socket.io').listen(server);

var usernames = {};

io.sockets.on('connection', function (socket) {
// When the client emits 'sendchat' this listens and executes
socket.on('sendchat', function(data) {
io.sockets.emit('updatechat', socket.username, data);
});

// When the client emites 'adduser' this listens and executes
socket.on('adduser', function(username) {
// Store the username in the socket session for this client
socket.username = username;
// add the client's username to the global list
usernames[username] = username;
// echo to the client they've connected
socket.emit('updatechat', 'SERVER', 'you have connected');
// echo globally (all clients) that a person has connected
socket.broadcast.emit('updatechat', 'SERVER', username + ' has connected');
// update the list of users in chat, client-side
io.sockets.emit('updateusers', usernames);
});

socket.on('disconnect', function() {
// remove the username from global usernames list
delete usernames[socket.username];
// update list of users in chat, client-side
io.sockets.emit('updateusers', usernames);
// echo globally that the client has left
socket.broadcast.emit('updatechat', 'SERVER', socket.username + ' has disconnected');
});
});

我如何清理他们的输入以防止此类事情,我尝试过谷歌搜索 XSS 防护预防、清理 html 输入等,但我什么也没找到!

客户端代码:

socket.on('updatechat', function(username, data) {
$('#conversation').append('<b>'+username+ ':</b>' + data.replace() + '<br>');
});

最佳答案

正如我在评论中提到的,不要从您的服务器发送整条消息。您正在浪费带宽并将表示层混合到服务器中,这将使以后的事情变得非常麻烦。

不要发出此 updatechat 消息,而是尝试发送一些更有用的内容,例如带有用户名的 userDisconnected 。让客户端代码显示客户端已断开连接的消息。

现在,对于您的客户,请执行以下操作:

socket.on('userDisconnected', function(username, data) {
$('#conversation').append(
$('<span>').addClass('serverMessage').text(username + ' has disconnected'),
);
});

这里的关键是你使用$.text()来设置innerText。 HTML 变得无关紧要。

关于javascript - 我用sockets.io做了一个简单的聊天室,如何防止XSS攻击?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16512568/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com