-
Notifications
You must be signed in to change notification settings - Fork 0
링크 생성 시 OG 이미지 S3 업로드 및 중복 방지 구현 #165
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
링크 생성 시 OG 이미지 S3 업로드 및 중복 방지 구현 #165
Conversation
5c37e5d to
e3d8696
Compare
📊 코드 커버리지 리포트
|
Goder-0
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ckdals4600 @minibr
지금 현 구조상으로 보면, front로 부터 링크 생성시에 imageUrl을 받는데요.
해당 구조의 경우, SSRF 리스크가 존재합니다.
og 크롤링시에 캐싱을 해두고, 그걸 접근하도록 하는 것은 어떻게 생각하시나요?
추후, 개선사항으로 남겨두는 것 또한 좋다고 생각합니다.
의견 공유 부탁드립니다.
src/main/java/com/sofa/linkiving/domain/link/abstraction/ImageUploader.java
Show resolved
Hide resolved
좋은 지적 감사합니다! 말씀해주신 대로 클라이언트로부터 임의의 URL을 받아 서버에서 요청을 보내게 될 경우 SSRF 보안 취약점이 발생할 수 있고, 원본 사이트의 정책에 따라 이미지 로딩이 차단되거나 원본이 삭제될 위험도 있다고 판단됩니다. 서비스의 안정성과 보안을 위해 구조 개선이 필요할 것 같습니다. |
e3d8696 to
6b76ab3
Compare
이슈 생성 부탁드립니다. 리베이스 요청드립니다. |
4006eef to
50af5c9
Compare
|
@Goder-0 리베이스 완료하였습니다 |
50af5c9 to
3f1f7cd
Compare
관련 이슈
PR 설명
작업 내용
1. AWS 리소스 및 환경 설정
PutObject,GetObject)을 가진 IAM 사용자 설정 완료.application.yml에 AWS 관련 프로퍼티(AccessKey, SecretKey, Bucket, Region) 추가 및 환경 변수 분리 적용.spring-cloud-starter-awsspring-cloud-aws-dependencieorg.apache.httpcomponents:httpclient:Spring Cloud AWS가 S3와 통신하기 위한 라이브러리2.
UrlConnectionFactory인터페이스 분리S3ImageUploader내부에서new URL(url).openConnection()을 직접 호출.UrlConnectionFactory인터페이스를 통해 연결 객체를 생성하도록 분리.3. S3 이미지 업로더 구현 (
S3ImageUploader)리소스 최적화와 안정성을 고려하여 로직 설계
고유 키 생성 전략 (
generateUniqueKeyFromUrl):대신UUID.nameUUIDFromBytes(url.getBytes())` 사용jpg를 기본값으로 적용하여 파일명 안정성 확보S3 URL 구성 (
buildS3Url):getUrl메서드 등을 호출하는 대신,https://{bucket}.s3.{region}.amazonaws.com/{key}형식의 표준 문자열을 직접 포맷팅하여 반환함으로써 불필요한 연산 감소중복 제거 및 캐싱 (Deduplication):
s3Template.objectExists()를 호출S3 URL을 즉시 반환.유효성 검사 및 스트리밍:
URLConnection에 **Connection/Read Timeout(3초)**을 설정하여 스레드 차단 방지Content-Type이image/*가 아닌 경우 업로드를 거부하고 기본 이미지 반환InputStream을 사용하여 파일을 메모리에 온전히 적재하지 않고 스트림 방식으로 업로드하여 메모리 효율을 향상 시킴Soft Fail (Fallback):
4. 비즈니스 로직 적용 (
LinkFacade)createLink메서드 내에서linkService호출 전imageUploader.uploadFromUrl(imageUrl)을 수행하도록 흐름 추가테스트 작성 (Test Coverage)
1. Integration Test (
LinkApiIntegrationTest)given(imageUploader...).willReturn(...))을 통해 S3 URL이 DB에 정상 저장되는지 확인.2. Unit Test (
S3ImageUploaderTest)S3Template과UrlConnectionFactory를 Mock 객체로 주입받아 테스트 수행.Content-Type이application/pdf등일 때 업로드를 거부하고 원본 URL을 반환하는지 확인.3. Facade Test (
LinkFacadeTest)createLink호출 시imageUploader->linkService순서로 호출되는지 검증.