简体   繁体   English

在JavaScript中使用ASP.Net Request.QueryString是否安全?

[英]Is use of the ASP.Net Request.QueryString safe in JavaScript?

Consider the following code: 请考虑以下代码:

function redirect() {
    window.location = "../../index.aspx?<%=Request.QueryString%>";
}

Is this code safe or can it be exploited by an XSS attack? 这段代码是安全的还是可以被XSS攻击利用?

If so: 如果是这样:

  1. How? 怎么样?
  2. How to prevent it? 怎么预防呢?

Consider this as a querystring: 将此视为查询字符串:

Xx"; alert('pwned'); window.location ="whatever

Basically, you are allowing completely arbitrary JavaScript to be injected. 基本上,您允许注入完全任意的JavaScript。

Best solution: never take direct user input and use it this way. 最佳解决方案:永远不要直接用户输入并以这种方式使用它。

Second best solution: encode it for use in a JavaScript string before using it there. 第二个最佳解决方案:在使用之前对其进行编码以在JavaScript字符串中使用。 A simple " breaks out here. 一个简单的“爆发在这里。

Also; 也; do not mistakenly do HTML encoding for this. 不要错误地为此做HTML编码。 That won't work right and will still be vulnerable. 这将无法正常工作,仍将是脆弱的。

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

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