Skip to content

Commit

Permalink
Feature/adding integration tests (#144)
Browse files Browse the repository at this point in the history
* Extracted all common header value tests to a single Integration test for the default middleware setup

* Entire codebase now uses file-scoped namespaces

* Ran dotnet-format on codebase

* Minor version bump
  • Loading branch information
jamie-taylor-rjj authored Dec 4, 2024
1 parent 5603757 commit f3ef6d1
Show file tree
Hide file tree
Showing 50 changed files with 2,350 additions and 2,459 deletions.
31 changes: 15 additions & 16 deletions src/Constants.cs
Original file line number Diff line number Diff line change
@@ -1,29 +1,28 @@
namespace OwaspHeaders.Core
namespace OwaspHeaders.Core;

public static class Constants
{
public static class Constants
{
public const string StrictTransportSecurityHeaderName = "Strict-Transport-Security";
public const string StrictTransportSecurityHeaderName = "Strict-Transport-Security";

public const string XFrameOptionsHeaderName = "X-Frame-Options";
public const string XFrameOptionsHeaderName = "X-Frame-Options";

public const string XssProtectionHeaderName = "X-XSS-Protection";
public const string XssProtectionHeaderName = "X-XSS-Protection";

public const string XContentTypeOptionsHeaderName = "X-Content-Type-Options";
public const string XContentTypeOptionsHeaderName = "X-Content-Type-Options";

public const string ContentSecurityPolicyHeaderName = "Content-Security-Policy";
public const string ContentSecurityPolicyHeaderName = "Content-Security-Policy";

public const string ContentSecurityPolicyReportOnlyHeaderName = "Content-Security-Policy-Report-Only";
public const string ContentSecurityPolicyReportOnlyHeaderName = "Content-Security-Policy-Report-Only";

public const string XContentSecurityPolicyHeaderName = "X-Content-Security-Policy";
public const string XContentSecurityPolicyHeaderName = "X-Content-Security-Policy";

public const string PermittedCrossDomainPoliciesHeaderName = "X-Permitted-Cross-Domain-Policies";
public const string PermittedCrossDomainPoliciesHeaderName = "X-Permitted-Cross-Domain-Policies";

public const string ReferrerPolicyHeaderName = "Referrer-Policy";
public const string ReferrerPolicyHeaderName = "Referrer-Policy";

public const string CacheControlHeaderName = "Cache-Control";
public const string CacheControlHeaderName = "Cache-Control";

public const string ExpectCtHeaderName = "Expect-CT";
public const string ExpectCtHeaderName = "Expect-CT";

public const string CrossOriginResourcePolicyHeaderName = "Cross-Origin-Resource-Policy";
}
public const string CrossOriginResourcePolicyHeaderName = "Cross-Origin-Resource-Policy";
}
11 changes: 5 additions & 6 deletions src/Enums/CspCommandType.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
namespace OwaspHeaders.Core.Enums
namespace OwaspHeaders.Core.Enums;

public enum CspCommandType
{
public enum CspCommandType
{
Directive,
Uri
}
Directive,
Uri
}
37 changes: 18 additions & 19 deletions src/Enums/CspSandboxType.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
namespace OwaspHeaders.Core.Enums
namespace OwaspHeaders.Core.Enums;

/// <remarks>Please note: these enum values are named after the CSP Sandbox Types
/// exactly. This is so that we can use the value as a string, without having to
/// do any C# string magic (and waste cycles doing so) to get the right names.
/// This does mean that Rider (et al.) will tell you that the naming convention
/// here is non-standard.</remarks>
public enum CspSandboxType
{
/// <remarks>Please note: these enum values are named after the CSP Sandbox Types
/// exactly. This is so that we can use the value as a string, without having to
/// do any C# string magic (and waste cycles doing so) to get the right names.
/// This does mean that Rider (et al.) will tell you that the naming convention
/// here is non-standard.</remarks>
public enum CspSandboxType
{
allowForms,
allowModals,
allowOrientationLock,
allowPointerLock,
allowPopups,
allowPopupsToEscapeSandbox,
allowPresentation,
allowSameOrigin,
allowScripts,
allowTopNavigation
}
allowForms,
allowModals,
allowOrientationLock,
allowPointerLock,
allowPopups,
allowPopupsToEscapeSandbox,
allowPresentation,
allowSameOrigin,
allowScripts,
allowTopNavigation
}
35 changes: 17 additions & 18 deletions src/Enums/CspUriType.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
namespace OwaspHeaders.Core.Enums
namespace OwaspHeaders.Core.Enums;

public enum CspUriType
{
public enum CspUriType
{
Base,
DefaultUri,
Script,
Object,
Style,
Img,
Media,
Frame,
Child,
FrameAncestors,
Font,
Connect,
Manifest,
Form
}
Base,
DefaultUri,
Script,
Object,
Style,
Img,
Media,
Frame,
Child,
FrameAncestors,
Font,
Connect,
Manifest,
Form
}
35 changes: 17 additions & 18 deletions src/Enums/ReferrerPolicyOptions.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
namespace OwaspHeaders.Core.Enums
namespace OwaspHeaders.Core.Enums;

/// <remarks>Please note: these enum values are named after Referrer Policy Options
/// exactly. This is so that we can use the value as a string, without having to
/// do any C# string magic (and waste cycles doing so) to get the right names.
/// This does mean that Rider (et al.) will tell you that the naming convention
/// here is non-standard.</remarks>
public enum ReferrerPolicyOptions
{
/// <remarks>Please note: these enum values are named after Referrer Policy Options
/// exactly. This is so that we can use the value as a string, without having to
/// do any C# string magic (and waste cycles doing so) to get the right names.
/// This does mean that Rider (et al.) will tell you that the naming convention
/// here is non-standard.</remarks>
public enum ReferrerPolicyOptions
{
noReferrer,
noReferrerWhenDowngrade,
origin,
originWhenCrossOrigin,
sameOrigin,
strictOrigin,
strictWhenCrossOrigin,
unsafeUrl
};
}
noReferrer,
noReferrerWhenDowngrade,
origin,
originWhenCrossOrigin,
sameOrigin,
strictOrigin,
strictWhenCrossOrigin,
unsafeUrl
};
29 changes: 14 additions & 15 deletions src/Enums/XFrameOptions.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
namespace OwaspHeaders.Core.Enums
namespace OwaspHeaders.Core.Enums;

/// <remarks>Please note: these enum values are named after the X-Frame-Options
/// values exactly. This is so that we can use the value as a string, without
/// having to do any C# string magic (and waste cycles doing so) to get the right
/// names.
/// This does mean that Rider (et al.) will tell you that the naming convention
/// here is non-standard.</remarks>
public enum XFrameOptions
{
/// <remarks>Please note: these enum values are named after the X-Frame-Options
/// values exactly. This is so that we can use the value as a string, without
/// having to do any C# string magic (and waste cycles doing so) to get the right
/// names.
/// This does mean that Rider (et al.) will tell you that the naming convention
/// here is non-standard.</remarks>
public enum XFrameOptions
{
Deny,
Sameorigin,
Allowfrom,
AllowAll
};
}
Deny,
Sameorigin,
Allowfrom,
AllowAll
};
31 changes: 15 additions & 16 deletions src/Enums/XPermittedCrossDomainOptionValue.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
namespace OwaspHeaders.Core.Enums
namespace OwaspHeaders.Core.Enums;

/// <remarks>
/// Please note: these enum values are named after the X-Permitted-Cross-Domain-Options
/// values exactly. This is so that we can use the value as a string, without having to
/// do any C# string magic (and waste cycles doing so) to get the right names.
/// This does mean that Rider (et al.) will tell you that the naming convention
/// here is non-standard.</remarks>
public enum XPermittedCrossDomainOptionValue
{
/// <remarks>
/// Please note: these enum values are named after the X-Permitted-Cross-Domain-Options
/// values exactly. This is so that we can use the value as a string, without having to
/// do any C# string magic (and waste cycles doing so) to get the right names.
/// This does mean that Rider (et al.) will tell you that the naming convention
/// here is non-standard.</remarks>
public enum XPermittedCrossDomainOptionValue
{
none,
masterOnly,
byContentType,
byFtpFileType,
all
};
}
none,
masterOnly,
byContentType,
byFtpFileType,
all
};
53 changes: 26 additions & 27 deletions src/Extensions/ContentSecurityPolicyExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,36 +1,35 @@
namespace OwaspHeaders.Core.Extensions
namespace OwaspHeaders.Core.Extensions;

public static class ContentSecurityPolicyExtensions
{
public static class ContentSecurityPolicyExtensions
/// <summary>
/// Used to set the Content Security Policy URIs for a given <see cref="CspUriType"/>
/// </summary>
public static SecureHeadersMiddlewareConfiguration SetCspUris(
this SecureHeadersMiddlewareConfiguration config,
List<ContentSecurityPolicyElement> baseUri,
CspUriType cspUriType)
{
/// <summary>
/// Used to set the Content Security Policy URIs for a given <see cref="CspUriType"/>
/// </summary>
public static SecureHeadersMiddlewareConfiguration SetCspUris(
this SecureHeadersMiddlewareConfiguration config,
List<ContentSecurityPolicyElement> baseUri,
CspUriType cspUriType)
if (config.UseContentSecurityPolicy)
{
if (config.UseContentSecurityPolicy)
{
config.ContentSecurityPolicyConfiguration?.SetCspUri(baseUri, cspUriType);
}

return config;
config.ContentSecurityPolicyConfiguration?.SetCspUri(baseUri, cspUriType);
}

/// <summary>
/// Used to set up the Content Security Policy Sandbox for a given or multiple
/// <see cref="CspSandboxType"/>s
/// </summary>
public static SecureHeadersMiddlewareConfiguration SetCspSandBox
(this SecureHeadersMiddlewareConfiguration config, params CspSandboxType[] sandboxType)
{
if (config.UseContentSecurityPolicy)
{
config.ContentSecurityPolicyConfiguration?.SetSandbox(sandboxType);
}
return config;
}

return config;
/// <summary>
/// Used to set up the Content Security Policy Sandbox for a given or multiple
/// <see cref="CspSandboxType"/>s
/// </summary>
public static SecureHeadersMiddlewareConfiguration SetCspSandBox
(this SecureHeadersMiddlewareConfiguration config, params CspSandboxType[] sandboxType)
{
if (config.UseContentSecurityPolicy)
{
config.ContentSecurityPolicyConfiguration?.SetSandbox(sandboxType);
}

return config;
}
}
Loading

0 comments on commit f3ef6d1

Please sign in to comment.