diff --git a/changelog/v0.2.21.md b/changelog/v0.2.21.md new file mode 100644 index 0000000..382d389 --- /dev/null +++ b/changelog/v0.2.21.md @@ -0,0 +1,8 @@ +# v0.2.21 + +Rate-limit sign-in attempts. + +## Fixed + +- Password login and first-run setup share the admin auth limiter, so repeated + failed attempts from one address get 429s instead of unlimited password guessing. diff --git a/path-gateway/pom.xml b/path-gateway/pom.xml index 99350ac..1646d74 100644 --- a/path-gateway/pom.xml +++ b/path-gateway/pom.xml @@ -7,7 +7,7 @@ io.github.an1ndra path-parent - 0.2.20 + 0.2.21 ../pom.xml diff --git a/path-gateway/src/main/java/com/anindra/path/admin/AdminAuthController.java b/path-gateway/src/main/java/com/anindra/path/admin/AdminAuthController.java index 3c5ac6a..31bbda3 100644 --- a/path-gateway/src/main/java/com/anindra/path/admin/AdminAuthController.java +++ b/path-gateway/src/main/java/com/anindra/path/admin/AdminAuthController.java @@ -28,6 +28,7 @@ public class AdminAuthController { private final GatewayProperties props; private final ObjectMapper mapper; private final AuditService audit; + private final com.anindra.path.config.AdminAuthLimiter authLimiter; public record Credentials(String username, String password) { } @@ -45,15 +46,37 @@ public Map status() { @PostMapping("/auth/setup") public Map setup(@RequestBody Credentials input, HttpServletRequest request) { - AdminAuthService.SessionResult result = auth.setup(input.username(), input.password()); - return sessionResponse(result, request); + limitAuthAttempts(request); + try { + AdminAuthService.SessionResult result = auth.setup(input.username(), input.password()); + authLimiter.onSuccess(request.getRemoteAddr()); + return sessionResponse(result, request); + } catch (RuntimeException e) { + authLimiter.onFailure(request.getRemoteAddr()); + throw e; + } } @PostMapping("/auth/login") public Map login(@RequestBody Credentials input, HttpServletRequest request) { - AdminAuthService.SessionResult result = auth.login(input.username(), input.password(), - request.getRemoteAddr(), request.getHeader("User-Agent")); - return sessionResponse(result, request); + limitAuthAttempts(request); + try { + AdminAuthService.SessionResult result = auth.login(input.username(), input.password(), + request.getRemoteAddr(), request.getHeader("User-Agent")); + authLimiter.onSuccess(request.getRemoteAddr()); + return sessionResponse(result, request); + } catch (RuntimeException e) { + authLimiter.onFailure(request.getRemoteAddr()); + throw e; + } + } + + private void limitAuthAttempts(HttpServletRequest request) { + long retry = authLimiter.retryAfterSeconds(request.getRemoteAddr()); + if (retry > 0) { + throw ApiException.rateLimited( + "Too many failed sign-in attempts. Retry in " + retry + "s.", (int) retry); + } } @GetMapping("/auth/me") diff --git a/path-spring-ai/pom.xml b/path-spring-ai/pom.xml index 1abbfd6..19c6413 100644 --- a/path-spring-ai/pom.xml +++ b/path-spring-ai/pom.xml @@ -7,7 +7,7 @@ io.github.an1ndra path-parent - 0.2.20 + 0.2.21 ../pom.xml diff --git a/pom.xml b/pom.xml index 760710a..794cabc 100644 --- a/pom.xml +++ b/pom.xml @@ -13,7 +13,7 @@ io.github.an1ndra path-parent - 0.2.20 + 0.2.21 pom path-parent Build parent for Path, the provider-agnostic LLM gateway, and its Spring AI adapter