I am using XPath to select XML nodes in a SimpleXMLElement. My XPath expression is variable:
$xml->xpath("a[@href = '{$some_specific_value_of_href}']");
Since I do not have full control over the value of the $some_specific_value_of_href
variable, the above code is prone to an injection attack, where a malicious value of the variable will produce an arbitrary set of elements.
Most mature XML APIs, such as the lxml library for Python, allow the user to specify placeholder variable names in an XPath expression, and compile the XPath expression without running the risk of an injection attack:
xml.xpath(
"a[@href = $placeholder_variable_name]", # Python performs no substitution here
placeholder_variable_name=some_specific_value_of_href
);
Is there similar functionality available for PHP 7?