‪TYPO3CMS  ‪main
ModelServiceTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use PHPUnit\Framework\Attributes\DataProvider;
21 use PHPUnit\Framework\Attributes\Test;
30 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
31 
32 final class ‪ModelServiceTest extends FunctionalTestCase
33 {
35 
36  protected function ‪setUp(): void
37  {
38  parent::setUp();
39  $this->subject = $this->get(ModelService::class);
40  }
41 
42  protected function ‪tearDown(): void
43  {
44  unset($this->subject);
45  }
46 
47  public static function ‪enumSourceInterfaceIsBuiltFromStringDataProvider(): \Generator
48  {
49  yield 'nonce-proxy' => ["'nonce-anything'", SourceKeyword::nonceProxy];
50  yield 'none' => ["'none'", SourceKeyword::none];
51  yield 'self' => ["'self'", SourceKeyword::self];
52  yield 'strict-dynamic' => ["'strict-dynamic'", SourceKeyword::strictDynamic];
53  yield 'unsafe-inline' => ["'unsafe-inline'", SourceKeyword::unsafeInline];
54  yield 'unsafe-eval' => ["'unsafe-eval'", SourceKeyword::unsafeEval];
55 
56  yield 'blob:' => ['blob:', SourceScheme::blob];
57  yield 'data:' => ['data:', SourceScheme::data];
58  yield 'https:' => ['https:', SourceScheme::https];
59  yield 'wss:' => ['wss:', ‪SourceScheme::wss];
60  }
61 
62  #[DataProvider('enumSourceInterfaceIsBuiltFromStringDataProvider')]
63  #[Test]
64  public function ‪enumSourceInterfaceIsBuiltFromString(string $string, ‪SourceInterface $expectation): void
65  {
66  self::assertSame($expectation, $this->subject->buildSourceFromString($string));
67  }
68 
69  #[Test]
70  public function ‪uriValueIsBuiltFromString(): void
71  {
72  $uri = 'https://*.example.org/';
73  $source = $this->subject->buildSourceFromString($uri);
74  self::assertInstanceOf(UriValue::class, $source);
75  self::assertSame($uri, (string)$source);
76  }
77 
78  #[Test]
79  public function ‪rawValueIsBuiltFromString(): void
80  {
81  $value = 'https:////slashes.example.org';
82  $source = $this->subject->buildSourceFromString($value);
83  self::assertInstanceOf(RawValue::class, $source);
84  self::assertSame($value, (string)$source);
85  }
86 
87  #[Test]
88  public function ‪hashValueIsBuiltFromString(): void
89  {
90  $hash = hash('sha256', 'test', true);
91  $hashB64 = base64_encode($hash);
92  $value = sprintf("'sha256-%s'", $hashB64);
93  $source = $this->subject->buildSourceFromString($value);
94  self::assertInstanceOf(HashValue::class, $source);
95  self::assertSame($hashB64, $source->value);
96  }
97 
98  #[Test]
99  public function ‪urlHashProxyIsBuiltFromString(): void
100  {
101  ‪$url = 'https://example.org/file.js';
102  $value = '\'hash-proxy-{"type":"sha256","urls":["' . $url . '"]}\'';
103  $source = $this->subject->buildSourceFromString($value);
104 
105  self::assertInstanceOf(HashProxy::class, $source);
106  $object = new \ReflectionObject($source);
107  $property = $object->getProperty('urls');
108  self::assertSame(‪$url, $property->getValue($source)[0] ?? null);
109  }
110 
111  public static function ‪buildMutationFromArrayThrowsValueErrorDataProvider(): \Generator
112  {
113  yield 'undefined mutation mode' => [
114  [
115  'directive' => 'script-src',
116  'sources' => ['https:'],
117  ],
118  '"" is not a valid backing value for enum',
119  ];
120  yield 'invalid mutation mode' => [
121  [
122  'mode' => 'invalid',
123  'directive' => 'script-src',
124  'sources' => ['https:'],
125  ],
126  '"invalid" is not a valid backing value for enum',
127  ];
128  yield 'undefined directive' => [
129  [
130  'mode' => 'extend',
131  'sources' => ['https:'],
132  ],
133  '"" is not a valid backing value for enum',
134  ];
135  yield "source 'unsafe-inline' used as directive" => [
136  [
137  'mode' => 'extend',
138  'directive' => "'unsafe-inline'",
139  'sources' => ['https:'],
140  ],
141  '"\'unsafe-inline\'" is not a valid backing value for enum',
142  ];
143  }
144 
145  #[DataProvider('buildMutationFromArrayThrowsValueErrorDataProvider')]
146  #[Test]
147  public function ‪buildMutationFromArrayThrowsValueError(array $array, string $expectedErrorMessage): void
148  {
149  $this->expectException(\ValueError::class);
150  $this->expectExceptionMessage($expectedErrorMessage);
151  $this->subject->buildMutationFromArray($array);
152  }
153 }
‪TYPO3\CMS\Core\Tests\Functional\Security\ContentSecurityPolicy\ModelServiceTest\tearDown
‪tearDown()
Definition: ModelServiceTest.php:42
‪TYPO3\CMS\Core\Tests\Functional\Security\ContentSecurityPolicy\ModelServiceTest
Definition: ModelServiceTest.php:33
‪TYPO3\CMS\Core\Tests\Functional\Security\ContentSecurityPolicy\ModelServiceTest\rawValueIsBuiltFromString
‪rawValueIsBuiltFromString()
Definition: ModelServiceTest.php:79
‪TYPO3\CMS\Core\Security\ContentSecurityPolicy\RawValue
Definition: RawValue.php:27
‪TYPO3\CMS\Core\Security\ContentSecurityPolicy\HashValue
Definition: HashValue.php:27
‪TYPO3\CMS\Core\Tests\Functional\Security\ContentSecurityPolicy\ModelServiceTest\buildMutationFromArrayThrowsValueError
‪buildMutationFromArrayThrowsValueError(array $array, string $expectedErrorMessage)
Definition: ModelServiceTest.php:147
‪TYPO3\CMS\Core\Tests\Functional\Security\ContentSecurityPolicy\ModelServiceTest\uriValueIsBuiltFromString
‪uriValueIsBuiltFromString()
Definition: ModelServiceTest.php:70
‪TYPO3\CMS\Core\Security\ContentSecurityPolicy\ModelService
Definition: ModelService.php:28
‪TYPO3\CMS\Core\Security\ContentSecurityPolicy\SourceInterface
Definition: SourceInterface.php:27
‪TYPO3\CMS\Core\Tests\Functional\Security\ContentSecurityPolicy\ModelServiceTest\enumSourceInterfaceIsBuiltFromStringDataProvider
‪static enumSourceInterfaceIsBuiltFromStringDataProvider()
Definition: ModelServiceTest.php:47
‪TYPO3\CMS\Core\Tests\Functional\Security\ContentSecurityPolicy\ModelServiceTest\setUp
‪setUp()
Definition: ModelServiceTest.php:36
‪TYPO3\CMS\Core\Tests\Functional\Security\ContentSecurityPolicy\ModelServiceTest\hashValueIsBuiltFromString
‪hashValueIsBuiltFromString()
Definition: ModelServiceTest.php:88
‪TYPO3\CMS\Core\Security\ContentSecurityPolicy\HashProxy
Definition: HashProxy.php:32
‪TYPO3\CMS\Core\Tests\Functional\Security\ContentSecurityPolicy
Definition: ModelServiceTest.php:18
‪TYPO3\CMS\Core\Security\ContentSecurityPolicy\wss
‪@ wss
Definition: SourceScheme.php:33
‪TYPO3\CMS\Core\Tests\Functional\Security\ContentSecurityPolicy\ModelServiceTest\urlHashProxyIsBuiltFromString
‪urlHashProxyIsBuiltFromString()
Definition: ModelServiceTest.php:99
‪TYPO3\CMS\Core\Security\ContentSecurityPolicy\SourceKeyword
‪SourceKeyword
Definition: SourceKeyword.php:25
‪TYPO3\CMS\Webhooks\Message\$url
‪identifier readonly UriInterface $url
Definition: LoginErrorOccurredMessage.php:36
‪TYPO3\CMS\Core\Tests\Functional\Security\ContentSecurityPolicy\ModelServiceTest\buildMutationFromArrayThrowsValueErrorDataProvider
‪static buildMutationFromArrayThrowsValueErrorDataProvider()
Definition: ModelServiceTest.php:111
‪TYPO3\CMS\Core\Security\ContentSecurityPolicy\UriValue
Definition: UriValue.php:29
‪TYPO3\CMS\Core\Tests\Functional\Security\ContentSecurityPolicy\ModelServiceTest\enumSourceInterfaceIsBuiltFromString
‪enumSourceInterfaceIsBuiltFromString(string $string, SourceInterface $expectation)
Definition: ModelServiceTest.php:64
‪TYPO3\CMS\Core\Security\ContentSecurityPolicy\SourceScheme
‪SourceScheme
Definition: SourceScheme.php:25
‪TYPO3\CMS\Core\Tests\Functional\Security\ContentSecurityPolicy\ModelServiceTest\$subject
‪ModelService $subject
Definition: ModelServiceTest.php:34