Skip to content

Commit 21d6e7b

Browse files
committed
mango: fix validation of use_index
The `binary:split/2` function will split the input only at the first occurrence of the pattern which is not suitable for identifying each part of the index name, therefore validating it. Add the `global` option to fully break the index name into parts and let the related validation logic work as expected.
1 parent e21d0e9 commit 21d6e7b

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

src/mango/src/mango_error.erl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,12 @@ info(mango_opts, {invalid_bulk_docs, Val}) ->
253253
[Val]
254254
)
255255
};
256+
info(mango_opts, {invalid_index_name, Val}) ->
257+
{
258+
400,
259+
<<"invalid_index_name">>,
260+
fmt("Invalid index name: ~w", [Val])
261+
};
256262
info(mango_opts, {invalid_ejson, Val}) ->
257263
{
258264
400,

src/mango/src/mango_opts.erl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ validate_bulk_docs(Else) ->
262262
?MANGO_ERROR({invalid_bulk_docs, Else}).
263263

264264
validate_use_index(IndexName) when is_binary(IndexName) ->
265-
case binary:split(IndexName, <<"/">>) of
265+
case binary:split(IndexName, <<"/">>, [global]) of
266266
[DesignId] ->
267267
{ok, [DesignId]};
268268
[<<"_design">>, DesignId] ->

src/mango/test/05-index-selection-test.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,16 @@ def test_explain_sort_reverse(self):
211211
)
212212
self.assertEqual(resp_explain["index"]["type"], "json")
213213

214+
def test_use_index_with_invalid_name(self):
215+
for index in ["foo/bar/baz", ["foo", "bar", "baz"]]:
216+
with self.subTest(index=index):
217+
try:
218+
self.db.find({"manager": True}, use_index=index)
219+
except Exception as e:
220+
self.assertEqual(e.response.status_code, 400)
221+
else:
222+
raise AssertionError("did not fail on invalid index name")
223+
214224
def test_use_index_without_fallback(self):
215225
with self.subTest(use_index="valid"):
216226
docs = self.db.find(

0 commit comments

Comments
 (0)