Esempio n. 1
0
        /// <summary>
        ///     Sets custom sources for the CSP directive.
        /// </summary>
        /// <typeparam name="T">The type of the CSP directive configuration object.</typeparam>
        /// <param name="directive">The CSP directive configuration object.</param>
        /// <param name="sources">One or more custom sources.</param>
        /// <returns>The CSP directive configuration object.</returns>
        public static T CustomSources <T>(this T directive, params string[] sources) where T : class, ICspDirectiveBasicConfiguration
        {
            if (directive == null)
            {
                throw new ArgumentNullException(nameof(directive));
            }
            if (sources.Length == 0)
            {
                throw new ArgumentException("You must supply at least one source.", nameof(sources));
            }

            try
            {
                var type         = typeof(T);
                var enableHashes = type == typeof(ICspDirectiveConfiguration) || type == typeof(ICspDirectiveUnsafeInlineConfiguration);
                directive.CustomSources = sources
                                          .Select(s => (enableHashes ? CspHashSource.Parse(s) : null) ?? CspUriSource.Parse(s).ToString())
                                          .ToArray();
            }
            catch (InvalidCspSourceException e)
            {
                throw new ArgumentException("Invalid source. Details: " + e.Message, nameof(sources), e);
            }

            return(directive);
        }
Esempio n. 2
0
        public void Parse_ValidHashSource_ReturnsSource(string validHashSource)
        {
            var expectedSource = $"'{validHashSource}'";

            var result = CspHashSource.Parse(validHashSource);

            Assert.Equal(expectedSource, result);
        }
        public override void Validate(object value)
        {
            var source = (string)value;

            if (String.IsNullOrEmpty(source))
            {
                return;
            }

            try
            {
                if (_expectHashSources && CspHashSource.Parse(source) != null)
                {
                    return;
                }
                CspUriSource.Parse(source);
            }
            catch (Exception e)
            {
                throw new ConfigurationErrorsException("Invalid source " + source + ". Details: " + e.Message, e);
            }
        }
Esempio n. 4
0
        public void Parse_InValidBase64_ReturnsNull(string validHashSource)
        {
            var result = CspHashSource.Parse(validHashSource);

            Assert.Null(result);
        }