Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(request): Add RateLimit and RateLimit-Policy headers #39790

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@
* @package OC\AppFramework\Middleware\Security
*/
class RateLimitingMiddleware extends Middleware {
protected int $limit;
protected int $period;
protected int $attempts;

public function __construct(
protected IRequest $request,
protected IUserSession $userSession,
Expand All @@ -85,7 +89,9 @@ public function beforeController(Controller $controller, string $methodName): vo
$rateLimit = $this->readLimitFromAnnotationOrAttribute($controller, $methodName, 'UserRateThrottle', UserRateLimit::class);

if ($rateLimit !== null) {
$this->limiter->registerUserRequest(
$this->limit = $rateLimit->getLimit();
$this->period = $rateLimit->getPeriod();
$this->attempts = $this->limiter->registerUserRequest(
$rateLimitIdentifier,
$rateLimit->getLimit(),
$rateLimit->getPeriod(),
Expand All @@ -100,7 +106,9 @@ public function beforeController(Controller $controller, string $methodName): vo
$rateLimit = $this->readLimitFromAnnotationOrAttribute($controller, $methodName, 'AnonRateThrottle', AnonRateLimit::class);

if ($rateLimit !== null) {
$this->limiter->registerAnonRequest(
$this->limit = $rateLimit->getLimit();
$this->period = $rateLimit->getPeriod();
$this->attempts = $this->limiter->registerAnonRequest(
$rateLimitIdentifier,
$rateLimit->getLimit(),
$rateLimit->getPeriod(),
Expand Down Expand Up @@ -140,6 +148,17 @@ protected function readLimitFromAnnotationOrAttribute(Controller $controller, st
return null;
}

/**
* {@inheritDoc}
*/
public function afterController(Controller $controller, string $methodName, Response $response): Response {
$response->setHeaders([
'RateLimit' => 'limit=' . $this->limit . ', remaining=' . max(0, $this->limit - $this->attempts) . ', reset=' . $this->period,
'RateLimit-Policy' => $this->limit . ';w=' . $this->period,
]);
return $response;
}

/**
* {@inheritDoc}
*/
Expand All @@ -154,9 +173,14 @@ public function afterException(Controller $controller, string $methodName, \Exce
[],
TemplateResponse::RENDER_AS_GUEST
);
$response->setStatus($exception->getCode());
}

$response->setStatus($exception->getCode());
$response->setHeaders([
'RateLimit' => 'limit=' . $this->limit . ', remaining=' . max(0, $this->limit - $this->attempts) . ', reset=' . $this->period,
'RateLimit-Policy' => $this->limit . ';w=' . $this->period,
]);

return $response;
}

Expand Down
12 changes: 7 additions & 5 deletions lib/private/Security/RateLimiting/Limiter.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,15 @@ private function register(
string $userIdentifier,
int $period,
int $limit,
): void {
): int {
$existingAttempts = $this->backend->getAttempts($methodIdentifier, $userIdentifier);
if ($existingAttempts >= $limit) {
throw new RateLimitExceededException();
}

$this->backend->registerAttempt($methodIdentifier, $userIdentifier, $period);

return $existingAttempts;
}

/**
Expand All @@ -66,11 +68,11 @@ public function registerAnonRequest(
int $anonLimit,
int $anonPeriod,
string $ip,
): void {
): int {
$ipSubnet = (new IpAddress($ip))->getSubnet();

$anonHashIdentifier = hash('sha512', 'anon::' . $identifier . $ipSubnet);
$this->register($identifier, $anonHashIdentifier, $anonPeriod, $anonLimit);
return $this->register($identifier, $anonHashIdentifier, $anonPeriod, $anonLimit);
}

/**
Expand All @@ -84,8 +86,8 @@ public function registerUserRequest(
int $userLimit,
int $userPeriod,
IUser $user,
): void {
): int {
$userHashIdentifier = hash('sha512', 'user::' . $identifier . $user->getUID());
$this->register($identifier, $userHashIdentifier, $userPeriod, $userLimit);
return $this->register($identifier, $userHashIdentifier, $userPeriod, $userLimit);
}
}
Loading