简体   繁体   English

如何防止XSS用于表单操作URL?

[英]How to prevent XSS for the form action URL?

We use Shibboleth's SingleSingOut(SSO) to do the authentication.Shibboleth is an open-source project which has been integrated into our project. 我们使用Shibboleth的SingleSingOut(SSO)进行身份验证。Shibboleth是一个开源项目,已集成到我们的项目中。 Shibboleth will do the redirect to login.jsp page, if the user has not been authenticated.Now we have customized login.jsp page to support localization. 如果用户尚未通过身份验证,Shibboleth将重定向到login.jsp页面。现在,我们已经定制了login.jsp页面以支持本地化。 So, the form actionUrl has to be sent by the Shibboleth IDP(Identity Provider) to perform the authentication. 因此,必须由Shibboleth IDP(身份提供者)发送actionUrl表单以执行身份验证。 Here is the below sample code which the Shibboleth has provided: 这是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>

Now I have used the OWASP ZAP tool to check the security attack. 现在,我已经使用OWASP ZAP工具来检查安全攻击。 It has raised a High Risk at the below code 它在以下代码处引发了高风险

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

It has told that there can be XSS (Cross-site Scripting) attack so, it has asked me to encode the above code. 它说可能存在XSS(跨站点脚本)攻击,所以它要求我对以上代码进行编码。

How can I prevent the XSS(Cross Site Scripting) for the form action url. 如何防止表单动作网址使用XSS(跨站点脚本)。 Because, it is an untrusted data is there any way to encode the URL. 因为,这是一种不受信任的数据,因此有任何方法可以对URL进行编码。 After some research I found that, its better to use ESAPI.encoder().encodeForURL('url'); 经过一番研究,我发现最好使用ESAPI.encoder()。encodeForURL('url'); method. 方法。 My doubt is that, is it a correct way to use the ESAPI.encoder().encodeForURL('url') for the form action URL? 我的疑问是,将ESAPI.encoder()。encodeForURL('url')用于表单操作URL是正确的方法吗?

From the Cross_Site_Scripting Prevention Cheat sheet Cross_Site_Scripting预防备忘单中

Actual form action url: 实际表单操作网址:

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

Encoded form action url: 编码的表单操作网址:

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

Any suggestions would be appreciated. 任何建议,将不胜感激。

ESAPI.encoder().encodeForURL() is not correct because this does percent encoding of the whole string which can corrupt the url. ESAPI.encoder().encodeForURL()是不正确的,因为这会对整个字符串进行百分比编码,这可能会损坏url。 It's meant more for encoding individual parameters within a url. 这意味着更多用于在URL中编码单个参数。

In this context, within an attribute, ESAPI.encoder().encodeForHTMLAttribute() should be used. 在这种情况下,应在属性内使用ESAPI.encoder().encodeForHTMLAttribute()

There is an extra problem here though. 但是这里还有一个额外的问题。 If the url is not trusted, then the user could be sending their log in details to an untrusted site. 如果该网址不受信任,则用户可能会将其登录详细信息发送到不受信任的站点。 You should check exactly where the url comes from and make sure the user can't control its contents. 您应该检查网址的确切位置,并确保用户无法控制其内容。

If the url is always of a similar format, then you can check against this in the controller. 如果网址始终是相似的格式,则可以在控制器中对此进行检查。

Your logic is fundamentally flawed if you are using user input for the form action. 如果您将用户输入用于表单操作,则您的逻辑从根本上是有缺陷的。

What is the bigger picture of your code? 您的代码的总体情况是什么? We can help you design it better so that you are not using user input as a form action to start with 我们可以帮助您更好地进行设计,以免您将用户输入作为表单操作的开头

you should use tools like "modsecurity" which uses "regular expression" and has some rules to prevent xss attacks. 您应该使用“ modsecurity”之类的工具,该工具使用“正则表达式”并具有一些防止xss攻击的规则。 Take a look at : 看一眼 :

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

I hope they make sense ... 我希望他们有道理...

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

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