From 8ddd61b34d52ad2fbb253c5bc2a38f58a509a678 Mon Sep 17 00:00:00 2001 From: Adam Dubiel Date: Mon, 27 Oct 2014 13:27:07 +0100 Subject: [PATCH] Validate negative sampling --- lib/helpers.js | 13 +++++++++++-- test/helpers_tests.js | 1 + 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/helpers.js b/lib/helpers.js index 98c6225..52ba3a3 100644 --- a/lib/helpers.js +++ b/lib/helpers.js @@ -11,6 +11,15 @@ function isNumber(str) { return Boolean(str && !isNaN(str)); } +function isValidSampleRate(str) { + var validSampleRate = false; + if(str.length > 1 && str[0] === '@') { + var numberStr = str.substring(1); + validSampleRate = isNumber(numberStr) && numberStr[0] != '-'; + } + return validSampleRate; +} + function is_valid_packet(fields) { // test for existing metrics type @@ -19,8 +28,8 @@ function is_valid_packet(fields) { } // filter out malformed sample rates - if (fields[2] !== undefined) { - if (fields[2].length <= 1 || fields[2][0] != '@' || !isNumber(fields[2].substring(1))) { + if(fields[2] !== undefined) { + if(!isValidSampleRate(fields[2])) { return false; } } diff --git a/test/helpers_tests.js b/test/helpers_tests.js index d10da0b..cb76984 100644 --- a/test/helpers_tests.js +++ b/test/helpers_tests.js @@ -112,6 +112,7 @@ module.exports = { test.equals(helpers.is_valid_packet(['345345', 'ms', '@.']), false); test.equals(helpers.is_valid_packet(['345345', 'ms', '@.1.']), false); test.equals(helpers.is_valid_packet(['345345', 'ms', '@.1.2.3']), false); + test.equals(helpers.is_valid_packet(['345345', 'ms', '@-1.0']), false); test.done(); },