ILIAS  trunk Revision v13.0_alpha-62-g3b72d83f2e5
BaseDeliveryIsolationTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
28use ILIAS\HTTP\Services as HttpServices;
29use PHPUnit\Framework\TestCase;
30use Psr\Http\Message\ResponseInterface;
31use Psr\Http\Message\ServerRequestInterface;
32use Psr\Http\Message\UriInterface;
33
37final class BaseDeliveryIsolationTest extends TestCase
38{
39 private function delivery(IsolationConfig $isolation, string $request_host = 'app.example.org'): BaseDelivery
40 {
41 $uri = $this->createStub(UriInterface::class);
42 $uri->method('getHost')->willReturn($request_host);
43
44 $request = $this->createStub(ServerRequestInterface::class);
45 $request->method('getUri')->willReturn($uri);
46
47 $http = $this->createStub(HttpServices::class);
48 $http->method('request')->willReturn($request);
49
50 $rb = $this->createStub(ResponseBuilder::class);
51
52 return new class ($http, $rb, $rb, $isolation) extends BaseDelivery {
53 public function exposedHostAllowed(): bool
54 {
55 return $this->isRequestHostAllowed();
56 }
57
58 public function exposedOnContentHost(): bool
59 {
60 return $this->isRequestOnContentHost();
61 }
62
63 public function exposedIsolationHeaders(ResponseInterface $r): ResponseInterface
64 {
65 return $this->applyIsolationHeaders($r);
66 }
67
68 public function exposedGeneralHeaders(ResponseInterface $r): ResponseInterface
69 {
70 return $this->setGeneralHeaders(
71 $r,
72 '/tmp/user_content.txt',
73 'text/plain',
74 'user_content.txt',
75 Disposition::INLINE
76 );
77 }
78 };
79 }
80
81 private function activeConfig(): IsolationConfig
82 {
85 IsolationConfig::KEY_CONTENT_DOMAIN => 'https://content.example.org',
86 IsolationConfig::KEY_ILIAS_DOMAIN => 'https://app.example.org',
87 ]);
88 }
89
91 {
92 $delivery = $this->delivery(IsolationConfig::disabled(), 'anything.example.org');
93 $this->assertTrue($delivery->exposedHostAllowed());
94 }
95
97 {
98 $delivery = $this->delivery($this->activeConfig(), 'content.example.org');
99 $this->assertTrue($delivery->exposedHostAllowed());
100 }
101
102 public function testHostAllowedIsCaseInsensitive(): void
103 {
104 $delivery = $this->delivery($this->activeConfig(), 'Content.Example.ORG');
105 $this->assertTrue($delivery->exposedHostAllowed());
106 }
107
109 {
110 $delivery = $this->delivery($this->activeConfig(), 'app.example.org');
111 $this->assertFalse($delivery->exposedHostAllowed());
112 }
113
115 {
116 $delivery = $this->delivery(IsolationConfig::disabled(), 'content.example.org');
117 $this->assertFalse($delivery->exposedOnContentHost());
118 }
119
121 {
122 $delivery = $this->delivery($this->activeConfig(), 'content.example.org');
123 $this->assertTrue($delivery->exposedOnContentHost());
124 }
125
127 {
128 $delivery = $this->delivery($this->activeConfig(), 'CONTENT.example.org');
129 $this->assertTrue($delivery->exposedOnContentHost());
130 }
131
133 {
134 $delivery = $this->delivery($this->activeConfig(), 'app.example.org');
135 $this->assertFalse($delivery->exposedOnContentHost());
136 }
137
139 {
140 $delivery = $this->delivery(IsolationConfig::disabled());
141 $r = $delivery->exposedIsolationHeaders((new MessageFactoryImpl())->response());
142
143 $this->assertFalse($r->hasHeader('X-Content-Type-Options'));
144 $this->assertFalse($r->hasHeader('Cross-Origin-Resource-Policy'));
145 $this->assertFalse($r->hasHeader('Referrer-Policy'));
146 $this->assertFalse($r->hasHeader('Access-Control-Allow-Origin'));
147 }
148
149 public function testIsolationHeadersWhenActive(): void
150 {
151 $delivery = $this->delivery($this->activeConfig());
152 $r = $delivery->exposedIsolationHeaders((new MessageFactoryImpl())->response());
153
154 $this->assertSame('nosniff', $r->getHeaderLine('X-Content-Type-Options'));
155 $this->assertSame('cross-origin', $r->getHeaderLine('Cross-Origin-Resource-Policy'));
156 $this->assertSame('no-referrer', $r->getHeaderLine('Referrer-Policy'));
157 $this->assertSame('https://app.example.org', $r->getHeaderLine('Access-Control-Allow-Origin'));
158 $this->assertSame('Origin', $r->getHeaderLine('Vary'));
159 }
160
162 {
163 $delivery = $this->delivery($this->activeConfig());
164 $r = $delivery->exposedGeneralHeaders((new MessageFactoryImpl())->response());
165
166 $this->assertSame('nosniff', $r->getHeaderLine('X-Content-Type-Options'));
167 $this->assertSame('cross-origin', $r->getHeaderLine('Cross-Origin-Resource-Policy'));
168 $this->assertSame('no-referrer', $r->getHeaderLine('Referrer-Policy'));
169 $this->assertSame('https://app.example.org', $r->getHeaderLine('Access-Control-Allow-Origin'));
170 $this->assertSame('Origin', $r->getHeaderLine('Vary'));
171 }
172
174 {
175 $delivery = $this->delivery($this->activeConfig());
176 $r = $delivery->exposedGeneralHeaders((new MessageFactoryImpl())->response());
177
178 $this->assertSame('text/plain', $r->getHeaderLine('Content-Type'));
179 $this->assertSame(
180 'inline; filename="user_content.txt"; filename*=UTF-8\'\'user_content.txt',
181 $r->getHeaderLine('Content-Disposition')
182 );
183 }
184
186 {
187 $delivery = $this->delivery(IsolationConfig::disabled());
188 $r = $delivery->exposedGeneralHeaders((new MessageFactoryImpl())->response());
189
190 $this->assertSame('text/plain', $r->getHeaderLine('Content-Type'));
191 $this->assertFalse($r->hasHeader('X-Content-Type-Options'));
192 $this->assertFalse($r->hasHeader('Cross-Origin-Resource-Policy'));
193 $this->assertFalse($r->hasHeader('Referrer-Policy'));
194 $this->assertFalse($r->hasHeader('Access-Control-Allow-Origin'));
195 }
196}
Read-only configuration of the IRSS User Content Isolation feature.
The currently used psr-7 implementation is created and published by guzzle under the MIT license.
Class Services.
Definition: Services.php:43
delivery(IsolationConfig $isolation, string $request_host='app.example.org')
$request
@ilCtrl_Calls ilObjLearningSequenceContentGUI: ilObjLearningSequenceConditionsGUI
Definition: ltiauth.php:39