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

Not possible to use allowedOrigins "*" in StompEndpointRegistry after upgrade to Spring Boot 2.4.0 #26111

Closed
opersson opened this issue Nov 18, 2020 · 9 comments
Assignees
Labels
in: messaging Issues in messaging modules (jms, messaging) status: superseded An issue that has been superseded by another type: regression A bug that is also a regression

Comments

@opersson
Copy link

Not sure if this should be filed under Spring Boot or Spring framework, but I put it here since Spring Boot Starter is in use.

After upgrading to use Spring Boot 2.4.0 from 2.3.x, it does not seem to be possible to use allowedOrigins = "*" in the StompEndpointRegistry. When connecting it results in the following Error:

java.lang.IllegalArgumentException: When allowCredentials is true, allowedOrigins cannot contain the special value "*"since that cannot be set on the "Access-Control-Allow-Origin" response header. To allow credentials to a set of origins, list them explicitly or consider using "allowedOriginPatterns" instead.

However, allowedOriginPatterns i not something that is available on the StompEndpointRegistry, only allowedOrigins is available.

Code to reproduce
package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.StompWebSocketEndpointRegistration;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;

@SpringBootApplication
@EnableWebSocketMessageBroker
public class DemoApplication implements WebSocketMessageBrokerConfigurer {

	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}

	@Override
	public void registerStompEndpoints(StompEndpointRegistry registry) {
		StompWebSocketEndpointRegistration registration = registry.addEndpoint("/endpoint");
		registration.setAllowedOrigins("*");
		registration.withSockJS();
	}
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script>
        fetch("http://localhost:8080/endpoint")
            .then(response => console.log(response));
    </script>
</head>
<body>
</body>
</html>
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.4.0</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.example</groupId>
	<artifactId>demo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>demo</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<java.version>11</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-websocket</artifactId>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

Start the server on port 8080 and host the host the html file on another port and open it in a browser.

@wilkinsona
Copy link
Member

wilkinsona commented Nov 18, 2020

Thanks for the report. The behaviour that you're seeing is due to these changes in Spring Framework. We'll transfer this issue to the Framework team so that they can take a look.

@bclozel bclozel transferred this issue from spring-projects/spring-boot Nov 18, 2020
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Nov 18, 2020
@rstoyanchev rstoyanchev self-assigned this Nov 18, 2020
@rstoyanchev rstoyanchev added in: messaging Issues in messaging modules (jms, messaging) type: regression A bug that is also a regression and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Nov 18, 2020
@rstoyanchev rstoyanchev added this to the 5.3.2 milestone Nov 18, 2020
@rstoyanchev
Copy link
Contributor

#25016 introduced the ability to configure allowedOriginPatterns in addition to just allowedOrigins. It lets you define more flexible patterns while the latter is literally the value to return in the Access-Control-Allow-Origin header and for that "*" is not allowed in combination with allowCredentials=true. The change introduced equivalent allowedOriginPatterns methods in the WebMvc and the WebFlux config, but not in the SockJS config and the AbstractSocketJsService.

I'll add those for 5.3.2. You'll then need to switch to allowedOriginPatterns instead of allowedOrigins but that gives you an option to define more precisely the allowed domain patterns. In the mean time, you might be able to work around by listing specific domains if that's feasible.

@rstoyanchev rstoyanchev linked a pull request Nov 18, 2020 that will close this issue
@rstoyanchev
Copy link
Contributor

This is now superseded by #26108.

@rstoyanchev rstoyanchev removed this from the 5.3.2 milestone Nov 18, 2020
@rstoyanchev rstoyanchev added the status: superseded An issue that has been superseded by another label Nov 18, 2020
rieckpil added a commit to stratospheric-dev/stratospheric that referenced this issue Nov 20, 2020
@JeromeRider
Copy link

When I implement WebSocketMessageBrokerConfigurer, I'm not asked to override registerStompEndpoints() method. Am I supposed to use Java 11 ? I'm on Java 8 - Maven 2.4.2

@rstoyanchev
Copy link
Contributor

rstoyanchev commented Feb 10, 2021

@JeromeRider, all are default methods and so none are required. Imagine that there are several of these in one application. They are all applied, and one might register endpoints while others might not.

@richvim
Copy link

richvim commented Feb 19, 2021

This massively burned us during upgrade, why was this breaking change not mentioned in the release notes?

@rstoyanchev
Copy link
Contributor

@richvim I'm sorry to hear that. It is mentioned here although I can see how we should probably more explicitly mention STOMP web messaging as part of that.

@alrawasabed
Copy link

alrawasabed commented Feb 23, 2021

The interface WebSocketHandlerRegistration is still missing setAllowedOriginPatterns in spring-websocket-5.3.3
I use it when I override registerWebSockerHandlers(WebSocketHandlerRegistry registry) when implementing WebSocketConfigurer

It's supposed to be there right?

@rstoyanchev
Copy link
Contributor

@alrawasabed yes it should be there. I've created #26593.

cjswoduddn pushed a commit to cjswoduddn/appeal-api that referenced this issue Mar 15, 2021
cors 간단한 이슈 해결
cors정책을 모든 도메인에 대한 허용으로 바꾸기 위해
addAllowedOrigin("*")으로 했었는데
When allowCredentials is true, allowedOrigins cannot contain the special value "*"since that cannot be set on the "Access-Control-Allow-Origin" response header. To allow credentials to a set of origins, list them explicitly or consider using "allowedOriginPatterns" instead.

브라우저에서 요청을 보내면 위와같은 알아 듣기 힘든 오류를 뱉음
spring-projects/spring-framework#26111
버전 문제인 듯하고 좀더 조사필요

일단 addAllowedOriginPattern("*")으로 메소드를 변경하니 원하는 대로 동작
CaptainIRS added a commit to delta/codecharacter-server-2020 that referenced this issue Mar 21, 2021
lndobryden pushed a commit to FordLabs/retroquest that referenced this issue May 25, 2021
lndobryden pushed a commit to FordLabs/retroquest that referenced this issue May 26, 2021
oxbx06 pushed a commit to cisagov/FLAREclient-Java that referenced this issue Jul 27, 2021
yurloc added a commit to yurloc/optaweb-vehicle-routing that referenced this issue Jan 23, 2022
`Access-Control-Allow-Origin: *` is not allowed when
`Access-Control-Allow-Credentials: true`. Spring introduced
allowedOriginPatterns() in 5.3.2. If any of the patterns matches the
origin of the _request_, the it is used as a value of
`Access-Control-Allow-Origin` in the response.

For context, see
spring-projects/spring-framework#26111.
yurloc added a commit to kiegroup/optaweb-vehicle-routing that referenced this issue Jan 26, 2022
`Access-Control-Allow-Origin: *` is not allowed when
`Access-Control-Allow-Credentials: true`. Spring introduced
allowedOriginPatterns() in 5.3.2. If any of the patterns matches the
origin of the _request_, the it is used as a value of
`Access-Control-Allow-Origin` in the response.

For context, see
spring-projects/spring-framework#26111.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: messaging Issues in messaging modules (jms, messaging) status: superseded An issue that has been superseded by another type: regression A bug that is also a regression
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants