예제 #1
0
        /// <summary>
        /// Load encoder element
        /// </summary>
        /// <param name="encoderConfig"></param>
        /// <returns></returns>
        internal static IEncoder LoadEncoder(EncoderElement encoderConfig)
        {
            Debug.Assert(encoderConfig != null);

            // Create encoder
            IEncoder encoder = null;

            if (!string.IsNullOrEmpty(encoderConfig.Type))
            {
                encoder = ObjectBuilder.Build <IEncoder>(encoderConfig.Type);
            }
            else
            {
                // Create default encoder and load all local codec defs
                encoder = new Encoder();
                LoadCodecs(encoder, typeof(Encoder).Assembly, MatchHelper.WildcardToRegex(@"Owasp.Esapi.Codecs.*"));
            }

            CodecCollection codecs = encoderConfig.Codecs;

            // Load codec assemblies
            foreach (AddinAssemblyElement codecAssembly in codecs.Assemblies)
            {
                try {
                    Assembly assembly  = Assembly.Load(codecAssembly.Name);
                    Regex    typeMatch = MatchHelper.WildcardToRegex(codecAssembly.Types);

                    LoadCodecs(encoder, assembly, typeMatch);
                }
                catch (Exception exp) {
                    Esapi.Logger.Warning(LogLevels.WARN, "Failed to load codec assembly", exp);
                }
            }

            // Specific codecs
            foreach (CodecElement codecElement in codecs)
            {
                string failMessage = string.Format("Failed to load codec \"{0}\"", codecElement.Name);

                try {
                    ICodec codec = AddinBuilder <ICodec> .MakeInstance(codecElement);

                    encoder.AddCodec(codecElement.Name, codec);
                }
                catch (Exception exp) {
                    Esapi.Logger.Warning(LogLevels.WARN, failMessage, exp);
                }
            }

            return(encoder);
        }
예제 #2
0
        /// <summary>
        /// Load validator instance
        /// </summary>
        /// <param name="validatorConfig"></param>
        /// <returns></returns>
        internal static IValidator LoadValidator(ValidatorElement validatorConfig)
        {
            Debug.Assert(validatorConfig != null);

            IValidator validator = null;

            // Create custom
            if (!string.IsNullOrEmpty(validatorConfig.Type))
            {
                validator = ObjectBuilder.Build <IValidator>(validatorConfig.Type);
            }
            else
            {
                // Create default and load local rules
                validator = new Validator();
                LoadValidationRules(validator, typeof(Validator).Assembly, MatchHelper.WildcardToRegex(@"Owasp.Esapi.ValidationRules.*"));
            }

            ValidationRuleCollection rules = validatorConfig.Rules;

            // Add rule assemblies
            foreach (AddinAssemblyElement ruleAssembly in rules.Assemblies)
            {
                try {
                    Assembly assembly  = Assembly.Load(ruleAssembly.Name);
                    Regex    typeMatch = MatchHelper.WildcardToRegex(ruleAssembly.Types);

                    LoadValidationRules(validator, assembly, typeMatch);
                }
                catch (Exception exp) {
                    Esapi.Logger.Warning(LogLevels.WARN, "Failed to load validation rule assembly", exp);
                }
            }

            // Rules
            foreach (ValidationRuleElement ruleElement in rules)
            {
                string failMessage = string.Format("Failed to load validation rule \"{0}\"", ruleElement.Name);

                try {
                    IValidationRule rule = AddinBuilder <IValidationRule> .MakeInstance(ruleElement);

                    validator.AddRule(ruleElement.Name, rule);
                }
                catch (Exception exp) {
                    Esapi.Logger.Warning(LogLevels.WARN, failMessage, exp);
                }
            }

            return(validator);
        }