Skip to content

Commit cb73317

Browse files
committed
fix(dev): use localhost port wildcard for default CORS/WebSocket origins
Replace the hardcoded :8080 default origins with the localhost port-wildcard pattern (http://localhost:[*], http://127.0.0.1:[*]) so the allowlist no longer assumes the server runs on 8080 (it may run on any port, e.g. ng serve on 4200 or a user-set server.port). Switch both origin sinks from setAllowedOrigins to setAllowedOriginPatterns (AdkWebCorsConfig HTTP CORS and WebSocketConfig /run_live), since the [*] port pattern is only honored by the *Patterns API; with setAllowedOrigins it would be treated as a literal and match nothing. Security posture is unchanged: any localhost port is allowed, while remote origins, suffix tricks (evil.localhost.com) and non-http schemes remain blocked.
1 parent de85300 commit cb73317

3 files changed

Lines changed: 3 additions & 3 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public class AdkWebCorsConfig {
4646
public CorsConfigurationSource corsConfigurationSource(AdkWebCorsProperties corsProperties) {
4747
CorsConfiguration configuration = new CorsConfiguration();
4848

49-
configuration.setAllowedOrigins(corsProperties.origins());
49+
configuration.setAllowedOriginPatterns(corsProperties.origins());
5050
configuration.setAllowedMethods(corsProperties.methods());
5151
configuration.setAllowedHeaders(corsProperties.headers());
5252
configuration.setAllowCredentials(corsProperties.allowCredentials());

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public record AdkWebCorsProperties(
3737
origins =
3838
origins != null && !origins.isEmpty()
3939
? origins
40-
: List.of("http://localhost:8080", "http://127.0.0.1:8080");
40+
: List.of("http://localhost:[*]", "http://127.0.0.1:[*]");
4141
methods =
4242
methods != null && !methods.isEmpty()
4343
? methods

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,6 @@ public WebSocketConfig(
4242
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
4343
registry
4444
.addHandler(liveWebSocketHandler, "/run_live")
45-
.setAllowedOrigins(corsProperties.origins().toArray(new String[0]));
45+
.setAllowedOriginPatterns(corsProperties.origins().toArray(new String[0]));
4646
}
4747
}

0 commit comments

Comments
 (0)