2012-09-13 2 views
0

내 사이트에서 사용자 입력을 필터링하고 사용자가 자신의 게시물 (예 : youtube 및 vimeo)에 동영상을 추가하는 데 사용할 수있는 태그 만 사용하고 싶습니다.사용자 입력을 암호화하지만 iframe을 활성화하십시오.

첫 번째 아이디어는 strip_tags()을 사용하고 태그를 사용하는 것입니다. 하지만 나중에이 함수에 대한 기사를 읽었습니다. Article on Reddit

그래서 저는 XSS를 사용할 수 있으므로 사용하는 것은 좋지 않다고 생각합니다.

이 문제를 어떻게 해결할 수 있습니까?

업데이트 : 나는이 경우 과용되지 않을 해결책을 원합니다.

답변

1

HTML을 게시하는 대신 사용자가 입력 한 내용을 YouTube 비디오 링크로 검색 한 다음 코드에서 직접 연결할 수도 있습니다.

나는 오늘 아침에 StackOverflow에이 코드를 발견 How do I find all YouTube video ids in a string using a regex?

그것은 유튜브 URL의 문자열을 검색하고 링크로 대체합니다. 다음은 수정 된 <iframe/>

// Linkify youtube URLs which are not already links. 
// From https://stackoverflow.com/questions/5830387/php-regex-find-all-youtube-video-ids-in-string 
function linkifyYouTubeURLs($text) { 
    $text = preg_replace('~ 
     # Match non-linked youtube URL in the wild. (Rev:20111012) 
     https?://   # Required scheme. Either http or https. 
     (?:[0-9A-Z-]+\.)? # Optional subdomain. 
     (?:    # Group host alternatives. 
      youtu\.be/  # Either youtu.be, 
     | youtube\.com # or youtube.com followed by 
      \S*    # Allow anything up to VIDEO_ID, 
      [^\w\-\s]  # but char before ID is non-ID char. 
     )     # End host alternatives. 
     ([\w\-]{11})  # $1: VIDEO_ID is exactly 11 chars. 
     (?=[^\w\-]|$)  # Assert next char is non-ID or EOS. 
     [?=&+%\w-]*  # Consume any URL (query) remainder. 
     ~ix', 
     ' 

<iframe width="560" height="315" src="http://www.youtube.com/embed/$1"></iframe> 

', 
     $text); 
    return $text; 
} 

에 URL을 대체하는 코드의 버전 당신은 이런 식으로 구현할 수 있습니다 :

<?php 
$text = 'This is my comment. It contains an XSS attack!: 
<script type="text/javascript"> 
    alert(\'bam\'); 
</script> 

I learned about XSS on YouTube: 
http://www.youtube.com/watch?v=i38LMZyKIqI 
'; 

// Sanitize XSS (e.g.: convert '<' to '&lt;') 
$output = htmlspecialchars($text); 

$pattern = []; 

$output = linkifyYouTubeURLs($output); 

// Add natural line breaks 
$output = nl2br($output); 

echo $output; 

?> 

XSS 공격 중지,하지만 유튜브 링크 동영상으로 변환됩니다. Vimeo 및 다른 주요 비디오 공급자와 함께 사용하려면이 기능을 더 수정할 수도 있습니다.

http://codepad.viper-7.com/8w0h1F

0

이 작업을 시도 할 수 :

$out = preg_replace("#<(?!/?iframe[ >])#i","&lt;",$in); 

그러나 사용자가 XSS의 어떤 원인 iframe 태그에 이벤트 핸들러를 넣을 수있는 점에 유의하십시오.

+0

예, 나는이 작업을 수행 할 수 있습니다 여기에

행동의 코드입니다. 하지만 난 모든 XSS를 방지하려면 :) – warmspringwinds

관련 문제