简体   繁体   English

避免HTML和Scripts标签呈现

[英]Avoiding HTML and Scripts tags renders

I've been working in a Chat Box, I use Node.js + socket.io for client-server communication so I can't execute PHP code(I've read that is possible but It's not recommended and difficult to achieve). 我一直在聊天室中工作,我使用Node.js + socket.io进行客户端-服务器通信,所以我无法执行PHP代码(我读过这是可能的,但是不建议这样做,而且很难实现)。 Everything works fine except when users send messages with HTML or Scripts tags. 除非用户发送带有HTML或Scripts标记的消息,否则一切正常。

I've tried to remove those tags with a function that I found in here . 我尝试使用在这里找到的功能删除这些标签。 It works fine, but only for HTML tags, then I thought: Well, I can remove Script tags first using regular expressions. 它工作正常,但仅适用于HTML标记,然后我想到:好吧,我可以先使用正则表达式删除Script标记。 Something like this 像这样

.replace(/<script.*>.*<\\/script>/ims, " ")

and then remove the HTML tags, but I'm not sure if this method is hacks proof, and that's a lot of processes for each message sent. 然后删除HTML标记,但是我不确定此方法是否可以防止黑客入侵, 并且每条发送的消息都要经过很多过程。

Is there any form of appending text $("#div").append(message); 是否有附加文本$("#div").append(message); without rendering HTML/JavaScript code ? 没有呈现HTML / JavaScript代码? --- Print the text just the way it is, like in twitch.tv , in there, if you send a message with HTML/JS/PHP code, the message is printed just like the way you type it. ---按原样打印文本,例如在twitch.tv中,如果您使用HTML / JS / PHP代码发送消息,则消息的打印方式与键入方式相同。

or, is there any other way to remove tags ? 或者,还有其他删除标签的方法吗?

Thanks for your time. 谢谢你的时间。

只需在div后面附加消息设置为文本即可。

$("#div").append( $("<div>").text(message) );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM