Skip to content

Commit a613375

Browse files
karenzsheaDenis Chapligindanpat
authored
Validate source/destination indices correctly in nodejs support (#5595)
* validate source/destination indices correctly Co-authored-by: Denis Chapligin <[email protected]> Co-authored-by: Daniel Patterson <[email protected]>
1 parent 960269f commit a613375

File tree

4 files changed

+22
-8
lines changed

4 files changed

+22
-8
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
- Misc:
1111
- CHANGED: Cleanup NodeJS dependencies [#5945](https://github.com/Project-OSRM/osrm-backend/pull/5945)
1212
- CHANGED: Unify `.osrm.turn_penalites_index` dump processing same with `.osrm.turn_weight_penalties` and `.osrm.turn_duration_penalties` [#5868](https://github.com/Project-OSRM/osrm-backend/pull/5868)
13+
- FIXED: Properly validate source/destination validation in NodeJS table service [#5595](https://github.com/Project-OSRM/osrm-backend/pull/5595/files)
1314
- FIXED: turn.roads_on_the_left not containing incoming roads and turn.roads_on_the_right not containing outgoing roads on two-way roads [#5128](https://github.com/Project-OSRM/osrm-backend/issues/5128)
1415
- Profile:
1516
- ADDED: Profile debug script which fetches a way from OSM then outputs the result of the profile. [#5908](https://github.com/Project-OSRM/osrm-backend/pull/5908)
@@ -20,6 +21,7 @@
2021
- FIXED: Fix vector bool permutation in graph contraction step [#5882](https://github.com/Project-OSRM/osrm-backend/pull/5882)
2122
- API:
2223
- FIXED: Undo libosrm API break by adding old interface as method overload [#5861](https://github.com/Project-OSRM/osrm-backend/pull/5861)
24+
- FIXED: Fixed validation of sources/destinations when accessed via node bindings [#5595](https://github.com/Project-OSRM/osrm-backend/pull/5595)
2325

2426
# 5.23.0
2527
- Changes from 5.22.0

include/nodejs/node_osrm_support.hpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1210,10 +1210,9 @@ argumentsToTableParameter(const Nan::FunctionCallbackInfo<v8::Value> &args,
12101210
if (source->IsUint32())
12111211
{
12121212
size_t source_value = Nan::To<unsigned>(source).FromJust();
1213-
if (source_value > params->coordinates.size())
1213+
if (source_value >= params->coordinates.size())
12141214
{
1215-
Nan::ThrowError(
1216-
"Source indices must be less than or equal to the number of coordinates");
1215+
Nan::ThrowError("Source indices must be less than the number of coordinates");
12171216
return table_parameters_ptr();
12181217
}
12191218

@@ -1250,9 +1249,9 @@ argumentsToTableParameter(const Nan::FunctionCallbackInfo<v8::Value> &args,
12501249
if (destination->IsUint32())
12511250
{
12521251
size_t destination_value = Nan::To<unsigned>(destination).FromJust();
1253-
if (destination_value > params->coordinates.size())
1252+
if (destination_value >= params->coordinates.size())
12541253
{
1255-
Nan::ThrowError("Destination indices must be less than or equal to the number "
1254+
Nan::ThrowError("Destination indices must be less than the number "
12561255
"of coordinates");
12571256
return table_parameters_ptr();
12581257
}

test/nodejs/table.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ tables.forEach(function(annotation) {
130130
});
131131

132132
test('table: ' + annotation + ' throws on invalid arguments', function(assert) {
133-
assert.plan(15);
133+
assert.plan(17);
134134
var osrm = new OSRM(data_path);
135135
var options = {annotations: [annotation.slice(0,-1)]};
136136
assert.throws(function() { osrm.table(options); },
@@ -157,21 +157,27 @@ tables.forEach(function(annotation) {
157157
/Sources must be an array of indices \(or undefined\)/);
158158
options.sources = [0, 4];
159159
assert.throws(function() { osrm.table(options, function(err, response) {}) },
160-
/Source indices must be less than or equal to the number of coordinates/);
160+
/Source indices must be less than the number of coordinates/);
161161
options.sources = [0.3, 1.1];
162162
assert.throws(function() { osrm.table(options, function(err, response) {}) },
163163
/Source must be an integer/);
164+
options.sources = [0, 1, 2];
165+
assert.throws(function() { osrm.table(options, function(err, response) {}) },
166+
/Source indices must be less than the number of coordinates/);
164167

165168
options.destinations = true;
166169
delete options.sources;
167170
assert.throws(function() { osrm.table(options, function(err, response) {}) },
168171
/Destinations must be an array of indices \(or undefined\)/);
169172
options.destinations = [0, 4];
170173
assert.throws(function() { osrm.table(options, function(err, response) {}) },
171-
/Destination indices must be less than or equal to the number of coordinates/);
174+
/Destination indices must be less than the number of coordinates/);
172175
options.destinations = [0.3, 1.1];
173176
assert.throws(function() { osrm.table(options, function(err, response) {}) },
174177
/Destination must be an integer/);
178+
options.destinations = [0, 4];
179+
assert.throws(function() { osrm.table(options, function(err, response) {}) },
180+
/Destination indices must be less than the number of coordinates/);
175181

176182
// does not throw: the following two have been changed in OSRM v5
177183
options.sources = [0, 1];

unit_tests/server/parameters_parser.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,13 @@ BOOST_AUTO_TEST_CASE(invalid_table_urls)
108108
BOOST_CHECK_EQUAL(
109109
testInvalidOptions<TableParameters>("1,2;3,4?annotations=durations&fallback_speed=-1"),
110110
28UL);
111+
// TODO(danpat): this is only testing invalid grammar which isn't capable of checking
112+
// for values that need to reference other things currently. These
113+
// requests are gramatically correct, but semantically incorrect.
114+
// The table service properly fails these, as it checks IsValid() after
115+
// parsing, which fails when sources/destinations are too large
116+
// BOOST_CHECK_EQUAL(testInvalidOptions<TableParameters>("1,2;3,4?sources=2"), 7UL);
117+
// BOOST_CHECK_EQUAL(testInvalidOptions<TableParameters>("1,2;3,4?destinations=2"), 7UL);
111118
}
112119

113120
BOOST_AUTO_TEST_CASE(valid_route_hint)

0 commit comments

Comments
 (0)