测试ESAPI.validator()的负面情况.isValidFileContent()

时间:2012-06-22 06:50:39

标签: java owasp esapi

我需要测试API owasps的负面情况ESAPI.validator()。isValidFileContent()

我已经尝试传递.exe和.ini文件的字节,其中测试通过,即返回类型 true 表示它是有效的文件内容。

什么被视为无效文件?

ESAPI.properties中是否需要任何配置?

1 个答案:

答案 0 :(得分:0)

为了找到一些负面的测试场景,我认为你需要通过api方法定义和所有这些。根据您的要求,您可能会发现链接http://code.google.com/p/owasp-esapi-java/source/browse/trunk/src/org/owasp/esapi/reference/DefaultValidator.java?r=364&spec=svn364更有帮助。请告诉我你的经历和成果...... NEGATIVE TEST SCENARIOS--如果允许null为false,则不允许null。如果文件大小超过属性文件中预定义的文件大小,或者是在同一方法中传递的字节数(传递的第三个参数),则它将抛出异常。如果错误地定义了上下文或者使用任何其他上下文来上传文件,则会抛出异常。在所有其他情况下,它将返回true,即有效文件。

 IMPLEMENTATION BELOW:
 /**
         * Returns true if input is valid file content.
         */
        public boolean isValidFileContent(String context, byte[] input, int maxBytes, boolean allowNull) throws IntrusionException {
                try {
                        getValidFileContent( context, input, maxBytes, allowNull);
                        return true;
                } catch( Exception e ) {
                        return false;
                }
        }

/**
         * Returns validated file content as a byte array. Invalid input
         * will generate a descriptive ValidationException, and input that is clearly an attack
         * will generate a descriptive IntrusionException. 
         */
        public byte[] getValidFileContent(String context, byte[] input, int maxBytes, boolean allowNull) throws ValidationException, IntrusionException {
                if (isEmpty(input)) {
                        if (allowNull) return null;
                        throw new ValidationException( context + ": Input required", "Input required: context=" + context + ", input=" + input, context );
                }

                long esapiMaxBytes = ESAPI.securityConfiguration().getAllowedFileUploadSize();
                if (input.length > esapiMaxBytes ) throw new ValidationException( context + ": Invalid file content can not exceed " + esapiMaxBytes + " bytes", "Exceeded ESAPI max length", context );
                if (input.length > maxBytes ) throw new ValidationException( context + ": Invalid file content can not exceed " + maxBytes + " bytes", "Exceeded maxBytes ( " + input.length + ")", context );

                return input;
        }

 /**
         * Helper function to check if a byte array is empty
         * 
         * @param input string input value
         * @return boolean response if input is empty or not
         */
        private final boolean isEmpty(byte[] input) {
                return (input==null || input.length == 0);
        }

现在我认为你可以根据你的“要求”开发一些最好的消极和积极的测试场景。 是是您需要在属性文件中定义上载文件大小。 n INVALID文件是不通过上述参数测试的文件,而不是根据ESAPI的这种方法在任何其他用户定义的参数上传递。