Esempio n. 1
0
        public void Build_ForAllHeaders_WhenNotUsingNonce_HasPerRequestValuesReturnsTrue()
        {
            var builder = new CspBuilder();

            builder.AddDefaultSrc().Self().Blob().Data().From("http://testUrl.com");
            builder.AddConnectSrc().Self().Blob().Data().From("http://testUrl.com");
            builder.AddFontSrc().Self().Blob().Data().From("http://testUrl.com");
            builder.AddObjectSrc().Self().Blob().Data().From("http://testUrl.com");
            builder.AddFormAction().Self().Blob().Data().From("http://testUrl.com");
            builder.AddWorkerSrc().Self().Blob().Data().From("http://testUrl.com");
            builder.AddImgSrc().Self().Blob().Data().From("http://testUrl.com");
            builder.AddStyleSrc().Self().Blob().Data().From("http://testUrl.com");
            builder.AddMediaSrc().Self().Blob().Data().From("http://testUrl.com");
            builder.AddFrameAncestors().Self().Blob().Data().From("http://testUrl.com");
            builder.AddBaseUri().Self().Blob().Data().From("http://testUrl.com");
            builder.AddUpgradeInsecureRequests();
            builder.AddBlockAllMixedContent();

            // add nonce
            builder.AddScriptSrc().WithNonce();

            var result = builder.Build();

            result.HasPerRequestValues.Should().BeTrue();
        }
Esempio n. 2
0
        public void Build_WhenNoValues_ReturnsNull()
        {
            var builder = new CspBuilder();

            var result = builder.Build();

            result.Should().BeNullOrEmpty();
        }
Esempio n. 3
0
        public void Build_WhenNoValues_HasPerRequestValuesReturnsFalse()
        {
            var builder = new CspBuilder();

            var result = builder.Build();

            result.HasPerRequestValues.Should().BeFalse();
        }
Esempio n. 4
0
        public void Build_AddUpgradeInsecureRequests_AddsValue()
        {
            var builder = new CspBuilder()
                          .AddUpgradeInsecureRequests();

            var result = builder.Build();

            result.Should().Be("upgrade-insecure-requests");
        }
Esempio n. 5
0
        public void Build_AddBlockAllMixedContent_AddsValue()
        {
            var builder = new CspBuilder()
                          .AddBlockAllMixedContent();

            var result = builder.Build();

            result.Should().Be("block-all-mixed-content");
        }
Esempio n. 6
0
        public void Build_ReportUri_AddsValue()
        {
            var builder = new CspBuilder()
                          .AddReportUri()
                          .To("http://testUrl.com");

            var result = builder.Build();

            result.Should().Be("report-uri http://testUrl.com");
        }
Esempio n. 7
0
        public void Build_CustomDirective_AddsValues()
        {
            var builder = new CspBuilder();

            builder.AddCustomDirective("report-to");
            builder.AddCustomDirective("plugin-types", "application/x-shockwave-flash");

            var result = builder.Build();

            result.Should().Be("report-to; plugin-types application/x-shockwave-flash");
        }
Esempio n. 8
0
        public void Build_AddingTheSameDirectiveTwice_OverwritesThePreviousCopy()
        {
            var builder = new CspBuilder();

            builder.AddDefaultSrc().Self();
            builder.AddDefaultSrc().None();

            var result = builder.Build();

            result.Should().Be("default-src 'none'");
        }
Esempio n. 9
0
        /// <summary>
        /// Configure a content security policy
        /// </summary>
        /// <param name="configure">Configure the CSP</param>
        /// <param name="asReportOnly">If true, the header is added as report only</param>
        /// <returns>The configured <see cref="ContentSecurityPolicyHeader "/></returns>
        public static ContentSecurityPolicyHeader Build(Action <CspBuilder> configure, bool asReportOnly)
        {
            var builder = new CspBuilder();

            configure(builder);

            var cspResult = builder.Build();

            return(cspResult.HasPerRequestValues
                ? new ContentSecurityPolicyHeader(cspResult.Builder, asReportOnly)
                : new ContentSecurityPolicyHeader(cspResult.ConstantValue, asReportOnly));
        }
Esempio n. 10
0
        public void Build_AddFrameAncestors_WhenAddsMultipleValue_ReturnsAllValues()
        {
            var builder = new CspBuilder();

            builder.AddFrameAncestors()
            .Self()
            .Blob()
            .Data()
            .From("http://testUrl.com");

            var result = builder.Build();

            result.Should().Be("frame-ancestors 'self' blob: data: http://testUrl.com");
        }
Esempio n. 11
0
        public void Build_AddDefaultSrc_WhenAddsMultipleValueEnumerable_ReturnsAllValues()
        {
            var builder = new CspBuilder();

            builder.AddDefaultSrc()
            .Self()
            .Blob()
            .Data()
            .From(new [] { "http://testUrl.com", "http://testUrl2.com" });

            var result = builder.Build();

            result.ConstantValue.Should().Be("default-src 'self' blob: data: http://testUrl.com http://testUrl2.com");
        }
Esempio n. 12
0
        public void Build_AddBaseUri_WhenAddsMultipleValue_ReturnsAllValues()
        {
            var builder = new CspBuilder();

            builder.AddBaseUri()
            .Self()
            .Blob()
            .Data()
            .From("http://testUrl.com");

            var result = builder.Build();

            result.Should().Be("base-uri 'self' blob: data: http://testUrl.com");
        }
Esempio n. 13
0
        public void Build_AddFormAction_WhenAddsMultipleValue_ReturnsAllValues()
        {
            var builder = new CspBuilder();

            builder.AddFormAction()
            .Self()
            .Blob()
            .Data()
            .From("http://testUrl.com");

            var result = builder.Build();

            result.Should().Be("form-action 'self' blob: data: http://testUrl.com");
        }
Esempio n. 14
0
        public void Build_AddObjectSrc_WhenAddsMultipleValue_ReturnsAllValues()
        {
            var builder = new CspBuilder();

            builder.AddObjectSrc()
            .Self()
            .Blob()
            .Data()
            .From("http://testUrl.com");

            var result = builder.Build();

            result.Should().Be("object-src 'self' blob: data: http://testUrl.com");
        }
Esempio n. 15
0
        public void Build_AddStyleSrc_WhenAddsMultipleValue_ReturnsAllValues()
        {
            var builder = new CspBuilder();

            builder.AddStyleSrc()
            .Self()
            .ReportSample()
            .Blob()
            .Data()
            .From("http://testUrl.com");

            var result = builder.Build();

            result.ConstantValue.Should().Be("style-src 'self' 'report-sample' blob: data: http://testUrl.com");
        }
Esempio n. 16
0
        public void Build_AddDefaultSrc_WhenIncludesNone_OnlyWritesNone()
        {
            var builder = new CspBuilder();

            builder.AddDefaultSrc()
            .Self()
            .Blob()
            .Data()
            .From("http://testUrl.com")
            .None();

            var result = builder.Build();

            result.Should().Be("default-src 'none'");
        }
Esempio n. 17
0
        public void Build_AddSrciptSrc_WhenAddsInsecureValues_ReturnsAllValues()
        {
            var builder = new CspBuilder();

            builder.AddScriptSrc()
            .Self()
            .UnsafeEval()
            .UnsafeInline()
            .StrictDynamic()
            .ReportSample()
            .From("http://testUrl.com");

            var result = builder.Build();

            result.Should().Be("script-src 'self' 'unsafe-eval' 'unsafe-inline' 'strict-dynamic' 'report-sample' http://testUrl.com");
        }
Esempio n. 18
0
        public void Build_AddSrciptSrc_WhenAddsNonce_HasPerRequestValuesReturnsTrue()
        {
            var builder = new CspBuilder();

            builder.AddScriptSrc()
            .Self()
            .UnsafeEval()
            .UnsafeInline()
            .StrictDynamic()
            .ReportSample()
            .WithNonce()
            .From("http://testUrl.com");

            var result = builder.Build();

            result.HasPerRequestValues.Should().BeTrue();
        }
Esempio n. 19
0
        public void Builder_WhenUsingNonce_AddsNonceToCSP()
        {
            var builder = new CspBuilder();

            builder.AddScriptSrc().WithNonce();
            builder.AddStyleSrc().WithNonce();
            builder.AddCustomDirectiveBuilder("test-directive").WithNonce();

            var result = builder.Build();

            var httpContext = new DefaultHttpContext();
            var nonce       = "ABC123";

            httpContext.SetNonce(nonce);

            var csp = result.Builder(httpContext);

            csp.Should().Be($"script-src 'nonce-{nonce}'; style-src 'nonce-{nonce}'; test-directive 'nonce-{nonce}'");
        }
Esempio n. 20
0
        public void Build_AddSrciptSrc_WhenDoesntAddNonce_BuilderThrowsInvalidOperation()
        {
            var builder = new CspBuilder();

            builder.AddScriptSrc()
            .Self()
            .UnsafeEval()
            .UnsafeInline()
            .StrictDynamic()
            .ReportSample()
            .From("http://testUrl.com");

            var result = builder.Build();

            result.Invoking(x =>
            {
                var val = x.Builder;
            })
            .ShouldThrow <InvalidOperationException>();
        }
Esempio n. 21
0
        public void Build_ForAllHeaders_WhenNotUsingNonce_HasPerRequestValuesReturnsFalse()
        {
            var builder = new CspBuilder();

            builder.AddDefaultSrc().Self().Blob().Data().From("http://testUrl.com");
            builder.AddConnectSrc().Self().Blob().Data().From("http://testUrl.com");
            builder.AddFontSrc().Self().Blob().Data().From("http://testUrl.com");
            builder.AddObjectSrc().Self().Blob().Data().From("http://testUrl.com");
            builder.AddFormAction().Self().Blob().Data().From("http://testUrl.com");
            builder.AddImgSrc().Self().Blob().Data().From("http://testUrl.com");
            builder.AddScriptSrc().Self().UnsafeEval().UnsafeInline().StrictDynamic().ReportSample().From("http://testUrl.com");
            builder.AddStyleSrc().Self().Blob().Data().From("http://testUrl.com");
            builder.AddMediaSrc().Self().Blob().Data().From("http://testUrl.com");
            builder.AddFrameAncestors().Self().Blob().Data().From("http://testUrl.com");
            builder.AddBaseUri().Self().Blob().Data().From("http://testUrl.com");
            builder.AddUpgradeInsecureRequests();
            builder.AddBlockAllMixedContent();

            var result = builder.Build();

            result.HasPerRequestValues.Should().BeFalse();
        }