fix: 코드 리뷰 기반 보안·안정성 개선 (14건)
All checks were successful
Server CI/CD / deploy (push) Successful in 1m36s
All checks were successful
Server CI/CD / deploy (push) Successful in 1m36s
- unsafe 타입 단언 → safe assertion (chain handler 11곳, auth Logout) - Repository 에러 시 nil 반환으로 통일 (chain, auth, announcement) - string ID → uint 파싱으로 타입 안전성 확보 (auth, announcement) - CORS AllowHeaders에 Idempotency-Key, X-API-Key 추가 - /verify 엔드포인트 rate limiter 적용 - Redis 호출에 context timeout 적용 (auth, idempotency 미들웨어) - chain handler 에러 응답에서 내부 정보 노출 방지 - f.Close() 에러 검사 추가 (download service 2곳) - 공지사항 Delete 404 응답 추가 - 회원가입 롤백 시 Delete 에러 로깅 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -47,8 +47,10 @@ func Auth(c *fiber.Ctx) error {
|
||||
userID := uint(userIDFloat)
|
||||
|
||||
// Redis 세션 확인
|
||||
ctx, cancel := context.WithTimeout(context.Background(), redisTimeout)
|
||||
defer cancel()
|
||||
key := fmt.Sprintf("session:%d", userID)
|
||||
stored, err := database.RDB.Get(context.Background(), key).Result()
|
||||
stored, err := database.RDB.Get(ctx, key).Result()
|
||||
if err != nil || stored != tokenStr {
|
||||
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{"error": "만료되었거나 로그아웃된 세션입니다"})
|
||||
}
|
||||
|
||||
@@ -3,6 +3,8 @@ package middleware
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"a301_server/pkg/database"
|
||||
@@ -10,6 +12,7 @@ import (
|
||||
)
|
||||
|
||||
const idempotencyTTL = 10 * time.Minute
|
||||
const redisTimeout = 5 * time.Second
|
||||
|
||||
type cachedResponse struct {
|
||||
StatusCode int `json:"s"`
|
||||
@@ -24,8 +27,15 @@ func Idempotency(c *fiber.Ctx) error {
|
||||
return c.Next()
|
||||
}
|
||||
|
||||
redisKey := "idempotency:" + key
|
||||
ctx := context.Background()
|
||||
// userID가 있으면 키에 포함하여 사용자 간 캐시 충돌 방지
|
||||
redisKey := "idempotency:"
|
||||
if uid, ok := c.Locals("userID").(uint); ok {
|
||||
redisKey += fmt.Sprintf("u%d:", uid)
|
||||
}
|
||||
redisKey += key
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), redisTimeout)
|
||||
defer cancel()
|
||||
|
||||
// Check if this key was already processed
|
||||
cached, err := database.RDB.Get(ctx, redisKey).Bytes()
|
||||
@@ -48,7 +58,9 @@ func Idempotency(c *fiber.Ctx) error {
|
||||
if status >= 200 && status < 300 {
|
||||
cr := cachedResponse{StatusCode: status, Body: c.Response().Body()}
|
||||
if data, err := json.Marshal(cr); err == nil {
|
||||
database.RDB.Set(ctx, redisKey, data, idempotencyTTL)
|
||||
if err := database.RDB.Set(ctx, redisKey, data, idempotencyTTL).Err(); err != nil {
|
||||
log.Printf("WARNING: idempotency cache write failed (key=%s): %v", key, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user