△/SecureCoding
SQL 삽입 공격 : Hibernate
KkingKkang
2023. 2. 1. 15:50
728x90
- 질의문 생성 시 상수 문자열만 사용한다.
- 외부의 입력에 따라 질의문을 수정해야 한다면 인자를 받는 질의문을 상수 문자열로 생성한 후,
쿼리의 인자값을 setParameter(), set<타입이름>() 등의 메소드를 사용하여 설정한다.
안전하지 않은 코드 1 ▼
public void listHoney() {
Session session = new Configuration().configure().buildSessionFactory().openSession();
try {
Properties props = new Properties();
String fileName = "Hibernate.properties";
FileInputStream in = new FileInputStream(fileName);
props.load(in);
//외부로부터 입력을 받음
String idValue = props.getProperty("idLow");
Query query = session.createQuery("from Address a where a.name='" + idValue);
query.list();
}
catch(IOException e) {}
}
안전한 코드 ▼
public void listHoney() {
Session session = new Configuration().configure().buildSessionFactory().openSession();
try {
Properties props = new Properties();
String fileName = "Hibernate.properties";
FileInputStream in = new FileInputStream(fileName);
if (in == null || in.available() <= 0) return;
props.load(in);
//외부로부터 입력을 받는다.
String idValue = props.getProperty("idLow");
//입력값에 대한 유효성을 검사한다.
if(idValue == null || "".equals(idValue)) idValue = "defaultID";
//SQL query 문장을 작성한다.
Query query = session.createsSQLQuery("select h from Honey as h where h.id '= :idVal'");
query.setParameter("idVal",idValue);
query.list();
} catch(IOException e) {}
728x90