Skip to content
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

[BUGFIX] Allow DOMText and implicitly DOMCdataSection #72

Merged
merged 1 commit into from
Feb 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 11 additions & 28 deletions src/Sanitizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -638,41 +638,24 @@ public function setUseNestingLimit($limit)
$this->useNestingLimit = (int) $limit;
}

/**
* Determines whether a node is safe or not.
*
* @param \DOMNode $node
* @return bool
*/
protected function isNodeSafe(\DOMNode $node) {
$safeNodes = [
'#text',
];

if (!in_array($node->nodeName, $safeNodes, true)) {
return false;
}

return true;
}

/**
* Remove nodes that are either invalid or malformed.
*
* @param \DOMNode $currentElement The current element.
*/
protected function cleanUnsafeNodes(\DOMNode $currentElement) {
// Replace CDATA node with encoded text node
if ($currentElement instanceof \DOMCdataSection) {
$textNode = $currentElement->ownerDocument->createTextNode($currentElement->nodeValue);
$currentElement->parentNode->replaceChild($textNode, $currentElement);
// If the element doesn't have a tagname, remove it and continue with next iteration
if (!property_exists($currentElement, 'tagName')) {
if (!$this->isNodeSafe($currentElement)) {
$currentElement->parentNode->removeChild($currentElement);
$this->xmlIssues[] = array(
'message' => 'Suspicious node \'' . $currentElement->nodeName . '\'',
'line' => $currentElement->getLineNo(),
);

return;
}
} elseif (!$currentElement instanceof \DOMElement && !$currentElement instanceof \DOMText) {
$currentElement->parentNode->removeChild($currentElement);
$this->xmlIssues[] = array(
'message' => 'Suspicious node \'' . $currentElement->nodeName . '\'',
'line' => $currentElement->getLineNo(),
);
return;
}

if ( $currentElement->childNodes && $currentElement->childNodes->length > 0 ) {
Expand Down
16 changes: 16 additions & 0 deletions tests/SanitizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,22 @@ public function testInvalidNodesAreHandled()
$sanitizer->minify(false);
$cleanData = $sanitizer->sanitize($initialData);

self::assertXmlStringEqualsXmlString($expected, $cleanData);
}

/**
* @test
*/
public function cdataSectionIsSanitized()
{
$dataDirectory = __DIR__ . '/data';
$initialData = file_get_contents($dataDirectory . '/cdataTest.svg');
$expected = file_get_contents($dataDirectory . '/cdataClean.svg');

$sanitizer = new Sanitizer();
$sanitizer->minify(false);
$cleanData = $sanitizer->sanitize($initialData);

self::assertXmlStringEqualsXmlString($expected, $cleanData);
}
}
7 changes: 7 additions & 0 deletions tests/data/cdataClean.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 17 additions & 0 deletions tests/data/cdataTest.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion tests/data/htmlClean.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.