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

Use @PreMatching for CORS Preflight request #11097

Merged
merged 9 commits into from
Oct 10, 2024
Merged

Use @PreMatching for CORS Preflight request #11097

merged 9 commits into from
Oct 10, 2024

Conversation

dstepanov
Copy link
Contributor

No description provided.

return Optional.of(HttpStatus.FORBIDDEN);
if (hasAllowedHeaders(request, config)) {
List<UriRouteMatch<Object, Object>> anyUriRoutes = router.findAny(request);
if (anyUriRoutes.isEmpty()) {
Copy link
Contributor Author

@dstepanov dstepanov Aug 19, 2024

Choose a reason for hiding this comment

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

This is how it worked before, but I think that's not correct. methodToMatchOptional is OPTIONS for preflight check and there is no such method in mocked tests

Copy link
Member

Choose a reason for hiding this comment

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

methodToMatchOptional can be not OPTIONS if there's a Access-Control-Request-Method header

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Shouldt'n we fail OPTIONS call if there is no route present under that path?

Copy link
Member

Choose a reason for hiding this comment

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

No, in that case the preflight should return a good response and the actual cors request should return the 404. This will allow the cors client to see that the route is missing. Otherwise it will just get a permission error

@graemerocher
Copy link
Contributor

seems to be failing?


then:
HttpStatus.OK == response.status()
HttpStatus.FORBIDDEN == response.status()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I had to rewite the mocked test. This is not passing anymore, the mock didn't include the proper origin mapping so the request filter was skipped. Please check how it supposted to work @yawkat @sdelamo

@sdelamo sdelamo added this to the 4.7.0 milestone Oct 7, 2024
@@ -37,6 +37,12 @@ class OptionsRequestAttributesSpec extends Specification {
HttpClientResponseException e = thrown()
e.response.status == HttpStatus.METHOD_NOT_ALLOWED

and:
MyFilter myFilter = ctx.getBean(MyFilter)
!myFilter.containsRouteInfo
Copy link
Contributor

Choose a reason for hiding this comment

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

@dstepanov I had to this change as well.

@sdelamo
Copy link
Contributor

sdelamo commented Oct 10, 2024

@dstepanov I have rewritten the test and the filter slightly. I think it is now correct. @yawkat can you check it out again.

@sdelamo sdelamo requested a review from yawkat October 10, 2024 10:08
@sdelamo sdelamo changed the title Cors should use pre-matching request check Use @PreMatching for CORS Preflight request Oct 10, 2024
@dstepanov
Copy link
Contributor Author

LGTM

Copy link

sonarcloud bot commented Oct 10, 2024

@sdelamo sdelamo merged commit 4f28175 into 4.7.x Oct 10, 2024
21 checks passed
@sdelamo sdelamo deleted the cors branch October 10, 2024 12:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

4 participants