Skip to content
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

Updating HttpMethodMetadata based on ICorsMetadata #43077

Merged
merged 12 commits into from Aug 12, 2022

Conversation

brunolins16
Copy link
Member

Fixes #42994

@ghost ghost added the area-runtime label Aug 3, 2022
@brunolins16 brunolins16 requested a review from a team August 3, 2022 18:27
@brunolins16 brunolins16 marked this pull request as ready for review August 3, 2022 20:38
@brunolins16 brunolins16 requested a review from a team as a code owner August 4, 2022 22:15
src/Http/Routing/src/RouteEndpointBuilder.cs Outdated Show resolved Hide resolved
src/Http/Routing/src/RouteEndpointBuilder.cs Outdated Show resolved Hide resolved
@@ -19,52 +19,6 @@ public class CorsApplicationModelProviderTest
{
private readonly IOptions<MvcOptions> OptionsWithoutEndpointRouting = Options.Create(new MvcOptions { EnableEndpointRouting = false });

[Fact]
public void OnProvidersExecuting_SetsEndpointMetadata_IfCorsAttributeIsPresentOnController()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume we have some end-to-end tests covering this scenario. Do you know where they are?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here:

public abstract class CorsTestsBase<TStartup> : IClassFixture<MvcTestFixture<TStartup>> where TStartup : class

public class CorsEndpointRoutingTests : CorsTestsBase<CorsWebSite.Startup>

{
// Since we found a CORS metadata we will update it
// to make sure the acceptCorsPreflight is set to true.
httpMethodMetadata.AcceptCorsPreflight = true;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Metadata is supposed to be immutable. Rather than modifying the HTTP method metadata, have you considered checking for ICorsMetadata inside the HTTP method policy? If it's present, then go ahead with accepting preflight.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated to this PR but while we are on subject of immutable metadata. IAuthorizeData has public setters for its properties. Wondering if it should be deprecated and removed though it would be a breaking change.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Metadata doesn't have to be immutable, and it's not worth breaking changes. It's a pattern to try to encourage.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Metadata is supposed to be immutable. Rather than modifying the HTTP method metadata, have you considered checking for ICorsMetadata inside the HTTP method policy? If it's present, then go ahead with accepting preflight.

@JamesNK that was my first idea but after talking with @halter73 the suggestion to mutate the metadata sounded reasonable

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@halter73 I can easily make the the changes in Http method policy without need a public api, do you have any concerns?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only behavior that I don't like about not mutating the metadata, change only the policy, is that the AcceptsCorsPreFlight will never be true, so, there is no reason for it to exist.

Copy link
Member

@halter73 halter73 Aug 10, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JamesNK Interesting idea. I also don't love mutating the metadata. We had a conversation earlier about how we could avoid this by replacing the metadata instead (#43077 (comment)), but that has other issues.

Having the CORS middleware HttpMethodMatcherPolicy handle this itself is another approach that I like. My problem with it is that:

The only behavior that I don't like about not mutating the metadata, change only the policy, is that the AcceptsCorsPreFlight will never be true, so, there is no reason for it to exist.

If we go this route, we need to obsolete AcceptCorsPreflight and tell people to add the appropriate attribute the the endpoints metadata instead. I don't have much of a preference. As you say, we do have other mutable metadata, so we're not breaking entirely new ground here.

I've already approved this PR and I still think it's okay as is, but if people want to obsolete AcceptCorsPreflight instead, I'm okay with that too. That will need to be a new API proposal, but we could probably get that approved over email this afternoon if we think it's worth it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have enough time before code complete to think and test this through to offer a strong opinion 😢

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, if not objections I am planning to merge the PR as is. It is broken since 3.x, so I feel like it is a must to have change.

@brunolins16 brunolins16 merged commit ad076ff into dotnet:main Aug 12, 2022
@ghost ghost added this to the 7.0-rc1 milestone Aug 12, 2022
@prkemp
Copy link

prkemp commented Oct 11, 2022

Can this change be merged into .net 6 for a subsequent release? I'm interested in the fix for issue "Cors not working in Minimal API endpoints" #42994. I'm not keen to upgrade to .net 7 yet. Thanks

@ghost
Copy link

ghost commented Oct 11, 2022

Hi @prkemp. It looks like you just commented on a closed PR. The team will most probably miss it. If you'd like to bring something important up to their attention, consider filing a new issue and add enough details to build context.

@BrennanConroy
Copy link
Member

There is a workaround for .net 6, add .WithMetadata(new HttpMethodMetadata(new string[] { "POST" }, acceptCorsPreflight: true)); to any affected endpoints.

@brunolins16 brunolins16 deleted the brunolins16/issues/42994 branch October 11, 2022 17:04
@prkemp
Copy link

prkemp commented Oct 14, 2022

Great, thanks for this.

@ghost
Copy link

ghost commented Oct 14, 2022

Hi @prkemp. It looks like you just commented on a closed PR. The team will most probably miss it. If you'd like to bring something important up to their attention, consider filing a new issue and add enough details to build context.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Cors not working in Minimal API endpoints
7 participants