CI 코드

제목 SELECT() 사용 시 문제점
글쓴이 마냐 작성시각 2010/09/16 21:50:05
댓글 : 2 추천 : 0 스크랩 : 0 조회수 : 17269   RSS

° 관련링크 : http://codeigniter.com/forums/viewthread/166881
http://bitbucket.org/ellislab/codeigniter/issue/112/database-class-active-record-select-method-2nd-parameter
http://codeigniter.com/forums/viewthread/71886

>> 원인
$this->db->select(“QUERY”, FALSE);
select 쿼리에서 두번째 파라미터로 FALSE를 주는 경우

>> 문제
해당 select 쿼리가 발생한 시점부터 이후 쿼리에 대한 테이블, 필드에 대한 식별자 보호 Protecting identifiers가 적용되지 않습니다.
http://codeigniter-kr.org/user_guide/database/queries.html

- 문제 코드

DB_active_rec.php - 78 line
$this->_protect_identifiers = $escape;

>> 해결
DB_active_rec.php 수정
1) 75 ~ 79 line 삭제 
2) 92 line
$this->ar_select[] = $val;
=>
$this->ar_select[] = $this->_protect_identifiers($val, FALSE, $escape);

3) 208 line
$this->ar_select[] = $sql;
=>
$this->ar_select[] = $this->_protect_identifiers($sql);

4) 1504 ~ 1510 line 삭제

----

$this->output->enable_profiler(true);
위 문제가 동일하게 일어나는지 테스트 부탁드립니다.

 다음글 듬직이님의 헬퍼 ip 부분 추가.
 이전글 CI의 사용자 인증 소스파일 (3)

댓글

변종원(웅파) / 2010/09/17 16:57:04 / 추천 0
false로 주게되면 ci core에서 자동으로 체크해줬던 sql injection 방어가 적용이 안되서
수동으로 쿼리내용에 대한 체크를 해줘야 합니다.
대신 말씀하신 경우처럼 어쩔수 없이 mysql함수를 select에 써야할 경우엔 기술하신대로 false로
사용하시면 됩니다. ^^

select와는 조금 경우가 다르지만 where의 경우에는 조금 더 자유도가 있습니다.

$arr = array('scene_start_time <=' => $this->uri->segment(9), 'time+scene_start_time >=' => $this->uri->segment(11));
$this->db->where('c.clip_no', $this->uri->segment(7));
$this->db->where($arr, '', false);
첫번째줄 배열의 두번째 선언을 보시면 +가 있습니다. 이거 처리하려고
세번째 줄에서 false로 선언. false 선언 안하면 에러 납니다. ^^
마냐 / 2010/09/17 17:12:47 / 추천 0

답변 감사합니다.

WHERE() 의 경우 문제가 없지만
SELECT("QUERY", FALSE) 를 실행할 경우

DB_active_rec.php - 78 line
$this->_protect_identifiers = $escape;
$escape 값을 $this에 직접 적용하는데 이후 초기화하는 부분이 없습니다.
그래서 해당 페이지에서 쿼리가 실행되는 동안 $this->_protect_identifiers 값이 FALSE로 고정됩니다.

그래서 http://codeigniter.com/forums/viewthread/166881 처럼
SELECT("QUERY", FALSE) 가 실행되는 시점부터
order_by 및 이후 실행되는 쿼리에 대한 테이블 및 필드에 대한 식별자 보호 함수가 적용되지 않습니다.

1.7.2 버전이며 버그로 판단되기는 한데
검색해봐도 관련글이 거의 없어서 정확한지는 모르겠습니다. -_-a