mirror of
https://github.com/nextcloud/server.git
synced 2026-04-07 10:06:16 -04:00
Add validation in the Metric constructor that rejects invalid OpenMetrics label names with InvalidArgumentException. Sanitize app IDs at the source in AppsInfo by replacing hyphens with underscores before creating the Metric. Fixes nextcloud/server#59247 Signed-off-by: moktamd <moktamd@users.noreply.github.com>
36 lines
821 B
PHP
36 lines
821 B
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
/**
|
|
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
|
*/
|
|
|
|
namespace OCP\OpenMetrics;
|
|
|
|
/**
|
|
* @since 33.0.0
|
|
*/
|
|
final readonly class Metric {
|
|
public function __construct(
|
|
public int|float|bool|MetricValue $value = false,
|
|
/** @var string[] */
|
|
public array $labels = [],
|
|
public int|float|null $timestamp = null,
|
|
) {
|
|
$this->validateLabels();
|
|
}
|
|
|
|
public function label(string $name): ?string {
|
|
return $this->labels[$name] ?? null;
|
|
}
|
|
|
|
private function validateLabels(): void {
|
|
foreach ($this->labels as $label => $_value) {
|
|
if (preg_match('/^[a-zA-Z_][a-zA-Z0-9_]*$/', (string)$label) !== 1) {
|
|
throw new \InvalidArgumentException('Invalid OpenMetrics label name: "' . $label . '"');
|
|
}
|
|
}
|
|
}
|
|
}
|