TIP게시판

제목 XSS Clean 을 켜놓은 상태에서 POST 한글 텍스트가 깨질 때
글쓴이 JayT. 작성시각 2013/12/26 17:39:13
댓글 : 4 추천 : 0 스크랩 : 0 조회수 : 12458   RSS

안녕하세요.

작년쯤에 CI를 알게 되어서 사이트를 개발하고 여름쯤부터 운영하고 있습니다. 


게시판이 있는데 CK에디터를 달아서 쓰고 있었는데요.

얼마전부터 사이트에 XSS 필터를 켜놓았더니

POST 값으로 들어온 한글이 깨지는 경우가 발생하더라구요.

$this->input->post( '변수명', TRUE ); 로 받으면 한글이 중간중간 깨지는 ㅠ;


그리고 한글이 깨지게 되면 텍스트 데이터가 DB에 제대로 들어가질 않아서 <p> 태그만 남고 싹 사라지는 현상이 ...


인터넷 뒤져봐도 해결방법이 없는것 같고 해서

System 에서 XSS 필터링하는 Security 라이브러리를 찬찬히 뜯어보았죠...

( System/Core/Security.php )


xss_clean 함수에서 replace 하는 구문을 하나씩만 적용해봤더니..

604번줄에 _remove_evil_attributes 함수를 실행한 후에 한글이 깨지는 현상이 발생하더군요..

뭔가 태그에서 위험한 속성을 (예를들면 뭐 onClick 이런건가.. 암튼) 없애주는 코드인듯한데

거두절미하고 641번줄에 아래 부분이 실질적으로 악마속성(..)을 치환해주는 코드인데

패턴에 한글을 추가해봤습니다.

<?

// replace illegal attribute strings that are inside an html tag
if (count($attribs) > 0)
{
    $str = preg_replace('/(<?)(\/?[^><]+?)([^가-힣A-Za-z<>\-])(.*?)('.implode('|', $attribs).')(.*?)([\s><]?)([><]*)/i', '$1$2 $4$6$7$8', $str, -1, $count);
}

?>

가-힣 이라고 보이시죠?

패턴을 보니까 A-Za-z<>\- 는 제외하고... 뭐 이런 뜻인듯한데.. (제가 정규식을 정확히 몰라요 ㅠㅠ)

암튼 이걸 적용하고나니... 정상 작동하더군요 ㅎㅎ


혹시 저랑 비슷한 문제 겪는 분들... 참고하세요~~


 다음글 (왕 초보의 경험담)controller에서 타 cont... (3)
 이전글 템플릿 메소드 패턴을 이용한 레이아웃 (5)

댓글

케이든 / 2013/12/26 17:59:14 / 추천 0
 힇 아니고 힣 으로 하세요~
한대승(불의회상) / 2013/12/26 18:12:03 / 추천 0
좋은 정보 감사 합니다. ^^
JayT. / 2014/01/03 16:31:21 / 추천 0
가-힣 으로 수정했습니다 ㅎㅎ
힣 이 마지막 문자인가보네요.
/ 2014/02/17 18:20:01 / 추천 0
 좋은 정보 감사합니다.

/application/core/MY_Security.php 추가

코어 확장해서 수정 적용했습니다.

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 
class MY_Security extends CI_Security {
        function __construct() {
                parent::__construct();
        }
 
        protected function _remove_evil_attributes($str, $is_image) {
                // All javascript event handlers (e.g. onload, onclick, onmouseover), style, and xmlns
                $evil_attributes = array('on\w*', 'style', 'xmlns', 'formaction');
 
                if ($is_image === TRUE)
                {
                        /*
                         * Adobe Photoshop puts XML metadata into JFIF images, 
                         * including namespacing, so we have to allow this for images.
                         */
                        unset($evil_attributes[array_search('xmlns', $evil_attributes)]);
                }
 
                do {
                        $count = 0;
                        $attribs = array();
 
                        // find occurrences of illegal attribute strings with quotes (042 and 047 are octal quotes)
                        preg_match_all('/('.implode('|', $evil_attributes).')\s*=\s*(\042|\047)([^\\2]*?)(\\2)/is', $str, $matches, PREG_SET_ORDER);
 
                        foreach ($matches as $attr)
                        {
                                $attribs[] = preg_quote($attr[0], '/');
                        }
 
                        // find occurrences of illegal attribute strings without quotes
                        preg_match_all('/('.implode('|', $evil_attributes).')\s*=\s*([^\s>]*)/is', $str, $matches, PREG_SET_ORDER);
 
                        foreach ($matches as $attr)
                        {
                                $attribs[] = preg_quote($attr[0], '/');
                        }
 
                        // replace illegal attribute strings that are inside an html tag
                        if (count($attribs) > 0)
                        {
                                $str = preg_replace('/(<?)(\/?[^><]+?)([^가-힣A-Za-z<>\-])(.*?)('.implode('|', $attribs).')(.*?)([\s><]?)([><]*)/i', '$1$2 $4$6$7$8', $str, -1, $count);
                        }
 
                } while ($count);
 
                return $str;
        }
}