-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Fix/18552 cors wildcard credentials #19020
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
base: main
Are you sure you want to change the base?
Changes from all commits
ac99102
919f95f
43a1d2a
6f0f75c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,6 +1,7 @@ | ||||||
| using Microsoft.AspNetCore.Cors.Infrastructure; | ||||||
| using Microsoft.Extensions.Logging; | ||||||
| using Microsoft.Extensions.Options; | ||||||
| using CorsConstants = Microsoft.AspNetCore.Cors.Infrastructure.CorsConstants; | ||||||
|
|
||||||
| namespace OrchardCore.Cors.Services; | ||||||
|
|
||||||
|
|
@@ -16,71 +17,77 @@ public CorsOptionsConfiguration(CorsService corsService, ILogger<CorsOptionsConf | |||||
| } | ||||||
|
|
||||||
| public void Configure(CorsOptions options) | ||||||
| { | ||||||
| var corsSettings = _corsService.GetSettingsAsync().GetAwaiter().GetResult(); | ||||||
| if (corsSettings?.Policies == null || !corsSettings.Policies.Any()) | ||||||
| { | ||||||
| return; | ||||||
| } | ||||||
|
|
||||||
| foreach (var corsPolicy in corsSettings.Policies) | ||||||
| { | ||||||
| if (corsPolicy.AllowCredentials && corsPolicy.AllowAnyOrigin) | ||||||
| { | ||||||
| _logger.LogWarning("Using AllowCredentials and AllowAnyOrigin at the same time is considered a security risk, the {PolicyName} policy will not be loaded.", corsPolicy.Name); | ||||||
| continue; | ||||||
| } | ||||||
|
|
||||||
| options.AddPolicy(corsPolicy.Name, configurePolicy => | ||||||
| { | ||||||
| if (corsPolicy.AllowAnyHeader) | ||||||
| { | ||||||
| configurePolicy.AllowAnyHeader(); | ||||||
| } | ||||||
| else | ||||||
| { | ||||||
| configurePolicy.WithHeaders(corsPolicy.AllowedHeaders); | ||||||
| } | ||||||
| { | ||||||
| var corsSettings = _corsService.GetSettingsAsync().GetAwaiter().GetResult(); | ||||||
| if (corsSettings?.Policies == null || !corsSettings.Policies.Any()) | ||||||
| { | ||||||
| return; | ||||||
| } | ||||||
|
Comment on lines
19
to
+25
|
||||||
|
|
||||||
| if (corsPolicy.AllowAnyMethod) | ||||||
| { | ||||||
| configurePolicy.AllowAnyMethod(); | ||||||
| } | ||||||
| else | ||||||
| { | ||||||
| configurePolicy.WithMethods(corsPolicy.AllowedMethods); | ||||||
| } | ||||||
| foreach (var corsPolicy in corsSettings.Policies) | ||||||
| { | ||||||
| var allowAnyOrigin = corsPolicy.AllowAnyOrigin | ||||||
| || corsPolicy.AllowedOrigins?.Any(origin => | ||||||
| string.Equals(origin?.Trim(), CorsConstants.AnyOrigin, StringComparison.Ordinal)) == true; | ||||||
|
Comment on lines
+29
to
+31
|
||||||
|
|
||||||
| if (corsPolicy.AllowAnyOrigin) | ||||||
| { | ||||||
| configurePolicy.AllowAnyOrigin(); | ||||||
| } | ||||||
| else | ||||||
| { | ||||||
| configurePolicy.WithOrigins(corsPolicy.AllowedOrigins); | ||||||
| } | ||||||
| if (corsPolicy.AllowCredentials && allowAnyOrigin) | ||||||
| { | ||||||
| _logger.LogWarning( | ||||||
| "Using AllowCredentials and AllowAnyOrigin at the same time is considered a security risk, the {PolicyName} policy will not be loaded.", | ||||||
|
||||||
| "Using AllowCredentials and AllowAnyOrigin at the same time is considered a security risk, the {PolicyName} policy will not be loaded.", | |
| "Using AllowCredentials with any origin (including '*') is considered a security risk, the {PolicyName} policy will not be loaded.", |
Copilot
AI
Apr 2, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
options.DefaultPolicyName ??= corsSettings.Policies.FirstOrDefault()?.Name; can set the default policy name to a policy that was skipped above (e.g., invalid AllowCredentials + any-origin). That leaves DefaultPolicyName pointing at a policy that was never added via AddPolicy, which can cause runtime failures when the pipeline calls UseCors() without a policy name. Prefer selecting the first successfully-added policy name (track it during the loop), or leave DefaultPolicyName unset when no policies were loaded.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This method now treats an origin entry of "" (including whitespace-trimmed) as "any origin". Since the UI warning shown on save is framed in terms of "AllowAnyOrigin", it can become confusing when the checkbox is off but "" is present in the list. Consider adjusting the user-facing warning text to reference "any origin / '*'" rather than only the checkbox name.