简体   繁体   English

jquery - 如何转义 html 以防止 XSS?

[英]jquery - how to escape html to prevent XSS?

I'm using laravel, when a user sends a text message, it may contain some malicious code.我正在使用 Laravel,当用户发送短信时,它可能包含一些恶意代码。 When I use {{}} it will show me the exact text the user has sent.当我使用{{}}它会显示用户发送的确切文本。 If he has sent如果他已经发送

<script>alert("malicious")</script>

it will show exactly the same and it's good, but when I use jquery ajax, I put the fetched data to some variables within some html tags and lastly append all of them to a main tag like so:它将显示完全相同并且很好,但是当我使用 jquery ajax 时,我将获取的数据放入一些 html 标签内的一些变量中,最后将它们全部附加到主标签中,如下所示:

   data = '';
    //loop starts here // some otger codes deleted for cleanness
    data += "<h2>"+response.name+"</h2>";
    data += "<p>"+response.description+"</p>";
    $('#mydata').html(data);

and now the problem is that if I use html() the user malicious code will be executed and if I don't, the result will not be shown as html.现在的问题是,如果我使用html() ,用户恶意代码将被执行,如果我不使用,结果将不会显示为 html。

I guess I should do something with $.parseHTML , isn't it?我想我应该用$.parseHTML ,不是吗?

Thanks谢谢

You should use jQuery text() to encode the data.您应该使用 jQuery text()对数据进行编码。

$('#mydata').text(data);

EDIT: To create the content of #mydata you can use编辑:要创建 #mydata 的内容,您可以使用

$('#mydata')
  .html("")
  .append($("<h2></h2>").text(response.name))
  .append($("<p></p>").text(response.description))

You can also do this to remove tags...您也可以这样做来删除标签...

$('#mydata').text(data).replace(/(<([^>]+)>)/ig,"");

read more on this post Todo List Sanitizing Input jQuery阅读这篇文章Todo List Sanitizing Input jQuery 的更多信息

you cannot render user data as HTML and escape it into safe way in the same time.无法将用户数据呈现为 HTML 并同时将其转义为安全的方式。 You may assume that some god-level regex could help you to drop just attributes but not tags.你可能会认为一些神级的正则表达式可以帮助你只删除属性而不是标签。 Unfortunately there are so many ways to inject JS into markup then you will never be sure.不幸的是,有很多方法可以将 JS 注入标记中,那么您永远无法确定。

So you have just few options:所以你只有几个选择:

  1. ignore risks at all完全忽视风险

  2. escape all the things (either using jQuery's text() or escaping on backend side with htmlspecialchars()转义所有内容(使用 jQuery 的 text() 或使用 htmlspecialchars() 在后端转义

  3. use non-HTML markup that is translated to HTML by simple rules in controlled way使用通过简单规则以受控方式转换为 HTML 的非 HTML 标记

I used: 我用了:

npm js-htmlencode

and it worked. 而且有效。

We had a similar scenario in of the project I worked.我们在我工作的项目中有类似的场景。 We used to get html content from server side which should be appended to DOM using Jquery.我们过去常常从服务器端获取 html 内容,这些内容应该使用 Jquery 附加到 DOM 中。 Before adding it to Add, we wanted to validate the HTML content we received from Server to safe guard the XSS security issues.在将其添加到 Add 之前,我们想验证从 Server 收到的 HTML 内容,以保护 XSS 安全问题。 Following is the generic method to encode the HTML content,以下是对 HTML 内容进行编码的通用方法,

function htmlEncode(source) {
  return $("<div>").text(source).html();
}

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

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