如何防止表单操作URL的XSS?

时间:2014-12-26 10:09:39

标签: javascript html cross-site zap

我们使用Shibboleth的SingleSingOut(SSO)进行身份验证.Shibboleth是一个已经集成到我们项目中的开源项目。如果用户尚未经过身份验证,Shibboleth将重定向到login.jsp页面。现在我们已经定制了login.jsp页面以支持本地化。因此,必须由Shibboleth IDP(身份提供者)发送表单actionUrl以执行身份验证。以下是Shibboleth提供的以下示例代码:

    <% if(request.getAttribute("actionUrl") != null){ %>
      <form id="login" action="<%=request.getAttribute("actionUrl")%>" method="post">
    <% }else{ %>
      <form id="login" action="j_security_check" method="post">
    <% } %>

      <% if ("true".equals(request.getAttribute("loginFailed"))) { %>
        <section>
          <p class="form-element form-error">Login has failed. Double-check your username and password.</p>
        </section>
      <% } %>

      <legend>
        Log in to <idpui:serviceName/>
      </legend>

      <section>
        <label for="username">Username</label>
        <input class="form-element form-field" name="j_username" type="text" value="">
      </section>

      <section>
        <label for="password">Password</label>
        <input class="form-element form-field" name="j_password" type="password" value="">
      </section>

      <section>
        <button class="form-element form-button" type="submit">Login</button>
      </section>
    </form>

现在我使用OWASP ZAP工具检查安全攻击。它在以下代码中引发了高风险

  <form id="login" action="<%=request.getAttribute("actionUrl")%>" method="post">

它告诉可能存在XSS(跨站点脚本)攻击,所以它要求我对上面的代码进行编码。

如何阻止表单操作URL的XSS(跨站点脚本)。因为,它是一个不受信任的数据是否有任何方法来编码URL。经过一些研究后我发现,最好使用ESAPI.encoder()。encodeForURL(&#39; url&#39;);方法。我怀疑的是,对于表单操作URL,使用ESAPI.encoder()。encodeForURL(&#39; url&#39;)是否正确?

来自Cross_Site_Scripting Prevention Cheat sheet

实际表单操作网址:

     <form name="loginForm" id="login" method="POST" action="<%=request.getParameter("actionUrl")%>">

编码表单操作网址:

     <form name="loginForm" id="login" method="POST" action="<%=ESAPI.encoder().encodeForURL(request.getParameter("actionUrl"))%>">

任何建议都将不胜感激。

3 个答案:

答案 0 :(得分:3)

ESAPI.encoder().encodeForURL()不正确,因为这会对可能损坏网址的整个字符串进行百分比编码。对于在网址中编码单个参数,这意味着更多。

在此上下文中,应在属性中使用ESAPI.encoder().encodeForHTMLAttribute()

但这里还有一个额外的问题。如果该网址不受信任,则该用户可能会将其登录详细信息发送到不受信任的网站。您应该确切地检查网址的来源,并确保用户无法控制其内容。

如果网址的格式类似,则可以在控制器中对此进行检查。

答案 1 :(得分:0)

如果您使用用户输入进行表单操作,那么您的逻辑基本上是有缺陷的。

您的代码的大局是什么?我们可以帮助您更好地设计它,以便您不会将用户输入用作表单操作以

开头

答案 2 :(得分:-1)

你应该使用像“modsecurity”这样的工具,它使用“正则表达式”,并有一些规则来防止xss攻击。 看看:

http://www.opensourceforu.com/2011/08/securing-apache-part-10-mod_security/ http://blog.spiderlabs.com/2013/09/modsecurity-xss-evasion-challenge-results.html

我希望他们有意义......