File tree Expand file tree Collapse file tree
dev/src/main/java/com/google/adk/web Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1616
1717package com .google .adk .web .config ;
1818
19+ import org .slf4j .Logger ;
20+ import org .slf4j .LoggerFactory ;
1921import org .springframework .context .annotation .Bean ;
2022import org .springframework .context .annotation .Configuration ;
2123import org .springframework .web .cors .CorsConfiguration ;
4244@ Configuration
4345public 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 ());
Original file line number Diff line number Diff line change 1616
1717package com .google .adk .web .websocket ;
1818
19+ import com .google .adk .web .config .AdkWebCorsProperties ;
1920import org .springframework .beans .factory .annotation .Autowired ;
2021import org .springframework .context .annotation .Configuration ;
2122import org .springframework .web .socket .config .annotation .EnableWebSocket ;
2829public 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}
You can’t perform that action at this time.
0 commit comments