* @package OC\AppFramework\Middleware\Security
*/
class BruteForceMiddleware extends Middleware {
+ private int $delaySlept = 0;
+
public function __construct(
protected ControllerMethodReflector $reflector,
protected Throttler $throttler,
if ($this->reflector->hasAnnotation('BruteForceProtection')) {
$action = $this->reflector->getAnnotationParameter('BruteForceProtection', 'action');
- $this->throttler->sleepDelayOrThrowOnMax($this->request->getRemoteAddress(), $action);
+ $this->delaySlept += $this->throttler->sleepDelayOrThrowOnMax($this->request->getRemoteAddress(), $action);
} else {
$reflectionMethod = new ReflectionMethod($controller, $methodName);
$attributes = $reflectionMethod->getAttributes(BruteForceProtection::class);
/** @var BruteForceProtection $protection */
$protection = $attribute->newInstance();
$action = $protection->getAction();
- $this->throttler->sleepDelayOrThrowOnMax($remoteAddress, $action);
+ $this->delaySlept += $this->throttler->sleepDelayOrThrowOnMax($remoteAddress, $action);
}
}
}
$action = $this->reflector->getAnnotationParameter('BruteForceProtection', 'action');
$ip = $this->request->getRemoteAddress();
$this->throttler->registerAttempt($action, $ip, $response->getThrottleMetadata());
- $this->throttler->sleepDelayOrThrowOnMax($ip, $action);
+ $this->delaySlept += $this->throttler->sleepDelayOrThrowOnMax($ip, $action);
} else {
$reflectionMethod = new ReflectionMethod($controller, $methodName);
$attributes = $reflectionMethod->getAttributes(BruteForceProtection::class);
if (!isset($metaData['action']) || $metaData['action'] === $action) {
$this->throttler->registerAttempt($action, $ip, $metaData);
- $this->throttler->sleepDelayOrThrowOnMax($ip, $action);
+ $this->delaySlept += $this->throttler->sleepDelayOrThrowOnMax($ip, $action);
}
}
} else {
}
}
+ if ($this->delaySlept) {
+ $headers = $response->getHeaders();
+ if (!isset($headers['X-Nextcloud-Bruteforce-Throttled'])) {
+ $headers['X-Nextcloud-Bruteforce-Throttled'] = $this->delaySlept . 'ms';
+ $response->setHeaders($headers);
+ }
+ }
+
return parent::afterController($controller, $methodName, $response);
}