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
'△ > SecureCoding' 카테고리의 다른 글
JS) SQL 삽입 (0) | 2023.02.24 |
---|---|
보안결정을 신뢰할 수 없는 입력 값에 의존 (2) | 2023.02.01 |
안전하지 않은 리플렉션 (2) | 2023.02.01 |
JS) eval() 을 사용하면 안되는 이유 (0) | 2023.02.01 |
적절한 인증 없는 중요기능 허용 (0) | 2022.11.21 |
댓글