- 외부 입력이 파일 시스템을 조작하는 경로를 직접 제어할 수 있거나 영향을 끼치면 위험하다.
- 외부의 입력을 통해 파일의 생성 및 접근을 허용하지 말고, 외부 입력에 따라 접근이 허용된 파일의 리스트에서 선택하도록 프로그램을 작성하는 것이 바람직하다.
안전하지 않은 코드 ▼
import java.io.*;
import java.net.URLDecoder;
import java.sql.*;
import java.util.*;
import javax.servlet.*;
public class DocumentService extends HttpServlet {
private final String APPLY_STYLE_COMMAND = "apply_style";
private final String USER_ID_PARAM = "user_id";
private final String STYLE_FILE_NAME_PARAM = "style_file_name";
private final int BUFFER_SIZE = 256;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String command = request.getParameter("command");
if (command.equals(APPLY_STYLE_COMMAND)) {
String userId = request.getParmeter(USER_ID_PARAM);
String styleFileName = request.getParameter(STYLE_FILE_NAME_PARAM);
String userHomePath = getUserHomeDir(userId);
byte [] buffer = new byte[BUFFER_SIZE];
FileInputStream inputStream = new FileInputStream(userHomePath + styleFileName);
inputStream.read(buffer);
}
}
}
안전한 코드 ▼
import java.io.*;
import java.net.URLDecoder;
import java.sql.*;
import java.util.*;
import javax.servlet.*;
public class DocumentService extends HttpServlet {
private final String APPLY_STYLE_COMMAND = "apply_style";
private final String USER_ID_PARAM = "user_id";
private final String STYLE_FILE_NAME_PARAM = "style_name";
private final int BUFFER_SIZE = 256;
private Hashtable<String, String> styleFileNames;
public DocummentService() {
styleFileNames = new Hashtable<String, String>();
styleFileNames.put("Normal","NormalStyle.cfg");
styleFileNames.put("Classic","ClassicStyle_1.cfg");
styleFileNames.put("Gothic","ClassicStyle_2.cfg");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String command = request.getParameter("command");
if (command.equals(APPLY_STYLE_COMMAND)) {
String userId = request.getParmeter(USER_ID_PARAM);
String styleName = request.getParameter(STYLE_NAME_PARAM);
String userHomePath = getUserHomeDir(userId);
String styleFilePath = userHomePath + styleFileNames.get(styleName);
byte [] buffer = new byte[BUFFER_SIZE];
FileInputStream inputStream = new FileInputStream(userHomePath + styleName);
inputStream.read(buffer);
}
}
}
- 외부의 입력(userId)으로 부터 값을 받은 후 먼저 그것을 이미 정해진 styleFileNames와 매칭시켜 실제적인 파일명으로 만들었다.
반응형
'Web Security > SecureCoding' 카테고리의 다른 글
정수 오버플로우 (0) | 2022.11.17 |
---|---|
HTTP 응답 분할 (0) | 2022.11.16 |
상대 디렉터리 경로 조작 (0) | 2022.11.16 |
크로스사이트 요청 위조 (0) | 2022.11.16 |
신뢰 되지 않는 URL 주소로 자동 접속 연결 (0) | 2022.11.15 |
댓글