Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TDEAL-16: Security improvements #231

Merged
merged 93 commits into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
93 commits
Select commit Hold shift + click to select a range
dad5c9d
Add constants for content security policy directives.
sarahelsaig Dec 22, 2023
c761c22
Add middleware extension method and extension point for building Cont…
sarahelsaig Dec 22, 2023
02fb71b
Add documentation.
sarahelsaig Dec 22, 2023
93f32b6
Allow inline scripts and rename extension method.
sarahelsaig Dec 22, 2023
d47d807
Fix UseContentSecurityPolicyHeader for OC basic features.
sarahelsaig Dec 22, 2023
1226806
Add AntiClickjackingContentSecurityPolicyProvider.
sarahelsaig Dec 22, 2023
339741e
Secure anti forgery token.
sarahelsaig Dec 22, 2023
c3beb82
Change default SameSite attribute of SetCookieForever.
sarahelsaig Dec 22, 2023
e547d69
Also pass in HttpContext.
sarahelsaig Dec 22, 2023
9b14fdd
Config common directives.
sarahelsaig Dec 22, 2023
b3f9572
Add spelling word "clickjacking".
sarahelsaig Dec 26, 2023
2358851
Add missing newline.
sarahelsaig Dec 26, 2023
32ddf01
Fix CA1052.
sarahelsaig Dec 26, 2023
fcb78ee
Add form-action directive.
sarahelsaig Dec 26, 2023
d4cf8fe
Merge remote-tracking branch 'origin/dev' into issue/TDEAL-16
sarahelsaig Dec 26, 2023
deb91ba
Add ConfigureSessionCookieAlwaysSecure.
sarahelsaig Dec 26, 2023
4f33eb4
Add UseContentTypeOptionsHeader.
sarahelsaig Dec 26, 2023
4f8b5e6
Add defaults extension methods.
sarahelsaig Dec 28, 2023
4df453c
Fix usings.
sarahelsaig Dec 28, 2023
c87124e
Delete .github/actions/spelling/allow/security.txt
sarahelsaig Dec 28, 2023
d01ce29
Update Lombiq.HelpfulLibraries.AspNetCore/Security/ApplicationBuilder…
sarahelsaig Dec 28, 2023
9633f9a
Update Lombiq.HelpfulLibraries.AspNetCore/Security/ApplicationBuilder…
sarahelsaig Dec 28, 2023
cfba7e2
Fix ConfigureSecurityDefaults.
sarahelsaig Dec 29, 2023
be0e243
Merge branch 'issue/TDEAL-16' of https://github.com/Lombiq/Helpful-Li…
sarahelsaig Dec 29, 2023
826815d
Add more documentation for the default security extension methods.
sarahelsaig Dec 29, 2023
242f4b4
Add InlineStartup and change ConfigureSecurityDefaults to use it.
sarahelsaig Dec 29, 2023
02e3149
Rename UseContentTypeOptionsHeader to UseNosniffContentTypeOptionsHeader
sarahelsaig Dec 29, 2023
cf26ddd
Update xmldoc to make it clear that it's a quote.
sarahelsaig Dec 29, 2023
c9bc0ae
Update documentation.
sarahelsaig Dec 29, 2023
0c308db
Typo.
sarahelsaig Dec 29, 2023
539ac6e
Annoying SA1629 doesn't let me have a block of quote on its own
sarahelsaig Dec 29, 2023
3536a5c
Merge remote-tracking branch 'origin/issue/TDEAL-16' into issue/TDEAL-16
sarahelsaig Dec 29, 2023
4bf1cd2
Some code cleanup.
sarahelsaig Dec 29, 2023
f60e643
Add AddContentSecurityPolicyProvider
sarahelsaig Dec 29, 2023
7651b15
These may be amended during program setup.
sarahelsaig Dec 29, 2023
e9dd931
Merge the anti-clickjacking provider into the main UseContentSecurity…
sarahelsaig Dec 29, 2023
2627cb5
Permit fonts.gstatic.com
sarahelsaig Dec 29, 2023
d3b0e50
Update security documentation.
sarahelsaig Dec 29, 2023
945f7c2
Prevent adding the "Content-Security-Policy" header twice.
sarahelsaig Dec 29, 2023
fcf8959
Skip security middlewares during setup.
sarahelsaig Dec 29, 2023
9647be5
Control inline scripts and styles separately.
sarahelsaig Dec 29, 2023
a2a8651
Fix formatting.
sarahelsaig Dec 29, 2023
7f97b99
Adjust doc.
sarahelsaig Dec 29, 2023
cdae9cc
Merge remote-tracking branch 'origin/dev' into issue/TDEAL-16
sarahelsaig Dec 30, 2023
1586b19
Merge branch 'issue/TDEAL-16' of https://github.com/Lombiq/Helpful-Li…
sarahelsaig Dec 30, 2023
2491839
Update the header at the end of the pipeline.
sarahelsaig Jan 4, 2024
57f8ede
Add VueContentSecurityPolicyProvider
sarahelsaig Jan 5, 2024
2d20cee
Merge branch 'issue/TDEAL-16' of https://github.com/Lombiq/Helpful-Li…
sarahelsaig Jan 5, 2024
56245a4
Move it into the OnStarting event.
sarahelsaig Jan 5, 2024
7c1c76a
No need for isDeferred after all.
sarahelsaig Jan 5, 2024
2ae4224
unusing
sarahelsaig Jan 5, 2024
a205528
Update Lombiq.HelpfulLibraries.AspNetCore/Security/CdnContentSecurity…
sarahelsaig Jan 6, 2024
2e5b7f5
Use GetServices.
sarahelsaig Jan 6, 2024
0d37c79
comment
sarahelsaig Jan 6, 2024
5cf2a8d
Update Lombiq.HelpfulLibraries.AspNetCore/Security/CdnContentSecurity…
sarahelsaig Jan 6, 2024
45d3129
Code cleanup in CdnContentSecurityPolicyProvider.
sarahelsaig Jan 6, 2024
02ff2f5
Add font-src to CdnContentSecurityPolicyProvider.
sarahelsaig Jan 6, 2024
3cda6bc
Fall back if font-src is not defined
sarahelsaig Jan 6, 2024
5a8dca8
Use already existing constant for default tenant name.
sarahelsaig Jan 6, 2024
e5ed11d
Exclude CSP for error page and non-HTML.
sarahelsaig Jan 6, 2024
a0793b7
Fix content type check.
sarahelsaig Jan 7, 2024
4441bf5
Need CSP for 404 after all.
sarahelsaig Jan 7, 2024
32b529e
Remove stray exclamation point.
sarahelsaig Jan 7, 2024
42ffcb0
The other way around.
sarahelsaig Jan 7, 2024
204cc49
Use ConcurrentBag instead of ReadOnlyCollection so these are editable.
sarahelsaig Jan 8, 2024
a26713a
Update connect-src with all permitted sources.
sarahelsaig Jan 8, 2024
0b2c7d7
Add UseStrictAndSecureCookies
sarahelsaig Jan 9, 2024
fc1a38e
Fix errors.
sarahelsaig Jan 9, 2024
fb99d1e
Fix analyzer violations.
sarahelsaig Jan 9, 2024
889cb22
Add UseStrictAndSecureCookies to the security defaults.
sarahelsaig Jan 9, 2024
6a373ce
Don't insert duplicate CSP header.
sarahelsaig Jan 10, 2024
b1a6a07
Merge branch 'issue/TDEAL-16' of https://github.com/Lombiq/Helpful-Li…
sarahelsaig Jan 10, 2024
49940d6
Code cleanup.
sarahelsaig Jan 10, 2024
8c8a473
Additional remarks about HTTPS.
sarahelsaig Jan 10, 2024
2ee9107
Add attribute to mark unsafe-eval actions.
sarahelsaig Jan 10, 2024
71fe999
Code styling.
sarahelsaig Jan 10, 2024
8d6aee7
Add ContentSecurityPolicyAttribute.
sarahelsaig Jan 10, 2024
6774eb6
Add class doc and simplify pattern.
sarahelsaig Jan 10, 2024
0b6e366
Add ResourceManagerContentSecurityPolicyProvider.
sarahelsaig Jan 10, 2024
7d94e89
Code styling.
sarahelsaig Jan 10, 2024
591441c
ThenUpdateAsync.
sarahelsaig Jan 10, 2024
54b9d23
make it virtual
sarahelsaig Jan 10, 2024
6a42332
Better ThenUpdateAsync.
sarahelsaig Jan 10, 2024
c9c9e1c
Make DirectiveName protected.
sarahelsaig Jan 10, 2024
76162f4
Finish comment.
sarahelsaig Jan 12, 2024
4193052
Update Lombiq.HelpfulLibraries.AspNetCore/Security/ApplicationBuilder…
sarahelsaig Jan 12, 2024
f9993f5
Update Lombiq.HelpfulLibraries.OrchardCore/Security/OrchardCoreBuilde…
sarahelsaig Jan 12, 2024
29d34d8
Documentation cross-linking.
sarahelsaig Jan 12, 2024
520cd28
Document MergeWordSets.
sarahelsaig Jan 12, 2024
b68e2de
Code styling.
sarahelsaig Jan 12, 2024
fc86b3c
Add ConfigureSecurityDefaultsWithStaticFiles.
sarahelsaig Jan 12, 2024
764c93a
Update Lombiq.HelpfulLibraries.Common/Extensions/StringExtensions.cs
sarahelsaig Jan 12, 2024
dc9180c
Rephrase docstring.
sarahelsaig Jan 14, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Lombiq.HelpfulLibraries.AspNetCore/Docs/Security.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@
- `ServiceCollectionExtensions`: Extensions methods for `IServiceCollection`, e.g. `AddContentSecurityPolicyProvider()` is a shortcut to register `IContentSecurityPolicyProvider` in dependency injection.

There is a similar section for security extensions related to Orchard Core [here](../../Lombiq.HelpfulLibraries.OrchardCore/Docs/Security.md).

These extensions provide additional security and can resolve issues reported by the [ZAP security scanner](https://github.com/Lombiq/UI-Testing-Toolbox/blob/dev/Lombiq.Tests.UI/Docs/SecurityScanning.md).
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,10 @@ public static IApplicationBuilder UseContentSecurityPolicyHeader(
// No need to do content security policy on non-HTML responses.
if (context.Response.ContentType?.ContainsOrdinalIgnoreCase(MediaTypeNames.Text.Html) != true) return;

// The thought behind this provider model is that if you need something else than the default, you should
// add a provider that only applies the additional directive on screens where it's actually needed. This way
// we maintain minimal permissions. If you need additional
// The thought behind this provider model is that if you need something else than the default, you
// should add a provider that only applies the additional directive on screens where it's actually
// needed. This way we maintain minimal permissions. Also if you need additional permissions for a
// specific action you can use the [ContentSecurityPolicyAttribute(value, name, parentName)] attribute.
foreach (var provider in context.RequestServices.GetServices<IContentSecurityPolicyProvider>())
{
await provider.UpdateAsync(securityPolicies, context);
Expand All @@ -88,7 +89,7 @@ public static IApplicationBuilder UseContentSecurityPolicyHeader(
/// Internet Explorer and Chrome to perform MIME-sniffing on the response body, potentially causing the response
/// body to be interpreted and displayed as a content type other than the declared content type. Current (early
/// 2014) and legacy versions of Firefox will use the declared content type (if one is set), rather than performing
/// MIME-sniffing." As written in <a href="https://www.zaproxy.org/docs/alerts/10021/">the documentation</a>.
/// MIME-sniffing." As written in <see href="https://www.zaproxy.org/docs/alerts/10021/">the documentation</see>.
/// </para></remarks>
public static IApplicationBuilder UseNosniffContentTypeOptionsHeader(this IApplicationBuilder app) =>
app.Use(async (context, next) =>
Expand Down
9 changes: 9 additions & 0 deletions Lombiq.HelpfulLibraries.Common/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,15 @@ private static (string? Left, string? Separator, string? Right) Partition(
return (text![..index], text[index..end], text[end..]);
}

/// <summary>
/// Combines all provided parameters into a single string and eliminates duplicates. This can be used to get the
/// union of space separated word lists. For example it's used to build the values of individual directives in the
/// <c>Content-Security-Policy</c> HTTP header.
/// </summary>
/// <example>
/// Given the words "script-src 'self'" and otherWords containing "script-src example.com", the result would be
/// "script-src 'self' example.com".
/// </example>
public static string MergeWordSets(this string words, params string[] otherWords) =>
Piedone marked this conversation as resolved.
Show resolved Hide resolved
Piedone marked this conversation as resolved.
Show resolved Hide resolved
string.Join(
separator: ' ',
Expand Down
2 changes: 2 additions & 0 deletions Lombiq.HelpfulLibraries.OrchardCore/Docs/Security.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@
- `SecurityOrchardCoreBuilderExtensions`: Adds `BuilderExtensions` extensions. For example, the `ConfigureSecurityDefaults()` that provides some default security configuration for Orchard Core.
Piedone marked this conversation as resolved.
Show resolved Hide resolved

There is a similar section for security extensions related to ASP.NET Core [here](../../Lombiq.HelpfulLibraries.AspNetCore/Docs/Security.md).

These extensions provide additional security and can resolve issues reported by the [ZAP security scanner](https://github.com/Lombiq/UI-Testing-Toolbox/blob/dev/Lombiq.Tests.UI/Docs/SecurityScanning.md).
Piedone marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Microsoft.AspNetCore.Antiforgery;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.StaticFiles;
using OrchardCore.Environment.Shell;
using OrchardCore.Environment.Shell.Models;
using System.Linq;
Expand Down Expand Up @@ -61,7 +62,26 @@ public static OrchardCoreBuilder ConfigureAntiForgeryAlwaysSecure(this OrchardCo
public static OrchardCoreBuilder ConfigureSecurityDefaults(
Piedone marked this conversation as resolved.
Show resolved Hide resolved
this OrchardCoreBuilder builder,
bool allowInlineScript = true,
bool allowInlineStyle = false)
bool allowInlineStyle = false) =>
builder.ConfigureSecurityDefaultsInner(allowInlineScript, allowInlineStyle, useStaticFiles: false);

/// <summary>
/// The same as <see cref="ConfigureSecurityDefaults"/>, but also registers the <see cref="StaticFileMiddleware"/>
/// at the end of the chain. It's important to not do this earlier (e.g. with <c>app.UseStaticFiles()</c> because
/// it short-circuits the call chain when delivering static files so later middlewares are not executed and so the
/// <c>X-Content-Type-Options: nosniff</c> header doesn't get applied to those files.
Piedone marked this conversation as resolved.
Show resolved Hide resolved
/// </summary>
public static OrchardCoreBuilder ConfigureSecurityDefaultsWithStaticFiles(
this OrchardCoreBuilder builder,
bool allowInlineScript = true,
bool allowInlineStyle = false) =>
builder.ConfigureSecurityDefaultsInner(allowInlineScript, allowInlineStyle, useStaticFiles: true);

private static OrchardCoreBuilder ConfigureSecurityDefaultsInner(
this OrchardCoreBuilder builder,
bool allowInlineScript,
bool allowInlineStyle,
bool useStaticFiles)
{
builder.ApplicationServices.AddInlineStartup(
services => services
Expand All @@ -82,8 +102,11 @@ public static OrchardCoreBuilder ConfigureSecurityDefaults(
.UseContentSecurityPolicyHeader(allowInlineScript, allowInlineStyle)
.UseNosniffContentTypeOptionsHeader()
.UseStrictAndSecureCookies();

if (useStaticFiles) app.UseStaticFiles();
},
order: 99); // Makes this service load fairly late. This should make the setup detection more accurate.

return builder
.ConfigureAntiForgeryAlwaysSecure()
.AddTenantFeatures("OrchardCore.Diagnostics");
Expand Down