diff --git a/lib/cartodb/server_options.js b/lib/cartodb/server_options.js index 1af0634c..c2264c1d 100644 --- a/lib/cartodb/server_options.js +++ b/lib/cartodb/server_options.js @@ -7,6 +7,7 @@ var LZMA = require('lzma').LZMA; var TemplateMaps = require('./template_maps.js'); var MapConfigNamedLayersAdapter = require('./models/mapconfig_named_layers_adapter'); var CdbRequest = require('./models/cdb_request'); +var assert = require('assert'); // Whitelist query parameters and attach format var REQUEST_QUERY_PARAMS_WHITELIST = [ @@ -233,10 +234,14 @@ module.exports = function(redisPool) { me.addCacheChannel = function(app, req, cb) { // skip non-GET requests, or requests for which there's no response if ( req.method != 'GET' || ! req.res ) { cb(null, null); return; } - if (req.profiler) req.profiler.start('addCacheChannel'); + if (req.profiler) { + req.profiler.start('addCacheChannel'); + } var res = req.res; var cache_policy = req.query.cache_policy; - if ( req.params.token ) cache_policy = 'persist'; + if ( req.params.token ) { + cache_policy = 'persist'; + } if ( cache_policy == 'persist' ) { res.header('Cache-Control', 'public,max-age=31536000'); // 1 year } else { @@ -256,8 +261,10 @@ module.exports = function(redisPool) { res.header('Last-Modified', lastUpdated.toUTCString()); me.generateCacheChannel(app, req, function(err, channel){ - if (req.profiler) req.profiler.done('generateCacheChannel'); - if (req.profiler) req.profiler.end(); + if (req.profiler) { + req.profiler.done('generateCacheChannel'); + req.profiler.end(); + } if ( ! err ) { res.header('X-Cache-Channel', channel); cb(null, channel); @@ -312,8 +319,12 @@ module.exports = function(redisPool) { // take place before proceeding. Error will be logged // asyncronously cartoData.incMapviewCount(username, mapconfig.stat_tag, function(err) { - if (req.profiler) req.profiler.done('incMapviewCount'); - if ( err ) console.log("ERROR: failed to increment mapview count for user '" + username + "': " + err); + if (req.profiler) { + req.profiler.done('incMapviewCount'); + } + if ( err ) { + console.log("ERROR: failed to increment mapview count for user '" + username + "': " + err); + } done(); }); @@ -329,8 +340,10 @@ module.exports = function(redisPool) { queryTablesApi.getAffectedTablesAndLastUpdatedTime(username, sql, this); }, function handleAffectedTablesAndLastUpdatedTime(err, result) { - if (req.profiler) req.profiler.done('queryTablesAndLastUpdated'); - if ( err ) throw err; + if (req.profiler) { + req.profiler.done('queryTablesAndLastUpdated'); + } + assert.ifError(err); var cacheChannel = me.buildCacheChannel(dbName, result.affectedTables); me.channelCache[cacheKey] = cacheChannel; @@ -420,7 +433,7 @@ module.exports = function(redisPool) { cartoData.getUserMapKey(user, this); }, function checkApiKey(err, val){ - if (err) throw err; + assert.ifError(err); return ( val && givenKey == val ) ? 1 : 0; }, function finish(err, authorized) { @@ -444,8 +457,10 @@ module.exports = function(redisPool) { that.authorizedByAPIKey(req, this); }, function checkApiKey(err, authorized){ - if (req.profiler) req.profiler.done('authorizedByAPIKey'); - if (err) throw err; + if (req.profiler) { + req.profiler.done('authorizedByAPIKey'); + } + assert.ifError(err); // if not authorized by api_key, continue if (authorized !== 1) { @@ -463,7 +478,7 @@ module.exports = function(redisPool) { }); }, function checkSignAuthorized(err, signed_by){ - if (err) throw err; + assert.ifError(err); if (req.profiler) { if ( req.params._authorizedByApiKey ) { req.profiler.done('setDBAuth'); @@ -476,7 +491,9 @@ module.exports = function(redisPool) { // request not authorized by signer. // if table was given, continue to check table privacy - if ( req.params.table ) return null; + if ( req.params.table ) { + return null; + } // if no signer name was given, let dbparams and // PostgreSQL do the rest. @@ -494,22 +511,28 @@ module.exports = function(redisPool) { // Authorized by "signed_by" ! _.extend(req.params, { _authorizedBySigner: signed_by }); pgConnection.setDBAuth(signed_by, req.params, function(err) { - if (req.profiler) req.profiler.done('setDBAuth'); + if (req.profiler) { + req.profiler.done('setDBAuth'); + } callback(err, true); // authorized (or error) }); }, function getDatabase(err){ - if (err) throw err; + assert.ifError(err); // NOTE: only used to get to table privacy cartoData.getUserDBName(user, this); }, function getPrivacy(err, dbname){ - if (err) throw err; - if (req.profiler) req.profiler.done('tablePrivacy_getUserDBName'); + assert.ifError(err); + if (req.profiler) { + req.profiler.done('tablePrivacy_getUserDBName'); + } cartoData.getTablePrivacy(dbname, req.params.table, this); }, function(err, privacy){ - if (req.profiler) req.profiler.done('getTablePrivacy'); + if (req.profiler) { + req.profiler.done('getTablePrivacy'); + } callback(err, privacy !== "0"); } ); @@ -538,7 +561,9 @@ module.exports = function(redisPool) { lzmaWorker.decompress( lzma, function(result) { - if (req.profiler) req.profiler.done('LZMA decompress'); + if (req.profiler) { + req.profiler.done('LZMA decompress'); + } try { delete req.query.lzma; _.extend(req.query, JSON.parse(result)); @@ -565,11 +590,15 @@ module.exports = function(redisPool) { //console.log("Request parameters include token " + req.params.token); var tksplit = req.params.token.split(':'); req.params.token = tksplit[0]; - if ( tksplit.length > 1 ) req.params.cache_buster= tksplit[1]; + if ( tksplit.length > 1 ) { + req.params.cache_buster= tksplit[1]; + } tksplit = req.params.token.split('@'); if ( tksplit.length > 1 ) { req.params.signer = tksplit.shift(); - if ( ! req.params.signer ) req.params.signer = user; + if ( ! req.params.signer ) { + req.params.signer = user; + } else if ( req.params.signer !== user ) { var err = new Error('Cannot use map signature of user "' + req.params.signer + '" on database of user "' + user + '"'); @@ -591,15 +620,19 @@ module.exports = function(redisPool) { // for cartodb, ensure interactivity is cartodb_id or user specified req.params.interactivity = req.params.interactivity || 'cartodb_id'; - if (req.profiler) req.profiler.done('req2params.setup'); + if (req.profiler) { + req.profiler.done('req2params.setup'); + } step( function getPrivacy(){ me.authorize(req, this); }, function gatekeep(err, authorized){ - if (req.profiler) req.profiler.done('authorize'); - if(err) throw err; + if (req.profiler) { + req.profiler.done('authorize'); + } + assert.ifError(err); if(!authorized) { err = new Error("Sorry, you are unauthorized (permission denied)"); err.http_status = 403; @@ -608,7 +641,7 @@ module.exports = function(redisPool) { return null; }, function getDatabase(err){ - if(err) throw err; + assert.ifError(err); pgConnection.setDBConn(user, req.params, this); }, function finishSetup(err) {