From 144da87db3a770098a583567b6ff8083d93bdb81 Mon Sep 17 00:00:00 2001 From: Colin-Alexa Robinson Date: Wed, 1 Nov 2017 13:41:14 -0400 Subject: [PATCH 1/3] escape pipes in tags datadog splits data from type annotations using a '|' character. Node/express allows pipe characters in routes (regexes). To avoid errors, escape pipe characters before sending statsd data --- lib/index.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/index.js b/lib/index.js index f193cdc..57665e3 100644 --- a/lib/index.js +++ b/lib/index.js @@ -9,6 +9,10 @@ module.exports = function (options) { let base_url = options.base_url || false; let response_code = options.response_code || false; + function escapePipes(str) { + return str.replace(/\|/g, "\\|"); + } + return function (req, res, next) { if (!req._startTime) { req._startTime = new Date(); @@ -31,19 +35,19 @@ module.exports = function (options) { } let statTags = [ - `route:${baseUrl}${req.route.path}` + escapePipes(`route:${baseUrl}${req.route.path}`) ].concat(dynamicTags); if (options.method) { - statTags.push(`method:${req.method.toLowerCase()}`); + statTags.push(escapePipes(`method:${req.method.toLowerCase()}`)); } if (options.protocol && req.protocol) { - statTags.push(`protocol:${req.protocol}`); + statTags.push(escapePipes(`protocol:${req.protocol}`)); } if (path !== false) { - statTags.push(`path:${baseUrl}${req.path}`); + statTags.push(escapePipes(`path:${baseUrl}${req.path}`)); } if (response_code) { From 4720eee480a6a7600610f88ee105f20f67658a98 Mon Sep 17 00:00:00 2001 From: Colin-Alexa Robinson Date: Wed, 1 Nov 2017 13:49:40 -0400 Subject: [PATCH 2/3] update version for escape pipes fix --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 0a1d005..06f8092 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@condenast/connect-datadog", - "version": "0.0.7", + "version": "0.0.8", "description": "Datadog middleware for Connect JS / Express", "main": "index.js", "repository": { From ecfb85c09f3f373653352c3e0232249b7ca93939 Mon Sep 17 00:00:00 2001 From: Colin-Alexa Robinson Date: Thu, 2 Nov 2017 17:39:42 -0400 Subject: [PATCH 3/3] refactor make regexp literal const and guard undefined strings --- lib/index.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/index.js b/lib/index.js index 57665e3..116fe7f 100644 --- a/lib/index.js +++ b/lib/index.js @@ -8,9 +8,10 @@ module.exports = function (options) { let path = options.path || false; let base_url = options.base_url || false; let response_code = options.response_code || false; + const RE_PIPE = /\|/g; function escapePipes(str) { - return str.replace(/\|/g, "\\|"); + return str && str.replace(RE_PIPE, "\\|"); } return function (req, res, next) {