Add disallow_doctype() configurator (no-DOCTYPE policy guard)#107
Merged
Conversation
7c5b187 to
5d9ce28
Compare
Loading untrusted XML through the Dom layer offered no policy against
DOCTYPE-bearing documents. On PHP 8.4, Dom\XMLDocument::createFromString
does not fetch external entities by default and libxml already aborts
entity-amplification (billion-laughs) attacks itself, but it does parse a
DOCTYPE and expand small internal entities.
This adds a disallow_doctype() configurator that rejects any document whose
doctype is present, throwing a dedicated DoctypeNotAllowedException. It is a
defence-in-depth policy (refuse the DOCTYPE surface wholesale) and catches
the internal-entity / external-DTD-reference cases libxml permits, before
downstream code uses the document:
Document::fromXmlString($untrustedXml, disallow_doctype());
It is not a DoS mitigation: a post-parse check cannot prevent parse-time
expansion, and libxml already handles amplification.
5d9ce28 to
9dfb089
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Adds a
disallow_doctype()DOM configurator that rejects any document carrying a<!DOCTYPE ...>declaration, throwing a dedicatedVeeWee\Xml\Exception\DoctypeNotAllowedException. It runs on the parsed document, so it applies uniformly to every loader — string, file and node alike.What it protects against — and what it does not
Being precise here, because libxml already does most of the work. With the default options this library passes to
Dom\XMLDocument(PHP 8.4, no extra libxml flags), libxml already:file:///…is never resolved);SYSTEM "http://…"triggers no request);So on the default path you are already protected against the active XXE vectors, with or without this PR. The only thing libxml still expands are small, non-amplifying internal entities — which is not an attack when the attacker already authored the whole document.
What
disallow_doctype()adds is therefore policy + defence-in-depth, not a libxml-gap fix:php.ini.What it does NOT protect against: it runs after parsing, so it cannot undo anything done during parsing. If the loader is given unsafe options —
LIBXML_NOENTorLIBXML_DTDLOAD— libxml reads the file / fetches the DTD while parsing, before this configurator runs. The rejection is then too late. Verified on PHP 8.4:LIBXML_NOENTsubstitutes afile:///entity into the DOM during parse.Takeaway: keeping the default options is what actually keeps you safe; do not enable
LIBXML_NOENT/LIBXML_DTDLOADfor untrusted input. This configurator is the policy layer on top.How
A configurator throwing when
$document->doctype !== null, composed with the existing loaders (fromXmlString,fromXmlFile, …). Smallest surface; one reusable primitive that works for every input source. A pre-parse string guard / options-owning "secure loader" was intentionally not added — it would only cover the string input path and would not generalise to files, streams or already-parsed nodes; the option-hygiene point above is the real lever and it is universal.Tests
<!DOCTYPE r>file:///…)SYSTEM "http://…")Notes
DoctypeNotAllowedExceptionextends global\RuntimeExceptionand implementsVeeWee\Xml\Exception\ExceptionInterface, mirroring the existingEncodingExceptionprecedent. It can't extendVeeWee\Xml\Exception\RuntimeException(that class isfinal); the shared catchable contract isExceptionInterface.✅ Full suite (693 tests) · Psalm clean (100% inferred) · php-cs-fixer clean