【问题标题】:Allowing dangerous query strings允许危险的查询字符串
【发布时间】:2008-12-03 23:02:04
【问题描述】:

我需要能够允许包含“”等字符的查询字符串。但是,将 id=mi

从客户端检测到一个潜在危险的 Request.QueryString 值 (id="mi

如果我首先对 url 进行 url 编码(以创建 id=mi%3Cke),我仍然会收到相同的错误。我可以通过将 ValidateRequest="false" 放入 Page 指令来解决此问题,但如果可能的话,我宁愿不这样做。

那么无论如何允许在查询字符串中使用这些字符而不关闭 ValidateRequest?

编辑:我希望用户也能够手动输入网址,因此以某种方式对它们进行编码可能不起作用。

【问题讨论】:

  • 如果它包含'',它不是格式错误的URL吗?它们不应该分别编码为 %3C 和 %3E 吗?
  • 是的,我提出了我也尝试过的问题。还是没有骰子。

标签: asp.net


【解决方案1】:

我遇到了类似的问题。我选择对查询字符串进行 base64 编码来解决它。 使用

System.Text.ASCIIEncoding.ASCII.GetBytes

以字节形式获取字符串 然后

System.Convert.ToBase64String

把它变成一个“安全”的字符串。

要取回它,请使用:

System.Convert.FromBase64String

然后:

System.Text.ASCIIEncoding.ASCII.GetString

反转流动的极性。

【讨论】:

    【解决方案2】:

    有点谷歌搜索,我不这么认为。 异常似乎发生在您的代码甚至运行之前,因此您无法捕获异常。 我喜欢 base64 的编码或其他想法。

    【讨论】:

      【解决方案3】:

      您可以加密您的 id 值来解决此问题,而不是 URL 编码。然后您可能需要对加密字符串进行 URL 编码。

      【讨论】:

      • 这适用于我在页面上创建的链接,但不适用于用户直接在浏览器中键入的链接。
      【解决方案4】:

      我认为你有一些选择。您可以按照您的指示进行操作并关闭 ValidateRequest。然后,您需要自己处理任何输入清理。或者你可以只允许某些字符,或者让用户使用元语言来输入它们,即,而不是 '' 替换为 ']' 或者在提交自己之前重新编码这些元语言(或 Base64)。自己进行重新编码需要 Javascript 可用于使用禁止字符的查询。您可能仍需要进行输入清理。

      快速了解 jquery 实现:

       $(document).ready( function() {
          $('form').bind('submit', function() {
              $('form' > 'input[type=text]').each( function(i) {
                 if (this.value) {
                    this.value = encode(this.value);
                 }
              });
          });
       });
      
       function encode(value) {
          return ...suitable encoding...
       }
      

      【讨论】:

        【解决方案5】:

        我遇到了同样的问题,但是我偶然发现了这种 javascript 编码方法:

        <script type="text/javascript">  
        
         var unencodedText = "This is my text that contains whitespaces and characters like  and Ø";  
         var encodedText = "";  
         var decodedText = "";  
         alert('unencodedText: ' + unencodedText);  
        
         //To encode whitespaces and the 'Ø' character - use encodeURI  
         encodedText = encodeURI(unencodedText);  
         //We see that whitespaces and 'Ø' are encoded, but the '' is still there:  
         alert('encodedText: ' + encodedText);  
        
         //If we decode it we should get our unencodedText back  
         decodedText = decodeURI(encodedText);  
         alert('decodedText: ' + decodedText);  
        
         //To also encode the '' we use the encodeURIComponent  
         encodedText = encodeURIComponent(unencodedText);  
         //Now all the characters have been encoded:  
         alert('encodedText: ' + encodedText);  
        
         //To get our unencodedText back we now need to use the decodeURIComponent  
         decodedText = decodeURIComponent(encodedText);  
         alert('decodedText: ' + decodedText);  
        
        </script>
        

        如果您正在处理更复杂的符号,那么您可能希望对 url 使用 encodeURIComponent。

        我从this link.偷了这颗宝石

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-09-02
          • 2013-04-01
          • 2015-02-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多