Programming/기타

SQL Injection 에 대한 대응

armyost 2023. 10. 13. 10:42
728x90

공통 

API 서버를 개발할때 Request로 넘어오는 Parameter의 자료형, 데이터크기, 데이터 형식을 사전에 Exception처리를 할 수 있도록 해야 좋다.

데이터형식은 정규화 할수 있다면 표현식을 사용하면 좋다. 

예) 

if (stringValue != null && stringValue.matches("^[a-zA-Z]{2}[0-9]{3}$")) {
            System.out.println(stringValue + " is a ProductCode.");
            return "MessageCode";
} else if (stringValue != null && stringValue.matches("^[a-zA-Z]{2}-[a-zA-Z0-9]*-[a-zA-Z0-9]*$")) {
            System.out.println(stringValue + " is a ProductCode.");
            return "MessageCode";
} else {
            System.out.println(stringValue + " is not a ProductCode. So replace it to CM000");
            return "Not a MessageCode";
}

 

JAVA

- JPA : JPA에서 제공하는 API를 사용하면 안전하다.

- Mybatis : #{} : 내부적으로 PreparedStatement를 사용하기 때문에 sql injection 공격에 안전하다. 쿼리와 입력값 분리한다 (문자열 형태로 들어와서 파라미터 형태가 됨)

 

Python

- Django, Flask ORM API 사용 :  제공하는 API를 사용하면 안전하다.

- SQLAlchemy + QueryString: SQLAlchemy에서는 바인딩 변수로 접근하여 별도의 문자열 형태로 들어와서 SQL Injection 공격에 안전하다.