Skip to content

Commit

Permalink
Merge pull request #294 from oat-sa/fix/TR-1412/xml-encode-values-cg
Browse files Browse the repository at this point in the history
Fix TR-1412 XML-encode values – CG

fix: XML-encode a value before inserting it into DOM
  • Loading branch information
wazelin committed Jul 16, 2021
2 parents c660c79 + 60c7fb0 commit b24986d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
8 changes: 4 additions & 4 deletions qtism/data/storage/xml/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public static function anonimizeElement(DOMElement $element)
$node = $stack->pop();

if ($node->nodeType === XML_ELEMENT_NODE && $node->childNodes->length > 0 && in_array($node, $traversed, true) === false) {
array_push($traversed, $node);
$traversed[] = $node;
$stack->push($node);

for ($i = 0; $i < $node->childNodes->length; $i++) {
Expand All @@ -139,9 +139,9 @@ public static function anonimizeElement(DOMElement $element)
$newNode->appendChild(array_pop($children));
}

array_push($children, $newNode);
$children[] = $newNode;
} else {
array_push($children, $node->cloneNode());
$children[] = $node->cloneNode();
}
}

Expand Down Expand Up @@ -262,7 +262,7 @@ public static function valueAsString($value)
if (is_bool($value)) {
return $value === true ? 'true' : 'false';
}
return (string)$value;
return htmlspecialchars($value, ENT_XML1, 'UTF-8');
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,35 @@ public function testUnmarshall21()
$this::assertCount(1, $values);
$this::assertEquals('tplx', $values[0]->getValue());
}

public function testMarshallHtmlEntities21()
{
$values = new ValueCollection([new Value('non&nbsp;breaking&nbsp;space', BaseType::STRING)]);
$defaultValue = new DefaultValue($values);
$templateDeclaration = new TemplateDeclaration('tpl1', BaseType::STRING, Cardinality::SINGLE, $defaultValue);
$element = $this->getMarshallerFactory('2.1.0')->createMarshaller($templateDeclaration)->marshall($templateDeclaration);

$dom = new DOMDocument('1.0', 'UTF-8');
$element = $dom->importNode($element, true);
$this::assertEquals('<templateDeclaration identifier="tpl1" cardinality="single" baseType="string"><defaultValue><value>non&amp;nbsp;breaking&amp;nbsp;space</value></defaultValue></templateDeclaration>', $dom->saveXML($element));
}

public function testUnmarshallHtmlEntities21()
{
$element = $this->createDOMElement('
<templateDeclaration identifier="tpl1" cardinality="single" baseType="string"><defaultValue><value>non&amp;nbsp;breaking&amp;nbsp;space</value></defaultValue></templateDeclaration>
');

$component = $this->getMarshallerFactory('2.1.0')->createMarshaller($element)->unmarshall($element);
$this::assertInstanceOf(TemplateDeclaration::class, $component);
$this::assertEquals('tpl1', $component->getIdentifier());
$this::assertEquals(Cardinality::SINGLE, $component->getCardinality());
$this::assertEquals(BaseType::STRING, $component->getBaseType());

$default = $component->getDefaultValue();
$this::assertInstanceOf(DefaultValue::class, $default);
$values = $default->getValues();
$this::assertCount(1, $values);
$this::assertEquals('non&nbsp;breaking&nbsp;space', $values[0]->getValue());
}
}

0 comments on commit b24986d

Please sign in to comment.