-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Fix validation logic of php:function() callbacks in dom and xsl #12593
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
--TEST-- | ||
php:function() edge cases | ||
--EXTENSIONS-- | ||
dom | ||
--FILE-- | ||
<?php | ||
|
||
$doc = new DOMDocument(); | ||
$doc->loadHTML('<a href="https://php.net">hello</a>'); | ||
$xpath = new DOMXpath($doc); | ||
$xpath->registerNamespace("php", "http://php.net/xpath"); | ||
$xpath->registerPHPFunctions(); | ||
try { | ||
$xpath->query("//a[php:function(3)]"); | ||
} catch (TypeError $e) { | ||
echo $e->getMessage(), "\n"; | ||
} | ||
try { | ||
$xpath->query("//a[php:function()]"); | ||
} catch (Throwable $e) { | ||
echo $e->getMessage(), "\n"; | ||
} | ||
|
||
?> | ||
--EXPECT-- | ||
Handler name must be a string | ||
Function name must be passed as the first argument |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -70,6 +70,11 @@ static void dom_xpath_ext_function_php(xmlXPathParserContextPtr ctxt, int nargs, | |
return; | ||
} | ||
|
||
if (UNEXPECTED(nargs == 0)) { | ||
zend_throw_error(NULL, "Function name must be passed as the first argument"); | ||
return; | ||
} | ||
|
||
fci.param_count = nargs - 1; | ||
if (fci.param_count > 0) { | ||
fci.params = safe_emalloc(fci.param_count, sizeof(zval), 0); | ||
|
@@ -132,7 +137,7 @@ static void dom_xpath_ext_function_php(xmlXPathParserContextPtr ctxt, int nargs, | |
if (obj->stringval == NULL) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe add a comment that this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ack |
||
zend_type_error("Handler name must be a string"); | ||
xmlXPathFreeObject(obj); | ||
goto cleanup; | ||
goto cleanup_no_callable; | ||
} | ||
Comment on lines
137
to
141
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should be able to check that the string is indeed a valid callable before allocating the arguments of it. Moreover, (for master) you could use the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Not easy to do, it's a stack machine so the function name comes last. I'd have to store the past arguments anyway so I can't avoid the allocation.
TIL. But that will also be slower because of the hashtable management code. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Okay, that code makes way more sense |
||
ZVAL_STRING(&fci.function_name, (char *) obj->stringval); | ||
xmlXPathFreeObject(obj); | ||
|
@@ -177,6 +182,7 @@ static void dom_xpath_ext_function_php(xmlXPathParserContextPtr ctxt, int nargs, | |
cleanup: | ||
zend_string_release_ex(callable, 0); | ||
zval_ptr_dtor_nogc(&fci.function_name); | ||
cleanup_no_callable: | ||
if (fci.param_count > 0) { | ||
for (i = 0; i < nargs - 1; i++) { | ||
zval_ptr_dtor(&fci.params[i]); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
--TEST-- | ||
php:function() edge cases | ||
--EXTENSIONS-- | ||
xsl | ||
--FILE-- | ||
<?php | ||
|
||
function test($input) { | ||
$xsl = new DomDocument(); | ||
$xsl->loadXML('<?xml version="1.0" encoding="iso-8859-1" ?> | ||
<xsl:stylesheet version="1.0" | ||
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" | ||
xmlns:php="http://php.net/xsl"> | ||
<xsl:template match="/"> | ||
<xsl:value-of select="php:function(' . $input . ')" /> | ||
</xsl:template> | ||
</xsl:stylesheet>'); | ||
|
||
$inputdom = new DomDocument(); | ||
$inputdom->loadXML('<?xml version="1.0" encoding="iso-8859-1" ?> | ||
<today></today>'); | ||
|
||
$proc = new XsltProcessor(); | ||
$proc->registerPhpFunctions(); | ||
$xsl = $proc->importStylesheet($xsl); | ||
try { | ||
$proc->transformToDoc($inputdom); | ||
} catch (Exception $e) { | ||
echo $e->getMessage(), "\n"; | ||
} | ||
} | ||
|
||
try { | ||
test(""); | ||
} catch (Throwable $e) { | ||
echo $e->getMessage(), "\n"; | ||
} | ||
|
||
test("3"); | ||
|
||
?> | ||
--EXPECTF-- | ||
Function name must be passed as the first argument | ||
|
||
Warning: XSLTProcessor::transformToDoc(): Handler name must be a string in %s on line %d |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would the for loop below be more clear as:
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed, I'll change it soon