Skip to content

Commit

Permalink
mango: fix validation of use_index
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
pgj committed Oct 5, 2023
1 parent 3cd732e commit 449edf0
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/mango/src/mango_error.erl
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,12 @@ info(mango_opts, {invalid_bulk_docs, Val}) ->
[Val]
)
};
info(mango_opts, {invalid_index_name, Val}) ->
{
400,
<<"invalid_index_name">>,
fmt("Invalid index name: ~w", [Val])
};
info(mango_opts, {invalid_ejson, Val}) ->
{
400,
Expand Down
2 changes: 1 addition & 1 deletion src/mango/src/mango_opts.erl
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ validate_bulk_docs(Else) ->
?MANGO_ERROR({invalid_bulk_docs, Else}).

validate_use_index(IndexName) when is_binary(IndexName) ->
case binary:split(IndexName, <<"/">>) of
case binary:split(IndexName, <<"/">>, [global]) of
[DesignId] ->
{ok, [DesignId]};
[<<"_design">>, DesignId] ->
Expand Down
10 changes: 10 additions & 0 deletions src/mango/test/05-index-selection-test.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,16 @@ def test_explain_sort_reverse(self):
)
self.assertEqual(resp_explain["index"]["type"], "json")

def test_use_index_with_invalid_name(self):
for index in ["foo/bar/baz", ["foo", "bar", "baz"]]:
with self.subTest(index=index):
try:
self.db.find({"manager": True}, use_index=index)
except Exception as e:
self.assertEqual(e.response.status_code, 400)
else:
raise AssertionError("did not fail on invalid index name")

def test_use_index_without_fallback(self):
with self.subTest(use_index="valid"):
docs = self.db.find(
Expand Down

0 comments on commit 449edf0

Please sign in to comment.