Skip to content

Commit f446f9f

Browse files
authored
Merge pull request #95 from PandaTechAM/development
Cors header fix
2 parents 7dd34e6 + 42121f0 commit f446f9f

File tree

2 files changed

+39
-57
lines changed

2 files changed

+39
-57
lines changed

src/SharedKernel/Extensions/CorsExtensions.cs

Lines changed: 36 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ namespace SharedKernel.Extensions;
77

88
public static class CorsExtensions
99
{
10+
private static readonly string[] ExposedHeaders = ["Content-Disposition"];
11+
1012
public static WebApplicationBuilder AddCors(this WebApplicationBuilder builder)
1113
{
1214
if (builder.Environment.IsProduction())
@@ -21,7 +23,8 @@ public static WebApplicationBuilder AddCors(this WebApplicationBuilder builder)
2123
.WithOrigins(allowedOrigins)
2224
.AllowCredentials()
2325
.AllowAnyMethod()
24-
.AllowAnyHeader()));
26+
.AllowAnyHeader()
27+
.WithExposedHeaders(ExposedHeaders)));
2528
}
2629
else
2730
{
@@ -30,7 +33,8 @@ public static WebApplicationBuilder AddCors(this WebApplicationBuilder builder)
3033
.SetIsOriginAllowed(_ => true)
3134
.AllowCredentials()
3235
.AllowAnyMethod()
33-
.AllowAnyHeader()));
36+
.AllowAnyHeader()
37+
.WithExposedHeaders(ExposedHeaders)));
3438
}
3539

3640
return builder;
@@ -45,28 +49,21 @@ public static WebApplication UseCors(this WebApplication app)
4549
private static string[] SplitOrigins(this string input)
4650
{
4751
if (string.IsNullOrWhiteSpace(input))
48-
{
49-
throw new ArgumentException("Cors Origins cannot be null or empty.");
50-
}
51-
52-
var result = input.Split([';', ','], StringSplitOptions.RemoveEmptyEntries);
53-
54-
for (var i = 0; i < result.Length; i++)
55-
{
56-
result[i] = result[i]
57-
.Trim();
58-
59-
if (ValidationHelper.IsUri(result[i], false))
60-
{
61-
continue;
62-
}
63-
64-
Console.WriteLine($"Removed invalid cors origin: {result[i]}");
65-
result[i] = string.Empty;
66-
}
67-
68-
return result.Where(x => !string.IsNullOrEmpty(x))
69-
.ToArray();
52+
throw new ArgumentException("CORS origins cannot be null or empty.", nameof(input));
53+
54+
return input
55+
.Split([';', ','], StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)
56+
.Where(origin =>
57+
{
58+
if (ValidationHelper.IsUri(origin, false))
59+
{
60+
return true;
61+
}
62+
63+
Console.WriteLine($"Removed invalid CORS origin: {origin}");
64+
return false;
65+
})
66+
.ToArray();
7067
}
7168

7269
private static string[] EnsureWwwAndNonWwwVersions(this string[] uris)
@@ -75,39 +72,24 @@ private static string[] EnsureWwwAndNonWwwVersions(this string[] uris)
7572

7673
foreach (var uri in uris)
7774
{
78-
if (!Uri.TryCreate(uri, UriKind.Absolute, out var parsedUri))
79-
{
80-
continue;
81-
}
82-
83-
var uriString = parsedUri.ToString()
84-
.TrimEnd('/');
85-
86-
result.Add(uriString);
87-
75+
if (!Uri.TryCreate(uri, UriKind.Absolute, out var parsed)) continue;
8876

89-
var hostWithoutWww = parsedUri.Host.StartsWith("www.")
90-
? parsedUri.Host.Substring(4)
91-
: parsedUri.Host;
77+
var bare = parsed.Host.StartsWith("www.", StringComparison.OrdinalIgnoreCase)
78+
? parsed.Host[4..]
79+
: parsed.Host;
9280

93-
var uriWithoutWww = new UriBuilder(parsedUri)
94-
{
95-
Host = hostWithoutWww
96-
}.Uri
97-
.ToString()
98-
.TrimEnd('/');
99-
100-
var uriWithWww = new UriBuilder(parsedUri)
101-
{
102-
Host = "www." + hostWithoutWww
103-
}.Uri
104-
.ToString()
105-
.TrimEnd('/');
106-
107-
result.Add(uriWithoutWww);
108-
result.Add(uriWithWww);
81+
result.Add(BuildOrigin(parsed, bare));
82+
result.Add(BuildOrigin(parsed, "www." + bare));
10983
}
11084

111-
return new List<string>(result).ToArray();
85+
return [..result];
11286
}
87+
88+
private static string BuildOrigin(Uri source, string host) =>
89+
new UriBuilder(source)
90+
{
91+
Host = host
92+
}.Uri
93+
.ToString()
94+
.TrimEnd('/');
11395
}

src/SharedKernel/SharedKernel.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
<PackageIcon>pandatech.png</PackageIcon>
2222
<PackageReadmeFile>README.md</PackageReadmeFile>
2323

24-
<Version>2.2.2</Version>
25-
<PackageReleaseNotes>Request and Response logging modernized and has extreme perf boost for logging</PackageReleaseNotes>
24+
<Version>2.2.3</Version>
25+
<PackageReleaseNotes>Cors now will start to expose Content-Disposition header</PackageReleaseNotes>
2626

2727
<GenerateDocumentationFile>true</GenerateDocumentationFile>
2828
<IncludeSymbols>true</IncludeSymbols>
@@ -71,7 +71,7 @@
7171
<PackageReference Include="Pandatech.FluentMinimalApiMapper" Version="4.0.0"/>
7272
<PackageReference Include="Pandatech.PandaVaultClient" Version="6.0.0"/>
7373
<PackageReference Include="Pandatech.ResponseCrafter" Version="7.0.0"/>
74-
<PackageReference Include="Scalar.AspNetCore" Version="2.12.50"/>
74+
<PackageReference Include="Scalar.AspNetCore" Version="2.13.1" />
7575
<PackageReference Include="Serilog.AspNetCore" Version="10.0.0"/>
7676
<PackageReference Include="Serilog.Sinks.Async" Version="2.1.0"/>
7777
<PackageReference Include="Serilog.Sinks.Grafana.Loki" Version="8.3.2"/>

0 commit comments

Comments
 (0)