0

我在我的项目中使用 HDIV 来保护 OWASP 列表,但文本框被接受<script>alert(1);</script>为输入并保存到数据库。

我想为所有 OWASP 问题编写测试用例。

下面是项目配置

web.xml配置

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>               
        WEB-INF/spring/applicationContext-db.xml
        WEB-INF/spring/spring-security.xml
        WEB-INF/spring/hdiv-config.xml
    </param-value>
</context-param>

webmvc-config.xml配置

<import resource="applicationContext-hdiv.xml" />

applicationContext-hdiv.xml配置

<beans>
    <bean id="requestDataValueProcessor" class="org.springframework.security.web.servlet.support.csrf.CsrfRequestDataValueProcessor" />


<bean id="editableValidator" class="org.hdiv.web.validator.EditableParameterValidator"/>
    <mvc:annotation-driven validator="editableValidator" />
</beans>

hdiv-config.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:hdiv="http://www.hdiv.org/schema/hdiv" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
               http://www.hdiv.org/schema/hdiv http://www.hdiv.org/schema/hdiv/hdiv.xsd">

            <hdiv:config excludedExtensions="css,js,ttf" errorPage="/manage/security-error" maxPagesPerSession="10" confidentiality="true" strategy="memory" randomName="true"> 
                <hdiv:sessionExpired loginPage="/main/common" homePage="/"/>
                <hdiv:startPages method="get">/,/.*,/manage/.*,/login</hdiv:startPages>
            </hdiv:config>

            <hdiv:validation id="customValidation" componentType="text">
                <hdiv:acceptedPattern><![CDATA[^[a-zA-Z0-9@.\-_]*$]]></hdiv:acceptedPattern>
                <hdiv:rejectedPattern><![CDATA[(\s|\S)*(--)(\s|\S)*]]></hdiv:rejectedPattern>
            </hdiv:validation>

            <hdiv:editableValidations registerDefaults="true">
                <hdiv:validationRule url=".*" enableDefaults="false">customValidation</hdiv:validationRule>
            </hdiv:editableValidations>         
        </beans> 
4

2 回答 2

2

XSS 是输出问题,而不是输入问题。输入验证是关于确保数据根据域是正确的。因此,例如,您想检查一个预计需要一年的字段是否实际收到了预期范围内的数字。您可能还想确保只使用允许的字符。在许多情况下,这将阻止许多攻击。

然而,对于复杂的输入,这不再可行。考虑一个您希望允许用户发表评论的文本字段。应该允许用户写评论,例如“An 因此 x < 4”。现在我们允许用于构建 html 标签的字符。

现在我们有两个选择:

  1. 使用工具去除危险的 HTML - 在某些时候可能会失败
  2. 使用 OWASP XSS 预防备忘单中描述的上下文感知转义
于 2015-01-03T09:17:38.000 回答
0

从“applicationContext-hdiv.xml”文件中删除“requestDataValueProcessor”和“editableValidator”bean,它们是由标签自动创建的。

查看此项目配置以获取工作示例: https ://github.com/hdiv/hdiv-spring-mvc-showcase

于 2015-01-05T11:03:16.037 回答