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

jcoglan pushed a commit to branch mango-match-failures
in repository https://gitbox.apache.org/repos/asf/couchdb.git

commit 5404017833c4568d6a6d1b07f95ad3b69cf37f8c
Author: James Coglan <[email protected]>
AuthorDate: Mon Jul 20 15:01:11 2026 +0100

    fix: Reject Mango VDUs if they contain invalid field names or invalid 
operators
---
 src/mango/src/mango_native_proc.erl           |  8 ++++--
 test/elixir/test/config/suite.elixir          |  2 ++
 test/elixir/test/validate_doc_update_test.exs | 36 +++++++++++++++++++++++++++
 3 files changed, 44 insertions(+), 2 deletions(-)

diff --git a/src/mango/src/mango_native_proc.erl 
b/src/mango/src/mango_native_proc.erl
index 68ddd88c1..a4d06cf8c 100644
--- a/src/mango/src/mango_native_proc.erl
+++ b/src/mango/src/mango_native_proc.erl
@@ -108,8 +108,12 @@ handle_call({prompt, [<<"validate_fun">>, Selector0 | 
_Rest]}, _From, St) ->
                 Error -> {reply, {error, Error}, St}
             end
     catch
-        throw:{mango_error, mango_selector, Error} ->
-            {reply, {error, Error}, St}
+        throw:{mango_error, mango_selector, {invalid_operator, Op}} ->
+            Msg = io_lib:format("invalid operator: ~p", [Op]),
+            {reply, {error, {compilation_error, Msg}}, St};
+        throw:{mango_error, mango_util, {invalid_field_name, Field}} ->
+            Msg = io_lib:format("invalid field name: ~p", [Field]),
+            {reply, {error, {compilation_error, Msg}}, St}
     end;
 handle_call({prompt, [<<"ddoc">>, <<"new">>, DDocId, {DDoc}]}, _From, St) ->
     NewSt =
diff --git a/test/elixir/test/config/suite.elixir 
b/test/elixir/test/config/suite.elixir
index 1ea321414..7d0c0c56e 100644
--- a/test/elixir/test/config/suite.elixir
+++ b/test/elixir/test/config/suite.elixir
@@ -536,6 +536,8 @@
     "Mango VDU rejects a doc if any existing ddoc fails to match",
     "invalid Mango VDU is detected on doc update",
     "Mango VDU rejects a design doc if it contains unknown fields",
+    "Mango VDU rejects a design doc if it contains an invalid field name",
+    "Mango VDU rejects a design doc if it contains an invalid operator",
   ],
   "SecurityValidationTest": [
     "Author presence and user security",
diff --git a/test/elixir/test/validate_doc_update_test.exs 
b/test/elixir/test/validate_doc_update_test.exs
index 502630404..3723508a1 100644
--- a/test/elixir/test/validate_doc_update_test.exs
+++ b/test/elixir/test/validate_doc_update_test.exs
@@ -282,4 +282,40 @@ defmodule ValidateDocUpdateTest do
     assert resp.status_code == 400
     assert resp.body["error"] == "compilation_error"
   end
+
+  @tag :with_db
+  test "Mango VDU rejects a design doc if it contains an invalid field name", 
context do
+    set_config({"couchdb", "validate_vdu", "true"})
+    db = context[:db_name]
+
+    ddoc = %{
+      language: "query",
+
+      validate_doc_update: %{
+        "newDoc." => %{"$type" => "string"}
+      }
+    }
+
+    resp = Couch.put("/#{db}/_design/mango-test-2", body: ddoc)
+    assert resp.status_code == 400
+    assert resp.body["error"] == "compilation_error"
+  end
+
+  @tag :with_db
+  test "Mango VDU rejects a design doc if it contains an invalid operator", 
context do
+    set_config({"couchdb", "validate_vdu", "true"})
+    db = context[:db_name]
+
+    ddoc = %{
+      language: "query",
+
+      validate_doc_update: %{
+        "newDoc.a" => %{"$nope" => 1}
+      }
+    }
+
+    resp = Couch.put("/#{db}/_design/mango-test-2", body: ddoc)
+    assert resp.status_code == 400
+    assert resp.body["error"] == "compilation_error"
+  end
 end

Reply via email to