Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(richobjectstrings): Add missing placeholder validation #49116

Merged
merged 3 commits into from
Nov 7, 2024
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
33 changes: 15 additions & 18 deletions lib/private/RichObjectStrings/Validator.php
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -16,30 +18,22 @@
* @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();
Expand All @@ -53,7 +47,10 @@ public function validate($subject, array $parameters) {
}
}

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');
}
Expand All @@ -66,7 +63,7 @@ public function validate($subject, array $parameters) {
* @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');
}
Expand Down Expand Up @@ -94,7 +91,7 @@ protected function validateParameter(array $parameter) {
* @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];
}
Expand Down
13 changes: 11 additions & 2 deletions lib/public/RichObjectStrings/IValidator.php
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -11,11 +14,17 @@
* @since 11.0.0
*/
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;
}
47 changes: 46 additions & 1 deletion tests/lib/RichObjectStrings/ValidatorTest.php
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -12,7 +14,7 @@
use Test\TestCase;

class ValidatorTest extends TestCase {
public function test(): void {
public function testValidate(): void {
$v = new Validator(new Definitions());
$v->validate('test', []);
$v->validate('test {string1} test {foo} test {bar}.', [
Expand Down Expand Up @@ -57,4 +59,47 @@ public function test(): void {
],
]);
}

public static function dataValidateParameterKeys(): array {
return [
'not a string' => ['key' => 0, 'throws' => 'Parameter key is invalid'],
'@ is not allowed' => ['key' => 'user@0', 'throws' => 'Parameter key is invalid'],
'? is not allowed' => ['key' => 'user?0', 'throws' => 'Parameter key is invalid'],
'slash is not allowed' => ['key' => 'user/0', 'throws' => 'Parameter key is invalid'],
'backslash is not allowed' => ['key' => 'user\\0', 'throws' => 'Parameter key is invalid'],
'hash is not allowed' => ['key' => 'user#0', 'throws' => 'Parameter key is invalid'],
'space is not allowed' => ['key' => 'user 0', 'throws' => 'Parameter key is invalid'],
'has to start with letter, but is number' => ['key' => '0abc', 'throws' => 'Parameter key is invalid'],
'has to start with letter, but is dot' => ['key' => '.abc', 'throws' => 'Parameter key is invalid'],
'has to start with letter, but is slash' => ['key' => '-abc', 'throws' => 'Parameter key is invalid'],
'has to start with letter, but is underscore' => ['key' => '_abc', 'throws' => 'Parameter key is invalid'],
['key' => 'user-0', 'throws' => null],
['key' => 'user_0', 'throws' => null],
['key' => 'user.0', 'throws' => null],
['key' => 'a._-0', 'throws' => null],
];
}

/**
* @dataProvider dataValidateParameterKeys
*/
public function testValidateParameterKeys(mixed $key, ?string $throws): void {

if ($throws !== null) {
$this->expectExceptionMessage($throws);
}

$v = new Validator(new Definitions());
$v->validate('{' . $key . '}', [
$key => [
'type' => 'highlight',
'id' => 'identifier',
'name' => 'Display name',
],
]);

if ($throws === null) {
$this->addToAssertionCount(1);
}
}
}
Loading