簡體   English   中英

如何防止XSS用於表單操作URL?

[英]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是正確的方法嗎?

Cross_Site_Scripting預防備忘單中

實際表單操作網址:

     <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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM