似乎无法获得ESAPI Validator getValidInput()为URL参数工作

问题描述:

我正在尝试使用ESAPI编码器来识别和规范URL编码的查询参数。它有点类似,但并不像API所表明的那样。这里是我的课,并在下面了它的输出:似乎无法获得ESAPI Validator getValidInput()为URL参数工作

CODE

package test.test; 

import org.owasp.esapi.ESAPI; 
import org.owasp.esapi.Validator; 
import org.owasp.esapi.errors.EncodingException; 
import org.owasp.esapi.errors.IntrusionException; 
import org.owasp.esapi.errors.ValidationException; 

public class ESAPITester { 

    public static void main(String argsp[]) throws ValidationException, 
    IntrusionException, EncodingException { 

     String searchString = "-/+=_ !$*[email protected]"; 
     String singleEncoded = ESAPI.encoder().encodeForURL(searchString); 
     String doubleEncoded = ESAPI.encoder().encodeForURL(singleEncoded); 
     Validator validator = ESAPI.validator(); 
     System.out.println("Searched  : " + searchString); 
     System.out.println("Single encoded : " + singleEncoded); 
     System.out.println("Double encoded : " + doubleEncoded); 
     System.out.println("Decode from URL : " + ESAPI.encoder().decodeFromURL(singleEncoded)); 
     System.out.println("Canonicalized : " + ESAPI.encoder().canonicalize(singleEncoded)); 
     System.out.println("Valid input  : " + validator.getValidInput("http", 
       searchString, "HTTPParameterValue", 100, true, true)); 
     System.out.println("Valid from Encoded : " + validator.getValidInput("http", 
       singleEncoded, "HTTPParameterValue", 100, true, true)); 

    } 
} 

输出

Searched  : -/+=_ !$*[email protected] 
Single encoded : -%2F%2B%3D_+%21%24*%3F%40 
Double encoded : -%252F%252B%253D_%2B%2521%2524*%253F%2540 
Decode from URL : -/ =_ !$*[email protected] 
Canonicalized : -/+=_+!$*[email protected] 
Valid input  : -/+=_ !$*[email protected] 
log4j:WARN No appenders could be found for logger (IntrusionDetector). 
log4j:WARN Please initialize the log4j system properly. 
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info. 
Exception in thread "main" org.owasp.esapi.errors.ValidationException: http: Invalid input. Please conform to regex ^[\p{L}\p{N}.\-/+=_ !$*[email protected]]{0,1000}$ with a maximum length of 100 
    at org.owasp.esapi.reference.validation.StringValidationRule.checkWhitelist(StringValidationRule.java:144) 
    at org.owasp.esapi.reference.validation.StringValidationRule.checkWhitelist(StringValidationRule.java:160) 
    at org.owasp.esapi.reference.validation.StringValidationRule.getValid(StringValidationRule.java:284) 
    at org.owasp.esapi.reference.DefaultValidator.getValidInput(DefaultValidator.java:214) 
    at test.test.ESAPITester.main(ESAPITester.java:25) 

我的问题是:为什么getValidInput()不规范化的URL编码的输入参数?我很好奇为什么canonicalize()方法会这样做,但getValidInput()与最终参数('canonicalize')设置为true不会。

+0

**更新**:在ESAPI.properties定义为 'HTTPParameterValue' 的正则表达式是:! '^ [\\ p {L} \\ p {N} \\ -/+ = _ $ *?@] {0,1000} $' – raTM

+0

您的发布代码正在使用'String searchString =“ -/+ = _!$ *?@”;'而不是像'singleEncoded'这样的URL编码字符串。所以我希望规范化的值等价于非规范化的版本。 – avgvstvs

+0

感谢您的回复@avgvstvs ....对您的问题,我同意canonicalize正在做它应该做的事情。我的问题更多的是为什么第二个validator.getValidInput()调用抛出一个异常,当它所要做的就是规范化输入并验证它与预期值匹配。 换句话说,对canonicalize()的直接调用起作用,但对getValidInput()的调用失败。 – raTM

所以,问题就变成了:

为什么第二validator.getValidInput()调用抛出一个异常,当 所有预期做的是规范化的输入和验证 它的预期值相匹配。换句话说,直接调用 canonicalize()会工作,但对getValidInput()的调用失败。

这里有点不对劲。在HTTPParameterValue您从OWASP源回购获得的版本,正则表达式是^[a-zA-Z0-9.\\-\\/[email protected]_ ]*$有人操纵HTTPParameterValue看起来更像SafeString^[\\s\\p{L}\\p{N}.]{0,1024}$

See line 440.

这是不对的。如果您需要自定义更改,请不要更改默认ESAPI值,请使用已建立的模式编写全新的validator.properties条目。

然而,您的测试仍然会失败,因为字符串解码为-/+=_ !$*[email protected]?是http查询中的保留字符。

From an earlier spec:

3.4。查询组件

查询组件是一个由 资源解释的信息字符串。

query   = *uric 

在一个查询组件,字符 “;”, “/”, “?” “:”, “@”,
“&”, “=”, “+”,”, “和”$“保留。

至于根据你在,^[\\p{L}\\p{N}.\\-/+=_ !$*[email protected]]{0,1000}$read the code.运行在266行你会看到受影响的方法正则表达式为什么输入失败。

这里是你想看看:

public String getValid(String context, String input) throws ValidationException 
    { 
     String data = null; 

     // checks on input itself 

     // check for empty/null 
     if(checkEmpty(context, input) == null) 
      return null; 

     if (validateInputAndCanonical) 
     { 
      //first validate pre-canonicalized data 

      // check length 
      checkLength(context, input); 

      // check whitelist patterns 
      checkWhitelist(context, input); 

      // check blacklist patterns 
      checkBlacklist(context, input); 

      // canonicalize 
      data = encoder.canonicalize(input); 

     } else { 

      //skip canonicalization 
      data = input;   
     } 

     // check for empty/null 
     if(checkEmpty(context, data, input) == null) 
      return null; 

     // check length 
     checkLength(context, data, input); 

     // check whitelist patterns 
     checkWhitelist(context, data, input); 

     // check blacklist patterns 
     checkBlacklist(context, data, input); 

     // validation passed 
     return data; 

之前就试图规范化您输入的正则表达式被检查。