Skip to content

Commit

Permalink
Check for duplicate label names, add missing checks on new code path.
Browse files Browse the repository at this point in the history
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
  • Loading branch information
brian-brazil committed Aug 13, 2019
1 parent d1d93b1 commit 2bf7a88
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
13 changes: 10 additions & 3 deletions prometheus_client/openmetrics/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,12 @@ def _parse_labels_with_state_machine(text):
if char == '\\':
state = 'labelvalueslash'
elif char == '"':
if not METRIC_LABEL_NAME_RE.match(''.join(labelname)):
raise ValueError("Invalid line: " + text)
labels[''.join(labelname)] = ''.join(labelvalue)
ln = ''.join(labelname)
if not METRIC_LABEL_NAME_RE.match(ln):
raise ValueError("Invalid line, bad label name: " + text)
if ln in labels:
raise ValueError("Invalid line, duplicate label name: " + text)
labels[ln] = ''.join(labelvalue)
labelname = []
labelvalue = []
state = 'endoflabelvalue'
Expand Down Expand Up @@ -217,6 +220,10 @@ def _parse_labels(text):
# Replace escaping if needed
if "\\" in label_value:
label_value = _replace_escaping(label_value)
if not METRIC_LABEL_NAME_RE.match(label_name):
raise ValueError("invalid line, bad label name: " + text)
if label_name in labels:
raise ValueError("invalid line, duplicate label name: " + text)
labels[label_name] = label_value

# Remove the processed label from the sub-slice for next iteration
Expand Down
5 changes: 5 additions & 0 deletions tests/openmetrics/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,11 @@ def test_invalid_input(self):
('a{a="1"b="2"} 1\n# EOF\n'),
('a{a="1",,b="2"} 1\n# EOF\n'),
('a{a="1",b="2",} 1\n# EOF\n'),
# Invalid labels.
('a{1="1"} 1\n# EOF\n'),
('a{a="1",a="1"} 1\n# EOF\n'),
('a{1=" # "} 1\n# EOF\n'),
('a{a=" # ",a=" # "} 1\n# EOF\n'),
# Missing value.
('a\n# EOF\n'),
('a \n# EOF\n'),
Expand Down

0 comments on commit 2bf7a88

Please sign in to comment.