Skip to content

Execute preflight checks before interceptors #29509

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

Closed
wants to merge 1 commit into from

Conversation

tianshuang
Copy link
Contributor

@tianshuang tianshuang commented Nov 17, 2022

There is a similar problem here: CorsInterceptor should add to the first interceptor in InterceptorChain? · Issue #22459. In this commit: CorsInterceptor at the front of the chain, the order of CorsInterceptor was adjusted to before all interceptors, but the processing logic of PreFlight was not adjusted.

When a PreFlight request is sent to the server, if one of the interceptors in the chain returns false in the preHandle method, then the related CORS headers will not be added, causing the browser to judge cross-domain, even if we explicitly configure allowedOrigins, but in fact there is no cross-domain, but the interceptor abort the chain in advance and the PreFlight logic is not executed.

Expected behavior:
CORS related headers are added in the response of the PreFlight request regardless of whether the interceptor abort the chain.

Actual behavior:
Browser block CORS request because PreFlight request not responding to 'Access-Control-Allow-Origin' header. Browser console print: Access to XMLHttpRequest at 'https://server.com/users' from origin 'https://www.another.com' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

Added unit test:
org.springframework.web.servlet.handler.HandlerMethodMappingTests#abortInterceptorInPreFlightRequestWithCorsConfig

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Nov 17, 2022
@rstoyanchev rstoyanchev added the in: core Issues in core modules (aop, beans, core, context, expression) label Jan 24, 2023
@tianshuang
Copy link
Contributor Author

@rstoyanchev

@sdeleuze sdeleuze self-assigned this Aug 27, 2023
@sdeleuze sdeleuze added in: web Issues in web modules (web, webmvc, webflux, websocket) and removed in: core Issues in core modules (aop, beans, core, context, expression) labels Aug 27, 2023
@rstoyanchev rstoyanchev assigned rstoyanchev and unassigned sdeleuze Sep 7, 2023
@rstoyanchev rstoyanchev added type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Sep 7, 2023
@rstoyanchev rstoyanchev added this to the 6.1.0-RC1 milestone Sep 7, 2023
@rstoyanchev rstoyanchev changed the title Execute preflight check logic before all interceptors Execute preflight checks before interceptors Sep 8, 2023
rstoyanchev pushed a commit that referenced this pull request Sep 8, 2023
@rstoyanchev
Copy link
Contributor

rstoyanchev commented Sep 8, 2023

@tianshuang thank you for the pull request. I've adjusted the implementation slightly and also opted to skip the rest of the interceptor chain. Arguably if CORS checks fail, we shouldn't proceed with further processing of any kind. That also aligns better with how actual requests are handled.

@tianshuang tianshuang deleted the preflight branch July 3, 2024 14:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants