Skip to content

Fix non-ASCII characters in scripts of fetched HTML pages - #9384

Merged
Alkarex merged 7 commits into
FreshRSS:edgefrom
shcheglovnd:fix-html-script-utf8
Oct 31, 2026
Merged

Alkarex merged 7 commits into
FreshRSS:edgefrom
shcheglovnd:fix-html-script-utf8

Conversation

@shcheglovnd

Copy link
Copy Markdown
Contributor

Changes proposed in this pull request:

  • Fix non-ASCII characters turning into HTML entities inside <script> of fetched HTML pages, e.g. titles of an HTML + XPath + JSON feed showing Wall Street&rsquo;s instead of Wall Street’s.
  • Cause: to add a <base>, enforceHtmlBase() re-serialises the page with libxml2 saveHTML(). For a whole document, libxml2 (2.9.14 in our Debian image) only knows the charset from <meta http-equiv="Content-Type">. With the more common <meta charset="UTF-8">, or with the byte order mark that enforceHttpEncoding() adds, it writes every non-ASCII character as an entity. That is harmless in text, but inside <script> entities are not decoded, so the JSON embedded in the page (e.g. Next.js __NEXT_DATA__) is changed.
  • UTF-8 documents are now serialised node by node, which writes UTF-8 (and keeps the byte order mark). Other charsets keep the previous output.

How to test the feature manually:

  1. Add a feed of type HTML + XPath + JSON (dot notation) for https://www.wsj.com/newsletters/the-10-point, with XPath to JSON //script[@id='__NEXT_DATA__'], item 0.props.pageProps.recentEditionArticles, title headline, URI articleUrl.
  2. Before: titles with an apostrophe show &rsquo;. After: ’.
  3. The same page as an HTML + XPath feed (titles from img/@alt) shows ’ before and after.

Tested on the 1.30.0 Docker image (PHP 8.4.24, libxml2 2.9.14) with the page above, for both feed types, before and after the change. The new unit test fails without the change for <meta charset> and for a byte order mark, and passes with it; the whole test suite, PHPStan and PHPCS pass.

Pull request checklist:

  • clear commit messages
  • code manually tested
  • unit tests written (optional if too hard)
  • documentation updated

Additional information can be found in the documentation.

To add a <base> to a fetched HTML page, enforceHtmlBase() re-serialises it
with libxml2 saveHTML(). For a whole document, libxml2 2.9 only knows the
charset from <meta http-equiv="Content-Type">: with <meta charset="UTF-8">
or a byte order mark, it writes every non-ASCII character as an HTML entity.
In text that is harmless, but inside <script> entities are not decoded, so
e.g. the JSON read by HTML + XPath + JSON feeds gets "Wall Street&rsquo;s"
instead of "Wall Street’s", which then shows in titles.

UTF-8 documents are now serialised node by node, which writes UTF-8.
Other charsets keep the previous output.
@shcheglovnd
shcheglovnd marked this pull request as ready for review October 2, 2026 02:01
Comment thread app/Utils/httpUtil.php Resolved
@Alkarex Alkarex added this to the 1.31.0 milestone Oct 2, 2026
@shcheglovnd
shcheglovnd requested a review from Frenzie October 2, 2026 23:07
Comment thread app/Utils/httpUtil.php Resolved
@shcheglovnd
shcheglovnd requested a review from Frenzie October 10, 2026 01:55

@Frenzie Frenzie left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems alright.

@shcheglovnd

Copy link
Copy Markdown
Contributor Author

Sorry for the delay with PR, I am going to update and merge it today

@shcheglovnd

Copy link
Copy Markdown
Contributor Author

Oh, right, the merge is only possible by the maintainer. @Alkarex ptal when you have time. Thanks.

@Alkarex
Alkarex merged commit acde1bf into FreshRSS:edge Oct 31, 2026
4 checks passed
@Alkarex

Alkarex commented Oct 31, 2026

Copy link
Copy Markdown
Member

Thanks 👍🏻

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants