Skip to content

Commit

Permalink
fix(mango): GET invalid path under _index should not cause 500
Browse files Browse the repository at this point in the history
Sending GET requests targeting paths under the `/{db}/_index`
endpoint, e.g. `/{db}/_index/something`, cause an internal error.
Change the endpoint's behavior to gracefully return HTTP 405
"Method Not Allowed" instead to be consistent with others.
  • Loading branch information
pgj authored and nickva committed Apr 19, 2023
1 parent df52be5 commit c1195e4
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/mango/src/mango_httpd.erl
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,6 @@ handle_index_req(#httpd{method = 'POST', path_parts = [_, _]} = Req, Db) ->
end
end,
chttpd:send_json(Req, {[{result, Status}, {id, Id}, {name, Name}]});
handle_index_req(#httpd{path_parts = [_, _]} = Req, _Db) ->
chttpd:send_method_not_allowed(Req, "GET,POST");
%% Essentially we just iterate through the list of ddoc ids passed in and
%% delete one by one. If an error occurs, all previous documents will be
%% deleted, but an error will be thrown for the current ddoc id.
Expand Down Expand Up @@ -189,7 +187,9 @@ handle_index_req(
?MANGO_ERROR({error_saving_ddoc, Error})
end;
handle_index_req(#httpd{path_parts = [_, _, _DDocId0, _Type, _Name]} = Req, _Db) ->
chttpd:send_method_not_allowed(Req, "DELETE").
chttpd:send_method_not_allowed(Req, "DELETE");
handle_index_req(#httpd{path_parts = [_, _ | _]} = Req, _Db) ->
chttpd:send_method_not_allowed(Req, "GET,POST").

handle_explain_req(#httpd{method = 'POST'} = Req, Db) ->
chttpd:validate_ctype(Req, "application/json"),
Expand Down
4 changes: 4 additions & 0 deletions src/mango/test/01-index-crud-test.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ def test_bad_ddocs(self):
else:
raise AssertionError("bad create index")

def test_bad_url(self):
r = self.db.sess.get(self.db.path("_index/foo"))
self.assertEqual(r.status_code, 405)

def test_create_idx_01(self):
fields = ["foo", "bar"]
ret = self.db.create_index(fields, name="idx_01")
Expand Down

0 comments on commit c1195e4

Please sign in to comment.