-
Notifications
You must be signed in to change notification settings - Fork 38.3k
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
Refine CORS documentation for wildcard processing #31143
Comments
From the description of this issue, it's not clear whether you are reporting a bug, requesting an enhancement, or asking a question. Can we take a step back and start over with:
You can read up good advice on how to create a minimal sample if this is a better option for you. |
What am I trying to achieve?
What am I tried? @Configuration
@EnableWebMvc
@RequiredArgsConstructor
public class CorsConfig implements WebMvcConfigurer {
private final CorsProperties corsProperties;
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**").combine(corsProperties.applyPermitDefaultValues());
}
} package example;
import static java.util.Objects.isNull;
import static org.springframework.util.CollectionUtils.isEmpty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.web.cors.CorsConfiguration;
@ConfigurationProperties("cors")
public class CorsProperties extends CorsConfiguration {
@Override
public CorsConfiguration applyPermitDefaultValues() {
if (isEmpty(getAllowedOrigins()) && isEmpty(getAllowedOrigins())) {
addAllowedOriginPattern(ALL);
}
if (isEmpty(getAllowedMethods())) {
addAllowedMethod(ALL);
}
if (isEmpty(getAllowedHeaders())) {
addAllowedHeader(ALL);
}
if (isEmpty(getExposedHeaders())) {
addExposedHeader(ALL);
}
if (isNull(getMaxAge())) {
setMaxAge(1800L);
}
if (isNull(getAllowCredentials())) {
setAllowCredentials(true);
//todo owerwrite "*" (ALL="*") in all fields dynamically based on request and response, when allow credentials
}
return this;
}
} What is expected bahaviour? |
Looks like there is potentially 2 things here:
@lowcasz Could you please confirm your are raising both the described potential bug and enhancement request? |
Sure ;) It can be like that. We are talking about situation when allowCredentials (Access-Control-Allow-Credentials) is true only. Without credentials actual version is ok. |
I'm sorry for late answer. I tested it on chrome only, and this browser actually works with asterisk even if credentials are allowed. |
After a discussion with the team, for headers and methods, we are not going to introduce pattern management. You can watch this draft commit to see the direction I am leaning towards. I have implemented support for copying the request method and headers but I will likely not be able to support exposed headers as that would require, if I am not mistaken, to copy the response headers which are not available when the
Are you talking about allowed methods, allowed headers or exposed headers here? If this is about exposed headers, did you checked headers were accessible from JavaScript with |
I have read actual Bean defining CORS and I know it is difficult to change. It is reason why I wrote it instead extend this class or create additional filters, becase o lot of people have the same problem and should be resolved on library level. It is why I think it is important issue. I know other problem without answer: How to process preflight requests? Instead of copy response headers, we can solve this problem in other way which support preflight requests too. We can create new issue when it is needed. I think that will be very important and hot feature, when any popular browser starts support this restrictions. |
Your example in #31143 (comment) shows overriding At best we can make decisions at runtime, in For expose headers, there isn't anything straight forward we can do to replace |
After some tests, I can confirm that Chrome, Safari and Firefox in practice all allow using |
Please keep this issue open as we will do some refinements. |
This commit refines CORS wildcard processing Javadoc to provides more details on how wildcards are handled for Access-Control-Allow-Methods, Access-Control-Allow-Headers and Access-Control-Expose-Headers CORS headers. For Access-Control-Expose-Headers, it is not possible to copy the response headers which are not available at the point when the CorsProcessor is invoked. Since all the major browsers seem to support wildcard including on requests with credentials, and since this is ultimately the user-agent responsibility to check on client-side what is authorized or not, Spring Framework continues to support this use case. See spring-projectsgh-31143
In CORS configuration we don't have access for request and response, e.g. by servlet filter
We have allowedOriginsPattern to override * wildcard when we are using allowCredentials=true
In this situation we overwrite allowed-origin header by request origin, but heders and methods are still not overwrite wildcard which is not allowed by documentation when we allow credentials.
When some configuration for requestHeaders, exposedHeaders, allowedMethods is null then all is clear,
but when we have allowed credentials and some list is defined with one value with asterisk then it should be overwritten according to CORS documentation.
It is problematically for preflighted requests when we don't know all request headers or exposed headers,
but in single request it should be overwritten according to headers from request and response when we allow credentials,
or we should have possibility to calculate cors headers dynamically based on request and response to fix it manually.
The text was updated successfully, but these errors were encountered: