Skip to content

Task 1-1: Security Hardening - Rate Limit & Code Validation

Phase: 1 - Security Priority: High Module: authentication Depends 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: @RateLimit là custom annotation được định nghĩa tại infrastructures/redis/annotation/RateLimit.java. Cần đảm bảo class này (và AOP aspect đi kèm) đã được include trong classpath của module authentication. 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 RateLimit implementation 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ính AuthService. Nó truy cập user.getAuthCode()user.getExpireAuthCode() từ UserModel. Đảm bảo UserModel có 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-codesign-in

3. Guard create-account-fake endpoint

File: authentication/controllers/auth/AuthController.java

DI / Import note: @Profile là annotation Spring chuẩn, import org.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.javaAuthService.java compile 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-code trong vòng 5 giây → request thứ 2 nhận HTTP 429.
  • [ ] Code validation: POST /api/auth/sign-in với code sai → nhận INVALID_CODE; với code đúng nhưng đã expired → nhận CODE_EXPIRED.
  • [ ] Profile guard: Endpoint POST /api/auth/create-account-fake không khả dụng khi spring.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.java
  • authentication/src/main/java/com/figpop/authentication/controllers/auth/AuthService.java

Notes

  • Rate limit implementation: infrastructures/redis/annotation/RateLimit.java
  • validateCode() logic: compare code field + check expireAuthCode instant