goto_url($url) 보안 규정 준수

goto_url($url) 보안 규정 준수

QA

goto_url($url) 보안 규정 준수

본문


 
// 메타태그를 이용한 URL 이동
// header("location:URL") 을 대체
function goto_url($url)
{
    run_event('goto_url', $url);
    if (function_exists('safe_filter_url_host')) {
        $url = safe_filter_url_host($url);
    }
    $url = str_replace("&", "&", $url);
    //echo "<script> location.replace('$url'); </script>";
    if (!headers_sent())
        header('Location: '.$url);
    else {
        echo '<script>';
        echo 'location.replace("'.$url.'");';
        echo '</script>';
        echo '<noscript>';
        echo '<meta http-equiv="refresh" content="0;url='.$url.'" />';
        echo '</noscript>';
    }
    exit;
}

 

위의 코드가 에러가 나서 아래와 같이 바꿔주니 에러 없이 잘 작동합니다.
Warning: Header may not contain more than a single header, new line detected in

갑자기 왜 에러가 났죠? 
크롬 보안이나 php 보안 규정이 강화 되었나요?

 


 
function goto_url($url)
{
    run_event('goto_url', $url);
    if (function_exists('safe_filter_url_host')) {
        $url = safe_filter_url_host($url);
    }
    // 줄바꿈 문자 제거
    $url = str_replace(["\r", "\n", '%0A', '%0D'], '', $url);
    $url = str_replace("&", "&", $url);
    if (!headers_sent()) {
        header('Location: '.$url);
    } else {
        echo '<script>';
        echo 'location.replace("'.htmlspecialchars($url, ENT_QUOTES).'");';
        echo '</script>';
        echo '<noscript>';
        echo '<meta http-equiv="refresh" content="0;url='.htmlspecialchars($url, ENT_QUOTES).'" />';
        echo '</noscript>';
    }
    exit;
}
 

이 질문에 댓글 쓰기 :

답변 1

function goto_url($url)
{
    run_event('goto_url', $url);

    if (function_exists('safe_filter_url_host')) {
        $url = safe_filter_url_host($url);
    }

    // 줄바꿈 문자 및 앞뒤공백 불필요한 문자를 제거 및 XSS 공격방지
    $url = str_replace(["\r", "\n", "%0A", "%0D"], "", $url);
    $url = trim($url); 
    $url = htmlspecialchars($url, ENT_QUOTES);

    if (!headers_sent()) {
        header("Location: " . $url);
    } else {
        echo '<script>';
        echo 'location.replace("' . $url . '");';
        echo '</script>';
        echo '<noscript>';
        echo '<meta http-equiv="refresh" content="0;url=' . $url . '" />';
        echo '</noscript>';
    }

    exit;
}

function goto_url($url)
{
    // URL 이동을 기록하거나 관련된 처리를 수행
    run_event('goto_url', $url);

    // URL을 필터링하는 함수가 존재하면?
    if (function_exists('safe_filter_url_host')) {
        $url = safe_filter_url_host($url);
    }

    // 불필요한 문자 제거 및 XSS방지, 줄바꿈 문자 제거 (header() 오류 방지)
    $url = str_replace(["\r", "\n", "%0A", "%0D"], "", $url);
   
    // 앞뒤 공백 제거 및 방지
    $url = trim($url);

    // HTML 특수 문자 변환 및 XSS 공격 방지
    $url = htmlspecialchars($url, ENT_QUOTES);

    if (!headers_sent()) { // 헤더가 이미 출력되지 않았다면
        header("Location: " . $url);
    } else { // 만약 헤더가 이미 출력되었을 경우 스크립트를 사용한 URL 이동
        echo '<script>';
        echo 'location.replace("' . $url . '");';
        echo '</script>';
       
        // 스크립트 사용할 수 없는 환경에서는 HTML 메타 태그를 이용해 이동
        echo '<noscript>';
        echo '<meta http-equiv="refresh" content="0;url=' . $url . '" />';
        echo '</noscript>';
    }
    exit;
}

답변을 작성하시기 전에 로그인 해주세요.
전체 62,625
QA 내용 검색

회원로그인

(주)에스아이알소프트 / 대표:홍석명 / (06211) 서울특별시 강남구 역삼동 707-34 한신인터밸리24 서관 1404호 / E-Mail: admin@sir.kr
사업자등록번호: 217-81-36347 / 통신판매업신고번호:2014-서울강남-02098호 / 개인정보보호책임자:김민섭(minsup@sir.kr)
© SIRSOFT