當應用程序嘗試反映href屬性中的輸出時,可能會出現基於URL上下文的XSS。
<a href="DATA_REFLECTS_HERE">DATA_REFLECTS_HERE</a>
正如你可以看到相同的變量可以使用2 diffirent上下文。第一個是在href裏面,第二個是直接HTML上下文。
大多數命令XSS有效載荷(javascript:alert(1)等)和緩解可以在以下示例中找到。
/**
* XSS protection function for URL context
* @usecases
* <a href="use this function if output reflects here">click</a>
* <img src="use this function if output reflects here">
* <iframe src="use this function if output reflects here">
* @description
* Only allows URLs that start with http(s) or ftp. e.g.,
* https://www.google.com
* Protection against JavaScript, VBScript and Data URI JavaScript code execution etc.
* @author Ashar Javed
* @Link https://twitter.com/soaj1664ashar
* @demo http://xssplaygroundforfunandlearn.netai.net/final.html
*/
function urlContextCleaner($url) {
if(preg_match("#^(?:(?:https?|ftp):{1})\/\/[^\"\s\\\\]*.[^\"\s\\\\]*$#iu",(string)$url,$match))
{
return $match[0];
}
else {
$noxss='javascript:void(0)';
return $noxss;
}
}
從抓起:https://github.com/symphonycms/xssfilter/blob/master/lib/xss.php
旁邊,就可以解決這個問題,而不必太麻煩了。如果您知道url只能是http協議,那麼在href屬性中添加http(s)前綴內聯,並對所有類型的引號進行編碼(單,雙和反勾號)
對於HTML上下文,只需使用經典編碼方法。
我認爲這是因爲它是一個動態創建的URL,在將它傳遞給openURL之前,您並未進行驗證: – Tim 2014-11-24 12:05:57