Skip to content

Commit c2b087c

Browse files
Merge pull request #1222 from g0w6y:fix/dev-server-wildcard-cors-websocket-origin
PiperOrigin-RevId: 942029517
2 parents 6834b36 + 5029081 commit c2b087c

2 files changed

Lines changed: 20 additions & 2 deletions

File tree

dev/src/main/java/com/google/adk/web/config/AdkWebCorsConfig.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package com.google.adk.web.config;
1818

19+
import org.slf4j.Logger;
20+
import org.slf4j.LoggerFactory;
1921
import org.springframework.context.annotation.Bean;
2022
import org.springframework.context.annotation.Configuration;
2123
import org.springframework.web.cors.CorsConfiguration;
@@ -42,8 +44,18 @@
4244
@Configuration
4345
public class AdkWebCorsConfig {
4446

47+
private static final Logger logger = LoggerFactory.getLogger(AdkWebCorsConfig.class);
48+
4549
@Bean
4650
public CorsConfigurationSource corsConfigurationSource(AdkWebCorsProperties corsProperties) {
51+
if (corsProperties.origins().contains("*")) {
52+
logger.warn(
53+
"CORS is configured to allow all origins (\"*\"), which is insecure and intended for"
54+
+ " local development only. This also applies to the /run_live WebSocket endpoint."
55+
+ " Set 'adk.web.cors.origins' to an explicit allowlist to restrict which origins"
56+
+ " may call the server.");
57+
}
58+
4759
CorsConfiguration configuration = new CorsConfiguration();
4860

4961
configuration.setAllowedOrigins(corsProperties.origins());

dev/src/main/java/com/google/adk/web/websocket/WebSocketConfig.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.google.adk.web.websocket;
1818

19+
import com.google.adk.web.config.AdkWebCorsProperties;
1920
import org.springframework.beans.factory.annotation.Autowired;
2021
import org.springframework.context.annotation.Configuration;
2122
import org.springframework.web.socket.config.annotation.EnableWebSocket;
@@ -28,14 +29,19 @@
2829
public class WebSocketConfig implements WebSocketConfigurer {
2930

3031
private final LiveWebSocketHandler liveWebSocketHandler;
32+
private final AdkWebCorsProperties corsProperties;
3133

3234
@Autowired
33-
public WebSocketConfig(LiveWebSocketHandler liveWebSocketHandler) {
35+
public WebSocketConfig(
36+
LiveWebSocketHandler liveWebSocketHandler, AdkWebCorsProperties corsProperties) {
3437
this.liveWebSocketHandler = liveWebSocketHandler;
38+
this.corsProperties = corsProperties;
3539
}
3640

3741
@Override
3842
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
39-
registry.addHandler(liveWebSocketHandler, "/run_live").setAllowedOrigins("*");
43+
registry
44+
.addHandler(liveWebSocketHandler, "/run_live")
45+
.setAllowedOrigins(corsProperties.origins().toArray(new String[0]));
4046
}
4147
}

0 commit comments

Comments
 (0)