Skip to content

Commit

Permalink
feat(reverse): clamp boundary.circle.radius for reverse queries
Browse files Browse the repository at this point in the history
  • Loading branch information
missinglink committed Mar 22, 2022
1 parent ad93fc4 commit 44ea887
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 10 deletions.
22 changes: 15 additions & 7 deletions sanitizer/_geo_reverse.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
var geo_common = require ('./_geo_common');
var _ = require('lodash');
var defaults = require('../query/reverse_defaults');
var LAT_LON_IS_REQUIRED = true,
CIRCLE_IS_REQUIRED = false;

const non_coarse_layers = ['venue', 'address', 'street'];
const _ = require('lodash');
const geo_common = require ('./_geo_common');
const LAT_LON_IS_REQUIRED = true;
const CIRCLE_IS_REQUIRED = false;
const CIRCLE_MIN_RADIUS = 0.00001;
const CIRCLE_MAX_RADIUS = 5.0;

// validate inputs, convert types and apply defaults
function _sanitize( raw, clean ){
Expand Down Expand Up @@ -32,6 +31,15 @@ function _sanitize( raw, clean ){
// santize the boundary.circle
geo_common.sanitize_circle( 'boundary.circle', clean, raw, CIRCLE_IS_REQUIRED );

// clamp the 'boundary.circle.radius' param to an absolute value range.
// note: large radius values have a severe performance impact.
if (_.isNumber(clean['boundary.circle.radius']) ){
clean['boundary.circle.radius'] = _.clamp(
clean['boundary.circle.radius'],
CIRCLE_MIN_RADIUS, CIRCLE_MAX_RADIUS
);
}

}
catch (err) {
messages.errors.push( err.message );
Expand Down
67 changes: 64 additions & 3 deletions test/unit/sanitizer/_geo_reverse.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,27 +61,88 @@ module.exports.tests.warning_situations = (test, common) => {
};

module.exports.tests.success_conditions = (test, common) => {
// note: this behaviour is historic and should probably be removed.
// it's possible to add non-numeric tokens to the boundary.circle.radius
// value and they are simply stripped out by parseFloat() with warning.
test('boundary.circle.radius must be a positive number', (t) => {
const raw = {
'point.lat': '12.121212',
'point.lon': '21.212121',
'boundary.circle.radius': '1km' // note the 'km'
};
const clean = {};
const errorsAndWarnings = sanitizer.sanitize(raw, clean);

t.equals(raw['boundary.circle.lat'], 12.121212);
t.equals(raw['boundary.circle.lon'], 21.212121);
t.equals(raw['boundary.circle.radius'], '1km');
t.equals(clean['boundary.circle.lat'], 12.121212);
t.equals(clean['boundary.circle.lon'], 21.212121);
t.equals(clean['boundary.circle.radius'], 1.0);

t.deepEquals(errorsAndWarnings, { errors: [], warnings: [] });
t.end();
});

test('boundary.circle.radius specified in request should override default', (t) => {
const raw = {
'point.lat': '12.121212',
'point.lon': '21.212121',
'boundary.circle.radius': '3248732857km' // this will never be the default
'boundary.circle.radius': '2'
};
const clean = {};
const errorsAndWarnings = sanitizer.sanitize(raw, clean);

t.equals(raw['boundary.circle.lat'], 12.121212);
t.equals(raw['boundary.circle.lon'], 21.212121);
t.equals(raw['boundary.circle.radius'], '3248732857km');
t.equals(raw['boundary.circle.radius'], '2');
t.equals(clean['boundary.circle.lat'], 12.121212);
t.equals(clean['boundary.circle.lon'], 21.212121);
t.equals(clean['boundary.circle.radius'], 3248732857.0);
t.equals(clean['boundary.circle.radius'], 2.0);

t.deepEquals(errorsAndWarnings, { errors: [], warnings: [] });
t.end();
});

test('boundary.circle.radius specified should be clamped to MAX', (t) => {
const raw = {
'point.lat': '12.121212',
'point.lon': '21.212121',
'boundary.circle.radius': '10000'
};
const clean = {};
const errorsAndWarnings = sanitizer.sanitize(raw, clean);

t.equals(raw['boundary.circle.lat'], 12.121212);
t.equals(raw['boundary.circle.lon'], 21.212121);
t.equals(raw['boundary.circle.radius'], '10000');
t.equals(clean['boundary.circle.lat'], 12.121212);
t.equals(clean['boundary.circle.lon'], 21.212121);
t.equals(clean['boundary.circle.radius'], 5.0);

t.deepEquals(errorsAndWarnings, { errors: [], warnings: [] });
t.end();
});

test('boundary.circle.radius specified should be clamped to MIN', (t) => {
const raw = {
'point.lat': '12.121212',
'point.lon': '21.212121',
'boundary.circle.radius': '0.000000000000001'
};
const clean = {};
const errorsAndWarnings = sanitizer.sanitize(raw, clean);

t.equals(raw['boundary.circle.lat'], 12.121212);
t.equals(raw['boundary.circle.lon'], 21.212121);
t.equals(raw['boundary.circle.radius'], '0.000000000000001');
t.equals(clean['boundary.circle.lat'], 12.121212);
t.equals(clean['boundary.circle.lon'], 21.212121);
t.equals(clean['boundary.circle.radius'], 0.00001);

t.deepEquals(errorsAndWarnings, { errors: [], warnings: [] });
t.end();
});
};

module.exports.all = (tape, common) => {
Expand Down

0 comments on commit 44ea887

Please sign in to comment.