Skip to content
Merged
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
27 changes: 18 additions & 9 deletions apps/workflowengine/lib/Check/RequestRemoteAddress.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -20,6 +22,7 @@ public function __construct(
protected IL10N $l,
protected IRequest $request,
) {
$this->checked = new CappedMemoryCache();
}

/**
Expand All @@ -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;
}

/**
Expand Down
12 changes: 8 additions & 4 deletions apps/workflowengine/lib/Check/RequestTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
namespace OCA\WorkflowEngine\Check;

use OCP\AppFramework\Utility\ITimeFactory;
use OCP\Cache\CappedMemoryCache;
use OCP\IL10N;
use OCP\WorkflowEngine\ICheck;

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
Expand All @@ -24,6 +24,7 @@ public function __construct(
protected IL10N $l,
protected ITimeFactory $timeFactory,
) {
$this->cachedResults = new CappedMemoryCache();
}

/**
Expand All @@ -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];
Expand All @@ -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;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion apps/workflowengine/lib/Check/RequestURL.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
Loading