Skip to content

Commit 599d410

Browse files
authored
Merge pull request #457 from mapbox/code-ql-fixes
Fix codescan bugs
2 parents 356ff96 + 0323db2 commit 599d410

File tree

5 files changed

+23
-6
lines changed

5 files changed

+23
-6
lines changed

lib/events.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ MapboxEventManager.prototype = {
272272
*/
273273
shouldEnableLogging: function(options){
274274
if (options.enableEventLogging === false) return false;
275-
if (options.origin && options.origin.indexOf('api.mapbox.com') == -1) return false;
275+
if (options.origin && options.origin !== 'https://api.mapbox.com') return false;
276276
// hard to make sense of events when a local instance is suplementing results from origin
277277
if (options.localGeocoder) return false;
278278
// hard to make sense of events when a custom filter is in use

lib/index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -569,10 +569,9 @@ MapboxGeocoder.prototype = {
569569

570570
_requestType: function(options, search) {
571571
var type;
572-
const reverseGeocodeCoordRgx = /^[ ]*(-?\d+\.?\d*)[, ]+(-?\d+\.?\d*)[ ]*$/;
573572
if (options.localGeocoderOnly) {
574573
type = GEOCODE_REQUEST_TYPE.LOCAL;
575-
} else if (options.reverseGeocode && reverseGeocodeCoordRgx.test(search)) {
574+
} else if (options.reverseGeocode && utils.REVERSE_GEOCODE_COORD_RGX.test(search)) {
576575
type = GEOCODE_REQUEST_TYPE.REVERSE;
577576
} else {
578577
type = GEOCODE_REQUEST_TYPE.FORWARD;
@@ -649,8 +648,9 @@ MapboxGeocoder.prototype = {
649648
case GEOCODE_REQUEST_TYPE.FORWARD: {
650649
// Ensure that any reverse geocoding looking request is cleaned up
651650
// to be processed as only a forward geocoding request by the server.
652-
const reverseGeocodeCoordRgx = /^[ ]*(-?\d+\.?\d*)[, ]+(-?\d+\.?\d*)*[ ]*$/;
653-
if (reverseGeocodeCoordRgx.test(search)) {
651+
const trimmedSearch = search.trim();
652+
const reverseGeocodeCoordRgx = /^(-?\d{1,3}(\.\d{0,256})?)[, ]+(-?\d{1,3}(\.\d{0,256})?)?$/;
653+
if (reverseGeocodeCoordRgx.test(trimmedSearch)) {
654654
search = search.replace(/,/g, ' ');
655655
}
656656
config = extend(config, { query: search });

lib/utils.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,10 @@ function getAddressInfo(feature) {
6060
return addrInfo;
6161
}
6262

63+
const REVERSE_GEOCODE_COORD_RGX = /^[ ]*(-?\d{1,3}(\.\d{0,256})?)[, ]+(-?\d{1,3}(\.\d{0,256})?)[ ]*$/;
64+
6365
module.exports = {
6466
transformFeatureToGeolocationText: transformFeatureToGeolocationText,
6567
getAddressInfo: getAddressInfo,
68+
REVERSE_GEOCODE_COORD_RGX: REVERSE_GEOCODE_COORD_RGX,
6669
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"scripts": {
99
"start": "budo debug/index.js --dir debug --live -- -t brfs ",
1010
"prepublish": "NODE_ENV=production && mkdir -p dist && browserify lib/index.js --transform [ babelify --global ] --standalone MapboxGeocoder | uglifyjs -c -m > dist/mapbox-gl-geocoder.min.js && cp lib/mapbox-gl-geocoder.css dist/",
11-
"test": "browserify -t envify test/index.js test/events.test.js | smokestack -b firefox | tap-status | tap-color",
11+
"test": "browserify -t envify test/index.js test/events.test.js test/utils.test.js | smokestack -b firefox | tap-status | tap-color",
1212
"docs": "documentation build lib/index.js --format=md > API.md",
1313
"pretest": "npm run lint",
1414
"lint": "eslint lib test",

test/utils.test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
var test = require('tape');
2+
var utils = require('../lib/utils');
3+
4+
test('REVERSE_GEOCODE_COORD_RGX', function (t) {
5+
t.ok(utils.REVERSE_GEOCODE_COORD_RGX.test('12, 34'), 'Reverse: "12, 34"');
6+
t.ok(utils.REVERSE_GEOCODE_COORD_RGX.test('1,2'), 'Reverse: "1,2"');
7+
t.ok(utils.REVERSE_GEOCODE_COORD_RGX.test('12.123, 34.345'), 'Reverse: "12.123, 34.345"');
8+
t.ok(utils.REVERSE_GEOCODE_COORD_RGX.test('12, 34.345'), 'Reverse: "12, 34.345"');
9+
t.ok(utils.REVERSE_GEOCODE_COORD_RGX.test('12., 34.'), 'Reverse: "12., 34."');
10+
t.ok(utils.REVERSE_GEOCODE_COORD_RGX.test('122, 41'), 'Reverse: "122, 41"');
11+
t.ok(utils.REVERSE_GEOCODE_COORD_RGX.test('12, 123'), 'Reverse: "12, 123"');
12+
t.notOk(utils.REVERSE_GEOCODE_COORD_RGX.test('1234, 4568'), 'Forward: "1234, 4568"');
13+
t.notOk(utils.REVERSE_GEOCODE_COORD_RGX.test('123 Main'), 'Forward: "123 Main"');
14+
})

0 commit comments

Comments
 (0)