Skip to content

Commit b91b490

Browse files
author
MPCoreDeveloper
committed
initial start of the project
1 parent 13e8053 commit b91b490

File tree

6 files changed

+109
-10
lines changed

6 files changed

+109
-10
lines changed

Posseth.SafeWebCore.sln

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,30 @@ Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio Version 17
44
VisualStudioVersion = 17.10.35013.160
55
MinimumVisualStudioVersion = 10.0.40219.1
6-
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{FB660E3C-6584-455E-8E5E-0BBBCFEB5CF8}"
6+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{CE342AB5-E63F-48AE-BDCC-5E21157E4EA9}"
77
ProjectSection(SolutionItems) = preProject
8-
src\Posseth.SafeWebCore\Posseth.SafeWebCore.csproj = src\Posseth.SafeWebCore\Posseth.SafeWebCore.csproj
8+
.gitattributes = .gitattributes
9+
.gitignore = .gitignore
10+
LICENSE.txt = LICENSE.txt
911
EndProjectSection
1012
EndProject
13+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Posseth.SafeWebCore", "src\Posseth.SafeWebCore\Posseth.SafeWebCore.csproj", "{3CC906DB-9397-47D8-BE88-48C89A1C841D}"
14+
EndProject
1115
Global
16+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
17+
Debug|Any CPU = Debug|Any CPU
18+
Release|Any CPU = Release|Any CPU
19+
EndGlobalSection
20+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
21+
{3CC906DB-9397-47D8-BE88-48C89A1C841D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
22+
{3CC906DB-9397-47D8-BE88-48C89A1C841D}.Debug|Any CPU.Build.0 = Debug|Any CPU
23+
{3CC906DB-9397-47D8-BE88-48C89A1C841D}.Release|Any CPU.ActiveCfg = Release|Any CPU
24+
{3CC906DB-9397-47D8-BE88-48C89A1C841D}.Release|Any CPU.Build.0 = Release|Any CPU
25+
EndGlobalSection
1226
GlobalSection(SolutionProperties) = preSolution
1327
HideSolutionNode = FALSE
1428
EndGlobalSection
1529
GlobalSection(ExtensibilityGlobals) = postSolution
16-
SolutionGuid = {8358C33E-91D1-4B06-A8B1-E417C80FCADE}
30+
SolutionGuid = {F9EDD709-73CF-4384-B3E6-8B70A701B402}
1731
EndGlobalSection
1832
EndGlobal

src/Posseth.SafeWebCore/Class1.cs

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Michel Posseth 2024/06/22 YYYY/MM/DD
2+
using System.Security.Cryptography;
3+
namespace Posseth.SafeWebCore
4+
{
5+
public class CspBuilder
6+
{
7+
private readonly Dictionary<string, List<string>> _directives = [];
8+
public string Nonce { get; init; } = GetNonce();
9+
private static string GetNonce()
10+
{
11+
using var rng = RandomNumberGenerator.Create();
12+
var nonceBytes = new byte[16]; // 128 bits
13+
rng.GetBytes(nonceBytes);
14+
return Convert.ToBase64String(nonceBytes);
15+
}
16+
public CspBuilder AddDirective(string directive, string policy)
17+
{
18+
if (!_directives.TryGetValue(directive, out List<string>? value))
19+
{
20+
value = [];
21+
_directives[directive] = value;
22+
}
23+
24+
if (policy.Contains("{nonce}"))
25+
{
26+
policy = policy.Replace("{nonce}", $"'nonce-{Nonce}'");
27+
}
28+
29+
value.Add(policy);
30+
return this;
31+
}
32+
public override string ToString()
33+
{
34+
return string.Join("; ", _directives.Select(d => $"{d.Key} {string.Join(" ", d.Value)}").ToArray());
35+
}
36+
}
37+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Michel Posseth 2024/06/22 YYYY/MM/DD
2+
using Microsoft.AspNetCore.Http;
3+
namespace Posseth.SafeWebCore
4+
{
5+
public class CspMiddleware(RequestDelegate next, CspBuilder builder)
6+
{
7+
private readonly RequestDelegate _next = next;
8+
private readonly string _policy = builder.ToString();
9+
10+
public async Task InvokeAsync(HttpContext context)
11+
{
12+
context.Response.Headers.Add("Content-Security-Policy", _policy);
13+
await _next(context);
14+
}
15+
}
16+
}
17+
18+
// Extension method for adding the middleware
19+
20+
//var builder = WebApplication.CreateBuilder(args);
21+
//var app = builder.Build();
22+
23+
//app.UseCsp(csp =>
24+
//{
25+
// csp.AddDirective("default-src", "'self'")
26+
// .AddDirective("script-src", "'self' https://trustedscripts.example.com");
27+
// // Add more directives as needed
28+
//});
29+
30+
//app.MapGet("/", () => "Hello World!");
31+
32+
//app.Run();
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Michel Posseth 2024/06/22 YYYY/MM/DD
2+
using System;
3+
using Microsoft.AspNetCore.Builder;
4+
namespace Posseth.SafeWebCore
5+
{
6+
public static class CspMiddlewareExtensions
7+
{
8+
public static IApplicationBuilder UseCsp(this IApplicationBuilder builder, Action<CspBuilder> configure)
9+
{
10+
var cspBuilder = new CspBuilder();
11+
configure(cspBuilder);
12+
return builder.UseMiddleware<CspMiddleware>(cspBuilder);
13+
}
14+
}
15+
}

src/Posseth.SafeWebCore/Posseth.SafeWebCore.csproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,12 @@
66
<Nullable>enable</Nullable>
77
</PropertyGroup>
88

9+
<ItemGroup>
10+
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.2.0" />
11+
</ItemGroup>
12+
13+
<ItemGroup>
14+
<Folder Include="Extensions\" />
15+
</ItemGroup>
16+
917
</Project>

0 commit comments

Comments
 (0)