Skip to content

Commit 504eae6

Browse files
committed
refactor: Apply rector Nextcloud 27 set
Signed-off-by: provokateurin <kate@provokateurin.de>
1 parent d59338b commit 504eae6

7 files changed

Lines changed: 92 additions & 80 deletions

File tree

REUSE.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ SPDX-FileCopyrightText = "2023 Nextcloud GmbH and Nextcloud contributors"
250250
SPDX-License-Identifier = "AGPL-3.0-or-later"
251251

252252
[[annotations]]
253-
path = ["apps/dav/lib/ExampleContentFiles/exampleContact.vcf", "tests/data/testvideo-remote-file.mp4", "tests/lib/AppFramework/Middleware/Mock/UseSessionController.php"]
253+
path = ["apps/dav/lib/ExampleContentFiles/exampleContact.vcf", "tests/data/testvideo-remote-file.mp4", "tests/lib/AppFramework/Middleware/Mock/UseSessionController.php", "tests/lib/AppFramework/Middleware/Security/Mock/BruteForceMiddlewareController.php", "tests/lib/AppFramework/Middleware/Security/Mock/RateLimitingMiddlewareController.php"]
254254
precedence = "aggregate"
255255
SPDX-FileCopyrightText = "2025 Nextcloud GmbH and Nextcloud contributors"
256256
SPDX-License-Identifier = "AGPL-3.0-or-later"

build/rector.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ public function shouldSkip(File $file, FullyQualifiedObjectType $fullyQualifiedO
7979
$nextcloudDir . '/apps/*/config/*',
8080
// The mock classes are excluded, as the tests explicitly test the annotations which should not be migrated to attributes
8181
$nextcloudDir . '/tests/lib/AppFramework/Middleware/Mock/*',
82+
$nextcloudDir . '/tests/lib/AppFramework/Middleware/Security/Mock/*',
8283
])
8384
// uncomment to reach your current PHP version
8485
// ->withPhpSets()
@@ -94,7 +95,7 @@ public function shouldSkip(File $file, FullyQualifiedObjectType $fullyQualifiedO
9495
'rename_property' => true,
9596
])
9697
->withSets([
97-
NextcloudSets::NEXTCLOUD_26,
98+
NextcloudSets::NEXTCLOUD_27,
9899
]);
99100

100101
$config->registerService(NextcloudNamespaceSkipVoter::class, tag:ClassNameImportSkipVoterInterface::class);

core/Controller/TwoFactorChallengeController.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
1515
use OCP\AppFramework\Http\Attribute\NoCSRFRequired;
1616
use OCP\AppFramework\Http\Attribute\OpenAPI;
17+
use OCP\AppFramework\Http\Attribute\UserRateLimit;
1718
use OCP\AppFramework\Http\Attribute\UseSession;
1819
use OCP\AppFramework\Http\RedirectResponse;
1920
use OCP\AppFramework\Http\StandaloneTemplateResponse;
@@ -150,7 +151,6 @@ public function showChallenge($challengeProviderId, $redirect_url) {
150151
/**
151152
* @TwoFactorSetUpDoneRequired
152153
*
153-
* @UserRateThrottle(limit=5, period=100)
154154
*
155155
* @param string $challengeProviderId
156156
* @param string $challenge
@@ -161,6 +161,7 @@ public function showChallenge($challengeProviderId, $redirect_url) {
161161
#[NoCSRFRequired]
162162
#[UseSession]
163163
#[FrontpageRoute(verb: 'POST', url: '/login/challenge/{challengeProviderId}')]
164+
#[UserRateLimit(limit: 5, period: 100)]
164165
public function solveChallenge($challengeProviderId, $challenge, $redirect_url = null) {
165166
$user = $this->userSession->getUser();
166167
$provider = $this->twoFactorManager->getProvider($user, $challengeProviderId);

tests/lib/AppFramework/Middleware/Security/BruteForceMiddlewareTest.php

Lines changed: 13 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -9,34 +9,13 @@
99

1010
use OC\AppFramework\Middleware\Security\BruteForceMiddleware;
1111
use OC\AppFramework\Utility\ControllerMethodReflector;
12-
use OCP\AppFramework\Controller;
13-
use OCP\AppFramework\Http\Attribute\BruteForceProtection;
1412
use OCP\AppFramework\Http\Response;
1513
use OCP\IRequest;
1614
use OCP\Security\Bruteforce\IThrottler;
1715
use Psr\Log\LoggerInterface;
16+
use Test\AppFramework\Middleware\Security\Mock\BruteForceMiddlewareController;
1817
use Test\TestCase;
1918

20-
class TestController extends Controller {
21-
/**
22-
* @BruteForceProtection(action=login)
23-
*/
24-
public function testMethodWithAnnotation() {
25-
}
26-
27-
public function testMethodWithoutAnnotation() {
28-
}
29-
30-
#[BruteForceProtection(action: 'single')]
31-
public function singleAttribute(): void {
32-
}
33-
34-
#[BruteForceProtection(action: 'first')]
35-
#[BruteForceProtection(action: 'second')]
36-
public function multipleAttributes(): void {
37-
}
38-
}
39-
4019
class BruteForceMiddlewareTest extends TestCase {
4120
/** @var ControllerMethodReflector */
4221
private $reflector;
@@ -74,7 +53,7 @@ public function testBeforeControllerWithAnnotation(): void {
7453
->method('sleepDelayOrThrowOnMax')
7554
->with('127.0.0.1', 'login');
7655

77-
$controller = new TestController('test', $this->request);
56+
$controller = new BruteForceMiddlewareController('test', $this->request);
7857
$this->reflector->reflect($controller, 'testMethodWithAnnotation');
7958
$this->bruteForceMiddleware->beforeController($controller, 'testMethodWithAnnotation');
8059
}
@@ -89,7 +68,7 @@ public function testBeforeControllerWithSingleAttribute(): void {
8968
->method('sleepDelayOrThrowOnMax')
9069
->with('::1', 'single');
9170

92-
$controller = new TestController('test', $this->request);
71+
$controller = new BruteForceMiddlewareController('test', $this->request);
9372
$this->reflector->reflect($controller, 'singleAttribute');
9473
$this->bruteForceMiddleware->beforeController($controller, 'singleAttribute');
9574
}
@@ -113,7 +92,7 @@ public function testBeforeControllerWithMultipleAttributes(): void {
11392
return 0;
11493
});
11594

116-
$controller = new TestController('test', $this->request);
95+
$controller = new BruteForceMiddlewareController('test', $this->request);
11796
$this->reflector->reflect($controller, 'multipleAttributes');
11897
$this->bruteForceMiddleware->beforeController($controller, 'multipleAttributes');
11998
}
@@ -126,7 +105,7 @@ public function testBeforeControllerWithoutAnnotation(): void {
126105
->expects($this->never())
127106
->method('sleepDelayOrThrowOnMax');
128107

129-
$controller = new TestController('test', $this->request);
108+
$controller = new BruteForceMiddlewareController('test', $this->request);
130109
$this->reflector->reflect($controller, 'testMethodWithoutAnnotation');
131110
$this->bruteForceMiddleware->beforeController($controller, 'testMethodWithoutAnnotation');
132111
}
@@ -155,7 +134,7 @@ public function testAfterControllerWithAnnotationAndThrottledRequest(): void {
155134
->method('registerAttempt')
156135
->with('login', '127.0.0.1');
157136

158-
$controller = new TestController('test', $this->request);
137+
$controller = new BruteForceMiddlewareController('test', $this->request);
159138
$this->reflector->reflect($controller, 'testMethodWithAnnotation');
160139
$this->bruteForceMiddleware->afterController($controller, 'testMethodWithAnnotation', $response);
161140
}
@@ -177,7 +156,7 @@ public function testAfterControllerWithAnnotationAndNotThrottledRequest(): void
177156
->expects($this->never())
178157
->method('registerAttempt');
179158

180-
$controller = new TestController('test', $this->request);
159+
$controller = new BruteForceMiddlewareController('test', $this->request);
181160
$this->reflector->reflect($controller, 'testMethodWithAnnotation');
182161
$this->bruteForceMiddleware->afterController($controller, 'testMethodWithAnnotation', $response);
183162
}
@@ -207,7 +186,7 @@ public function testAfterControllerWithSingleAttribute(): void {
207186
->method('registerAttempt')
208187
->with('single', '::1');
209188

210-
$controller = new TestController('test', $this->request);
189+
$controller = new BruteForceMiddlewareController('test', $this->request);
211190
$this->reflector->reflect($controller, 'singleAttribute');
212191
$this->bruteForceMiddleware->afterController($controller, 'singleAttribute', $response);
213192
}
@@ -254,7 +233,7 @@ public function testAfterControllerWithMultipleAttributesGeneralMatch(): void {
254233
$this->assertEquals($expected, func_get_args());
255234
});
256235

257-
$controller = new TestController('test', $this->request);
236+
$controller = new BruteForceMiddlewareController('test', $this->request);
258237
$this->reflector->reflect($controller, 'multipleAttributes');
259238
$this->bruteForceMiddleware->afterController($controller, 'multipleAttributes', $response);
260239
}
@@ -284,7 +263,7 @@ public function testAfterControllerWithMultipleAttributesSpecificMatch(): void {
284263
->method('registerAttempt')
285264
->with('second', '::1');
286265

287-
$controller = new TestController('test', $this->request);
266+
$controller = new BruteForceMiddlewareController('test', $this->request);
288267
$this->reflector->reflect($controller, 'multipleAttributes');
289268
$this->bruteForceMiddleware->afterController($controller, 'multipleAttributes', $response);
290269
}
@@ -297,7 +276,7 @@ public function testAfterControllerWithoutAnnotation(): void {
297276
->expects($this->never())
298277
->method('sleepDelayOrThrowOnMax');
299278

300-
$controller = new TestController('test', $this->request);
279+
$controller = new BruteForceMiddlewareController('test', $this->request);
301280
$this->reflector->reflect($controller, 'testMethodWithoutAnnotation');
302281
/** @var Response|\PHPUnit\Framework\MockObject\MockObject $response */
303282
$response = $this->createMock(Response::class);
@@ -312,7 +291,7 @@ public function testAfterControllerWithThrottledResponseButUnhandled(): void {
312291
->expects($this->never())
313292
->method('sleepDelayOrThrowOnMax');
314293

315-
$controller = new TestController('test', $this->request);
294+
$controller = new BruteForceMiddlewareController('test', $this->request);
316295
$this->reflector->reflect($controller, 'testMethodWithoutAnnotation');
317296
/** @var Response|\PHPUnit\Framework\MockObject\MockObject $response */
318297
$response = $this->createMock(Response::class);
@@ -321,7 +300,7 @@ public function testAfterControllerWithThrottledResponseButUnhandled(): void {
321300

322301
$this->logger->expects($this->once())
323302
->method('debug')
324-
->with('Response for Test\AppFramework\Middleware\Security\TestController::testMethodWithoutAnnotation got bruteforce throttled but has no annotation nor attribute defined.');
303+
->with('Response for Test\AppFramework\Middleware\Security\Mock\BruteForceMiddlewareController::testMethodWithoutAnnotation got bruteforce throttled but has no annotation nor attribute defined.');
325304

326305
$this->bruteForceMiddleware->afterController($controller, 'testMethodWithoutAnnotation', $response);
327306
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Test\AppFramework\Middleware\Security\Mock;
4+
5+
use OCP\AppFramework\Controller;
6+
use OCP\AppFramework\Http\Attribute\BruteForceProtection;
7+
8+
class BruteForceMiddlewareController extends Controller {
9+
/**
10+
* @BruteForceProtection(action=login)
11+
*/
12+
public function testMethodWithAnnotation() {
13+
}
14+
15+
public function testMethodWithoutAnnotation() {
16+
}
17+
18+
#[BruteForceProtection(action: 'single')]
19+
public function singleAttribute(): void {
20+
}
21+
22+
#[BruteForceProtection(action: 'first')]
23+
#[BruteForceProtection(action: 'second')]
24+
public function multipleAttributes(): void {
25+
}
26+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Test\AppFramework\Middleware\Security\Mock;
4+
5+
use OCP\AppFramework\Controller;
6+
use OCP\AppFramework\Http\Attribute\AnonRateLimit;
7+
use OCP\AppFramework\Http\Attribute\UserRateLimit;
8+
9+
class RateLimitingMiddlewareController extends Controller {
10+
/**
11+
* @UserRateThrottle(limit=20, period=200)
12+
* @AnonRateThrottle(limit=10, period=100)
13+
*/
14+
public function testMethodWithAnnotation() {
15+
}
16+
17+
/**
18+
* @AnonRateThrottle(limit=10, period=100)
19+
*/
20+
public function testMethodWithAnnotationFallback() {
21+
}
22+
23+
public function testMethodWithoutAnnotation() {
24+
}
25+
26+
#[UserRateLimit(limit: 20, period: 200)]
27+
#[AnonRateLimit(limit: 10, period: 100)]
28+
public function testMethodWithAttributes() {
29+
}
30+
31+
#[AnonRateLimit(limit: 10, period: 100)]
32+
public function testMethodWithAttributesFallback() {
33+
}
34+
}

0 commit comments

Comments
 (0)