-
Notifications
You must be signed in to change notification settings - Fork 0
Feat: 파일 업로드 보안 강화 #242
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Feat: 파일 업로드 보안 강화 #242
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2efb522
[#241] Feat: ImageValidator 허용 포맷 축소(JPEG/PNG) 및 ImageIO 파싱 검증 추가
jjh75607 ae86fb9
[#241] Feat: UPLOAD_RATE_LIMIT_EXCEEDED ErrorType 추가
jjh75607 be929ec
[#241] Feat: Redis 기반 사용자별 업로드 Rate Limit 구현
jjh75607 bf688b9
[#241] Feat: 업로드 엔드포인트에 Rate Limit 적용 및 설정 추가
jjh75607 08f2a6e
[#241] Refactor: 코드 리뷰 반영 — TTL 방어 처리, RuntimeException 핸들링, BDD 주석 추가
jjh75607 8543dd2
[#241] Refactor: 테스트 BDD 구조 보완 — 성공 케이스 주석을 // when & then으로 수정
jjh75607 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
src/main/java/soon/fridgely/global/security/ratelimit/UploadRateLimit.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| package soon.fridgely.global.security.ratelimit; | ||
|
|
||
| import java.lang.annotation.ElementType; | ||
| import java.lang.annotation.Retention; | ||
| import java.lang.annotation.RetentionPolicy; | ||
| import java.lang.annotation.Target; | ||
|
|
||
| @Target(ElementType.METHOD) | ||
| @Retention(RetentionPolicy.RUNTIME) | ||
| public @interface UploadRateLimit { | ||
| } |
67 changes: 67 additions & 0 deletions
67
src/main/java/soon/fridgely/global/security/ratelimit/UploadRateLimitAspect.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| package soon.fridgely.global.security.ratelimit; | ||
|
|
||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.aspectj.lang.annotation.Aspect; | ||
| import org.aspectj.lang.annotation.Before; | ||
| import org.springframework.beans.factory.annotation.Value; | ||
| import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
| import org.springframework.data.redis.core.StringRedisTemplate; | ||
| import org.springframework.security.authentication.AnonymousAuthenticationToken; | ||
| import org.springframework.security.core.Authentication; | ||
| import org.springframework.security.core.context.SecurityContextHolder; | ||
| import org.springframework.stereotype.Component; | ||
| import soon.fridgely.global.support.exception.CoreException; | ||
| import soon.fridgely.global.support.exception.ErrorType; | ||
| import soon.fridgely.global.support.logging.SlackMarkers; | ||
|
|
||
| import java.time.Duration; | ||
|
|
||
| @Slf4j | ||
| @RequiredArgsConstructor | ||
| @Aspect | ||
| @Component | ||
| @ConditionalOnProperty(name = "spring.cache.type", havingValue = "redis") | ||
| public class UploadRateLimitAspect { | ||
|
|
||
| private final StringRedisTemplate stringRedisTemplate; | ||
|
|
||
| @Value("${upload.rate-limit.max-requests}") | ||
| private int maxRequests; | ||
|
|
||
| @Value("${upload.rate-limit.period-seconds}") | ||
| private long periodSeconds; | ||
|
|
||
| @Before("@annotation(soon.fridgely.global.security.ratelimit.UploadRateLimit)") | ||
| public void checkRateLimit() { | ||
| Long userId = extractUserId(); | ||
| String key = "upload:ratelimit:" + userId; | ||
|
|
||
| Long count = stringRedisTemplate.opsForValue().increment(key); | ||
| Long ttl = stringRedisTemplate.getExpire(key); | ||
| if (ttl != null && ttl == -1L) { | ||
| stringRedisTemplate.expire(key, Duration.ofSeconds(periodSeconds)); | ||
| } | ||
| if (count != null && count > maxRequests) { | ||
| log.warn(SlackMarkers.SYSTEM, "[UploadRateLimitAspect] 업로드 Rate Limit 초과. (UserId={}, Count={})", userId, count); | ||
| throw new CoreException(ErrorType.UPLOAD_RATE_LIMIT_EXCEEDED); | ||
| } | ||
| } | ||
|
|
||
| private Long extractUserId() { | ||
| Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); | ||
| if (authentication == null || !authentication.isAuthenticated() | ||
| || authentication instanceof AnonymousAuthenticationToken) { | ||
| throw new CoreException(ErrorType.AUTHENTICATION_FAILED); | ||
| } | ||
| Object principal = authentication.getPrincipal(); | ||
| if (principal instanceof String str) { | ||
| try { | ||
| return Long.parseLong(str); | ||
| } catch (NumberFormatException e) { | ||
| throw new CoreException(ErrorType.AUTHENTICATION_FAILED); | ||
| } | ||
| } | ||
| throw new CoreException(ErrorType.AUTHENTICATION_FAILED); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.