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;
}