fix: escape Prometheus label values (#2702)

fix: escape Prometheus label values (#2699)

Co-authored-by: potatosips <potatosips@users.noreply.github.com>
This commit is contained in:
potatosips
2026-07-21 09:38:53 +02:00
committed by GitHub
co-authored by potatosips
parent 52fb584bb0
commit 0b97cf6d23
3 changed files with 52 additions and 2 deletions
+26
View File
@@ -0,0 +1,26 @@
import { describe, expect, test } from 'vitest';
import {
escapePrometheusLabelValue,
formatPrometheusLabels,
} from '#server/utils/prometheus';
describe('Prometheus label formatting', () => {
test('escapes quotes, backslashes, and newlines in label values', () => {
expect(escapePrometheusLabelValue('vpn"client')).toBe('vpn\\"client');
expect(escapePrometheusLabelValue('path\\client')).toBe('path\\\\client');
expect(escapePrometheusLabelValue('line one\nline two')).toBe(
'line one\\nline two'
);
});
test('formats escaped values without changing scalar values', () => {
expect(
formatPrometheusLabels({
interface: 'wg"0',
enabled: true,
name: 'home\\office\npeer',
})
).toBe('interface="wg\\"0",enabled="true",name="home\\\\office\\npeer"');
});
});