Task 1-1: Security Hardening - Rate Limit & Code Validation
Phase: 1 - Security Priority: High Module:
authenticationDepends on: Không có Blocks: task-4-1 Reference:docs/BountyHunter-Backend/details/feature-authentication/SPEC.md
Background
generate-code không có rate limit (bị comment out) và validateCode() trong signIn() cũng bị bỏ qua. Cần review và re-enable để tăng bảo mật.
Tasks
1. Re-enable rate limit cho generate-code
File: authentication/controllers/auth/AuthController.java
DI / Import note:
@RateLimitlà custom annotation được định nghĩa tạiinfrastructures/redis/annotation/RateLimit.java. Cần đảm bảo class này (và AOP aspect đi kèm) đã được include trong classpath của moduleauthentication. Import:import com.figpop.infrastructures.redis.annotation.RateLimit;
- [ ] Uncomment annotation
@RateLimit:// Trước: // @RateLimit(windowSeconds = 5, maxRequests = 1) // Sau: @RateLimit(windowSeconds = 5, maxRequests = 1) @PostMapping("/generate-code") public Result<CodeLoginResponse> generateCodeWithWeb3Auth(...) - [ ] Kiểm tra
RateLimitimplementation có dùng Redis không (consistent với multiple instances) - [ ] Test: 2 request trong 5 giây → lần 2 phải nhận 429
2. Review validateCode() trong signIn()
File: authentication/controllers/auth/AuthService.java
DI / Context note:
validateCode()là private method của chínhAuthService. Nó truy cậpuser.getAuthCode()vàuser.getExpireAuthCode()từUserModel. Đảm bảoUserModelcó các getter này trước khi re-enable. Logic chi tiết xem task-1-2.
- [ ] Xác nhận lý do tại sao
validateCode()bị comment out:// Dòng bị comment: // this.validateCode(user, baseMap.get("code")); - [ ] Nếu intentionally disabled → document lý do rõ ràng trong code comment
- [ ] Nếu unintentionally → re-enable và test flow
generate-code→sign-in
3. Guard create-account-fake endpoint
File: authentication/controllers/auth/AuthController.java
DI / Import note:
@Profilelà annotation Spring chuẩn, importorg.springframework.context.annotation.Profile. Không cần inject thêm bean — annotation này được xử lý bởi Spring container khi load context.
- [ ] Thêm profile guard:
@Profile("!main") @PostMapping("/create-account-fake") public void createAccountFake(@Valid @RequestBody BaseMap baseMap) { ... }
Verification / Acceptance Criteria
- [ ] Compile check:
AuthController.javavàAuthService.javacompile thành công sau khi uncomment — không có lỗi import thiếu (RateLimit,Profile). - [ ] Rate limit: Gửi 2 request
POST /api/auth/generate-codetrong vòng 5 giây → request thứ 2 nhận HTTP 429. - [ ] Code validation:
POST /api/auth/sign-invới code sai → nhậnINVALID_CODE; với code đúng nhưng đã expired → nhậnCODE_EXPIRED. - [ ] Profile guard: Endpoint
POST /api/auth/create-account-fakekhông khả dụng khispring.profiles.active=main(trả 404 hoặc không route). - [ ] Integration test: Các scenario trong task-4-1 liên quan đến rate limit và invalid code pass sau khi thay đổi.
Files to Modify
authentication/src/main/java/com/figpop/authentication/controllers/auth/AuthController.javaauthentication/src/main/java/com/figpop/authentication/controllers/auth/AuthService.java
Notes
- Rate limit implementation:
infrastructures/redis/annotation/RateLimit.java validateCode()logic: comparecodefield + checkexpireAuthCodeinstant