Skip to content

Instantly share code, notes, and snippets.

@antic183
Created December 8, 2017 09:21
Show Gist options
  • Save antic183/3de21f71d4e416a95791d86f1d5c7088 to your computer and use it in GitHub Desktop.
Save antic183/3de21f71d4e416a95791d86f1d5c7088 to your computer and use it in GitHub Desktop.
php htmlspecialchars is not enough to prevent an xss attack
<h1>php htmlspecialchars()</h1>
<h3>without END_QUOTES flag is an xss attack possible:</h3>
<?php
echo "<a href='" . htmlspecialchars("'onmouseover='a()'") . "'>link</a><br/>";
echo "<textarea style='overflow: none; width: 350px; border: none; resize: none; line-break: none;'>";
echo "<a href=''onmouseover='a()''>link</a>";
echo "</textarea><br/>";
echo "<a href='" . htmlspecialchars("'onmouseover='alert(123)'") . "'>link</a><br/>";
echo "<textarea style='overflow: none; width: 350px; border: none; resize: none; line-break: none;'>";
echo "<a href=''onmouseover='alert(123)''>link</a>";
echo "</textarea>";
?>
<h3>use the END_QUOTES flag for more security:</h3>
<?php
echo "<a href='" . htmlspecialchars("'onmouseover='a()'", ENT_QUOTES) . "'>link</a><br/>";
echo "<textarea style='overflow: none; width: 350px; border: none; resize: none; line-break: none;'>";
echo "<a href='&#039;onmouseover=&#039;a()&#039;'>link</a>";
echo "</textarea>";
?>
<script>function a() {alert('xss: execute internal function !');}</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment