Java 5 HTML转义为防止XSS

编程入门 行业动态 更新时间:2024-10-11 21:21:53
本文介绍了Java 5 HTML转义为防止XSS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在研究Java应用程序中的一些XSS预防。

I'm looking into some XSS prevention in my Java application.

我目前有自定义构建的例程,它将转义存储在数据库中的任何HTML,以便安全地显示在我的jsps。但是,如果可能的话,我宁愿使用内置/标准方法来执行此操作。

I currently have custom built routines that will escape any HTML stored in the database for safe display in my jsps. However I would rather use a built in/standard method to do this if possible.

我目前没有编码发送到数据库但希望开始执行的数据那也是。

I am not currently encoding data that gets sent to the database but would like to start doing that as well.

是否有任何内置方法可以帮助我实现这一目标?

Are there any built in methods that can help me to achieve this?

推荐答案

您通常在显示期间逃避XSS,而不是在商店期间。在JSP中,您可以使用 JSTL (只需删除< jstl-1.2.jar > / WEB-INF / lib ) < c:out> 代码或 fn:escapeXml 此功能。例如

You normally escape XSS during display, not during store. In JSP you can use the JSTL (just drop jstl-1.2.jar in /WEB-INF/lib) <c:out> tag or fn:escapeXml function for this. E.g.

<input name="foo" value="<c:out value="${param.foo}" />">

<input name="foo" value="${fn:escapeXml(param.foo)}">

就是这样。如果您在处理输入和/或存储在数据库中时也这样做,那么它全部分布在业务代码和/或数据库中。你不应该这样做,它只是维护麻烦,当你在不同的地方做这件事时你会冒险双重逃避或更多(例如& 将成为& amp; amp; 而不是& amp; ,以便最终用户真正看到& amp; 而不是& 在视图中。代码和数据库对XSS不敏感。只有视图是。你应该只将它转义为就在那里。

That's it. If you do it during processing the input and/or storing in DB as well, then it's all spread over the business code and/or in the database. You should not do that, it's only maintenance trouble and you will risk double-escapes or more when you do it at different places (e.g. & would become &amp;amp; instead of &amp; so that the enduser would literally see &amp; instead of & in view. The code and DB are not sensitive for XSS. Only the view is. You should then escape it only right there.

更新:您发布了4个关于同一主题的主题:

Update: you've posted 4 topics about the same subject:

  • 跨站点脚本 - 隐藏表格字段
  • HttpServletRequest - 快速编码网址和隐藏字段参数的方法
  • HttpServletRequest - SetParameter
  • 这一个。
  • Cross Site Scripting - Hidden Form Fields
  • HttpServletRequest - Quick way to encode url and hidden field paramaters
  • HttpServletRequest - SetParameter
  • This one.

I只会警告你:不需要在servlet / filter / javacode / database /中转义它。你只是不必要地使事情过于复杂。在显示期间逃脱它。这就是全部。

I will only warn you: you do not need to escape it in servlet/filter/javacode/database/whatever. You're only unnecessarily overcomplicating things. Just escape it during display. That's all.

更多推荐

Java 5 HTML转义为防止XSS

本文发布于:2023-11-24 22:09:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1627057.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:为防止   Java   HTML   XSS

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!