8000 Check for duplicate label names, add missing checks on new code path.… · clearasmudd/client_python@024423d · GitHub
[go: up one dir, main page]

Skip to content

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 024423d

Browse files
authored
Check for duplicate label names, add missing checks on new code path. (prometheus#445)
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
1 parent d1d93b1 commit 024423d

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

prometheus_client/openmetrics/parser.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,12 @@ def _parse_labels_with_state_machine(text):
139139
if char == '\\':
140140
state = 'labelvalueslash'
141141
elif char == '"':
142-
if not METRIC_LABEL_NAME_RE.match(''.join(labelname)):
143-
raise ValueError("Invalid line: " + text)
144-
labels[''.join(labelname)] = ''.join(labelvalue)
142+
ln = ''.join(labelname)
143+
if not METRIC_LABEL_NAME_RE.match(ln):
144+
raise ValueError("Invalid line, bad label name: " + text)
145+
if ln in labels:
146+
raise ValueError("Invalid line, duplicate label name: " + text)
147+
labels[ln] = ''.join(labelvalue)
145148
labelname = []
146149
labelvalue = []
147150
state = 'endoflabelvalue'
@@ -217,6 +220,10 @@ def _parse_labels(text):
217220
# Replace escaping if needed
218221
if "\\" in label_value:
219222
label_value = _replace_escaping(label_value)
223+
if not METRIC_LABEL_NAME_RE.match(label_name):
224+
raise ValueError("invalid line, bad label name: " + text)
225+
if label_name in labels:
226+
raise ValueError("invalid line, duplicate label name: " + text)
220227
labels[label_name] = label_value
221228

222229
# Remove the processed label from the sub-slice for next iteration

tests/openmetrics/test_parser.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,11 @@ def test_invalid_input(self):
560560
('a{a="1"b="2"} 1\n# EOF\n'),
561561
('a{a="1",,b="2"} 1\n# EOF\n'),
562562
('a{a="1",b="2",} 1\n# EOF\n'),
563+
# Invalid labels.
564+
('a{1="1"} 1\n# EOF\n'),
565+
('a{a="1",a="1"} 1\n# EOF\n'),
566+
('a{1=" # "} 1\n# EOF\n'),
567+
('a{a=" # ",a=" # "} 1\n# EOF\n'),
563568
# Missing value.
564569
('a\n# EOF\n'),
565570
('a \n# EOF\n'),

0 commit comments

Comments
 (0)
0