Ik wil graag voorkomen dat er javascript d.m.v. UBB in mijn website terecht komt. In een link kan dit op verschillende manieren. 2 voorbeelden zijn
- <a href="javascipt:alert('hoi')">link</a>
- <a href="<script>alert('hoi');</script>">link</a>
Bestaat er niet een mogelijkheid om te filtreren op javascript en de script-tags. Volgens mij is dat in mijn project gemakkelijker te implementeren.
Ik kwam dit probleem tegen toen ik op phpFreakz de hacking guide doornam(eerder genoemd in deze topic). In deze tutorial wordt ook een voorstel gedaan voor een functie, die dit probleem zou oplossen. Alleen werkt deze functie bij mij niet.
<?php
/* noXSS (a)
* String a: Input that you want to print in a
*
* Description
* Use this function if you want to print user input as an argument in a HTML
* tag. Don't allow XSS JS attacks in your webapplication and use this function
* instead of using htmlspecialchars(), which doesn't protect you against the
* javascript: protocol.
*
* Return
* It will return false if
* the beginning of the input string contains 'javascript:', otherwise it will
* return
*
* Examples
* <?php
* print '<a href="'.$_GET['input'].'>link</a>';
* ?>
*
* input: 'javascript:alert()' output: bool false
* input: 'JAVAscript:alert()' output: bool false
* input: ' javascript:alert()' output: bool false
* input: ' javascript:alert()' output: bool false
* input: 'javascript:alert()' output: bool false
* input: ' Javascript:alert()' output: bool false
* input: 'test <a href="#">' output: test <a href="#">yo
* input: '"><script>alert()</script><"' output: "><script>alert()
</script><"
* input: "'><script>alert()</script><'" output: '><script>alert()
</script><'
*/
function noXSS ($input)
{
// convert all input charakters to lowercase and convert ascii encoded charakters to normal charakters
$inputTmp = trim(asciiDecode(strtolower($input)));
// check if the user wants to execute javascript
if (substr($inputTmp, 0, 11) == 'javascript:')
{
trigger_error("Someone tried to exploit our UBB system! Original input: '$input'", E_USER_WARNING);
return false;
}
// replace: <, >, &, ", ' to ascii encoded values
return htmlspecialchars($input, ENT_QUOTES);
}
/* asciiDecode (a)
* String a: Text string
*
* Description
* All ASCII codes will be decoded in their represented value.
*/
function asciiDecode($input)
{
// get all ASCII encoded values
preg_match_all("(&#([0-9]{1,3});)", $input, $matches);
$asciiCodes = array_unique($matches[1]);
// replace them with their representing value
foreach ($asciiCodes as $asciiNr)
$input = str_replace("&#$asciiNr;", chr($asciiNr), $input);
return $input;
}
?>
<?php
/* noXSS (a)
* String a: Input that you want to print in a
*
* Description
* Use this function if you want to print user input as an argument in a HTML
* tag. Don't allow XSS JS attacks in your webapplication and use this function
* instead of using htmlspecialchars(), which doesn't protect you against the
* javascript: protocol.
*
* Return
* It will return false if
* the beginning of the input string contains 'javascript:', otherwise it will