Skip to content

Commit

Permalink
Implement DOM schema validation
Browse files Browse the repository at this point in the history
  • Loading branch information
tvdijen committed Dec 20, 2024
1 parent 6071ee9 commit 01bc683
Show file tree
Hide file tree
Showing 25 changed files with 100 additions and 34 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
"ext-pcre": "*",

"simplesamlphp/assert": "^1.6",
"simplesamlphp/composer-xmlprovider-installer": "~1.0.0",
"simplesamlphp/xml-common": "~1.22.0"
"simplesamlphp/composer-xmlprovider-installer": "~1.0.1",
"simplesamlphp/xml-common": "~1.23.2"
},
"require-dev": {
"simplesamlphp/simplesamlphp-test-framework": "^1.8"
Expand Down
3 changes: 3 additions & 0 deletions src/XML/env_200106/AbstractSoapElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,7 @@ abstract class AbstractSoapElement extends AbstractElement

/** @var string */
public const NS_PREFIX = 'SOAP-ENV';

/** @var string */
public const SCHEMA = 'resources/schemas/soap-envelope-1.1.xsd';
}
5 changes: 4 additions & 1 deletion src/XML/env_200106/Body.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
use SimpleSAML\XML\Exception\InvalidDOMElementException;
use SimpleSAML\XML\ExtendableAttributesTrait;
use SimpleSAML\XML\ExtendableElementTrait;
use SimpleSAML\XML\SchemaValidatableElementInterface;
use SimpleSAML\XML\SchemaValidatableElementTrait;
use SimpleSAML\XML\XsNamespace as NS;

use function array_diff;
Expand All @@ -22,10 +24,11 @@
*
* @package simplesaml/xml-soap
*/
final class Body extends AbstractSoapElement
final class Body extends AbstractSoapElement implements SchemaValidatableElementInterface
{
use ExtendableAttributesTrait;
use ExtendableElementTrait;
use SchemaValidatableElementTrait;

/** The namespace-attribute for the xs:any element */
public const XS_ANY_ELT_NAMESPACE = NS::ANY;
Expand Down
5 changes: 4 additions & 1 deletion src/XML/env_200106/Envelope.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,20 @@
use SimpleSAML\XML\Exception\TooManyElementsException;
use SimpleSAML\XML\ExtendableAttributesTrait;
use SimpleSAML\XML\ExtendableElementTrait;
use SimpleSAML\XML\SchemaValidatableElementInterface;
use SimpleSAML\XML\SchemaValidatableElementTrait;
use SimpleSAML\XML\XsNamespace as NS;

/**
* Class representing a env:Envelope element.
*
* @package simplesaml/xml-soap
*/
final class Envelope extends AbstractSoapElement
final class Envelope extends AbstractSoapElement implements SchemaValidatableElementInterface
{
use ExtendableElementTrait;
use ExtendableAttributesTrait;
use SchemaValidatableElementTrait;

/** The namespace-attribute for the xs:any element */
public const XS_ANY_ELT_NAMESPACE = NS::OTHER;
Expand Down
6 changes: 5 additions & 1 deletion src/XML/env_200106/Fault.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,18 @@
use SimpleSAML\XML\Exception\InvalidDOMElementException;
use SimpleSAML\XML\Exception\MissingElementException;
use SimpleSAML\XML\Exception\TooManyElementsException;
use SimpleSAML\XML\SchemaValidatableElementInterface;
use SimpleSAML\XML\SchemaValidatableElementTrait;

/**
* Class representing a env:Fault element.
*
* @package simplesaml/xml-soap
*/
final class Fault extends AbstractSoapElement
final class Fault extends AbstractSoapElement implements SchemaValidatableElementInterface
{
use SchemaValidatableElementTrait;

/**
* Initialize a env:Fault
*
Expand Down
5 changes: 4 additions & 1 deletion src/XML/env_200106/Header.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,20 @@
use SimpleSAML\XML\Exception\InvalidDOMElementException;
use SimpleSAML\XML\ExtendableAttributesTrait;
use SimpleSAML\XML\ExtendableElementTrait;
use SimpleSAML\XML\SchemaValidatableElementInterface;
use SimpleSAML\XML\SchemaValidatableElementTrait;
use SimpleSAML\XML\XsNamespace as NS;

/**
* Class representing a env:Header element.
*
* @package simplesaml/xml-soap
*/
final class Header extends AbstractSoapElement
final class Header extends AbstractSoapElement implements SchemaValidatableElementInterface
{
use ExtendableAttributesTrait;
use ExtendableElementTrait;
use SchemaValidatableElementTrait;

/** The namespace-attribute for the xs:any element */
public const XS_ANY_ELT_NAMESPACE = NS::OTHER;
Expand Down
3 changes: 3 additions & 0 deletions src/XML/env_200305/AbstractSoapElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,7 @@ abstract class AbstractSoapElement extends AbstractElement

/** @var string */
public const NS_PREFIX = 'env';

/** @var string */
public const SCHEMA = 'resources/schemas/soap-envelope-1.2.xsd';
}
5 changes: 4 additions & 1 deletion src/XML/env_200305/Body.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
use SimpleSAML\XML\Exception\InvalidDOMElementException;
use SimpleSAML\XML\ExtendableAttributesTrait;
use SimpleSAML\XML\ExtendableElementTrait;
use SimpleSAML\XML\SchemaValidatableElementInterface;
use SimpleSAML\XML\SchemaValidatableElementTrait;
use SimpleSAML\XML\XsNamespace as NS;

use function array_pop;
Expand All @@ -19,10 +21,11 @@
*
* @package simplesaml/xml-soap
*/
final class Body extends AbstractSoapElement
final class Body extends AbstractSoapElement implements SchemaValidatableElementInterface
{
use ExtendableAttributesTrait;
use ExtendableElementTrait;
use SchemaValidatableElementTrait;

/** The namespace-attribute for the xs:any element */
public const XS_ANY_ELT_NAMESPACE = NS::ANY;
Expand Down
5 changes: 4 additions & 1 deletion src/XML/env_200305/Envelope.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,19 @@
use SimpleSAML\XML\Exception\MissingElementException;
use SimpleSAML\XML\Exception\TooManyElementsException;
use SimpleSAML\XML\ExtendableAttributesTrait;
use SimpleSAML\XML\SchemaValidatableElementInterface;
use SimpleSAML\XML\SchemaValidatableElementTrait;
use SimpleSAML\XML\XsNamespace as NS;

/**
* Class representing a env:Envelope element.
*
* @package simplesaml/xml-soap
*/
final class Envelope extends AbstractSoapElement
final class Envelope extends AbstractSoapElement implements SchemaValidatableElementInterface
{
use ExtendableAttributesTrait;
use SchemaValidatableElementTrait;

/** The namespace-attribute for the xs:anyAttribute element */
public const XS_ANY_ATTR_NAMESPACE = NS::OTHER;
Expand Down
11 changes: 10 additions & 1 deletion src/XML/env_200305/Fault.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,22 @@

use DOMElement;
use SimpleSAML\Assert\Assert;
use SimpleSAML\XML\DOMDocumentFactory;
use SimpleSAML\XML\Exception\InvalidDOMElementException;
use SimpleSAML\XML\Exception\MissingElementException;
use SimpleSAML\XML\Exception\TooManyElementsException;
use SimpleSAML\XML\SchemaValidatableElementInterface;
use SimpleSAML\XML\SchemaValidatableElementTrait;

/**
* Class representing a env:Fault element.
*
* @package simplesaml/xml-soap
*/
final class Fault extends AbstractSoapElement
final class Fault extends AbstractSoapElement implements SchemaValidatableElementInterface
{
use SchemaValidatableElementTrait;

/**
* Initialize a env:Fault
*
Expand Down Expand Up @@ -140,6 +145,10 @@ public function toXML(?DOMElement $parent = null): DOMElement
$this->getDetail()->toXML($e);
}

// Dirty hack to get the namespaces in the right place. They cannot be in the env:Value element
$doc = DOMDocumentFactory::create();
$e = $doc->appendChild($doc->importNode($e, true));

return $e;
}
}
5 changes: 4 additions & 1 deletion src/XML/env_200305/Header.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,20 @@
use SimpleSAML\XML\Exception\InvalidDOMElementException;
use SimpleSAML\XML\ExtendableAttributesTrait;
use SimpleSAML\XML\ExtendableElementTrait;
use SimpleSAML\XML\SchemaValidatableElementInterface;
use SimpleSAML\XML\SchemaValidatableElementTrait;
use SimpleSAML\XML\XsNamespace as NS;

/**
* Class representing a env:Header element.
*
* @package simplesaml/xml-soap
*/
final class Header extends AbstractSoapElement
final class Header extends AbstractSoapElement implements SchemaValidatableElementInterface
{
use ExtendableAttributesTrait;
use ExtendableElementTrait;
use SchemaValidatableElementTrait;

/** The namespace-attribute for the xs:any element */
public const XS_ANY_ELT_NAMESPACE = NS::ANY;
Expand Down
14 changes: 12 additions & 2 deletions src/XML/env_200305/NotUnderstood.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@

use DOMElement;
use SimpleSAML\Assert\Assert;
use SimpleSAML\XML\Attribute as XMLAttribute;
use SimpleSAML\XML\Constants as C;
use SimpleSAML\XML\Exception\InvalidDOMElementException;
use SimpleSAML\XML\Exception\SchemaViolationException;
use SimpleSAML\XML\SchemaValidatableElementInterface;
use SimpleSAML\XML\SchemaValidatableElementTrait;

use function preg_split;

Expand All @@ -16,8 +20,10 @@
*
* @package simplesaml/xml-soap
*/
final class NotUnderstood extends AbstractSoapElement
final class NotUnderstood extends AbstractSoapElement implements SchemaValidatableElementInterface
{
use SchemaValidatableElementTrait;

/**
* Initialize a env:NotUnderstood
*
Expand Down Expand Up @@ -120,7 +126,11 @@ public function toXML(?DOMElement $parent = null): DOMElement
/** @phpstan-ignore-next-line */
if ($e->lookupNamespaceUri($prefix) === null && $e->lookupPrefix($namespaceUri) === null) {
// The namespace is not yet available in the document - insert it
$e->setAttribute('xmlns:' . $prefix, $namespaceUri);
//$attr = $e->createAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns');
//$attr->value = $namespaceUri;
//$e->setAttributeNS(C::NS_XS, 'xmlns:' . $prefix, $namespaceUri);
$attr = new XMLAttribute('http://www.w3.org/2000/xmlns/', 'xmlns', $prefix, $namespaceUri);
$attr->toXML($e);
}
}

Expand Down
6 changes: 5 additions & 1 deletion src/XML/env_200305/Upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,18 @@
use SimpleSAML\XML\Constants as C;
use SimpleSAML\XML\Exception\InvalidDOMElementException;
use SimpleSAML\XML\Exception\SchemaViolationException;
use SimpleSAML\XML\SchemaValidatableElementInterface;
use SimpleSAML\XML\SchemaValidatableElementTrait;

/**
* Class representing a env:Upgrade element.
*
* @package simplesaml/xml-soap
*/
final class Upgrade extends AbstractSoapElement
final class Upgrade extends AbstractSoapElement implements SchemaValidatableElementInterface
{
use SchemaValidatableElementTrait;

/**
* Initialize a env:Upgrade
*
Expand Down
2 changes: 0 additions & 2 deletions tests/XML/env_200106/BodyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ protected function setUp(): void
{
self::$testedClass = Body::class;

self::$schemaFile = dirname(__FILE__, 4) . '/resources/schemas/soap-envelope-1.1.xsd';

self::$xmlRepresentation = DOMDocumentFactory::fromFile(
dirname(__FILE__, 3) . '/resources/xml/env/200106/Body.xml',
);
Expand Down
2 changes: 0 additions & 2 deletions tests/XML/env_200106/EnvelopeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ public static function setUpBeforeClass(): void
{
self::$testedClass = Envelope::class;

self::$schemaFile = dirname(__FILE__, 4) . '/resources/schemas/soap-envelope-1.1.xsd';

self::$xmlRepresentation = DOMDocumentFactory::fromFile(
dirname(__FILE__, 3) . '/resources/xml/env/200106/Envelope.xml',
);
Expand Down
2 changes: 0 additions & 2 deletions tests/XML/env_200106/FaultTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ public static function setUpBeforeClass(): void
{
self::$testedClass = Fault::class;

self::$schemaFile = dirname(__FILE__, 4) . '/resources/schemas/soap-envelope-1.1.xsd';

self::$xmlRepresentation = DOMDocumentFactory::fromFile(
dirname(__FILE__, 3) . '/resources/xml/env/200106/Fault.xml',
);
Expand Down
2 changes: 0 additions & 2 deletions tests/XML/env_200106/HeaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ protected function setUp(): void
{
self::$testedClass = Header::class;

self::$schemaFile = dirname(__FILE__, 4) . '/resources/schemas/soap-envelope-1.1.xsd';

self::$xmlRepresentation = DOMDocumentFactory::fromFile(
dirname(__FILE__, 3) . '/resources/xml/env/200106/Header.xml',
);
Expand Down
2 changes: 0 additions & 2 deletions tests/XML/env_200305/BodyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ public static function setUpBeforeClass(): void
{
self::$testedClass = Body::class;

self::$schemaFile = dirname(__FILE__, 4) . '/resources/schemas/soap-envelope-1.2.xsd';

self::$xmlRepresentation = DOMDocumentFactory::fromFile(
dirname(__FILE__, 3) . '/resources/xml/env/200305/Body.xml',
);
Expand Down
2 changes: 0 additions & 2 deletions tests/XML/env_200305/EnvelopeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ public static function setUpBeforeClass(): void
{
self::$testedClass = Envelope::class;

self::$schemaFile = dirname(__FILE__, 4) . '/resources/schemas/soap-envelope-1.2.xsd';

self::$xmlRepresentation = DOMDocumentFactory::fromFile(
dirname(__FILE__, 3) . '/resources/xml/env/200305/Envelope.xml',
);
Expand Down
2 changes: 1 addition & 1 deletion tests/XML/env_200305/FaultTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public static function setUpBeforeClass(): void
{
self::$testedClass = Fault::class;

self::$schemaFile = dirname(__FILE__, 4) . '/resources/schemas/soap-envelope-1.2.xsd';
self::$schemaFile = dirname(__FILE__, 4) . '/tests/resources/schemas/simplesamlphp.xsd';

self::$xmlRepresentation = DOMDocumentFactory::fromFile(
dirname(__FILE__, 3) . '/resources/xml/env/200305/Fault.xml',
Expand Down
2 changes: 0 additions & 2 deletions tests/XML/env_200305/HeaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ public static function setUpBeforeClass(): void
{
self::$testedClass = Header::class;

self::$schemaFile = dirname(__FILE__, 4) . '/resources/schemas/soap-envelope-1.2.xsd';

self::$xmlRepresentation = DOMDocumentFactory::fromFile(
dirname(__FILE__, 3) . '/resources/xml/env/200305/Header.xml',
);
Expand Down
2 changes: 0 additions & 2 deletions tests/XML/env_200305/UpgradeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ public static function setUpBeforeClass(): void
{
self::$testedClass = Upgrade::class;

self::$schemaFile = dirname(__FILE__, 4) . '/resources/schemas/soap-envelope-1.2.xsd';

self::$xmlRepresentation = DOMDocumentFactory::fromFile(
dirname(__FILE__, 3) . '/resources/xml/env/200305/Upgrade.xml',
);
Expand Down
31 changes: 31 additions & 0 deletions tests/resources/schemas/simplesamlphp.xsd
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE schema
PUBLIC "-//W3C//DTD XMLSchema 200102//EN" "http://www.w3.org/2001/XMLSchema.dtd"
[
<!ATTLIST schema
xmlns:ds CDATA #FIXED "urn:x-simplesamlphp:namespace">
<!ENTITY ssp 'urn:x-simplesamlphp:namespace'>
<!ENTITY % p ''>
<!ENTITY % s ''>
]>

<!-- Schema for SimpleSAMLphp dummy classes -->


<schema xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:ssp="urn:x-simplesamlphp:namespace"
xmlns:ds="http://www.w3.org/2000/09/xmldsig#"
targetNamespace="urn:x-simplesamlphp:namespace"
version="0.1" elementFormDefault="qualified">

<import namespace='http://www.w3.org/2000/09/xmldsig#'
schemaLocation='../../../resources/schemas/soap-envelope-1.2.xsd'/>

<!-- Start Chunk -->

<element name="Chunk" type="string"/>

<!-- End Chunk -->

</schema>

Loading

0 comments on commit 01bc683

Please sign in to comment.