diff --git a/apps/workflowengine/lib/Check/RequestRemoteAddress.php b/apps/workflowengine/lib/Check/RequestRemoteAddress.php index c3f50f9967aa0..281f4a63b0ec0 100644 --- a/apps/workflowengine/lib/Check/RequestRemoteAddress.php +++ b/apps/workflowengine/lib/Check/RequestRemoteAddress.php @@ -6,11 +6,13 @@ */ namespace OCA\WorkflowEngine\Check; +use OCP\Cache\CappedMemoryCache; use OCP\IL10N; use OCP\IRequest; use OCP\WorkflowEngine\ICheck; class RequestRemoteAddress implements ICheck { + protected CappedMemoryCache $checked; /** * @param IL10N $l @@ -20,6 +22,7 @@ public function __construct( protected IL10N $l, protected IRequest $request, ) { + $this->checked = new CappedMemoryCache(); } /** @@ -29,18 +32,24 @@ public function __construct( */ #[\Override] public function executeCheck($operator, $value) { + $cacheKey = sha1($operator . $value); + + if (isset($this->checked[$cacheKey])) { + return $this->checked[$cacheKey]; + } + $actualValue = $this->request->getRemoteAddress(); $decodedValue = explode('/', $value); - if ($operator === 'matchesIPv4') { - return $this->matchIPv4($actualValue, $decodedValue[0], (int)$decodedValue[1]); - } elseif ($operator === '!matchesIPv4') { - return !$this->matchIPv4($actualValue, $decodedValue[0], (int)$decodedValue[1]); - } elseif ($operator === 'matchesIPv6') { - return $this->matchIPv6($actualValue, $decodedValue[0], (int)$decodedValue[1]); - } else { - return !$this->matchIPv6($actualValue, $decodedValue[0], (int)$decodedValue[1]); - } + $result = match ($operator) { + 'matchesIPv4' => $this->matchIPv4($actualValue, $decodedValue[0], (int)$decodedValue[1]), + '!matchesIPv4' => !$this->matchIPv4($actualValue, $decodedValue[0], (int)$decodedValue[1]), + 'matchesIPv6' => $this->matchIPv6($actualValue, $decodedValue[0], (int)$decodedValue[1]), + default => !$this->matchIPv6($actualValue, $decodedValue[0], (int)$decodedValue[1]), + }; + + $this->checked[$cacheKey] = $result; + return $result; } /** diff --git a/apps/workflowengine/lib/Check/RequestTime.php b/apps/workflowengine/lib/Check/RequestTime.php index 87b99c841d4cf..3c69e8d88d85a 100644 --- a/apps/workflowengine/lib/Check/RequestTime.php +++ b/apps/workflowengine/lib/Check/RequestTime.php @@ -7,6 +7,7 @@ namespace OCA\WorkflowEngine\Check; use OCP\AppFramework\Utility\ITimeFactory; +use OCP\Cache\CappedMemoryCache; use OCP\IL10N; use OCP\WorkflowEngine\ICheck; @@ -14,8 +15,7 @@ class RequestTime implements ICheck { public const REGEX_TIME = '([0-1][0-9]|2[0-3]):([0-5][0-9])'; public const REGEX_TIMEZONE = '([a-zA-Z]+(?:\\/[a-zA-Z\-\_]+)+)'; - /** @var bool[] */ - protected $cachedResults; + protected CappedMemoryCache $cachedResults; /** * @param ITimeFactory $timeFactory @@ -24,6 +24,7 @@ public function __construct( protected IL10N $l, protected ITimeFactory $timeFactory, ) { + $this->cachedResults = new CappedMemoryCache(); } /** @@ -33,7 +34,7 @@ public function __construct( */ #[\Override] public function executeCheck($operator, $value) { - $valueHash = md5($value); + $valueHash = sha1($operator . $value); if (isset($this->cachedResults[$valueHash])) { return $this->cachedResults[$valueHash]; @@ -51,7 +52,10 @@ public function executeCheck($operator, $value) { $in = $timestamp1 <= $timestamp || $timestamp <= $timestamp2; } - return ($operator === 'in') ? $in : !$in; + $result = ($operator === 'in') ? $in : !$in; + + $this->cachedResults[$valueHash] = $result; + return $result; } /** diff --git a/apps/workflowengine/lib/Check/RequestURL.php b/apps/workflowengine/lib/Check/RequestURL.php index 9f64517dff47b..da438e686d230 100644 --- a/apps/workflowengine/lib/Check/RequestURL.php +++ b/apps/workflowengine/lib/Check/RequestURL.php @@ -72,7 +72,7 @@ protected function isWebDAVRequest(): bool { if ($this->url === RequestURL::CLI) { return false; } - return substr($this->request->getScriptName(), 0 - strlen('/remote.php')) === '/remote.php' && ( + return str_ends_with($this->request->getScriptName(), '/remote.php') && ( $this->request->getPathInfo() === '/webdav' || str_starts_with($this->request->getPathInfo() ?? '', '/webdav/') || $this->request->getPathInfo() === '/dav/files'