aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorJoas Schilling <213943+nickvergessen@users.noreply.github.com>2024-11-07 14:25:22 +0100
committerGitHub <noreply@github.com>2024-11-07 14:25:22 +0100
commit31f4f67c0c5ceace8f4c75daa29a0b51ddd3264a (patch)
tree52dc971a4bb4b691c8514325a35a872796192e9a /lib
parentc6002ed3e9e3a0a93653200fad38e261541d74b7 (diff)
parentac693ee4d69c12f9135ca4ddea418d02823bed56 (diff)
downloadnextcloud-server-31f4f67c0c5ceace8f4c75daa29a0b51ddd3264a.tar.gz
nextcloud-server-31f4f67c0c5ceace8f4c75daa29a0b51ddd3264a.zip
Merge pull request #49116 from nextcloud/bugfix/noid/validate-parameter-keys
fix(richobjectstrings): Add missing placeholder validation
Diffstat (limited to 'lib')
-rw-r--r--lib/private/RichObjectStrings/Validator.php33
-rw-r--r--lib/public/RichObjectStrings/IValidator.php13
2 files changed, 26 insertions, 20 deletions
diff --git a/lib/private/RichObjectStrings/Validator.php b/lib/private/RichObjectStrings/Validator.php
index c7e4dcf50b9..d1148940200 100644
--- a/lib/private/RichObjectStrings/Validator.php
+++ b/lib/private/RichObjectStrings/Validator.php
@@ -1,4 +1,6 @@
<?php
+
+declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
@@ -16,30 +18,22 @@ use OCP\RichObjectStrings\IValidator;
* @since 11.0.0
*/
class Validator implements IValidator {
- /** @var Definitions */
- protected $definitions;
-
- /** @var array[] */
- protected $requiredParameters = [];
+ protected array $requiredParameters = [];
- /**
- * Constructor
- *
- * @param Definitions $definitions
- */
- public function __construct(Definitions $definitions) {
- $this->definitions = $definitions;
+ public function __construct(
+ protected Definitions $definitions,
+ ) {
}
/**
* @param string $subject
- * @param array[] $parameters
+ * @param array<non-empty-string, array<non-empty-string, string>> $parameters
* @throws InvalidObjectExeption
* @since 11.0.0
*/
- public function validate($subject, array $parameters) {
+ public function validate(string $subject, array $parameters): void {
$matches = [];
- $result = preg_match_all('/\{([a-z0-9]+)\}/i', $subject, $matches);
+ $result = preg_match_all('/\{(' . self::PLACEHOLDER_REGEX . ')\}/', $subject, $matches);
if ($result === false) {
throw new InvalidObjectExeption();
@@ -53,7 +47,10 @@ class Validator implements IValidator {
}
}
- foreach ($parameters as $parameter) {
+ foreach ($parameters as $placeholder => $parameter) {
+ if (!\is_string($placeholder) || !preg_match('/^(' . self::PLACEHOLDER_REGEX . ')$/i', $placeholder)) {
+ throw new InvalidObjectExeption('Parameter key is invalid');
+ }
if (!\is_array($parameter)) {
throw new InvalidObjectExeption('Parameter is malformed');
}
@@ -66,7 +63,7 @@ class Validator implements IValidator {
* @param array $parameter
* @throws InvalidObjectExeption
*/
- protected function validateParameter(array $parameter) {
+ protected function validateParameter(array $parameter): void {
if (!isset($parameter['type'])) {
throw new InvalidObjectExeption('Object type is undefined');
}
@@ -94,7 +91,7 @@ class Validator implements IValidator {
* @param array $definition
* @return string[]
*/
- protected function getRequiredParameters($type, array $definition) {
+ protected function getRequiredParameters(string $type, array $definition): array {
if (isset($this->requiredParameters[$type])) {
return $this->requiredParameters[$type];
}
diff --git a/lib/public/RichObjectStrings/IValidator.php b/lib/public/RichObjectStrings/IValidator.php
index 96b3b6ea743..122e5ca992d 100644
--- a/lib/public/RichObjectStrings/IValidator.php
+++ b/lib/public/RichObjectStrings/IValidator.php
@@ -1,4 +1,7 @@
<?php
+
+declare(strict_types=1);
+
/**
* SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
@@ -12,10 +15,16 @@ namespace OCP\RichObjectStrings;
*/
interface IValidator {
/**
+ * Only alphanumeric, dash, underscore and dot are allowed, starting with a character
+ * @since 31.0.0
+ */
+ public const PLACEHOLDER_REGEX = '[A-Za-z][A-Za-z0-9\-_.]+';
+
+ /**
* @param string $subject
- * @param array[] $parameters
+ * @param array<non-empty-string, array<non-empty-string, string>> $parameters
* @throws InvalidObjectExeption
* @since 11.0.0
*/
- public function validate($subject, array $parameters);
+ public function validate(string $subject, array $parameters): void;
}