일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 함수포함
- Unix 타임스탬프
- 평균의종말
- 심리학으로 읽는 그리스 신화
- 미시적동기
- 태양의 신
- 아주 작은 습관의 힘
- 중복삽입
- 토드로즈
- 전자정부프레임워크
- 날씨API
- 죽음1
- pem인증
- 개개인성
- 오기 오가스
- @RestController
- 트렌젝션분기
- 제임스 클리어
- 성공
- 자바
- 스마트에디터 0바이트
- 독서
- 칩히스 댄히스
- mysql
- 순간의 힘
- 보안성검토
- 가브리엘 웰즈
- 서평
- 토드 로즈
- 그리스로마 신화
Archives
- Today
- Total
삽질 하자! 파는만큼 보인다
[JAVA] 첨부파일 등록시 확장자를 변조하여 등록했을때 체크방법 본문
보안성검토에서 체크가 된 내역중 파일 확장자를 변조하여 등록 할 수 있는 보안성 문제가 발생하였다.
예를 들어 bat파일을 png파일로 확장자를 변형하여 파일 등록처리를 하는 문제였다.
if ("xls".equalsIgnoreCase(fileExt)
|| "xlsx".equalsIgnoreCase(fileExt)
|| "doc".equalsIgnoreCase(fileExt)
|| "docx".equalsIgnoreCase(fileExt)
|| "hwp".equalsIgnoreCase(fileExt)
|| "hwpx".equalsIgnoreCase(fileExt)
|| "ppt".equalsIgnoreCase(fileExt)
|| "pptx".equalsIgnoreCase(fileExt)
|| "txt".equalsIgnoreCase(fileExt)
|| "gif".equalsIgnoreCase(fileExt)
|| "jpg".equalsIgnoreCase(fileExt)
|| "jpeg".equalsIgnoreCase(fileExt)
|| "png".equalsIgnoreCase(fileExt)
|| "zip".equalsIgnoreCase(fileExt)
|| "bmp".equalsIgnoreCase(fileExt)
|| "pdf".equalsIgnoreCase(fileExt)) {
//확장자가 이미지 파일일경우
if(fileExt.equalsIgnoreCase("jpg") || fileExt.equalsIgnoreCase("jpeg") || fileExt.equalsIgnoreCase("png")
|| fileExt.equalsIgnoreCase("gif") || fileExt.equalsIgnoreCase("bmp")) {
Boolean checkResult = imageCheck(file,fileExt);
if(checkResult==false) {
continue;
}
}
위의 이미지는 파일 등록시 코드의 일부이다.
등록가능한 확장자를 if문으로 체크하고
다음 if문으로 이미지 파일인 경우에만 파일을 추가하는 로직을 구성했다.
imageCheck라는 메서드를 구현하여 이미지인지 확인하는 처리를 했다.
imageCheck 메서드는 해당 파일이 이미지일 경우 true를 반환하고 아닐경우는 false를 반환한다.
키포인트는 이미지파일에는 width, height 길이가 존재하기 때문에 해당 값을 체크하여
온전한 이미지 파일인지 파악이 가능하다.
public boolean imageCheck(MultipartFile file, String fileExt) throws Exception{
// 이미지 파일의 InputStream 가져오기
InputStream inputStream = file.getInputStream();
BufferedImage image = null;
try {
image = ImageIO.read(inputStream);
} catch (Exception e) {
// 이미지 파일이 아닌 경우 처리
e.printStackTrace();
return false;
}
if (image == null) {
return false;
}
int width = image.getWidth();
int height = image.getHeight();
if (width != 0 && height != 0) {
// 이미지 파일인 경우
return true;
}else {
// 이미지 파일이 아닌 경우
return false;
}
}
'JAVA' 카테고리의 다른 글
[전자정부] 버전이 업데이트시(3.9->3.10) 스마트 에디터 0바이트 문제 (0) | 2023.11.27 |
---|---|
[전자정부] RestController에 컬렉션 반환 오류 (0) | 2023.11.27 |
개발 블로그의 시작 (0) | 2023.11.27 |