Skip to content

Commit 4615871

Browse files
Merge pull request #62057 from nextcloud/backport/62039/stable34
[stable34] perf(workflow): Implement result cache for IP checks
2 parents dcd6801 + 3b8be83 commit 4615871

3 files changed

Lines changed: 27 additions & 14 deletions

File tree

apps/workflowengine/lib/Check/RequestRemoteAddress.php

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@
66
*/
77
namespace OCA\WorkflowEngine\Check;
88

9+
use OCP\Cache\CappedMemoryCache;
910
use OCP\IL10N;
1011
use OCP\IRequest;
1112
use OCP\WorkflowEngine\ICheck;
1213

1314
class RequestRemoteAddress implements ICheck {
15+
protected CappedMemoryCache $checked;
1416

1517
/**
1618
* @param IL10N $l
@@ -20,6 +22,7 @@ public function __construct(
2022
protected IL10N $l,
2123
protected IRequest $request,
2224
) {
25+
$this->checked = new CappedMemoryCache();
2326
}
2427

2528
/**
@@ -29,18 +32,24 @@ public function __construct(
2932
*/
3033
#[\Override]
3134
public function executeCheck($operator, $value) {
35+
$cacheKey = sha1($operator . $value);
36+
37+
if (isset($this->checked[$cacheKey])) {
38+
return $this->checked[$cacheKey];
39+
}
40+
3241
$actualValue = $this->request->getRemoteAddress();
3342
$decodedValue = explode('/', $value);
3443

35-
if ($operator === 'matchesIPv4') {
36-
return $this->matchIPv4($actualValue, $decodedValue[0], (int)$decodedValue[1]);
37-
} elseif ($operator === '!matchesIPv4') {
38-
return !$this->matchIPv4($actualValue, $decodedValue[0], (int)$decodedValue[1]);
39-
} elseif ($operator === 'matchesIPv6') {
40-
return $this->matchIPv6($actualValue, $decodedValue[0], (int)$decodedValue[1]);
41-
} else {
42-
return !$this->matchIPv6($actualValue, $decodedValue[0], (int)$decodedValue[1]);
43-
}
44+
$result = match ($operator) {
45+
'matchesIPv4' => $this->matchIPv4($actualValue, $decodedValue[0], (int)$decodedValue[1]),
46+
'!matchesIPv4' => !$this->matchIPv4($actualValue, $decodedValue[0], (int)$decodedValue[1]),
47+
'matchesIPv6' => $this->matchIPv6($actualValue, $decodedValue[0], (int)$decodedValue[1]),
48+
default => !$this->matchIPv6($actualValue, $decodedValue[0], (int)$decodedValue[1]),
49+
};
50+
51+
$this->checked[$cacheKey] = $result;
52+
return $result;
4453
}
4554

4655
/**

apps/workflowengine/lib/Check/RequestTime.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@
77
namespace OCA\WorkflowEngine\Check;
88

99
use OCP\AppFramework\Utility\ITimeFactory;
10+
use OCP\Cache\CappedMemoryCache;
1011
use OCP\IL10N;
1112
use OCP\WorkflowEngine\ICheck;
1213

1314
class RequestTime implements ICheck {
1415
public const REGEX_TIME = '([0-1][0-9]|2[0-3]):([0-5][0-9])';
1516
public const REGEX_TIMEZONE = '([a-zA-Z]+(?:\\/[a-zA-Z\-\_]+)+)';
1617

17-
/** @var bool[] */
18-
protected $cachedResults;
18+
protected CappedMemoryCache $cachedResults;
1919

2020
/**
2121
* @param ITimeFactory $timeFactory
@@ -24,6 +24,7 @@ public function __construct(
2424
protected IL10N $l,
2525
protected ITimeFactory $timeFactory,
2626
) {
27+
$this->cachedResults = new CappedMemoryCache();
2728
}
2829

2930
/**
@@ -33,7 +34,7 @@ public function __construct(
3334
*/
3435
#[\Override]
3536
public function executeCheck($operator, $value) {
36-
$valueHash = md5($value);
37+
$valueHash = sha1($operator . $value);
3738

3839
if (isset($this->cachedResults[$valueHash])) {
3940
return $this->cachedResults[$valueHash];
@@ -51,7 +52,10 @@ public function executeCheck($operator, $value) {
5152
$in = $timestamp1 <= $timestamp || $timestamp <= $timestamp2;
5253
}
5354

54-
return ($operator === 'in') ? $in : !$in;
55+
$result = ($operator === 'in') ? $in : !$in;
56+
57+
$this->cachedResults[$valueHash] = $result;
58+
return $result;
5559
}
5660

5761
/**

apps/workflowengine/lib/Check/RequestURL.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ protected function isWebDAVRequest(): bool {
7272
if ($this->url === RequestURL::CLI) {
7373
return false;
7474
}
75-
return substr($this->request->getScriptName(), 0 - strlen('/remote.php')) === '/remote.php' && (
75+
return str_ends_with($this->request->getScriptName(), '/remote.php') && (
7676
$this->request->getPathInfo() === '/webdav'
7777
|| str_starts_with($this->request->getPathInfo() ?? '', '/webdav/')
7878
|| $this->request->getPathInfo() === '/dav/files'

0 commit comments

Comments
 (0)