Skip to content

Commit 3da8a49

Browse files
committed
Restrict HTML namespace
1 parent 367d62c commit 3da8a49

File tree

3 files changed

+73
-1
lines changed

3 files changed

+73
-1
lines changed

ext/dom/element.c

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1879,13 +1879,35 @@ PHP_METHOD(DOM_Element, rename)
18791879
goto cleanup;
18801880
}
18811881

1882-
/* Check for duplicate attributes. */
18831882
if (nodep->type == XML_ATTRIBUTE_NODE) {
1883+
/* Check for duplicate attributes. */
18841884
xmlAttrPtr existing = xmlHasNsProp(nodep->parent, localname, namespace_uri && ZSTR_VAL(namespace_uri)[0] != '\0' ? BAD_CAST ZSTR_VAL(namespace_uri) : NULL);
18851885
if (existing != NULL && existing != (xmlAttrPtr) nodep) {
18861886
php_dom_throw_error_with_message(INVALID_MODIFICATION_ERR, "An attribute with the given name in the given namespace already exists", /* strict */ true);
18871887
goto cleanup;
18881888
}
1889+
} else {
1890+
ZEND_ASSERT(nodep->type == XML_ELEMENT_NODE);
1891+
1892+
/* Check for moving to or away from of the HTML namespace. */
1893+
bool is_currently_html_ns = php_dom_ns_is_fast(nodep, php_dom_ns_is_html_magic_token);
1894+
bool will_be_html_ns = namespace_uri != NULL && zend_string_equals_literal(namespace_uri, DOM_XHTML_NS_URI);
1895+
if (is_currently_html_ns != will_be_html_ns) {
1896+
if (is_currently_html_ns) {
1897+
php_dom_throw_error_with_message(
1898+
INVALID_MODIFICATION_ERR,
1899+
"It is not possible to move an element out of the HTML namespace because the HTML namespace is tied to the HTMLElement class",
1900+
/* strict */ true
1901+
);
1902+
} else {
1903+
php_dom_throw_error_with_message(
1904+
INVALID_MODIFICATION_ERR,
1905+
"It is not possible to move an element into the HTML namespace because the HTML namespace is tied to the HTMLElement class",
1906+
/* strict */ true
1907+
);
1908+
}
1909+
goto cleanup;
1910+
}
18891911
}
18901912

18911913
php_libxml_invalidate_node_list_cache(intern->document);
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
--TEST--
2+
Element renaming interaction with the HTML namespace 01
3+
--EXTENSIONS--
4+
dom
5+
--FILE--
6+
<?php
7+
8+
$dom = DOM\XMLDocument::createEmpty();
9+
$el = $dom->createElementNS("http://www.w3.org/1999/xhtml", "foo:bar");
10+
$el->rename("http://www.w3.org/1999/xhtml", "foo:baz");
11+
var_dump($el->nodeName, $el->namespaceURI, $el->prefix);
12+
13+
// Very subtly *not* the HTML namespace!
14+
$el = $dom->createElementNS("http://www.w3.org/1999/xhtml/", "foo:bar");
15+
$el->rename("urn:x", "foo:baz");
16+
var_dump($el->nodeName, $el->namespaceURI, $el->prefix);
17+
18+
?>
19+
--EXPECT--
20+
string(7) "foo:baz"
21+
string(28) "http://www.w3.org/1999/xhtml"
22+
string(3) "foo"
23+
string(7) "foo:baz"
24+
string(5) "urn:x"
25+
string(3) "foo"
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
--TEST--
2+
Element renaming interaction with the HTML namespace 02
3+
--EXTENSIONS--
4+
dom
5+
--FILE--
6+
<?php
7+
8+
$dom = DOM\XMLDocument::createEmpty();
9+
$el = $dom->createElementNS("http://www.w3.org/1999/xhtml", "foo:bar");
10+
try {
11+
$el->rename("urn:a", "foo:baz");
12+
} catch (DOMException $e) {
13+
echo $e->getMessage(), "\n";
14+
}
15+
$el = $dom->createElementNS("urn:a", "foo:bar");
16+
try {
17+
$el->rename("http://www.w3.org/1999/xhtml", "foo:baz");
18+
} catch (DOMException $e) {
19+
echo $e->getMessage(), "\n";
20+
}
21+
22+
?>
23+
--EXPECT--
24+
It is not possible to move an element out of the HTML namespace because the HTML namespace is tied to the HTMLElement class
25+
It is not possible to move an element into the HTML namespace because the HTML namespace is tied to the HTMLElement class

0 commit comments

Comments
 (0)