From a4b30357e78084e1498f26a111fbd685845a5d0e Mon Sep 17 00:00:00 2001 From: Zaid Abdulla Date: Thu, 1 Nov 2012 23:36:15 +0300 Subject: [PATCH] Sanitization for number values such as RSSI and CINR --- acs.coffee | 14 +++++++++----- sanitize.coffee | 19 +++++++++++++++++++ 2 files changed, 28 insertions(+), 5 deletions(-) create mode 100644 sanitize.coffee diff --git a/acs.coffee b/acs.coffee index 2168feb..2a1fb06 100644 --- a/acs.coffee +++ b/acs.coffee @@ -7,6 +7,7 @@ tr069 = require './tr-069' tasks = require './tasks' Memcached = require 'memcached' memcached = new Memcached(config.MEMCACHED_SOCKET) +sanitize = require('./sanitize').sanitize # Create MongoDB connections @@ -34,10 +35,13 @@ updateDevice = (deviceId, actions, callback) -> updates = {} if actions.parameterValues? for p in actions.parameterValues - if common.endsWith(p[0], '.') - updates["#{p[0]}_value"] = p[1] + v = sanitize(p[0], p[1]) + path = if common.endsWith(p[0], '.') then p[0] else "#{p[0]}." + if v is undefined + updates["#{path}_value"] = p[1] else - updates["#{p[0]}._value"] = p[1] + updates["#{path}_value"] = v + updates["#{path}_orig"] = p[1] if actions.parameterNames? for p in actions.parameterNames @@ -77,7 +81,7 @@ runTask = (sessionId, deviceId, task, reqParams, response) -> nextTask = (sessionId, deviceId, response, cookies) -> - cur = tasksCollection.find({'device' : deviceId, 'status' : 0}).sort(['timestamp']).limit(1) + cur = tasksCollection.find({'device' : deviceId}).sort(['timestamp']).limit(1) cur.nextObject( (err, task) -> reqParams = {} resParams = {} @@ -133,7 +137,7 @@ else devicesCollection.count({'_id' : deviceId}, (err, count) -> if not count util.log("#{deviceId}: new device detected") - task = {device : deviceId, name : 'init', timestamp : mongo.Timestamp(), status: 0} + task = {device : deviceId, name : 'init', timestamp : mongo.Timestamp()} tasksCollection.save(task, (err) -> util.log("#{deviceId}: Added init task #{task._id}") ) diff --git a/sanitize.coffee b/sanitize.coffee new file mode 100644 index 0000000..1769eac --- /dev/null +++ b/sanitize.coffee @@ -0,0 +1,19 @@ +FLOAT_REGEX = /^([\-\+]?[0-9\.]+)/ + +extractFloat = (numStr) -> + res = FLOAT_REGEX.exec(numStr) + if res != null + parseFloat(res[1]) + else + null + +exports.sanitize = (path, value) -> + switch path + when 'InternetGatewayDevice.WiMAX.Status.RSSI' + extractFloat(value) + when 'InternetGatewayDevice.WiMAX.Status.CINR1' + extractFloat(value) + when 'InternetGatewayDevice.WiMAX.Status.CINR2' + extractFloat(value) + else + undefined \ No newline at end of file