[英]How to prevent XSS for the form action URL?
我们使用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(跨站点脚本)攻击,所以它要求我对以上代码进行编码。
如何防止表单动作网址使用XSS(跨站点脚本)。 因为,这是一种不受信任的数据,因此有任何方法可以对URL进行编码。 经过一番研究,我发现最好使用ESAPI.encoder()。encodeForURL('url'); 方法。 我的疑问是,将ESAPI.encoder()。encodeForURL('url')用于表单操作URL是正确的方法吗?
实际表单操作网址:
<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"))%>">
任何建议,将不胜感激。
ESAPI.encoder().encodeForURL()
是不正确的,因为这会对整个字符串进行百分比编码,这可能会损坏url。 这意味着更多用于在URL中编码单个参数。
在这种情况下,应在属性内使用ESAPI.encoder().encodeForHTMLAttribute()
。
但是这里还有一个额外的问题。 如果该网址不受信任,则用户可能会将其登录详细信息发送到不受信任的站点。 您应该检查网址的确切位置,并确保用户无法控制其内容。
如果网址始终是相似的格式,则可以在控制器中对此进行检查。
如果您将用户输入用于表单操作,则您的逻辑从根本上是有缺陷的。
您的代码的总体情况是什么? 我们可以帮助您更好地进行设计,以免您将用户输入作为表单操作的开头
您应该使用“ 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
我希望他们有道理...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.