Skip to content
Open
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
1 change: 1 addition & 0 deletions core/Controller/OpenMetricsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ private function formatLabels(Metric $metric): string {

$labels = [];
foreach ($metric->labels as $label => $value) {
$label = preg_replace('/[^a-zA-Z0-9_]/', '_', (string)$label);
$labels[] .= $label . '=' . $this->escapeString((string)$value);
}

Expand Down
32 changes: 32 additions & 0 deletions tests/Core/Controller/OpenMetricsControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,38 @@ public function testGetMetrics(): void {
$this->assertStringMatchesFormat($expected, $fullOutput);
}

public function testLabelNamesAreSanitized(): void {
$this->exporterManager = $this->createMock(ExporterManager::class);
$this->exporterManager->method('export')->willReturnCallback(function (): Generator {
$metric = $this->createMock(IMetricFamily::class);
$metric->method('type')->willReturn(MetricType::info);
$metric->method('unit')->willReturn('');
$metric->method('name')->willReturn('apps_info');
$metric->method('help')->willReturn('Enabled applications');
$metric->method('metrics')->willReturnCallback(function () {
yield new Metric(1, ['hide-photos' => '1.0.0', 'normal_app' => '2.0.0']);
});
yield $metric;
});
$this->controller = new OpenMetricsController('core', $this->request, $this->config, $this->exporterManager, $this->logger);

$output = $this->createMock(IOutput::class);
$fullOutput = '';
$output->method('setOutput')
->willReturnCallback(function ($output) use (&$fullOutput): void {
$fullOutput .= $output;
});
$this->config->expects($this->once())
->method('getSystemValue')
->with('openmetrics_allowed_clients')
->willReturn(['192.168.0.0/16']);
$response = $this->controller->export();
$response->callback($output);
$this->assertStringContainsString('hide_photos="1.0.0"', $fullOutput);
$this->assertStringContainsString('normal_app="2.0.0"', $fullOutput);
$this->assertStringNotContainsString('hide-photos', $fullOutput);
}

public function testGetMetricsFromForbiddenIp(): void {
$this->config->expects($this->once())
->method('getSystemValue')
Expand Down