示例#1
0
        public static void DeleteUrl(UrlRule url, RequestFilteringSection section)
        {
            if (url == null)
            {
                return;
            }

            try {
                if (url.Allow)
                {
                    var target = section.AlwaysAllowedUrls.FirstOrDefault(u => u.Url.Equals(url.Url, StringComparison.OrdinalIgnoreCase));

                    if (target != null)
                    {
                        section.AlwaysAllowedUrls.Remove(target);
                    }
                }

                else
                {
                    var target = section.DenyUrlSequences.FirstOrDefault(u => u.Sequence.Equals(url.Url, StringComparison.OrdinalIgnoreCase));

                    if (target != null)
                    {
                        section.DenyUrlSequences.Remove(target);
                    }
                }
            }
            catch (FileLoadException e) {
                throw new LockedException(section.SectionPath, e);
            }
            catch (DirectoryNotFoundException e) {
                throw new ConfigScopeNotFoundException(e);
            }
        }
示例#2
0
        public static List <UrlRule> GetUrls(Site site, string path)
        {
            // Get request filtering section
            RequestFilteringSection requestFilteringSection = RequestFilteringHelper.GetRequestFilteringSection(site, path);


            // Consolidates the underlying allow query strings and deny query strings into a single collection
            List <UrlRule> urls = new List <UrlRule>();

            var allowedCollection = requestFilteringSection.AlwaysAllowedUrls;

            if (allowedCollection != null)
            {
                allowedCollection.ToList().ForEach(u => urls.Add(new UrlRule()
                {
                    Url   = u.Url.TrimStart(new char[] { '/' }),
                    Allow = true
                }));
            }

            var deniedCollection = requestFilteringSection.DenyUrlSequences;

            if (deniedCollection != null)
            {
                deniedCollection.ToList().ForEach(u => urls.Add(new UrlRule()
                {
                    Url   = u.Sequence,
                    Allow = false
                }));
            }

            return(urls);
        }
示例#3
0
        public static void AddHeaderLimit(HeaderLimit headerLimit, RequestFilteringSection section)
        {
            if (headerLimit == null)
            {
                throw new ArgumentNullException("headerLimit");
            }
            if (headerLimit.Header == null)
            {
                throw new ArgumentNullException("headerLimit.Header");
            }

            HeaderLimitCollection collection = section.RequestLimits.HeaderLimits;

            if (collection.Any(h => h.Header.Equals(headerLimit.Header)))
            {
                throw new AlreadyExistsException("headerLimit");
            }

            try {
                collection.Add(headerLimit);
            }
            catch (FileLoadException e) {
                throw new LockedException(section.SectionPath, e);
            }
            catch (DirectoryNotFoundException e) {
                throw new ConfigScopeNotFoundException(e);
            }
        }
示例#4
0
        public static UrlRule CreateUrl(dynamic model, RequestFilteringSection section)
        {
            if (model == null)
            {
                throw new ApiArgumentException("model");
            }

            string urlString = DynamicHelper.Value(model.url);

            if (string.IsNullOrEmpty(urlString))
            {
                throw new ApiArgumentException("url");
            }
            if (DynamicHelper.To <bool>(model.allow) == null)
            {
                throw new ApiArgumentException("allow");
            }
            bool allow = DynamicHelper.To <bool>(model.allow);

            return(new UrlRule()
            {
                Url = urlString,
                Allow = allow
            });
        }
示例#5
0
        public static void DeleteHeaderLimit(HeaderLimit headerLimit, RequestFilteringSection section)
        {
            if (headerLimit == null)
            {
                return;
            }

            HeaderLimitCollection collection = section.RequestLimits.HeaderLimits;

            // To utilize the remove functionality we must pull the element directly from the collection
            headerLimit = collection.FirstOrDefault(h => h.Header.Equals(headerLimit.Header));

            if (headerLimit != null)
            {
                try {
                    collection.Remove(headerLimit);
                }
                catch (FileLoadException e) {
                    throw new LockedException(section.SectionPath, e);
                }
                catch (DirectoryNotFoundException e) {
                    throw new ConfigScopeNotFoundException(e);
                }
            }
        }
示例#6
0
        public static void DeleteRule(Rule rule, RequestFilteringSection section)
        {
            if (rule == null)
            {
                return;
            }

            FilteringRuleCollection collection = section.FilteringRules;

            // To utilize the remove functionality we must pull the element directly from the collection
            rule = collection.FirstOrDefault(r => r.Name.Equals(rule.Name));

            if (rule != null)
            {
                try {
                    collection.Remove(rule);
                }
                catch (FileLoadException e) {
                    throw new LockedException(section.SectionPath, e);
                }
                catch (DirectoryNotFoundException e) {
                    throw new ConfigScopeNotFoundException(e);
                }
            }
        }
示例#7
0
        public static void AddRule(Rule rule, RequestFilteringSection section)
        {
            if (rule == null)
            {
                throw new ArgumentNullException("rule");
            }
            if (rule.Name == null)
            {
                throw new ArgumentNullException("rule.Name");
            }

            FilteringRuleCollection collection = section.FilteringRules;

            if (collection.Any(r => r.Name.Equals(rule.Name)))
            {
                throw new AlreadyExistsException("rule");
            }

            try {
                collection.Add(rule);
            }
            catch (FileLoadException e) {
                throw new LockedException(section.SectionPath, e);
            }
            catch (DirectoryNotFoundException e) {
                throw new ConfigScopeNotFoundException(e);
            }
        }
        public static void AddSegment(HiddenSegment segment, RequestFilteringSection section)
        {
            if (segment == null)
            {
                throw new ArgumentNullException("segment");
            }
            if (segment.Segment == null)
            {
                throw new ArgumentNullException("extension.Segment");
            }

            var collection = section.HiddenSegments;

            if (collection.Any(seg => seg.Segment.Equals(segment.Segment)))
            {
                throw new AlreadyExistsException("segment");
            }

            try {
                collection.Add(segment);
            }
            catch (FileLoadException e) {
                throw new LockedException(section.SectionPath, e);
            }
            catch (DirectoryNotFoundException e) {
                throw new ConfigScopeNotFoundException(e);
            }
        }
        public static void AddVerb(VerbElement verb, RequestFilteringSection section)
        {
            if (verb == null)
            {
                throw new ArgumentNullException("verb");
            }
            if (verb.Verb == null)
            {
                throw new ArgumentNullException("verb.Verb");
            }

            VerbCollection collection = section.Verbs;

            if (collection.Any(v => v.Verb.Equals(verb.Verb)))
            {
                throw new AlreadyExistsException("verb");
            }

            try {
                collection.Add(verb);
            }
            catch (FileLoadException e) {
                throw new LockedException(section.SectionPath, e);
            }
            catch (DirectoryNotFoundException e) {
                throw new ConfigScopeNotFoundException(e);
            }
        }
        public static void DeleteVerb(VerbElement verb, RequestFilteringSection section)
        {
            if (verb == null)
            {
                return;
            }

            VerbCollection collection = section.Verbs;

            // To utilize the remove functionality we must pull the element directly from the collection
            verb = collection.FirstOrDefault(v => v.Verb.Equals(verb.Verb));

            if (verb != null)
            {
                try {
                    collection.Remove(verb);
                }
                catch (FileLoadException e) {
                    throw new LockedException(section.SectionPath, e);
                }
                catch (DirectoryNotFoundException e) {
                    throw new ConfigScopeNotFoundException(e);
                }
            }
        }
示例#11
0
        public static List <HeaderLimit> GetHeaderLimits(Site site, string path, string configPath = null)
        {
            // Get request filtering section
            RequestFilteringSection requestFilteringSection = RequestFilteringHelper.GetRequestFilteringSection(site, path, configPath);

            var collection = requestFilteringSection.RequestLimits.HeaderLimits;

            if (collection != null)
            {
                return(collection.ToList());
            }
            return(new List <HeaderLimit>());
        }
        public static List <Extension> GetExtensions(Site site, string path, string configPath = null)
        {
            // Get request filtering section
            RequestFilteringSection requestFilteringSection = RequestFilteringHelper.GetRequestFilteringSection(site, path, configPath);

            var collection = requestFilteringSection.FileExtensions;

            if (collection != null)
            {
                return(collection.ToList());
            }
            return(new List <Extension>());
        }
        public static List <HiddenSegment> getSegments(Site site, string path)
        {
            // Get request filtering section
            RequestFilteringSection requestFilteringSection = RequestFilteringHelper.GetRequestFilteringSection(site, path);

            var collection = requestFilteringSection.HiddenSegments;

            if (collection != null)
            {
                return(collection.ToList());
            }
            return(new List <HiddenSegment>());
        }
        public static List <VerbElement> GetVerbs(Site site, string path, string configPath = null)
        {
            // Get request filtering section
            RequestFilteringSection requestFilteringSection = RequestFilteringHelper.GetRequestFilteringSection(site, path, configPath);

            var collection = requestFilteringSection.Verbs;

            if (collection != null)
            {
                return(collection.ToList());
            }
            return(new List <VerbElement>());
        }
        public object Post([FromBody] dynamic model)
        {
            Rule rule = null;
            Site site = null;
            RequestFilteringId reqId = null;

            if (model == null)
            {
                throw new ApiArgumentException("model");
            }
            // Rule must be created for a specific request filtering section
            if (model.request_filtering == null)
            {
                throw new ApiArgumentException("request_filtering");
            }
            if (!(model.request_filtering is JObject))
            {
                throw new ApiArgumentException(String.Empty, "request_filtering");
            }
            string reqUuid = DynamicHelper.Value(model.request_filtering.id);

            if (reqUuid == null)
            {
                throw new ApiArgumentException("request_filtering.id");
            }

            // Get the feature id
            reqId = new RequestFilteringId(reqUuid);

            // Get site the rule is for if applicable
            site = reqId.SiteId == null ? null : SiteHelper.GetSite(reqId.SiteId.Value);

            string configPath = ManagementUnit.ResolveConfigScope(model);
            RequestFilteringSection section = RequestFilteringHelper.GetRequestFilteringSection(site, reqId.Path, configPath);

            // Create filtering rule
            rule = RulesHelper.CreateRule(model, section);

            // Add it
            RulesHelper.AddRule(rule, section);

            // Save
            ManagementUnit.Current.Commit();

            //
            // Create response
            dynamic r = RulesHelper.ToJsonModel(rule, site, reqId.Path, null, true);

            return(Created(RulesHelper.GetLocation(r.id), r));
        }
        public static void DeleteQueryString(QueryStringRule queryString, RequestFilteringSection section)
        {
            if (string.IsNullOrEmpty(queryString.QueryString))
            {
                throw new ArgumentNullException("queryString.QueryString");
            }
            if (queryString == null)
            {
                return;
            }

            if (queryString.Allow)
            {
                var collection = section.AlwaysAllowedQueryStrings;

                var elem = collection.FirstOrDefault(s => s.QueryString.Equals(queryString.QueryString));
                if (elem != null)
                {
                    try {
                        collection.Remove(elem);
                    }
                    catch (FileLoadException e) {
                        throw new LockedException(section.SectionPath, e);
                    }
                    catch (DirectoryNotFoundException e) {
                        throw new ConfigScopeNotFoundException(e);
                    }
                }
            }
            else
            {
                var collection = section.DenyQueryStringSequences;

                var elem = collection.FirstOrDefault(s => s.Sequence.Equals(queryString.QueryString));
                if (elem != null)
                {
                    try {
                        collection.Remove(elem);
                    }
                    catch (FileLoadException e) {
                        throw new LockedException(section.SectionPath, e);
                    }
                    catch (DirectoryNotFoundException e) {
                        throw new ConfigScopeNotFoundException(e);
                    }
                }
            }
        }
示例#17
0
        public static void AddUrl(UrlRule url, RequestFilteringSection section)
        {
            if (url == null)
            {
                throw new ArgumentNullException("rule");
            }
            if (url.Url == null)
            {
                throw new ArgumentNullException("rule.Url");
            }

            try {
                if (url.Allow)
                {
                    var collection            = section.AlwaysAllowedUrls;
                    AlwaysAllowedUrl allowUrl = collection.CreateElement();

                    if (collection.Any(u => u.Url.Equals(url.Url)))
                    {
                        throw new AlreadyExistsException("url");
                    }

                    allowUrl.Url = url.Url;

                    collection.Add(allowUrl);
                }
                else
                {
                    var             collection   = section.DenyUrlSequences;
                    DenyUrlSequence denySequence = collection.CreateElement();

                    if (collection.Any(u => u.Sequence.Equals(url.Url)))
                    {
                        throw new AlreadyExistsException("url");
                    }

                    denySequence.Sequence = url.Url;

                    collection.Add(denySequence);
                }
            }
            catch (FileLoadException e) {
                throw new LockedException(section.SectionPath, e);
            }
            catch (DirectoryNotFoundException e) {
                throw new ConfigScopeNotFoundException(e);
            }
        }
示例#18
0
        public object Post([FromBody] dynamic model)
        {
            Extension extension = null;
            Site      site      = null;

            RequestFilteringId reqId = null;

            if (model == null)
            {
                throw new ApiArgumentException("model");
            }

            if (model.request_filtering == null)
            {
                throw new ApiArgumentException("request_filtering");
            }
            if (!(model.request_filtering is JObject))
            {
                throw new ApiArgumentException(String.Empty, "request_filtering");
            }
            string reqUuid = DynamicHelper.Value(model.request_filtering.id);

            if (reqUuid == null)
            {
                throw new ApiArgumentException("request_filtering.id");
            }

            // Get the feature id
            reqId = new RequestFilteringId(reqUuid);

            site = reqId.SiteId == null ? null : SiteHelper.GetSite(reqId.SiteId.Value);

            string configPath = ManagementUnit.ResolveConfigScope(model);
            RequestFilteringSection section = RequestFilteringHelper.GetRequestFilteringSection(site, reqId.Path, configPath);

            extension = ExtensionsHelper.CreateExtension(model, section);

            ExtensionsHelper.AddExtension(extension, section);

            ManagementUnit.Current.Commit();

            //
            // Create response
            dynamic ext = ExtensionsHelper.ToJsonModel(extension, site, reqId.Path);

            return(Created(ExtensionsHelper.GetLocation(ext.id), ext));
        }
示例#19
0
        public object Post([FromBody] dynamic model)
        {
            QueryStringRule    queryString = null;
            Site               site        = null;
            RequestFilteringId reqId       = null;

            if (model == null)
            {
                throw new ApiArgumentException("model");
            }
            if (model.request_filtering == null)
            {
                throw new ApiArgumentException("request_filtering");
            }
            if (!(model.request_filtering is JObject))
            {
                throw new ApiArgumentException("request_filtering");
            }
            string reqUuid = DynamicHelper.Value(model.request_filtering.id);

            if (reqUuid == null)
            {
                throw new ApiArgumentException("request_filtering.id");
            }

            reqId = new RequestFilteringId(reqUuid);

            site = reqId.SiteId == null ? null : SiteHelper.GetSite(reqId.SiteId.Value);

            string configPath = ManagementUnit.ResolveConfigScope(model);
            RequestFilteringSection section = RequestFilteringHelper.GetRequestFilteringSection(site, reqId.Path, configPath);

            queryString = QueryStringsHelper.CreateQueryString(model);

            QueryStringsHelper.AddQueryString(queryString, section);

            ManagementUnit.Current.Commit();

            //
            // Create response
            dynamic qs = QueryStringsHelper.ToJsonModel(queryString, site, reqId.Path);

            return(Created(QueryStringsHelper.GetLocation(qs.id), qs));
        }
示例#20
0
        public object Post([FromBody] dynamic model)
        {
            HiddenSegment      segment = null;
            Site               site    = null;
            RequestFilteringId reqId   = null;

            if (model == null)
            {
                throw new ApiArgumentException("model");
            }
            if (model.request_filtering == null)
            {
                throw new ApiArgumentException("request_filtering");
            }
            if (!(model.request_filtering is JObject))
            {
                throw new ApiArgumentException("request_filtering");
            }
            string reqUuid = DynamicHelper.Value(model.request_filtering.id);

            if (reqUuid == null)
            {
                throw new ApiArgumentException("request_filtering.id");
            }

            // Get the feature id
            reqId = new RequestFilteringId(reqUuid);

            site = reqId.SiteId == null ? null : SiteHelper.GetSite(reqId.SiteId.Value);

            string configPath = ManagementUnit.ResolveConfigScope(model);
            RequestFilteringSection section = RequestFilteringHelper.GetRequestFilteringSection(site, reqId.Path, configPath);

            segment = HiddenSegmentsHelper.CreateSegment(model, section);

            HiddenSegmentsHelper.AddSegment(segment, section);

            ManagementUnit.Current.Commit();

            dynamic hidden_segment = HiddenSegmentsHelper.ToJsonModel(segment, site, reqId.Path);

            return(Created(HiddenSegmentsHelper.GetLocation(hidden_segment.id), hidden_segment));
        }
        public object Post([FromBody] dynamic model)
        {
            HeaderLimit        headerLimit = null;
            Site               site        = null;
            RequestFilteringId reqId       = null;

            if (model == null)
            {
                throw new ApiArgumentException("model");
            }
            if (model.request_filtering == null)
            {
                throw new ApiArgumentException("request_filtering");
            }
            if (!(model.request_filtering is JObject))
            {
                throw new ApiArgumentException(String.Empty, "request_filtering");
            }
            string reqUuid = DynamicHelper.Value(model.request_filtering.id);

            if (reqUuid == null)
            {
                throw new ApiArgumentException("request_filtering.id");
            }

            reqId = new RequestFilteringId(reqUuid);

            site = reqId.SiteId == null ? null : SiteHelper.GetSite(reqId.SiteId.Value);

            string configPath = ManagementUnit.ResolveConfigScope(model);
            RequestFilteringSection section = RequestFilteringHelper.GetRequestFilteringSection(site, reqId.Path, configPath);

            headerLimit = HeaderLimitsHelper.CreateHeaderLimit(model, section);

            HeaderLimitsHelper.AddHeaderLimit(headerLimit, section);

            ManagementUnit.Current.Commit();

            dynamic header_limit = HeaderLimitsHelper.ToJsonModel(headerLimit, site, reqId.Path);

            return(Created(HeaderLimitsHelper.GetLocation(header_limit.id), header_limit));
        }
        public static HiddenSegment CreateSegment(dynamic model, RequestFilteringSection section)
        {
            if (model == null)
            {
                throw new ApiArgumentException("model");
            }

            string segmentName = DynamicHelper.Value(model.segment);

            if (string.IsNullOrEmpty(segmentName))
            {
                throw new ApiArgumentException("segment");
            }

            HiddenSegment segment = section.HiddenSegments.CreateElement();

            segment.Segment = segmentName;

            return(segment);
        }
示例#23
0
        public static Rule CreateRule(dynamic model, RequestFilteringSection section)
        {
            if (model == null)
            {
                throw new ApiArgumentException("model");
            }

            string name = DynamicHelper.Value(model.name);

            if (string.IsNullOrEmpty(name))
            {
                throw new ApiArgumentException("name");
            }

            Rule rule = section.FilteringRules.CreateElement();

            rule.Name = name;

            SetRule(rule, model);

            return(rule);
        }
示例#24
0
        public static HeaderLimit CreateHeaderLimit(dynamic model, RequestFilteringSection section)
        {
            if (model == null)
            {
                throw new ApiArgumentException("model");
            }

            string header = DynamicHelper.Value(model.header);

            if (string.IsNullOrEmpty(header))
            {
                throw new ApiArgumentException("header");
            }

            HeaderLimit headerLimit = section.RequestLimits.HeaderLimits.CreateElement();

            headerLimit.Header = header;

            UpdateHeaderLimit(headerLimit, model);

            return(headerLimit);
        }
示例#25
0
        public object Patch(string id, [FromBody] dynamic model)
        {
            RequestFilteringId reqId = new RequestFilteringId(id);

            Site site = reqId.SiteId == null ? null : SiteHelper.GetSite(reqId.SiteId.Value);

            if (reqId.SiteId != null && site == null)
            {
                Context.Response.StatusCode = (int)HttpStatusCode.NotFound;
                return(null);
            }

            // Check for config_scope
            string configPath = model == null ? null : ManagementUnit.ResolveConfigScope(model);
            RequestFilteringSection section = RequestFilteringHelper.GetRequestFilteringSection(site, reqId.Path, configPath);

            RequestFilteringHelper.UpdateFeatureSettings(model, section);

            ManagementUnit.Current.Commit();

            return(RequestFilteringHelper.ToJsonModel(site, reqId.Path));
        }
        public static Extension CreateExtension(dynamic model, RequestFilteringSection section)
        {
            if (model == null)
            {
                throw new ArgumentNullException("model");
            }

            string extension = DynamicHelper.Value(model.extension);

            if (string.IsNullOrEmpty(extension))
            {
                throw new ApiArgumentException("extension");
            }

            Extension ext = section.FileExtensions.CreateElement();

            ext.FileExtension = extension.StartsWith(".") ? extension : "." + extension;

            ext.Allowed = DynamicHelper.To <bool>(model.allow) ?? ext.Allowed;

            return(ext);
        }
        public static VerbElement CreateVerb(dynamic model, RequestFilteringSection section)
        {
            if (model == null)
            {
                throw new ApiArgumentException("model");
            }

            string verbString = DynamicHelper.Value(model.verb);

            if (string.IsNullOrEmpty(verbString))
            {
                throw new ApiArgumentException("verb");
            }

            VerbElement verb = section.Verbs.CreateElement();

            verb.Verb = verbString;

            UpdateVerb(verb, model);

            return(verb);
        }
        public static void AddQueryString(QueryStringRule queryString, RequestFilteringSection section)
        {
            if (queryString == null)
            {
                throw new ArgumentNullException("queryString");
            }
            if (string.IsNullOrEmpty(queryString.QueryString))
            {
                throw new ArgumentNullException("queryString.QueryString");
            }

            AlwaysAllowedQueryStringCollection allowCollection = section.AlwaysAllowedQueryStrings;
            DenyQueryStringSequenceCollection  denyCollection  = section.DenyQueryStringSequences;

            if (allowCollection.Any(s => s.QueryString.Equals(queryString.QueryString)) ||
                denyCollection.Any(s => s.Sequence.Equals(queryString.QueryString)))
            {
                throw new AlreadyExistsException("query_string");
            }

            try {
                if (queryString.Allow)
                {
                    allowCollection.Add(queryString.QueryString);
                }
                else
                {
                    denyCollection.Add(queryString.QueryString);
                }
            }
            catch (FileLoadException e) {
                throw new LockedException(section.SectionPath, e);
            }
            catch (DirectoryNotFoundException e) {
                throw new ConfigScopeNotFoundException(e);
            }
        }
        public static void DeleteSegment(HiddenSegment segment, RequestFilteringSection section)
        {
            if (segment == null)
            {
                return;
            }

            var collection = section.HiddenSegments;

            segment = collection.FirstOrDefault(s => s.Segment.Equals(segment.Segment));

            if (segment != null)
            {
                try {
                    collection.Remove(segment);
                }
                catch (FileLoadException e) {
                    throw new LockedException(section.SectionPath, e);
                }
                catch (DirectoryNotFoundException e) {
                    throw new ConfigScopeNotFoundException(e);
                }
            }
        }
        public static void DeleteExtension(Extension extension, RequestFilteringSection section)
        {
            if (extension == null)
            {
                return;
            }

            var collection = section.FileExtensions;

            extension = collection.FirstOrDefault(e => e.FileExtension.Equals(extension.FileExtension));

            if (extension != null)
            {
                try {
                    collection.Remove(extension);
                }
                catch (FileLoadException e) {
                    throw new LockedException(section.SectionPath, e);
                }
                catch (DirectoryNotFoundException e) {
                    throw new ConfigScopeNotFoundException(e);
                }
            }
        }