couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tonysu...@apache.org
Subject couchdb-mango git commit: Do not allow empty field name
Date Mon, 23 Jan 2017 23:13:49 GMT
Repository: couchdb-mango
Updated Branches:
  refs/heads/master 4afd60e84 -> 6660b37d6


Do not allow empty field name

Currently, the indexer crashes when a field name is empty. Even though
it's valid json, we should disallow empty field names to coincide
with selector syntax that requires a non-empty field name for queries.

COUCHDB-3202


Project: http://git-wip-us.apache.org/repos/asf/couchdb-mango/repo
Commit: http://git-wip-us.apache.org/repos/asf/couchdb-mango/commit/6660b37d
Tree: http://git-wip-us.apache.org/repos/asf/couchdb-mango/tree/6660b37d
Diff: http://git-wip-us.apache.org/repos/asf/couchdb-mango/diff/6660b37d

Branch: refs/heads/master
Commit: 6660b37d6813823804df3444f532abf6736936f7
Parents: 4afd60e
Author: Tony Sun <tony.sun@cloudant.com>
Authored: Tue Oct 18 14:01:48 2016 -0700
Committer: Tony Sun <tony.sun@cloudant.com>
Committed: Mon Jan 23 15:17:01 2017 -0800

----------------------------------------------------------------------
 src/mango_error.erl        |  2 +-
 src/mango_idx_text.erl     | 14 ++++++++++++++
 src/mango_sort.erl         |  2 ++
 test/01-index-crud-test.py |  6 ++++--
 4 files changed, 21 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb-mango/blob/6660b37d/src/mango_error.erl
----------------------------------------------------------------------
diff --git a/src/mango_error.erl b/src/mango_error.erl
index 76a8362..7d77b5e 100644
--- a/src/mango_error.erl
+++ b/src/mango_error.erl
@@ -152,7 +152,7 @@ info(mango_idx_text, {invalid_index_fields_definition, Def}) ->
         400,
         <<"invalid_index_fields_definition">>,
         fmt("Text Index field definitions must be of the form
-            {\"name\": \"fieldname\", \"type\":
+            {\"name\": \"non-empty fieldname\", \"type\":
                 \"boolean,number, or string\"}. Def: ~p", [Def])
     };
 info(mango_idx_view, {index_not_found, BadIdx}) ->

http://git-wip-us.apache.org/repos/asf/couchdb-mango/blob/6660b37d/src/mango_idx_text.erl
----------------------------------------------------------------------
diff --git a/src/mango_idx_text.erl b/src/mango_idx_text.erl
index 4cfda5a..ad9d2e8 100644
--- a/src/mango_idx_text.erl
+++ b/src/mango_idx_text.erl
@@ -159,13 +159,25 @@ def_to_json([{Key, Value} | Rest]) ->
 fields_to_json([]) ->
     [];
 fields_to_json([{[{<<"name">>, Name}, {<<"type">>, Type0}]} | Rest])
->
+    ok = validate_field_name(Name),
     Type = validate_field_type(Type0),
     [{[{Name, Type}]} | fields_to_json(Rest)];
 fields_to_json([{[{<<"type">>, Type0}, {<<"name">>, Name}]} | Rest])
->
+    ok = validate_field_name(Name),
     Type = validate_field_type(Type0),
     [{[{Name, Type}]} | fields_to_json(Rest)].
 
 
+%% In the future, we can possibly add more restrictive validation.
+%% For now, let's make sure the field name is not blank.
+validate_field_name(<<"">>) ->
+    throw(invalid_field_name);
+validate_field_name(Else) when is_binary(Else)->
+    ok;
+validate_field_name(_) ->
+    throw(invalid_field_name).
+
+
 validate_field_type(<<"string">>) ->
     <<"string">>;
 validate_field_type(<<"number">>) ->
@@ -181,6 +193,8 @@ validate_fields(Fields) ->
         _ ->
             mango_fields:new(Fields)
     catch error:function_clause ->
+        ?MANGO_ERROR({invalid_index_fields_definition, Fields});
+    throw:invalid_field_name ->
         ?MANGO_ERROR({invalid_index_fields_definition, Fields})
     end.
 

http://git-wip-us.apache.org/repos/asf/couchdb-mango/blob/6660b37d/src/mango_sort.erl
----------------------------------------------------------------------
diff --git a/src/mango_sort.erl b/src/mango_sort.erl
index 796ed33..17249c2 100644
--- a/src/mango_sort.erl
+++ b/src/mango_sort.erl
@@ -47,6 +47,8 @@ directions({Props}) ->
     [Dir || {_Name, Dir} <- Props].
 
 
+sort_field(<<"">>) ->
+    ?MANGO_ERROR({invalid_sort_field, <<"">>});
 sort_field(Field) when is_binary(Field) ->
     {Field, <<"asc">>};
 sort_field({[{Name, <<"asc">>}]}) when is_binary(Name) ->

http://git-wip-us.apache.org/repos/asf/couchdb-mango/blob/6660b37d/test/01-index-crud-test.py
----------------------------------------------------------------------
diff --git a/test/01-index-crud-test.py b/test/01-index-crud-test.py
index a44e149..342c94f 100644
--- a/test/01-index-crud-test.py
+++ b/test/01-index-crud-test.py
@@ -26,7 +26,8 @@ class IndexCrudTests(mango.DbPerClass):
             {"foo": "bar"},
             [{"foo": 2}],
             [{"foo": "asc", "bar": "desc"}],
-            [{"foo": "asc"}, {"bar": "desc"}]
+            [{"foo": "asc"}, {"bar": "desc"}],
+            [""]
         ]
         for fields in bad_fields:
             try:
@@ -254,7 +255,8 @@ class IndexCrudTests(mango.DbPerClass):
             [{"name": "foo3", "type": "garbage"}],
             [{"type": "number"}],
             [{"name": "age", "type": "number"} , {"name": "bad"}],
-            [{"name": "age", "type": "number"} , "bla"]
+            [{"name": "age", "type": "number"} , "bla"],
+            [{"name": "", "type": "number"} , "bla"]
         ]
         for fields in bad_fields:
             try:


Mime
View raw message