简体   繁体   English

一个潜在危险的Request.QueryString —如何在不关闭安全功能的情况下进行预防

[英]A potentially dangerous Request.QueryString — How to prevent without turning off safety features

I am getting this message when I perform a post with some data encoded in the query string. 当我执行带有查询字符串中编码的一些数据的帖子时,我收到此消息。 I have browsed the web on this and all the solutions are about turning off the validation -- which seems backward to me. 我已经在网上浏览了所有解决方案,都是关于关闭验证的,对我来说似乎是倒退的。 What I want to do is modify the query string so that it doesn't trigger the validation in the first place. 我想做的就是修改查询字符串,这样它就不会首先触发验证。

The query string is urlEncoded with this javascript: 查询字符串使用以下javascript进行urlEncoded:

var qs = 'i=' + id+ '&c=' + encodeURIComponent(c) + '&' + 'p=' + encodeURIComponent(p);

'Id' is just an integer, so the c and p parameters are the only ones likely to cause this, and they are both URIencoded. 'Id'只是一个整数,因此c和p参数是唯一可能导致此问题的参数,并且它们都经过URI编码。

What causes this error, and what, beyond uri encoding can I do to prevent the complaint? 是什么导致了此错误?除了uri编码之外,我还能做些什么来防止投诉? I don't like turning off safety features. 我不喜欢关闭安全功能。 It is smart to wear a safety belt when you are driving. 开车时系好安全带是明智的。

This is a safety belt only for people that haven't passed their driving test. 这是一条安全带,仅供未通过驾驶考试的人员使用。 If output is correctly encoded, the "potentially dangerous" query string value is no longer dangerous. 如果输出的编码正确,则“潜在危险”查询字符串值不再是危险的。

For example, if the character " is output to HTML this should be encoded as " , or if the character ' is output to JavaScript then it should be encoded as \\x27 . 例如,如果将字符"输出到HTML,则应将其编码为" ,或者如果将字符'输出到JavaScript,则应将其编码为\\x27

ASP.NET Request Validation only protects your code if you are not correctly encoding for output, and furthermore it only protects values that have been input via a website with Request Validation enabled. ASP.NET请求验证仅在您未正确编码输出代码时才保护您的代码,此外,它仅保护通过启用了请求验证的网站输入的值。 Anything input from any other sources (eg a shared database, another application or an external API) will not be validated by request validation. 来自任何其他来源(例如共享数据库,另一个应用程序或外部API)的任何输入都不会通过请求验证来验证。 This is why I would code your application to handle correct output encoding instead. 这就是为什么我会编码您的应用程序以处理正确的输出编码的原因。 If stackoverflow.com blocked potentially dangerous input then it would not be possible for people to write code like this in their posts: <script>alert('example');</script> , but with proper output encoding, as you can see this is safe. 如果stackoverflow.com阻止了潜在危险的输入,那么人们将不可能在他们的帖子中编写如下代码: <script>alert('example');</script> ,但是具有正确的输出编码,如您所见这很安全。

Check out my post on ASP.NET Security (A3-Cross-Site Scripting (XSS) section). 请查看我有关ASP.NET安全性的文章 (A3跨站点脚本(XSS)部分)。

Also see the OWASP XSS Prevention Cheat Sheet . 另请参阅OWASP XSS预防备忘单

The ASP.NET team doesn't want you rely on 'RequestValidation' so it is ok to turn it off (it's a crutch that gives a false sense of security because it isn't always up to speed). ASP.NET团队不希望您依赖“ RequestValidation”,因此可以将其关闭(这是拐杖,会给人一种虚假的安全感,因为它并不总是能达到最高速度)。

For info on why this ok and what you should do instead, watch this video starting at 11:10. 有关为什么可以这样做以及您应该怎么做的信息,请观看从11:10开始的视频 I would actually recommend watching the whole video. 实际上,我建议您观看整个视频。

You're right, most "fixes" for this tell you to turn off validation so it was kind of difficult to find something other than that. 没错,大多数“修复”都告诉您关闭验证,因此很难找到除此以外的其他东西。 I think you're going to have to turn it off just for that request and then manually validate it. 我认为您将不得不仅针对该请求将其关闭,然后手动对其进行验证。 According to microsoft, you can disable it for a request like this: 根据Microsoft,您可以为这样的请求禁用它:

Request.Unvalidated("userInput"); // Validation bypassed
Request.Unvalidated().Form["userInput"]; // Validation bypassed 

If you disable request validation, you must manually check the unvalidated user input for potentially dangerous input

See this article: http://msdn.microsoft.com/en-us/library/hh882339(v=vs.110).aspx 请参阅这篇文章: http : //msdn.microsoft.com/zh-cn/library/hh882339(v=vs.110).aspx

Good Luck! 祝好运!

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

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