프로그래밍/JAVA

Prevent Path Manipulation in Java

체크개발자 2020. 11. 30. 13:41

//Path manipulation 방지

  public static String FilePathFilter(String FilePath)
  {
     String result = FilePath;
        if(!"".equals(File))
     {
        FilePath= FilePath.replace('\\', '/'); // 시스템에 따라 file.separator 다르기때문에 java는 '/' 로 설정해야한다. 
        FilePath= FilePath.replaceAll("\\\\", "/"); 
        FilePath= FilePath.replaceAll("/[.]{1,2}", "");   //상위경로로 가는 것을 방지
        FilePath = FilePath.replaceAll("\\[.]{1,2}", ""); //상위경로로 가는 것을 방지
        result = FilePath.replaceAll("&", "");
       
     }
        return result;
  }