【问题标题】:Cross site scripting when using windows.location.href使用 windows.location.href 时的跨站点脚本
【发布时间】:2019-01-28 08:36:06
【问题描述】:

我正在使用 Windows.location.href=URl 从 java 脚本导航到 MVC 控制器方法。 我想在重定向时避免任何 XSS 攻击。我该怎么办

【问题讨论】:

  • 您的意思可能是window。您的 URI 包含什么值?用户输入?

标签: javascript model-view-controller cross-domain url-redirection x-xsrf-token


【解决方案1】:

您可以编写自己的 XSS 清理函数

function encodeHTML(s) {
    return s.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/"/g, '&quot;');
}

window.location.href = encodeHTML(URI);

【讨论】:

    【解决方案2】:

    这应该很容易。我已经为你准备好了解决方案。首先是一些理论上的理解

    规则 #0 - 切勿插入不受信任的数据,除非在允许的位置

    <script>...NEVER PUT UNTRUSTED DATA HERE...</script>   directly in a script
    
    <style>...NEVER PUT UNTRUSTED DATA HERE...</style>   directly in CSS
    

    规则 #1 - 在将不受信任的数据插入 HTML 元素内容之前,HTML 和 JavaScript 转义

    HTML ...在将不受信任的数据放在这里之前转义... JS

    <script>alert('...ESCAPE UNTRUSTED DATA BEFORE PUTTING HERE...')</script>     inside a quoted string
    

    转义

    & --> &amp;
     < --> &lt;
     > --> &gt;
     " --> &quot;
     ' --> &#x27;     &apos; not recommended because its not in the HTML spec (See: section 24.4.1) &apos; is in the XML and XHTML specs.
     / --> &#x2F;     forward s
    

    包含 lash 是因为它有助于结束 HTML 实体

    确保返回的 Content-Type 标头是 application/json 而不是 text/html。

    进入编码部分

    以下内容会对您有所帮助

    private String killXSS(String value) {
            if (value != null) {
                // NOTE: It's highly recommended to use the ESAPI library and uncomment the following line to
                // avoid encoded attacks.
                // value = ESAPI.encoder().canonicalize(value);
    
                // Avoid null characters
                value = value.replaceAll("", "");
    
                // Avoid anything between script tags
                Pattern scriptPattern = Pattern.compile("<script>(.*?)</script>", Pattern.CASE_INSENSITIVE);
                value = scriptPattern.matcher(value).replaceAll("");
    
                // Avoid anything in a src='...' type of expression
                scriptPattern = Pattern.compile("src[\r\n]*=[\r\n]*\\\'(.*?)\\\'", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
                value = scriptPattern.matcher(value).replaceAll("");
    
                scriptPattern = Pattern.compile("src[\r\n]*=[\r\n]*\\\"(.*?)\\\"", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
                value = scriptPattern.matcher(value).replaceAll("");
    
                // Remove any lonesome </script> tag
                scriptPattern = Pattern.compile("</script>", Pattern.CASE_INSENSITIVE);
                value = scriptPattern.matcher(value).replaceAll("");
    
                // Remove any lonesome <script ...> tag
                scriptPattern = Pattern.compile("<script(.*?)>", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
                value = scriptPattern.matcher(value).replaceAll("");
    
                // Avoid eval(...) expressions
                scriptPattern = Pattern.compile("eval\\((.*?)\\)", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
                value = scriptPattern.matcher(value).replaceAll("");
    
                // Avoid expression(...) expressions
                scriptPattern = Pattern.compile("expression\\((.*?)\\)", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
                value = scriptPattern.matcher(value).replaceAll("");
    
                // Avoid javascript:... expressions
                scriptPattern = Pattern.compile("javascript:", Pattern.CASE_INSENSITIVE);
                value = scriptPattern.matcher(value).replaceAll("");
    
                // Avoid vbscript:... expressions
                scriptPattern = Pattern.compile("vbscript:", Pattern.CASE_INSENSITIVE);
                value = scriptPattern.matcher(value).replaceAll("");
    
                // Avoid onload= expressions
                scriptPattern = Pattern.compile("onload(.*?)=", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
                value = scriptPattern.matcher(value).replaceAll("");
            }
            return value;
        }
    

    确保您发送到服务器的每个请求都通过上述代码剥离,您将永远不会成为 XSS 的受害者。

    【讨论】:

    • 最好在将数据注入输出中的任何位置之前对其进行清理。这保证了您的安全。
    • @Xiddoc 很好地回答了同样的卫生问题。根据我的理解,我们有两种消毒方式。每个业务规则特定 1 个,每个行业标准 2 个。提供的答案是 OWASP 网站在给出时引用的。
    • 根据原始问题的参考,提到的事情已经足够了
    • 检查 this 了解 OWASP 标准
    • 好吧,我不同意。请参考 OWASP 网站,而不是参考其他答案。
    猜你喜欢
    • 1970-01-01
    • 2012-08-25
    • 2014-01-27
    • 2011-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-13
    相关资源
    最近更新 更多