본문 바로가기

Programming/Java

다운로드 가능 파일 및 경로 확인

웹 취약점, 모의해킹 시 빈번히 나오는 권한 없는 디렉토리 내 파일 다운로드 가능한 취약점을 방어하기 위한 코드

 public static boolean checkFilePath(String filePath) {
  
  Boolean checkResult = true;
  
  //다운로드 허용할 디렉토리 설정
  String allowPath = "/test/";
  //금지 문자열 리스트
  String blockchar[] = {"..", "../", "..\\"};
  
  for(int i=0; i%lt;blockchar.length;i++) {
   if( filePath.indexOf(blockchar[i]) != -1 ){
    checkResult = false;
   }
  }
  
  //다운로드 허용된 디렉토리인지 체크
  String path = filePath.substring (0, filePath.lastIndexOf("/") + 1).toLowerCase();
  if(path.indexOf(allowPath) %lt;= -1 ){
      checkResult = false;
  }
  
  return checkResult;
 }