This is an automated email from the ASF dual-hosted git repository.

tonysun83 pushed a commit to branch 3.x
in repository https://gitbox.apache.org/repos/asf/couchdb.git


The following commit(s) were added to refs/heads/3.x by this push:
     new 2f6d5096d Fix index creation with empty ddoc should return 400 (#3990)
2f6d5096d is described below

commit 2f6d5096d2dee97b1cf45c9cb314752a8d0ee11f
Author: Jiahui Li <[email protected]>
AuthorDate: Tue Apr 26 16:10:55 2022 -0500

    Fix index creation with empty ddoc should return 400 (#3990)
    
    Creating an index with "ddoc":"" or "name":"" should return a 400 Bad 
Request.
    
    This fixes: https://github.com/apache/couchdb/issues/1472
---
 src/mango/src/mango_error.erl        |  6 +++---
 src/mango/src/mango_opts.erl         | 12 +++++++-----
 src/mango/test/01-index-crud-test.py |  8 ++++++--
 3 files changed, 16 insertions(+), 10 deletions(-)

diff --git a/src/mango/src/mango_error.erl b/src/mango/src/mango_error.erl
index 0301d079f..d8ae3fcbf 100644
--- a/src/mango/src/mango_error.erl
+++ b/src/mango/src/mango_error.erl
@@ -266,11 +266,11 @@ info(mango_opts, {invalid_selector_json, BadSel}) ->
         <<"invalid_selector_json">>,
         fmt("Selector must be a JSON object, not: ~w", [BadSel])
     };
-info(mango_opts, {invalid_index_name, BadName}) ->
+info(mango_opts, invalid_empty_string) ->
     {
         400,
-        <<"invalid_index_name">>,
-        fmt("Invalid index name: ~w", [BadName])
+        <<"invalid_empty_string">>,
+        <<"Index name or ddoc cannot be empty string">>
     };
 info(mango_opts, {multiple_text_operator, {invalid_selector, BadSel}}) ->
     {
diff --git a/src/mango/src/mango_opts.erl b/src/mango/src/mango_opts.erl
index 04fe5bbf8..96aa0eb42 100644
--- a/src/mango/src/mango_opts.erl
+++ b/src/mango/src/mango_opts.erl
@@ -27,7 +27,7 @@
     is_object/1,
     is_ok_or_false/1,
 
-    validate_idx_name/1,
+    validate_non_empty_string/1,
     validate_selector/1,
     validate_use_index/1,
     validate_bookmark/1,
@@ -56,13 +56,13 @@ validate_idx_create({Props}) ->
             {tag, name},
             {optional, true},
             {default, auto_name},
-            {validator, fun validate_idx_name/1}
+            {validator, fun validate_non_empty_string/1}
         ]},
         {<<"ddoc">>, [
             {tag, ddoc},
             {optional, true},
             {default, auto_name},
-            {validator, fun validate_idx_name/1}
+            {validator, fun validate_non_empty_string/1}
         ]},
         {<<"w">>, [
             {tag, w},
@@ -235,9 +235,11 @@ is_ok_or_false(false) ->
 is_ok_or_false(Else) ->
     ?MANGO_ERROR({invalid_ok_or_false_value, Else}).
 
-validate_idx_name(auto_name) ->
+validate_non_empty_string(<<>>) ->
+    ?MANGO_ERROR(invalid_empty_string);
+validate_non_empty_string(auto_name) ->
     {ok, auto_name};
-validate_idx_name(Else) ->
+validate_non_empty_string(Else) ->
     is_string(Else).
 
 validate_selector({Props}) ->
diff --git a/src/mango/test/01-index-crud-test.py 
b/src/mango/test/01-index-crud-test.py
index b60239992..dd70e7eea 100644
--- a/src/mango/test/01-index-crud-test.py
+++ b/src/mango/test/01-index-crud-test.py
@@ -69,7 +69,7 @@ class IndexCrudTests(mango.DbPerClass):
                 raise AssertionError("bad create index")
 
     def test_bad_names(self):
-        bad_names = [True, False, 1.5, {"foo": "bar"}, [None, False]]
+        bad_names = ["", True, False, 1.5, {"foo": "bar"}, [None, False]]
         for bn in bad_names:
             try:
                 self.db.create_index(["foo"], name=bn)
@@ -77,8 +77,12 @@ class IndexCrudTests(mango.DbPerClass):
                 self.assertEqual(e.response.status_code, 400)
             else:
                 raise AssertionError("bad create index")
+
+    def test_bad_ddocs(self):
+        bad_ddocs = ["", True, False, 1.5, {"foo": "bar"}, [None, False]]
+        for bd in bad_ddocs:
             try:
-                self.db.create_index(["foo"], ddoc=bn)
+                self.db.create_index(["foo"], ddoc=bd)
             except Exception as e:
                 self.assertEqual(e.response.status_code, 400)
             else:

Reply via email to