Skip to content

Commit 65f0f67

Browse files
authored
Use constant values for CorsPolicy.cs and CorsPolicyBuilder.cs (#54247)
1 parent b7738d2 commit 65f0f67

File tree

5 files changed

+30
-18
lines changed

5 files changed

+30
-18
lines changed

src/Middleware/CORS/src/Infrastructure/CorsConstants.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,16 @@ public static class CorsConstants
2626
/// </summary>
2727
public static readonly string AnyOrigin = "*";
2828

29+
/// <summary>
30+
/// The value for the Access-Control-Allow-Headers response header to allow all headers.
31+
/// </summary>
32+
public static readonly string AnyHeader = "*";
33+
34+
/// <summary>
35+
/// The value for the Access-Control-Allow-Methods response header to allow all methods.
36+
/// </summary>
37+
public static readonly string AnyMethod = "*";
38+
2939
/// <summary>
3040
/// The Access-Control-Request-Method request header.
3141
/// </summary>

src/Middleware/CORS/src/Infrastructure/CorsPolicy.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public bool AllowAnyHeader
3030
{
3131
get
3232
{
33-
if (Headers == null || Headers.Count != 1 || Headers[0] != "*")
33+
if (Headers == null || Headers.Count != 1 || Headers[0] != CorsConstants.AnyHeader)
3434
{
3535
return false;
3636
}
@@ -46,7 +46,7 @@ public bool AllowAnyMethod
4646
{
4747
get
4848
{
49-
if (Methods == null || Methods.Count != 1 || Methods[0] != "*")
49+
if (Methods == null || Methods.Count != 1 || Methods[0] != CorsConstants.AnyMethod)
5050
{
5151
return false;
5252
}
@@ -62,7 +62,7 @@ public bool AllowAnyOrigin
6262
{
6363
get
6464
{
65-
if (Origins == null || Origins.Count != 1 || Origins[0] != "*")
65+
if (Origins == null || Origins.Count != 1 || Origins[0] != CorsConstants.AnyOrigin)
6666
{
6767
return false;
6868
}

src/Middleware/CORS/src/Infrastructure/CorsPolicyBuilder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ public CorsPolicyBuilder AllowAnyOrigin()
171171
public CorsPolicyBuilder AllowAnyMethod()
172172
{
173173
_policy.Methods.Clear();
174-
_policy.Methods.Add("*");
174+
_policy.Methods.Add(CorsConstants.AnyMethod);
175175
return this;
176176
}
177177

@@ -182,7 +182,7 @@ public CorsPolicyBuilder AllowAnyMethod()
182182
public CorsPolicyBuilder AllowAnyHeader()
183183
{
184184
_policy.Headers.Clear();
185-
_policy.Headers.Add("*");
185+
_policy.Headers.Add(CorsConstants.AnyHeader);
186186
return this;
187187
}
188188

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
#nullable enable
2+
static readonly Microsoft.AspNetCore.Cors.Infrastructure.CorsConstants.AnyHeader -> string!
3+
static readonly Microsoft.AspNetCore.Cors.Infrastructure.CorsConstants.AnyMethod -> string!

src/Middleware/CORS/test/UnitTests/CorsServiceTests.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ public void EvaluatePolicy_CaseInsensitivePreflightRequest_OriginAllowed_Returns
339339
var policy = new CorsPolicy();
340340
policy.Origins.Add(CorsConstants.AnyOrigin);
341341
policy.Origins.Add("http://example.com");
342-
policy.Methods.Add("*");
342+
policy.Methods.Add(CorsConstants.AnyMethod);
343343

344344
// Act
345345
var result = corsService.EvaluatePolicy(requestContext, policy);
@@ -361,7 +361,7 @@ public void EvaluatePolicy_PreflightRequest_IsOriginAllowedReturnsTrue_ReturnsOr
361361
{
362362
IsOriginAllowed = origin => true
363363
};
364-
policy.Methods.Add("*");
364+
policy.Methods.Add(CorsConstants.AnyMethod);
365365

366366
// Act
367367
var result = corsService.EvaluatePolicy(requestContext, policy);
@@ -381,7 +381,7 @@ public void EvaluatePolicy_PreflightRequest_SupportsCredentials_AllowCredentials
381381
SupportsCredentials = true
382382
};
383383
policy.Origins.Add("http://example.com");
384-
policy.Methods.Add("*");
384+
policy.Methods.Add(CorsConstants.AnyMethod);
385385

386386
// Act
387387
var result = corsService.EvaluatePolicy(requestContext, policy);
@@ -401,7 +401,7 @@ public void EvaluatePolicy_PreflightRequest_NoPreflightMaxAge_NoPreflightMaxAgeS
401401
PreflightMaxAge = null
402402
};
403403
policy.Origins.Add(CorsConstants.AnyOrigin);
404-
policy.Methods.Add("*");
404+
policy.Methods.Add(CorsConstants.AnyMethod);
405405

406406
// Act
407407
var result = corsService.EvaluatePolicy(requestContext, policy);
@@ -421,7 +421,7 @@ public void EvaluatePolicy_PreflightRequest_PreflightMaxAge_PreflightMaxAgeSet()
421421
PreflightMaxAge = TimeSpan.FromSeconds(10)
422422
};
423423
policy.Origins.Add(CorsConstants.AnyOrigin);
424-
policy.Methods.Add("*");
424+
policy.Methods.Add(CorsConstants.AnyMethod);
425425

426426
// Act
427427
var result = corsService.EvaluatePolicy(requestContext, policy);
@@ -438,7 +438,7 @@ public void EvaluatePolicy_PreflightRequest_AnyMethod_ReturnsRequestMethod()
438438
var requestContext = GetHttpContext(method: "OPTIONS", origin: "http://example.com", accessControlRequestMethod: "GET");
439439
var policy = new CorsPolicy();
440440
policy.Origins.Add(CorsConstants.AnyOrigin);
441-
policy.Methods.Add("*");
441+
policy.Methods.Add(CorsConstants.AnyMethod);
442442

443443
// Act
444444
var result = corsService.EvaluatePolicy(requestContext, policy);
@@ -478,8 +478,8 @@ public void EvaluatePolicy_PreflightRequest_NoHeadersRequested_AllowedAllHeaders
478478
var requestContext = GetHttpContext(method: "OPTIONS", origin: "http://example.com", accessControlRequestMethod: "PUT");
479479
var policy = new CorsPolicy();
480480
policy.Origins.Add(CorsConstants.AnyOrigin);
481-
policy.Methods.Add("*");
482-
policy.Headers.Add("*");
481+
policy.Methods.Add(CorsConstants.AnyMethod);
482+
policy.Headers.Add(CorsConstants.AnyHeader);
483483

484484
// Act
485485
var result = corsService.EvaluatePolicy(requestContext, policy);
@@ -501,8 +501,8 @@ public void EvaluatePolicy_PreflightRequest_AllowAllHeaders_ReflectsRequestHeade
501501
accessControlRequestHeaders: new[] { "foo", "bar" });
502502
var policy = new CorsPolicy();
503503
policy.Origins.Add(CorsConstants.AnyOrigin);
504-
policy.Methods.Add("*");
505-
policy.Headers.Add("*");
504+
policy.Methods.Add(CorsConstants.AnyMethod);
505+
policy.Headers.Add(CorsConstants.AnyHeader);
506506

507507
// Act
508508
var result = corsService.EvaluatePolicy(requestContext, policy);
@@ -524,7 +524,7 @@ public void EvaluatePolicy_PreflightRequest_HeadersRequested_NotAllHeaderMatches
524524
accessControlRequestHeaders: new[] { "match", "noMatch" });
525525
var policy = new CorsPolicy();
526526
policy.Origins.Add(CorsConstants.AnyOrigin);
527-
policy.Methods.Add("*");
527+
policy.Methods.Add(CorsConstants.AnyMethod);
528528
policy.Headers.Add("match");
529529
policy.Headers.Add("foo");
530530

@@ -544,8 +544,8 @@ public void EvaluatePolicy_PreflightRequest_WithCredentials_ReflectsHeaders()
544544
var httpContext = GetHttpContext(method: "OPTIONS", origin: "http://example.com", accessControlRequestMethod: "PUT");
545545
var policy = new CorsPolicy();
546546
policy.Origins.Add("http://example.com");
547-
policy.Methods.Add("*");
548-
policy.Headers.Add("*");
547+
policy.Methods.Add(CorsConstants.AnyMethod);
548+
policy.Headers.Add(CorsConstants.AnyHeader);
549549
policy.SupportsCredentials = true;
550550

551551
// Act

0 commit comments

Comments
 (0)