From 1553dfb103e098e95d5444b1f97de4b20801aed0 Mon Sep 17 00:00:00 2001 From: Julian Simioni Date: Sat, 13 Oct 2018 11:19:55 -0400 Subject: [PATCH] fix(geo_common): check for invalid bbox where min=max This condition will cause Elasticsearch to throw an error, we should catch it outselves first. The error is more friendly than the case where min>max, but still an error. Connects https://github.com/pelias/api/pull/1050 --- sanitizer/_geo_common.js | 2 +- test/unit/sanitizer/_geo_common.js | 25 ++++++++++++++++++++----- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/sanitizer/_geo_common.js b/sanitizer/_geo_common.js index 469a3bbb..e8134cc1 100644 --- a/sanitizer/_geo_common.js +++ b/sanitizer/_geo_common.js @@ -48,7 +48,7 @@ function sanitize_bbox_min_max(raw, key_prefix) { const max = parseFloat(raw[`${key_prefix}.max_${dimension}`]); const min = parseFloat(raw[`${key_prefix}.min_${dimension}`]); - if (max < min) { + if (max <= min) { throw new Error(`min_${dimension} is larger than max_${dimension} in ${key_prefix}`); } }); diff --git a/test/unit/sanitizer/_geo_common.js b/test/unit/sanitizer/_geo_common.js index a8e6fc05..48485818 100644 --- a/test/unit/sanitizer/_geo_common.js +++ b/test/unit/sanitizer/_geo_common.js @@ -241,13 +241,12 @@ module.exports.tests.rect = function(test, common) { }; var mandatory = false; - sanitize.sanitize_rect( 'boundary.rect', clean, params, mandatory ); - t.equal(clean['boundary.rect.min_lat'], params['boundary.rect.min_lat'], 'min_lat approved'); - t.equal(clean['boundary.rect.max_lat'], params['boundary.rect.max_lat'], 'min_lat approved'); - t.equal(clean['boundary.rect.min_lon'], params['boundary.rect.min_lon'], 'min_lat approved'); - t.equal(clean['boundary.rect.max_lon'], params['boundary.rect.max_lon'], 'min_lat approved'); + t.throws( function(){ + sanitize.sanitize_rect( 'boundary.rect', clean, params, mandatory ); + }); t.end(); }); + test('invalid rect - partially specified', function (t) { var clean = {}; var params = { @@ -299,6 +298,22 @@ module.exports.tests.rect = function(test, common) { t.end(); }); + test('invalid rect - max/min equal', function (t) { + var clean = {}; + var params = { + 'boundary.rect.max_lat': 52, + 'boundary.rect.max_lon': 14, + 'boundary.rect.min_lat': 52, + 'boundary.rect.min_lon': 12 + }; + var mandatory = false; + + t.throws( function() { + sanitize.sanitize_rect( 'boundary.rect', clean, params, mandatory ); + }); + t.end(); + }); + test('invalid rect - out of range latitude', function(t) { var clean = {}; var params = {