@cryptotaxi247 / netdata-1 / commits / 4e033f43c

properly escape prometheus labels; fixes #2407

Costa Tsaousis (ktsaou) committed Jul 8, 2017 at 00:31 UTC 4e033f43cb07c1d963c6a0839316bd15671bcbad
1 file changed +22 -2
src/rrd2json.c
+22 -2
@@ -176,7 +176,7 @@ static inline size_t prometheus_name_copy(char *d, const char *s, size_t usable)
176 for(n = 0; *s && n < usable ; d++, s++, n++) {
177 register char c = *s;
178
179 - if(unlikely(c != '.' && !isalnum(c))) *d = '_';
179 + if(!isalnum(c)) *d = '_';
180 else *d = c;
181 }
182 *d = '\0';
@@ -184,13 +184,33 @@ static inline size_t prometheus_name_copy(char *d, const char *s, size_t usable)
184 return n;
185 }
186
187 +static inline size_t prometheus_label_copy(char *d, const char *s, size_t usable) {
188 + size_t n;
189 +
190 + // make sure we can escape one character without overflowing the buffer
191 + usable--;
192 +
193 + for(n = 0; *s && n < usable ; d++, s++, n++) {
194 + register char c = *s;
195 +
196 + if(unlikely(c == '"' || c == '\\' || c == '\n')) {
197 + *d++ = '\\';
198 + n++;
199 + }
200 + *d = c;
201 + }
202 + *d = '\0';
203 +
204 + return n;
205 +}
206 +
207 #define PROMETHEUS_ELEMENT_MAX 256
208
209 void rrd_stats_api_v1_charts_allmetrics_prometheus(RRDHOST *host, BUFFER *wb, int help, int types, int names) {
210 rrdhost_rdlock(host);
211
212 char hostname[PROMETHEUS_ELEMENT_MAX + 1];
193 - prometheus_name_copy(hostname, host->hostname, PROMETHEUS_ELEMENT_MAX);
213 + prometheus_label_copy(hostname, host->hostname, PROMETHEUS_ELEMENT_MAX);
214
215 // for each chart
216 RRDSET *st;