Skip to content

Commit 0ebd44a

Browse files
committed
Migrate tests of forwarded for headers check
Signed-off-by: Côme Chilliet <[email protected]>
1 parent 0107c7e commit 0ebd44a

File tree

2 files changed

+142
-88
lines changed

2 files changed

+142
-88
lines changed

apps/settings/tests/Controller/CheckSetupControllerTest.php

Lines changed: 2 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -204,87 +204,6 @@ public function removeTestDirectories() {
204204
$this->dirsToRemove = [];
205205
}
206206

207-
/**
208-
* @dataProvider dataForwardedForHeadersWorking
209-
*
210-
* @param array $trustedProxies
211-
* @param string $remoteAddrNotForwarded
212-
* @param string $remoteAddr
213-
* @param bool $result
214-
*/
215-
public function testForwardedForHeadersWorking(array $trustedProxies, string $remoteAddrNotForwarded, string $remoteAddr, bool $result): void {
216-
$this->config->expects($this->once())
217-
->method('getSystemValue')
218-
->with('trusted_proxies', [])
219-
->willReturn($trustedProxies);
220-
$this->request->expects($this->atLeastOnce())
221-
->method('getHeader')
222-
->willReturnMap([
223-
['REMOTE_ADDR', $remoteAddrNotForwarded],
224-
['X-Forwarded-Host', '']
225-
]);
226-
$this->request->expects($this->any())
227-
->method('getRemoteAddress')
228-
->willReturn($remoteAddr);
229-
230-
$this->assertEquals(
231-
$result,
232-
self::invokePrivate($this->checkSetupController, 'forwardedForHeadersWorking')
233-
);
234-
}
235-
236-
public function dataForwardedForHeadersWorking(): array {
237-
return [
238-
// description => trusted proxies, getHeader('REMOTE_ADDR'), getRemoteAddr, expected result
239-
'no trusted proxies' => [[], '2.2.2.2', '2.2.2.2', true],
240-
'trusted proxy, remote addr not trusted proxy' => [['1.1.1.1'], '2.2.2.2', '2.2.2.2', true],
241-
'trusted proxy, remote addr is trusted proxy, x-forwarded-for working' => [['1.1.1.1'], '1.1.1.1', '2.2.2.2', true],
242-
'trusted proxy, remote addr is trusted proxy, x-forwarded-for not set' => [['1.1.1.1'], '1.1.1.1', '1.1.1.1', false],
243-
];
244-
}
245-
246-
public function testForwardedHostPresentButTrustedProxiesNotAnArray(): void {
247-
$this->config->expects($this->once())
248-
->method('getSystemValue')
249-
->with('trusted_proxies', [])
250-
->willReturn('1.1.1.1');
251-
$this->request->expects($this->atLeastOnce())
252-
->method('getHeader')
253-
->willReturnMap([
254-
['REMOTE_ADDR', '1.1.1.1'],
255-
['X-Forwarded-Host', 'nextcloud.test']
256-
]);
257-
$this->request->expects($this->any())
258-
->method('getRemoteAddress')
259-
->willReturn('1.1.1.1');
260-
261-
$this->assertEquals(
262-
false,
263-
self::invokePrivate($this->checkSetupController, 'forwardedForHeadersWorking')
264-
);
265-
}
266-
267-
public function testForwardedHostPresentButTrustedProxiesEmpty(): void {
268-
$this->config->expects($this->once())
269-
->method('getSystemValue')
270-
->with('trusted_proxies', [])
271-
->willReturn([]);
272-
$this->request->expects($this->atLeastOnce())
273-
->method('getHeader')
274-
->willReturnMap([
275-
['REMOTE_ADDR', '1.1.1.1'],
276-
['X-Forwarded-Host', 'nextcloud.test']
277-
]);
278-
$this->request->expects($this->any())
279-
->method('getRemoteAddress')
280-
->willReturn('1.1.1.1');
281-
282-
$this->assertEquals(
283-
false,
284-
self::invokePrivate($this->checkSetupController, 'forwardedForHeadersWorking')
285-
);
286-
}
287-
288207
public function testCheck() {
289208
$this->config->expects($this->any())
290209
->method('getAppValue')
@@ -302,13 +221,8 @@ public function testCheck() {
302221
['appstoreenabled', true, false],
303222
]);
304223

305-
$this->request->expects($this->atLeastOnce())
306-
->method('getHeader')
307-
->willReturnMap([
308-
['REMOTE_ADDR', '4.3.2.1'],
309-
['X-Forwarded-Host', '']
310-
]);
311-
224+
$this->request->expects($this->never())
225+
->method('getHeader');
312226
$this->clientService->expects($this->never())
313227
->method('newClient');
314228
$this->checkSetupController
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* @copyright Copyright (c) 2021 Morris Jobke <[email protected]>
7+
*
8+
* @author Morris Jobke <[email protected]>
9+
* @author Côme Chilliet <[email protected]>
10+
*
11+
* @license GNU AGPL version 3 or any later version
12+
*
13+
* This program is free software: you can redistribute it and/or modify
14+
* it under the terms of the GNU Affero General Public License as
15+
* published by the Free Software Foundation, either version 3 of the
16+
* License, or (at your option) any later version.
17+
*
18+
* This program is distributed in the hope that it will be useful,
19+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
20+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21+
* GNU Affero General Public License for more details.
22+
*
23+
* You should have received a copy of the GNU Affero General Public License
24+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
25+
*
26+
*/
27+
namespace OCA\Settings\Tests;
28+
29+
use OCA\Settings\SetupChecks\ForwardedForHeaders;
30+
use OCP\IConfig;
31+
use OCP\IL10N;
32+
use OCP\IRequest;
33+
use OCP\IURLGenerator;
34+
use OCP\SetupCheck\SetupResult;
35+
use Test\TestCase;
36+
37+
class ForwardedForHeadersTest extends TestCase {
38+
private IL10N $l10n;
39+
private IConfig $config;
40+
private IURLGenerator $urlGenerator;
41+
private IRequest $request;
42+
private ForwardedForHeaders $check;
43+
44+
protected function setUp(): void {
45+
parent::setUp();
46+
47+
$this->l10n = $this->getMockBuilder(IL10N::class)
48+
->disableOriginalConstructor()->getMock();
49+
$this->l10n->expects($this->any())
50+
->method('t')
51+
->willReturnCallback(function ($message, array $replace) {
52+
return vsprintf($message, $replace);
53+
});
54+
$this->config = $this->getMockBuilder(IConfig::class)->getMock();
55+
$this->urlGenerator = $this->getMockBuilder(IURLGenerator::class)->getMock();
56+
$this->request = $this->getMockBuilder(IRequest::class)->getMock();
57+
$this->check = new ForwardedForHeaders(
58+
$this->l10n,
59+
$this->config,
60+
$this->urlGenerator,
61+
$this->request,
62+
);
63+
}
64+
65+
/**
66+
* @dataProvider dataForwardedForHeadersWorking
67+
*/
68+
public function testForwardedForHeadersWorking(array $trustedProxies, string $remoteAddrNotForwarded, string $remoteAddr, string $result): void {
69+
$this->config->expects($this->once())
70+
->method('getSystemValue')
71+
->with('trusted_proxies', [])
72+
->willReturn($trustedProxies);
73+
$this->request->expects($this->atLeastOnce())
74+
->method('getHeader')
75+
->willReturnMap([
76+
['REMOTE_ADDR', $remoteAddrNotForwarded],
77+
['X-Forwarded-Host', '']
78+
]);
79+
$this->request->expects($this->any())
80+
->method('getRemoteAddress')
81+
->willReturn($remoteAddr);
82+
83+
$this->assertEquals(
84+
$result,
85+
$this->check->run()->getSeverity()
86+
);
87+
}
88+
89+
public function dataForwardedForHeadersWorking(): array {
90+
return [
91+
// description => trusted proxies, getHeader('REMOTE_ADDR'), getRemoteAddr, expected result
92+
'no trusted proxies' => [[], '2.2.2.2', '2.2.2.2', SetupResult::SUCCESS],
93+
'trusted proxy, remote addr not trusted proxy' => [['1.1.1.1'], '2.2.2.2', '2.2.2.2', SetupResult::SUCCESS],
94+
'trusted proxy, remote addr is trusted proxy, x-forwarded-for working' => [['1.1.1.1'], '1.1.1.1', '2.2.2.2', SetupResult::SUCCESS],
95+
'trusted proxy, remote addr is trusted proxy, x-forwarded-for not set' => [['1.1.1.1'], '1.1.1.1', '1.1.1.1', SetupResult::WARNING],
96+
];
97+
}
98+
99+
public function testForwardedHostPresentButTrustedProxiesNotAnArray(): void {
100+
$this->config->expects($this->once())
101+
->method('getSystemValue')
102+
->with('trusted_proxies', [])
103+
->willReturn('1.1.1.1');
104+
$this->request->expects($this->atLeastOnce())
105+
->method('getHeader')
106+
->willReturnMap([
107+
['REMOTE_ADDR', '1.1.1.1'],
108+
['X-Forwarded-Host', 'nextcloud.test']
109+
]);
110+
$this->request->expects($this->any())
111+
->method('getRemoteAddress')
112+
->willReturn('1.1.1.1');
113+
114+
$this->assertEquals(
115+
SetupResult::ERROR,
116+
$this->check->run()->getSeverity()
117+
);
118+
}
119+
120+
public function testForwardedHostPresentButTrustedProxiesEmpty(): void {
121+
$this->config->expects($this->once())
122+
->method('getSystemValue')
123+
->with('trusted_proxies', [])
124+
->willReturn([]);
125+
$this->request->expects($this->atLeastOnce())
126+
->method('getHeader')
127+
->willReturnMap([
128+
['REMOTE_ADDR', '1.1.1.1'],
129+
['X-Forwarded-Host', 'nextcloud.test']
130+
]);
131+
$this->request->expects($this->any())
132+
->method('getRemoteAddress')
133+
->willReturn('1.1.1.1');
134+
135+
$this->assertEquals(
136+
SetupResult::WARNING,
137+
$this->check->run()->getSeverity()
138+
);
139+
}
140+
}

0 commit comments

Comments
 (0)