Passed
Push — master ( 5cdc85...37718d )
by Morris
38:53 queued 21:57
created

RateLimitingMiddleware::beforeController()   A

Complexity

Conditions 6
Paths 3

Size

Total Lines 21
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 18
nc 3
nop 2
dl 0
loc 21
rs 9.0444
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2017 Lukas Reschke <[email protected]>
4
 *
5
 * @author Lukas Reschke <[email protected]>
6
 * @author Roeland Jago Douma <[email protected]>
7
 *
8
 * @license GNU AGPL version 3 or any later version
9
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License as
12
 * published by the Free Software Foundation, either version 3 of the
13
 * License, or (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU Affero General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Affero General Public License
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
 *
23
 */
24
25
namespace OC\AppFramework\Middleware\Security;
26
27
use OC\AppFramework\Utility\ControllerMethodReflector;
28
use OC\Security\RateLimiting\Exception\RateLimitExceededException;
29
use OC\Security\RateLimiting\Limiter;
30
use OCP\AppFramework\Http\JSONResponse;
31
use OCP\AppFramework\Http\TemplateResponse;
32
use OCP\AppFramework\Middleware;
33
use OCP\IRequest;
34
use OCP\IUserSession;
35
36
/**
37
 * Class RateLimitingMiddleware is the middleware responsible for implementing the
38
 * ratelimiting in Nextcloud.
39
 *
40
 * It parses annotations such as:
41
 *
42
 * @UserRateThrottle(limit=5, period=100)
43
 * @AnonRateThrottle(limit=1, period=100)
44
 *
45
 * Those annotations above would mean that logged-in users can access the page 5
46
 * times within 100 seconds, and anonymous users 1 time within 100 seconds. If
47
 * only an AnonRateThrottle is specified that one will also be applied to logged-in
48
 * users.
49
 *
50
 * @package OC\AppFramework\Middleware\Security
51
 */
52
class RateLimitingMiddleware extends Middleware {
53
	/** @var IRequest $request */
54
	private $request;
55
	/** @var IUserSession */
56
	private $userSession;
57
	/** @var ControllerMethodReflector */
58
	private $reflector;
59
	/** @var Limiter */
60
	private $limiter;
61
62
	/**
63
	 * @param IRequest $request
64
	 * @param IUserSession $userSession
65
	 * @param ControllerMethodReflector $reflector
66
	 * @param Limiter $limiter
67
	 */
68
	public function __construct(IRequest $request,
69
								IUserSession $userSession,
70
								ControllerMethodReflector $reflector,
71
								Limiter $limiter) {
72
		$this->request = $request;
73
		$this->userSession = $userSession;
74
		$this->reflector = $reflector;
75
		$this->limiter = $limiter;
76
	}
77
78
	/**
79
	 * {@inheritDoc}
80
	 * @throws RateLimitExceededException
81
	 */
82
	public function beforeController($controller, $methodName) {
83
		parent::beforeController($controller, $methodName);
84
85
		$anonLimit = $this->reflector->getAnnotationParameter('AnonRateThrottle', 'limit');
86
		$anonPeriod = $this->reflector->getAnnotationParameter('AnonRateThrottle', 'period');
87
		$userLimit = $this->reflector->getAnnotationParameter('UserRateThrottle', 'limit');
88
		$userPeriod = $this->reflector->getAnnotationParameter('UserRateThrottle', 'period');
89
		$rateLimitIdentifier = get_class($controller) . '::' . $methodName;
90
		if($userLimit !== '' && $userPeriod !== '' && $this->userSession->isLoggedIn()) {
91
			$this->limiter->registerUserRequest(
92
				$rateLimitIdentifier,
93
				$userLimit,
0 ignored issues
show
Bug introduced by
$userLimit of type string is incompatible with the type integer expected by parameter $userLimit of OC\Security\RateLimiting...::registerUserRequest(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

93
				/** @scrutinizer ignore-type */ $userLimit,
Loading history...
94
				$userPeriod,
0 ignored issues
show
Bug introduced by
$userPeriod of type string is incompatible with the type integer expected by parameter $userPeriod of OC\Security\RateLimiting...::registerUserRequest(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

94
				/** @scrutinizer ignore-type */ $userPeriod,
Loading history...
95
				$this->userSession->getUser()
0 ignored issues
show
Bug introduced by
It seems like $this->userSession->getUser() can also be of type null; however, parameter $user of OC\Security\RateLimiting...::registerUserRequest() does only seem to accept OCP\IUser, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

95
				/** @scrutinizer ignore-type */ $this->userSession->getUser()
Loading history...
96
			);
97
		} elseif ($anonLimit !== '' && $anonPeriod !== '') {
98
			$this->limiter->registerAnonRequest(
99
				$rateLimitIdentifier,
100
				$anonLimit,
0 ignored issues
show
Bug introduced by
$anonLimit of type string is incompatible with the type integer expected by parameter $anonLimit of OC\Security\RateLimiting...::registerAnonRequest(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

100
				/** @scrutinizer ignore-type */ $anonLimit,
Loading history...
101
				$anonPeriod,
0 ignored issues
show
Bug introduced by
$anonPeriod of type string is incompatible with the type integer expected by parameter $anonPeriod of OC\Security\RateLimiting...::registerAnonRequest(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

101
				/** @scrutinizer ignore-type */ $anonPeriod,
Loading history...
102
				$this->request->getRemoteAddress()
103
			);
104
		}
105
	}
106
107
	/**
108
	 * {@inheritDoc}
109
	 */
110
	public function afterException($controller, $methodName, \Exception $exception) {
111
		if($exception instanceof RateLimitExceededException) {
112
			if (stripos($this->request->getHeader('Accept'),'html') === false) {
113
				$response = new JSONResponse(
114
					[
115
						'message' => $exception->getMessage(),
116
					],
117
					$exception->getCode()
118
				);
119
			} else {
120
					$response = new TemplateResponse(
121
						'core',
122
						'403',
123
							[
124
								'file' => $exception->getMessage()
125
							],
126
						'guest'
127
					);
128
					$response->setStatus($exception->getCode());
129
			}
130
131
			return $response;
132
		}
133
134
		throw $exception;
135
	}
136
}
137