TransWikia.com

Is XSS possible when using htmlspecialchars and https prefix check in href?

Information Security Asked by Awaaaaarghhh on January 12, 2021

There is a standard XSS exploitation technique where one can use javascript keyword in <a href=""> to execute javascript code. Example:

<a href="javascript:alert(42);">please clickme</a>

Let’s us consider PHP code which uses htmlspecialchars with combination of a prefix check, where the URL must always start with https://. Is there any way to exploit it in modern browsers like firefox, chrome?

<?php
// PHP 7
$url = htmlspecialchars($_GET["url"] ?? "", ENT_QUOTES);
$text = htmlspecialchars($_GET["text"] ?? "", ENT_QUOTES);
if ((substr($url, 0, 8) !== "https://")) {
    $url = "https://" . $url;
}
?>
<a href="<?= $url; >"><?= $text; ></a>

I know that there is a open redirect vulnerability. I tried things like https://@javascript://://alert(); but it seems that google chrome blocks it (medium.com 2017: Say goodbye to URLs with embedded credentials). [This question is not about securing that code – this question is solely about exploiting.]

Here are more examples from the past:

One Answer

When using htmlspecialchars, all HTML tags will be escaped.

So let's say you type in <a href="https://hello">hello</a>.

In source:

  • Without htmlspecialchars: <a href="https://hello">hello</a>
  • With htmlspecialchars ENT_QUOTES: &lt;a href=&quot;https://hello&quot;&gt;&lt;/a&gt;.

In browser:

  • Without htmlspecialchars: hello
  • With htmlspecialchars: <a href="https://hello">hello</a>

Answered by JNic on January 12, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP