Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 9A568200C14 for ; Tue, 24 Jan 2017 00:13:51 +0100 (CET) Received: by cust-asf.ponee.io (Postfix) id 98F59160B53; Mon, 23 Jan 2017 23:13:51 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id E3F18160B49 for ; Tue, 24 Jan 2017 00:13:50 +0100 (CET) Received: (qmail 35027 invoked by uid 500); 23 Jan 2017 23:13:50 -0000 Mailing-List: contact commits-help@couchdb.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@couchdb.apache.org Delivered-To: mailing list commits@couchdb.apache.org Received: (qmail 35018 invoked by uid 99); 23 Jan 2017 23:13:49 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 23 Jan 2017 23:13:49 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id D8E6BDFB0E; Mon, 23 Jan 2017 23:13:49 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: tonysun83@apache.org To: commits@couchdb.apache.org Message-Id: X-Mailer: ASF-Git Admin Mailer Subject: couchdb-mango git commit: Do not allow empty field name Date: Mon, 23 Jan 2017 23:13:49 +0000 (UTC) archived-at: Mon, 23 Jan 2017 23:13:51 -0000 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 Authored: Tue Oct 18 14:01:48 2016 -0700 Committer: Tony Sun 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: