Proposal: validating elements and attributes using Xpath
- Dominant language
- PHP
- Stars
- 29
- Forks
- 37
- PR merge metrics
- No merged PRs in 30d
Description
Currently validation on the received xml is made using simple DOM lookups like [DOMDocument::getElementsByTagName](https://www.php.net/manual/domdocument.getelementsbytagname.php).
While this technically works good enough to fool the SPID validator into thinking the xml is being validated and passing the tests, it's not really checking every aspect of the xml.
Take by example the first test for an element in a response object at [Response.php#L54](https://github.com/italia/spid-php-lib/blob/master/src/Spid/Saml/In/Response.php#L54):
```php
if ($xml->getElementsByTagName('Issuer')->length == 0) {
...
```
This is checking if an `` tag is present in the xml, but not validating if it's nested in the correct place in the xml.
So how do we correctly validate an xml?
Usually by using a XSD (XML Schema Definition) file, supported by php with the [DOMDocument::schemaValidate](https://www.php.net/manual/en/domdocument.schemavalidate.php) method. Unfortunately this method outputs some cryptic validation messages that are extremely precise but hard to interpret for users.
Imho a possible compromose would be to use xpath queries to check for elements.
First, a [DOMXpath](https://www.php.net/manual/en/class.domxpath.php) object is created on the [DOMDocument](https://www.php.net/manual/en/class.domdocument.php) object:
```php
$xpath = new \DOMXpath($doc);
$xpath->registerNamespace('samlp', 'urn:oasis:names:tc:SAML:2.0:protocol');
$xpath->registerNamespace('saml', 'urn:oasis:names:tc:SAML:2.0:assertion');
```
Then, this new object can be used to perform queries on the document similarly to how [css selectors work](https://devhints.io/xpath)
```
$issuer = $xpath->query('/samlp:Response/saml:Issuer')
```
You can check for elements, for their specific path in the DOM tree, for specific attributes, for specific values, and so on.
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.