8000 Fix unescaping take 2 by immerrr · Pull Request #291 · prometheus/client_python · GitHub
[go: up one dir, main page]

Skip to content

Fix unescaping take 2 #291

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 24, 2018
Merged
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
36 changes: 32 additions & 4 deletions prometheus_client/parser.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

from __future__ import unicode_literals

import re

try:
import StringIO
except ImportError:
Expand All @@ -19,11 +21,37 @@ def text_string_to_metric_families(text):
for metric_family in text_fd_to_metric_families(StringIO.StringIO(text)):
yield metric_family


ESCAPE_SEQUENCES = {
'\\\\': '\\',
'\\n': '\n',
'\\"': '"',
}


def replace_escape_sequence(match):
return ESCAPE_SEQUENCES[match.group(0)]


HELP_ESCAPING_RE = re.compile(r'\\[\\n]')
ESCAPING_RE = re.compile(r'\\[\\n"]')


def _replace_help_escaping(s):
return s.replace("\\n", "\n").replace('\\\\', '\\')
return HELP_ESCAPING_RE.sub(replace_escape_sequence, s)


def _replace_escaping(s):
return s.replace("\\n", "\n").replace('\\\\', '\\').replace('\\"', '"')
return ESCAPING_RE.sub(replace_escape_sequence, s)


def _is_character_escaped(s, charpos):
num_bslashes = 0
while (charpos > num_bslashes and
s[charpos - 1 - num_bslashes] == '\\'):
num_bslashes += 1
return num_bslashes % 2 == 1


def _parse_labels(labels_string):
labels = {}
Expand Down Expand Up @@ -52,7 +80,7 @@ def _parse_labels(labels_string):
i = 0
while i < len(value_substr):
i = value_substr.index('"', i)
if value_substr[i - 1] != "\\":
if not _is_character_escaped(value_substr, i):
break
i += 1

Expand All @@ -62,7 +90,7 @@ def _parse_labels(labels_string):
# Replace escaping if needed
if escaping:
label_value = _replace_escaping(label_value)
labels[label_name.strip()] = label_value.strip()
labels[label_name.strip()] = label_value

# Remove the processed label from the sub-slice for next iteration
sub_labels = sub_labels[quote_end + 1:]
Expand Down
64 changes: 63 additions & 1 deletion tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,14 +167,32 @@ def test_commas(self):
families = text_string_to_metric_families("""# TYPE a counter
# HELP a help
a{foo="bar",} 1
a{foo="baz", } 1
# TYPE b counter
# HELP b help
b{,} 2
# TYPE c counter
# HELP c help
c{ ,} 3
# TYPE d counter
# HELP d help
d{, } 4
""")
a = CounterMetricFamily("a", "help", labels=["foo"])
a.add_metric(["bar"], 1)
a.add_metric(["baz"], 1)
b = CounterMetricFamily("b", "help", value=2)
self.assertEqual([a, b], list(families))
c = CounterMetricFamily("c", "help", value=3)
d = CounterMetricFamily("d", "help", value=4)
self.assertEqual([a, b, c, d], list(families))

def test_multiple_trailing_commas(self):
text = """# TYPE a counter
# HELP a help
a{foo="bar",, } 1
"""
self.assertRaises(ValueError,
lambda: list(text_string_to_metric_families(text)))

def test_empty_brackets(self):
families = text_string_to_metric_families("""# TYPE a counter
Expand All @@ -200,6 +218,50 @@ def test_empty_label(self):
metric_family.add_metric([""], 2)
self.assertEqual([metric_family], list(families))

def test_label_escaping(self):
for escaped_val, unescaped_val in [
('foo', 'foo'),
('\\foo', '\\foo'),
('\\\\foo', '\\foo'),
('foo\\\\', 'foo\\'),
('\\\\', '\\'),
('\\n', '\n'),
('\\\\n', '\\n'),
('\\\\\\n', '\\\n'),
('\\"', '"'),
('\\\\\\"', '\\"')]:
families = list(text_string_to_metric_families("""
# TYPE a counter
# HELP a help
a{foo="%s",bar="baz"} 1
""" % escaped_val))
metric_family = CounterMetricFamily(
"a", "help", labels=["foo", "bar"])
metric_family.add_metric([unescaped_val, "baz"], 1)
self.assertEqual([metric_family], list(families))

def test_help_escaping(self):
for escaped_val, unescaped_val in [
('foo', 'foo'),
('\\foo', '\\foo'),
('\\\\foo', '\\foo'),
('foo\\', 'foo\\'),
('foo\\\\', 'foo\\'),
('\\n', '\n'),
('\\\\n', '\\n'),
('\\\\\\n', '\\\n'),
('\\"', '\\"'),
('\\\\"', '\\"'),
('\\\\\\"', '\\\\"')]:
families = list(text_string_to_metric_families("""
# TYPE a counter
# HELP a %s
a{foo="bar"} 1
""" % escaped_val))
metric_family = CounterMetricFamily("a", unescaped_val, labels=["foo"])
metric_family.add_metric(["bar"], 1)
self.assertEqual([metric_family], list(families))

def test_escaping(self):
families = text_string_to_metric_families("""# TYPE a counter
# HELP a he\\n\\\\l\\tp
Expand Down
0