Skip to content

Parser: ignore whitespace before the XML declaration - #1005

Merged
jtojnar merged 3 commits into
simplepie:masterfrom
jgalea:fix-xml-leading-whitespace
Nov 2, 2026
Merged

jtojnar merged 3 commits into
simplepie:masterfrom
jgalea:fix-xml-leading-whitespace

Conversation

@jgalea

@jgalea jgalea commented Oct 29, 2026

Copy link
Copy Markdown
Contributor

This PR makes the parser skip whitespace (space, tab, CR, LF) in front of the <?xml declaration, including whitespace after a BOM.

A leading newline is a common slip on feeds generated by PHP templates. Without this, expat/libxml reject the document with "XML or text declaration not at start of entity" ("Reserved XML Name") and the feed is reported as invalid XML. Leading whitespace is allowed for documents without a declaration, so dropping it can't change a valid document.

Tests: parsing a feed with "\n \t\r\n" before the declaration, and with a BOM followed by a newline. Both fail on master.

Comment thread src/Parser.php Outdated

// The XML declaration must be the very first thing in the document, but
// plenty of feeds are served with a stray newline or space in front of it.
$data = ltrim($data, "\x09\x0A\x0D\x20");

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.

I am a bit confused. The code above seems to suggest that arbitrary encoding is allowed, in which case this would corrupt UTF-16 documents. However, code bellow seems to assume ASCII-compatible encoding.

Either way, it is probably a better place to do this than

simplepie/src/File.php

Lines 278 to 281 in 7a0550c

// Leading whitespace may cause XML parsing errors (XML declaration cannot be preceded by anything other than BOM) so we trim it.
// Note that unlike built-in `trim` function’s default settings, we do not trim `\x00` to avoid breaking characters in UTF-16 or UTF-32 encoded strings.
// We also only do that when the whitespace is followed by `<`, so that we do not break e.g. UTF-16LE encoded whitespace like `\n\x00` in half.
$this->body = preg_replace('/^[ \n\r\t\v]+</', '<', $this->body);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

In practice it's always ASCII-compatible by the time it gets here. SimplePie::init() converts the body to UTF-8 and calls parse($utf8_data, 'UTF-8'), and xml_parser_create_ns() throws on anything other than UTF-8, ISO-8859-1 or US-ASCII, so the UTF-16 and UTF-32 BOM branches above only matter to someone calling Parser::parse() directly with already-broken input.

Still, ltrim() would split \n\x00 in UTF-16LE, so 72942d0 switches to the same rule as File: strip the whitespace only when < follows. Both new tests still fail without the line.

I left the copy in File alone, since Locator and the content type sniffer read File::$body before it reaches the parser. It can go once File is deprecated.

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.

since Locator and the content type sniffer read File::$body before it reaches the parser

Hmm, I guess that is why it was in the File then. Do we need to add this to those two places as well? Or should we just decide to put it in Psr18Client instead?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Neither, I think. With the strip in Parser, the copy in File is redundant:

  • The sniffer skips leading whitespace on its own (unknown() and feed_or_html() both start with a strspn() over whitespace).
  • Locator reads the body through loadHTML(), which doesn't care, and through is_feed(), which goes to the sniffer.

I checked it on libxml2 2.13.9 with the strip in File removed, using a feed with \n\n before the declaration. Served as text/html, text/xml and application/rss+xml, found through <link rel="alternate">, and found through an <a href> in the body, it parses with this PR in every case and fails in every case with master's Parser. Since Parser gets the body from every client, Psr18Client doesn't need its own copy.

I can drop the copy in File in this PR, or leave it to go when File does.

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.

Thanks for the explanation, I think keeping it there is fine for now.

Comment thread src/Parser.php Resolved
Comment thread CHANGELOG.md Resolved
@jtojnar jtojnar added this to the 1.10.0 milestone Nov 2, 2026
@jtojnar
jtojnar merged commit 79c756d into simplepie:master Nov 2, 2026
9 checks passed
Alkarex added a commit to FreshRSS/FreshRSS that referenced this pull request Nov 14, 2026
In particular:
* File,Psr18Client: Remove support for local files simplepie/simplepie#994
* Parser: ignore whitespace before the XML declaration simplepie/simplepie#1005
* Require PHP 7.4 or newer simplepie/simplepie#1006, simplepie/simplepie#992
* Parser: treat https Atom 1.0 namespace as Atom simplepie/simplepie#1010
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants