Skip to content

Commit 25d308e

Browse files
committed
Sanitize toArray-ouput on ContactPerson and Organization
1 parent 1a84289 commit 25d308e

4 files changed

Lines changed: 26 additions & 18 deletions

File tree

src/SAML2/XML/md/ContactPerson.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use SimpleSAML\XML\ExtendableAttributesTrait;
2020
use SimpleSAML\XML\Utils as XMLUtils;
2121

22+
use function array_filter;
2223
use function array_key_exists;
2324
use function array_map;
2425
use function array_pop;
@@ -246,7 +247,7 @@ public static function fromArray(array $data): static
246247
$Company = isset($data['Company']) ? new Company($data['Company']) : null;
247248
$GivenName = isset($data['GivenName']) ? new GivenName($data['GivenName']) : null;
248249
$SurName = isset($data['SurName']) ? new SurName($data['SurName']) : null;
249-
$Extensions = $data['Extensions'] ?? null;
250+
$Extensions = $data['Extensions'] ? new Extensions($data['Extensions']) : null;
250251

251252
$EmailAddress = [];
252253
if (array_key_exists('EmailAddress', $data)) {
@@ -306,7 +307,7 @@ public function toArray(): array
306307
'SurName' => $this->getSurName()?->getContent(),
307308
'EmailAddress' => [],
308309
'TelephoneNumber' => [],
309-
'Extensions' => $this->Extensions,
310+
'Extensions' => $this->Extensions->getList(),
310311
'attributes' => [],
311312
];
312313

@@ -322,6 +323,6 @@ public function toArray(): array
322323
$data['attributes'][] = $attr->toArray();
323324
}
324325

325-
return $data;
326+
return array_filter($data);
326327
}
327328
}

src/SAML2/XML/md/Organization.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use SimpleSAML\XML\ExtendableAttributesTrait;
2020
use SimpleSAML\XML\Utils as XMLUtils;
2121

22+
use function array_filter;
2223
use function array_key_exists;
2324
use function array_merge;
2425

@@ -205,7 +206,7 @@ public static function fromArray(array $data): static
205206
$orgURLs[] = OrganizationURL::fromArray($data['OrganizationURL']);
206207
}
207208

208-
$Extensions = $data['Extensions'] ?? null;
209+
$Extensions = $data['Extensions'] ? new Extensions($data['Extensions']) : null;
209210

210211
$attributes = [];
211212
if (array_key_exists('attributes', $data)) {
@@ -245,7 +246,7 @@ public function toArray(): array
245246
'OrganizationName' => [],
246247
'OrganizationDisplayName' => [],
247248
'OrganizationURL' => [],
248-
'Extensions' => $this->getExtensions(),
249+
'Extensions' => $this->getExtensions()?->getList(),
249250
'attributes' => [],
250251
];
251252

@@ -268,6 +269,6 @@ public function toArray(): array
268269
$data['attributes'][] = $attr->toArray();
269270
}
270271

271-
return $data;
272+
return array_filter($data);
272273
}
273274
}

tests/SAML2/XML/md/ContactPersonTest.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ final class ContactPersonTest extends TestCase
4141
use SchemaValidationTestTrait;
4242
use SerializableElementTestTrait;
4343

44+
/** @var \DOMDocument */
45+
protected DOMDocument $ext;
46+
4447

4548
/**
4649
*/
@@ -54,12 +57,16 @@ protected function setUp(): void
5457
dirname(__FILE__, 4) . '/resources/xml/md_ContactPerson.xml',
5558
);
5659

60+
$this->ext = DOMDocumentFactory::fromString(
61+
'<some:Ext xmlns:some="urn:mace:some:metadata:1.0">SomeExtension</some:Ext>',
62+
);
63+
5764
$this->arrayRepresentation = [
5865
'ContactType' => 'administrative',
5966
'Company' => 'SimpleSAMLphp',
6067
'GivenName' => 'Lead',
6168
'SurName' => 'Developer',
62-
'Extensions' => null,
69+
'Extensions' => [new Chunk($this->ext->documentElement)],
6370
'EmailAddress' => ['mailto:lead.developer@example.org'],
6471
'TelephoneNumber' => ['+1234567890'],
6572
'attributes' => [
@@ -82,10 +89,6 @@ protected function setUp(): void
8289
*/
8390
public function testMarshalling(): void
8491
{
85-
$ext = DOMDocumentFactory::fromString(
86-
'<some:Ext xmlns:some="urn:mace:some:metadata:1.0">SomeExtension</some:Ext>',
87-
);
88-
8992
$attr1 = new XMLAttribute('urn:test:something', 'test', 'attr1', 'testval1');
9093
$attr2 = new XMLAttribute('urn:test:something', 'test', 'attr2', 'testval2');
9194

@@ -96,7 +99,7 @@ public function testMarshalling(): void
9699
new SurName('Doe'),
97100
new Extensions(
98101
[
99-
new Chunk($ext->documentElement),
102+
new Chunk($this->ext->documentElement),
100103
],
101104
),
102105
[new EmailAddress('jdoe@test.company'), new EmailAddress('john.doe@test.company')],

tests/SAML2/XML/md/OrganizationTest.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ final class OrganizationTest extends TestCase
3434
use SchemaValidationTestTrait;
3535
use SerializableElementTestTrait;
3636

37+
/** @var \DOMDocument */
38+
protected DOMDocument $ext;
39+
3740

3841
/**
3942
*/
@@ -43,11 +46,15 @@ protected function setUp(): void
4346

4447
$this->testedClass = Organization::class;
4548

49+
$this->ext = DOMDocumentFactory::fromString(
50+
'<some:Ext xmlns:some="urn:mace:some:metadata:1.0">SomeExtension</some:Ext>'
51+
);
52+
4653
$this->arrayRepresentation = [
4754
'OrganizationName' => ['en' => 'SSP'],
4855
'OrganizationDisplayName' => ['en' => 'SimpleSAMLphp'],
4956
'OrganizationURL' => ['en' => 'https://simplesamlphp.org'],
50-
'Extensions' => null,
57+
'Extensions' => [new Chunk($this->ext->documentElement)],
5158
'attributes' => [
5259
[
5360
'namespaceURI' => 'urn:test:something',
@@ -72,17 +79,13 @@ protected function setUp(): void
7279
*/
7380
public function testMarshalling(): void
7481
{
75-
$ext = DOMDocumentFactory::fromString(
76-
'<some:Ext xmlns:some="urn:mace:some:metadata:1.0">SomeExtension</some:Ext>'
77-
);
78-
7982
$org = new Organization(
8083
[new OrganizationName('en', 'Identity Providers R US')],
8184
[new OrganizationDisplayName('en', 'Identity Providers R US, a Division of Lerxst Corp.')],
8285
[new OrganizationURL('en', 'https://IdentityProvider.com')],
8386
new Extensions(
8487
[
85-
new Chunk($ext->documentElement),
88+
new Chunk($this->ext->documentElement),
8689
],
8790
),
8891
);

0 commit comments

Comments
 (0)