Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Serializer] Make document type nodes ignorable when decoding XML #44363

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

Closed
boenner opened this issue Nov 30, 2021 · 0 comments
Closed

[Serializer] Make document type nodes ignorable when decoding XML #44363

boenner opened this issue Nov 30, 2021 · 0 comments

Comments

@boenner
Copy link
Contributor

boenner commented Nov 30, 2021

Symfony version(s) affected

4.4, 5.3, 5.4, 6.0

Description

When decoding XML using the XmlEncoder, ignoring the document type node with the decoder_ignored_node_types option is not possible.

How to reproduce

Using the XmlEncoder with the document type node included in the ignore nodes

$xmlEncoder = new XmlEncoder();
$xml = '<?xml version="1.0"?><!DOCTYPE foobar SYSTEM "http://foo.bar"><foo>bar</foo>';
$xmlEncoder->decode($xml, 'xml', [XmlEncoder::DECODER_IGNORED_NODE_TYPES => [XML_DOCUMENT_TYPE_NODE]])

throws a NotEncodableValueException because Document types are not allowed.

Possible Solution

Consider the ignored node types before checking the current node in the decode function:

$rootNode = null;
$decoderIgnoredNodeTypes = $context[self::DECODER_IGNORED_NODE_TYPES] ?? $this->defaultContext[self::DECODER_IGNORED_NODE_TYPES];
foreach ($dom->childNodes as $child) {
    if (\in_array($child->nodeType, $decoderIgnoredNodeTypes, true)) {
        continue;
    }
    if (\XML_DOCUMENT_TYPE_NODE === $child->nodeType) {
        throw new NotEncodableValueException('Document types are not allowed.');
    }
    if (!$rootNode) {
        $rootNode = $child;
    }
}

Additional Context

No response

@boenner boenner added the Bug label Nov 30, 2021
nicolas-grekas added a commit that referenced this issue Feb 18, 2022
This PR was merged into the 4.4 branch.

Discussion
----------

[Serializer] Make document type nodes ignorable

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #44363
| License       | MIT
| Doc PR        | -

As described in #44363: while looping over all nodes, the current node is checked against the list of ignored node types before throwing the `NotEncodableValueException` if it's a document type node.

Commits
-------

2c75e27 Make document type nodes ignorable
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants