-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathOCSAuthAPITest.php
More file actions
273 lines (238 loc) · 9.46 KB
/
Copy pathOCSAuthAPITest.php
File metadata and controls
273 lines (238 loc) · 9.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
<?php
/**
* @author Björn Schießle <bjoern@schiessle.org>
* @author Robin Appelman <icewind@owncloud.com>
* @author Roeland Jago Douma <rullzer@users.noreply.github.com>
*
* @copyright Copyright (c) 2018, ownCloud GmbH
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
namespace OCA\Federation\Tests\API;
use OC\BackgroundJob\JobList;
use OCA\Federation\Controller\OCSAuthAPIController;
use OCA\Federation\DbHandler;
use OCA\Federation\TrustedServers;
use OCP\AppFramework\Http;
use OCP\ILogger;
use OCP\IRequest;
use OCP\Security\ISecureRandom;
use Test\TestCase;
class OCSAuthAPITest extends TestCase {
/** @var \PHPUnit\Framework\MockObject\MockObject | IRequest */
private $request;
/** @var \PHPUnit\Framework\MockObject\MockObject | ISecureRandom */
private $secureRandom;
/** @var \PHPUnit\Framework\MockObject\MockObject | JobList */
private $jobList;
/** @var \PHPUnit\Framework\MockObject\MockObject | TrustedServers */
private $trustedServers;
/** @var \PHPUnit\Framework\MockObject\MockObject | DbHandler */
private $dbHandler;
/** @var \PHPUnit\Framework\MockObject\MockObject | ILogger */
private $logger;
/** @var OCSAuthAPIController */
private $ocsAuthApi;
public function setUp(): void {
parent::setUp();
$this->request = $this->createMock(IRequest::class);
$this->secureRandom = $this->createMock(ISecureRandom::class);
$this->trustedServers = $this->getMockBuilder(TrustedServers::class)
->disableOriginalConstructor()->getMock();
$this->dbHandler = $this->getMockBuilder(DbHandler::class)
->disableOriginalConstructor()->getMock();
$this->jobList = $this->getMockBuilder(JobList::class)
->disableOriginalConstructor()->getMock();
$this->logger = $this->getMockBuilder(ILogger::class)
->disableOriginalConstructor()->getMock();
$this->ocsAuthApi = new OCSAuthAPIController(
'federation',
$this->request,
$this->secureRandom,
$this->jobList,
$this->trustedServers,
$this->dbHandler,
$this->logger
);
}
/**
* @dataProvider dataTestRequestSharedSecret
*
* @param string $token
* @param string $localToken
* @param bool $isTrustedServer
* @param int $expected
*/
public function testRequestSharedSecret($token, $localToken, $isTrustedServer, $expected) {
$url = 'url';
$this->trustedServers
->expects($this->once())
->method('isTrustedServer')->with($url)->willReturn($isTrustedServer);
$this->dbHandler->expects($this->any())
->method('getToken')->with($url)->willReturn($localToken);
if ($expected === Http::STATUS_OK) {
$this->jobList->expects($this->once())->method('add')
->with('OCA\Federation\BackgroundJob\GetSharedSecret', ['url' => $url, 'token' => $token]);
$this->jobList->expects($this->once())->method('remove')
->with('OCA\Federation\BackgroundJob\RequestSharedSecret', ['url' => $url, 'token' => $localToken]);
} else {
$this->jobList->expects($this->never())->method('add');
$this->jobList->expects($this->never())->method('remove');
}
$result = $this->ocsAuthApi->requestSharedSecret($url, $token);
$this->assertSame($expected, $result['statuscode']);
}
public function dataTestRequestSharedSecret() {
// Token pairs are chosen so that hash('sha256', $token) vs hash('sha256', $localToken)
// ordering matches the expected response. Using raw strcmp() on the tokens
// would give different results for the first two cases, which is exactly the
// behaviour that the oracle-prevention fix changes.
//
// Case 1: hash(token) > hash(localToken) => remote wins, we schedule GetSharedSecret => 200
// token=0eihx/zCxwyV localToken=hgSXvmcFnOp+
// strcmp(token, localToken) < 0 (old code would return 403, NEW code returns 200)
//
// Case 2: hash(token) < hash(localToken) => we win, remote backs off => 403
// token=Dvu0e+f+cC5F localToken=FBv96rzfQZB0
// strcmp(token, localToken) < 0 (both old and new code agree here)
//
// Case 3: server not trusted => always 403 regardless of tokens
return [
['0eihx/zCxwyV', 'hgSXvmcFnOp+', true, Http::STATUS_OK],
['Dvu0e+f+cC5F', 'FBv96rzfQZB0', false, Http::STATUS_FORBIDDEN],
['Dvu0e+f+cC5F', 'FBv96rzfQZB0', true, Http::STATUS_FORBIDDEN],
];
}
/**
* Regression test: the old strcmp()-based comparison leaked ordering information
* about the stored localToken to unauthenticated callers (binary search oracle).
*
* This test uses a concrete token pair where strcmp(localToken, token) > 0
* (old code would return 403) but strcmp(hash(localToken), hash(token)) <= 0
* (new code returns 200), demonstrating that the fix changes the behaviour
* for cases that would have been exploitable as an oracle while still
* producing the correct deterministic tiebreak result.
*
* Without the fix this test would return STATUS_FORBIDDEN for a case where
* the hash-based comparison says the remote server's token wins.
*/
public function testRequestSharedSecretNoOracleLeakage() {
// strcmp('DD7EOAhPjsziDBGa', '/Ewru/vr674cUicB') = 21 (localToken > token, old code: 403)
// strcmp(hash256('DD7EOAhPjsziDBGa'), hash256('/Ewru/vr674cUicB')) = -48 (new code: 200)
$url = 'https://remote.example.com';
$localToken = 'DD7EOAhPjsziDBGa';
$token = '/Ewru/vr674cUicB';
$this->trustedServers
->expects($this->once())
->method('isTrustedServer')->with($url)->willReturn(true);
$this->dbHandler
->expects($this->any())
->method('getToken')->with($url)->willReturn($localToken);
// With the fix the hash of $token > hash of $localToken, so the remote server
// wins the tiebreak and we must schedule GetSharedSecret (=> 200).
$this->jobList->expects($this->once())->method('add')
->with('OCA\Federation\BackgroundJob\GetSharedSecret', ['url' => $url, 'token' => $token]);
$this->jobList->expects($this->once())->method('remove')
->with('OCA\Federation\BackgroundJob\RequestSharedSecret', ['url' => $url, 'token' => $localToken]);
$result = $this->ocsAuthApi->requestSharedSecret($url, $token);
$this->assertSame(Http::STATUS_OK, $result['statuscode']);
}
/**
* @dataProvider dataTestGetSharedSecret
*
* @param bool $isTrustedServer
* @param bool $isValidToken
* @param int $expected
*/
public function testGetSharedSecret($isTrustedServer, $isValidToken, $expected) {
$url = 'url';
$token = 'token';
/** @var OCSAuthAPIController | \PHPUnit\Framework\MockObject\MockObject $ocsAuthApi */
$ocsAuthApi = $this->getMockBuilder(OCSAuthAPIController::class)
->setConstructorArgs(
[
'federation',
$this->request,
$this->secureRandom,
$this->jobList,
$this->trustedServers,
$this->dbHandler,
$this->logger
]
)->setMethods(['isValidToken'])->getMock();
$this->trustedServers
->expects($this->any())
->method('isTrustedServer')->with($url)->willReturn($isTrustedServer);
$ocsAuthApi->expects($this->any())
->method('isValidToken')->with($url, $token)->willReturn($isValidToken);
if ($expected === Http::STATUS_OK) {
$this->secureRandom->expects($this->once())->method('generate')->with(32)
->willReturn('secret');
$this->trustedServers->expects($this->once())
->method('addSharedSecret')->willReturn($url, 'secret');
$this->dbHandler->expects($this->once())
->method('addToken')->with($url, '');
} else {
$this->secureRandom->expects($this->never())->method('getMediumStrengthGenerator');
$this->secureRandom->expects($this->never())->method('generate');
$this->trustedServers->expects($this->never())->method('addSharedSecret');
$this->dbHandler->expects($this->never())->method('addToken');
}
$result = $ocsAuthApi->getSharedSecret($url, $token);
$this->assertSame($expected, $result['statuscode']);
if ($expected === Http::STATUS_OK) {
$data = $result['data'];
$this->assertSame('secret', $data['sharedSecret']);
}
}
public function dataTestGetSharedSecret() {
return [
[true, true, Http::STATUS_OK],
[false, true, Http::STATUS_FORBIDDEN],
[true, false, Http::STATUS_FORBIDDEN],
[false, false, Http::STATUS_FORBIDDEN],
];
}
/**
* @dataProvider dataTestIsTokenValid
*/
public function testIsTokenValid($storedToken, $requestToken): void {
$url = 'url';
/** @var OCSAuthAPIController | \PHPUnit\Framework\MockObject\MockObject $ocsAuthApi */
$ocsAuthApi = $this->getMockBuilder(OCSAuthAPIController::class)
->setConstructorArgs(
[
'federation',
$this->request,
$this->secureRandom,
$this->jobList,
$this->trustedServers,
$this->dbHandler,
$this->logger
]
)->getMock();
$this->dbHandler
->method('getToken')->willReturn($storedToken);
$result = $ocsAuthApi->isValidToken($url, $requestToken);
$this->assertFalse($result);
}
public function dataTestIsTokenValid(): \Generator {
yield 'no match' => ['xxx', 'yyy'];
yield 'empty token stored' => ['', 'yyy'];
yield 'null token stored' => [null, 'yyy'];
yield 'empty token requested' => ['xxx', ''];
yield 'null token requested' => ['xxx', null];
}
}