8000 [HtmlSanitizer] Processing instructions misinterpreted as elements · Issue #54492 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[HtmlSanitizer] Processing instructions misinterpreted as elements #54492

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
nielsdos opened this issue Apr 4, 2024 · 5 comments
Closed

[HtmlSanitizer] Processing instructions misinterpreted as elements #54492

nielsdos opened this issue Apr 4, 2024 · 5 comments

Comments

@nielsdos
Copy link
nielsdos commented Apr 4, 2024

Symfony version(s) affected

7.0, perhaps lower too

Description

You're checking the node name in the node traversal, but not the node type.
So creating a processing instruction with the name of an allowed element results in a misinterpretation: the processing instruction will be considered as if it is an element. Fortunately, this has no security impact because we can only misinterpret nodes into an allowed element.

How to reproduce

<?php
require 'vendor/autoload.php';

use Symfony\Component\HtmlSanitizer\HtmlSanitizerConfig;
use Symfony\Component\HtmlSanitizer\HtmlSanitizer;

$config = (new HtmlSanitizerConfig())->allowElement("div");

$sanitizer = new HtmlSanitizer($config);
echo $sanitizer->sanitize("<?div x?>"), "\n";

Results in:

<div></div>

Possible Solution

Don't allow processing instructions, those aren't allowed by HTML5 anyway.

Additional Context

No response

@smnandre
Copy link
Member
smnandre commented Apr 6, 2024

If i'm reading this correctly, we could add some checks here, to discard some DOMNode types:

  • DOMComment
  • DOMProcessingInstruction

Any others ?

    private function visitChildren(\DOMNode $domNode, Cursor $cursor): void
    {
        foreach ($domNode->childNodes ?? [] as $child) {
            if ('#text' === $child->nodeName) {
                // Add text directly for performance
                $cursor->node->addChild(new TextNode($cursor->node, $child->nodeValue));
            } elseif (!$child instanceof \DOMText) {
                // Otherwise continue the visit recursively
                // Ignore comments for security reasons (interpreted differently by browsers)
                
                //  -- ?Add node type checks here ?--
                
                $this->visitNode($child, $cursor);
            }
        }
    }

/src/Symfony/Component/HtmlSanitizer/Visitor/DomVisitor.php

Poke @tgalopin maybe ?

@nielsdos
Copy link
Author
nielsdos commented Apr 6, 2024

As far as I can see, everything but processing instructions are already implicitly blocked because the other node types would start with a # symbol in their nodeName. PIs are the only cases where you can force a custom name (as shown in my OP example).

@smnandre
Copy link
Member
smnandre commented Apr 6, 2024

Hey @nielsdos i tried to send a patch, could you check if that's OK for you ? :)

@tgalopin
Copy link
Contributor
tgalopin commented Apr 7, 2024

Interesting, thanks for the report, and thanks @smnandre for the PR, I'm having a look!

@smnandre
Copy link
Member
smnandre commented Apr 7, 2024

Thank you @tgalopin !

@fabpot fabpot closed this as completed Apr 8, 2024
fabpot added a commit that referenced this issue Apr 8, 2024
This PR was merged into the 6.4 branch.

Discussion
----------

[HtmlSanitizer] Ignore Processing Instructions

| Q             | A
| ------------- | ---
| Branch?       | 6.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Issues        | Fix #54492
| License       | MIT

Ignore Processing Instructions (as comments) to avoid mixing them with standard DOM nodes (see #54492)

(targetting 6.4 as the component was released  in 6.1))

Commits
-------

3582bdd [HtmlSanitizer] Ignore Processing Instructions
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

5 participants
0