// -------------------------------------------------------------------------------------------------------------------- // // Copyright (c) James South. // Licensed under the Apache License, Version 2.0. // // // Represents an image security section within a configuration file. // // -------------------------------------------------------------------------------------------------------------------- namespace ImageProcessor.Web.Configuration { #region Using using System; using System.Configuration; using System.IO; using System.Xml; using ImageProcessor.Web.Helpers; #endregion /// /// Represents an image security section within a configuration file. /// public sealed class ImageSecuritySection : ConfigurationSection { #region Properties /// /// Gets or sets a value indicating whether the current application is allowed download remote files. /// /// if the current application is allowed download remote files; otherwise, . [ConfigurationProperty("allowRemoteDownloads", DefaultValue = false, IsRequired = true)] public bool AllowRemoteDownloads { get { return (bool)this["allowRemoteDownloads"]; } set { this["allowRemoteDownloads"] = value; } } /// /// Gets or sets the maximum allowed remote file timeout in milliseconds for the application. /// /// The maximum number of days to store an image in the cache. /// Defaults to 30000 (30 seconds) if not set. [ConfigurationProperty("timeout", DefaultValue = "300000", IsRequired = true)] public int Timeout { get { return (int)this["timeout"]; } set { this["timeout"] = value; } } /// /// Gets or sets the maximum allowed remote file size in bytes for the application. /// /// The maximum number of days to store an image in the cache. /// Defaults to 4194304 (4Mb) if not set. [ConfigurationProperty("maxBytes", DefaultValue = "4194304", IsRequired = true)] public int MaxBytes { get { return (int)this["maxBytes"]; } set { this["maxBytes"] = value; } } /// /// Gets or sets the prefix for remote files for the application. /// /// The prefix for remote files for the application. [ConfigurationProperty("remotePrefix", DefaultValue = "", IsRequired = true)] public string RemotePrefix { get { return (string)this["remotePrefix"]; } set { this["remotePrefix"] = value; } } /// /// Gets the /// /// The [ConfigurationProperty("whiteList", IsRequired = true)] public WhiteListElementCollection WhiteList { get { object o = this["whiteList"]; return o as WhiteListElementCollection; } } #endregion #region Methods /// /// Retrieves the security configuration section from the current application configuration. /// /// The cache configuration section from the current application configuration. public static ImageSecuritySection GetConfiguration() { ImageSecuritySection imageSecuritySection = ConfigurationManager.GetSection("imageProcessor/security") as ImageSecuritySection; if (imageSecuritySection != null) { return imageSecuritySection; } string section = ResourceHelpers.ResourceAsString("ImageProcessor.Web.Configuration.Resources.security.config"); XmlReader reader = new XmlTextReader(new StringReader(section)); imageSecuritySection = new ImageSecuritySection(); imageSecuritySection.DeserializeSection(reader); return imageSecuritySection; } #endregion /// /// Represents a whitelist collection configuration element within the configuration. /// public class WhiteListElementCollection : ConfigurationElementCollection { /// /// Gets or sets the whitelist item at the given index. /// /// The index of the whitelist item to get. /// The whitelist item at the given index. public SafeUrl this[int index] { get { return this.BaseGet(index) as SafeUrl; } set { if (this.BaseGet(index) != null) { this.BaseRemoveAt(index); } this.BaseAdd(index, value); } } /// /// Creates a new SafeURL configuration element. /// /// /// A new SafeURL configuration element. /// protected override ConfigurationElement CreateNewElement() { return new SafeUrl(); } /// /// Gets the element key for a specified whitelist configuration element. /// /// The ConfigurationElement to return the key for. /// The element key for a specified whitelist configuration element. protected override object GetElementKey(ConfigurationElement element) { return ((SafeUrl)element).Url; } } /// /// Represents a whitelist configuration element within the configuration. /// public class SafeUrl : ConfigurationElement { /// /// Gets or sets the url of the white listed file. /// /// The url of the white listed file. [ConfigurationProperty("url", DefaultValue = "", IsRequired = true)] public Uri Url { get { return (Uri)this["url"]; } set { this["url"] = value; } } /// /// Gets or sets a value indicating whether the white listed url is extension-less. /// [ConfigurationProperty("extensionLess", DefaultValue = false, IsRequired = false)] public bool ExtensionLess { get { return (bool)this["extensionLess"]; } set { this["extensionLess"] = value; } } /// /// Gets or sets the image format for the extension-less url. /// [ConfigurationProperty("imageFormat", DefaultValue = "", IsRequired = false)] public string ImageFormat { get { return (string)this["imageFormat"]; } set { this["imageFormat"] = value; } } } } }