-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Update CORS middleware to use endpoint metadata #4460
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
Conversation
|
Design questions:
|
Scratch that. While it is on for every request, the default policy name will resolve to |
src/CORS/test/Microsoft.AspNetCore.Cors.Test/CorsMiddlewareTests.cs
Outdated
Show resolved
Hide resolved
What does it do in that case? Does it have a default policy that it applies unless overridden? That seems super nice. |
2 similar comments
What does it do in that case? Does it have a default policy that it applies unless overridden? That seems super nice. |
What does it do in that case? Does it have a default policy that it applies unless overridden? That seems super nice. |
|
Github is telling me that my comments don't work, but then adding them anyway. Leaving the dupes because it's more A E S T H E T I C |
| { | ||
| public static class CorsEndpointConventionBuilderExtensions | ||
| { | ||
| public static IEndpointConventionBuilder RequireCors(this IEndpointConventionBuilder builder, string policyName) |
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.
can policy name by null? What does that mean?
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.
A null policy name means either the default policy or default policy name configured passed to the middleware constructor is used.
...CORS/src/Microsoft.AspNetCore.Cors/Infrastructure/CorsEndpointConventionBuilderExtensions.cs
Outdated
Show resolved
Hide resolved
src/CORS/src/Microsoft.AspNetCore.Cors/Infrastructure/CorsMiddleware.cs
Outdated
Show resolved
Hide resolved
rynowak
left a comment
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 looks on the right track 😁
|
We're adding required PR checks to ensure the repo compiles before you merge. Can you update this PR by rebasing on master? Alternative: if you plan to 'squash merge' this PR, you can also use a merge to update this PR. This can be easier to manage if you have lots of changes in this PR. |
066b987 to
bd480b9
Compare
|
@natemcmaster The build is failing with this message:
Doe the order that solutions are compiled need to change now that CORS depends on routing? |
|
🆙 📅 // @rynowak |
| { | ||
| app.UseEndpointRouting(routing => | ||
| { | ||
| routing.Map("/allow-origin", HandleRequest).WithCorsPolicy("AllowOrigin"); |
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.
Nice
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.
Could we add an overload that also accepts an actual instance of the policy e.g. WithCorsPolicy(builder => ...)?
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.
Possible. It would require new metadata. The current IEnableCorsAttribute only has a name on it.
| /// <summary> | ||
| /// A marker interface which can be used to identify CORS metdata. | ||
| /// </summary> | ||
| public interface ICorsAttribute |
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.
internal interface?
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.
It needs to be public so if anyone else is searching for CORS metadata they can use the marker interface to find the most significant metadata.
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.
ICorsPolicyMetadata?
Yes, CORS builds as a peer of Routing, which is why you're getting errors. https://github.com/aspnet/AspNetCore/blob/94e317f411ce618a56a64d1061f02b9eaa4998a3/build/buildorder.props#L18-L19 You can either bump the build order. Or, wait until I merge #4449 and rebase on master. |
94e317f to
a43f83b
Compare
|
Updated. It now builds on the server but requires an update. When I attempt to update the local deps I get this error: |
...CORS/src/Microsoft.AspNetCore.Cors/Infrastructure/CorsEndpointConventionBuilderExtensions.cs
Outdated
Show resolved
Hide resolved
rynowak
left a comment
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 looks super duper! Only a few minor comments
| [Theory] | ||
| [InlineData("Startup")] | ||
| [InlineData("StartupWithoutEndpointRouting")] | ||
| public async Task RunClientTests(string startup) |
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.
Could we make these two separate tests? RunClientTests_WithEndpoint and RunClientTests_WithoutEndpoint? Theories can be annoying to debug.
|
The CORS reorg was merged to master. Let me know if you have trouble rebasing this PR. |
|
Thanks. Rebasing is not a problem. This isn't urgent so I'm holding off until Middleware.sln builds in VS. |
c417b78 to
14d0d0f
Compare
|
Middleware.sln is building for me 👍 Rebased |
14d0d0f to
401f19c
Compare
| /// <summary> | ||
| /// A marker interface which can be used to identify CORS metdata. | ||
| /// </summary> | ||
| public interface ICorsAttribute |
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.
What about ICorsMetadata? Putting attribute in the name seems silly since it's not an attritube.
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.
Already done locally.
Will look like:
- ICorsMetadata
- IEnableCorsAttribute
- IDisableCorsAttribute
- ICorsPolicyMetadata
ICorsPolicyMetadata would have the policy name and a policy on it to support #4460 (comment). Constructor would only allow one.
Thoughts? An alternate is ICorsPolicyMetadata only have the CorsPolicy on it and we leave IEnableCorsAttribute as the way to set the policy name.
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.
I like this.
8ca21f8 to
71e9a0f
Compare
Addresses #4210