Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HTML Entities are being decoded, possibly leading to html tags being returned #109

Open
Wertisdk opened this issue Feb 6, 2025 · 0 comments

Comments

@Wertisdk
Copy link

Wertisdk commented Feb 6, 2025

When passing a string with HTML Entities, the convert method will decode these and return them as-is.
This can lead to some unintentional behaviour if you have used htmlentities to encode text with legitimate html code converted to "string" / HTML Entities.

See this test:

use PHPUnit\Framework\TestCase;
use Soundasleep\Html2Text;

class Html2TextTest extends TestCase
{
  public function testKeepsHtmlEntities()
  {
      $html = '<p>Test <b>bold</b> &lt;script&gt;alert(&quot;nope!&quot;)&lt;/script&gt; <script>alert(\'text\')</script> <i>italic</i> <u>underline</u> <s>strikethrough</s> <a href="http://example.com">link</a></p>';
      $expected = "Test bold &lt;script&gt;alert(&quot;example!&quot;)&lt;/script&gt;  italic underline strikethrough [link](http://example.com)";
  
      $this->assertEquals($expected, Html2Text::convert($html));
  }
}

Output:

Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'Test bold &lt;script&gt;alert(&quot;example!&quot;)&lt;/script&gt;  italic underline strikethrough [link](http://example.com)'
+'Test bold <script>alert("nope!")</script>  italic underline strikethrough [link](http://example.com)'

I believe this is unintentional behaviour, because we end up with raw HTML code - the exact opposite of the intention when calling convert.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant