I have a php page that echoes something like this:
echo "<div>" . $_REQUEST["id"] . "</div>";
This leads to XSS issue, which i tried to fix using htmlpurifier through a function that cleans $_REQUEST by reference, leading to this code:
function sanitizer(array &array) {
foreach ($array as $key => $value) {
$array[$key] = htmlpurifierInstance->purify($value);
}
}
sanitizer($_REQUEST);
echo "<div>" . $_REQUEST["id"] . "</div>";
After another checkmarx test, the issue stills pops up, what's the fix to this issue?
Sanitising HTML should be a very rare requirement, not something you do regularly on all input.
htmlspecialchars. This is not something you can do ahead of time, because the same variable might be used in multiple contexts.Never, ever, try to write a "universal" sanitising or escaping function. At best, you will end up mangling data by applying too many things at once; at worst, you'll defeat your own security.