From a3f0337c8e4731b22bfeec86ee609a2cbb920452 Mon Sep 17 00:00:00 2001 From: Karthikeya1500 Date: Wed, 25 Mar 2026 10:59:34 +0530 Subject: [PATCH 1/3] Fix ReferenceError in QuickJS dispatch If an unrecognized command is processed by globalThis.dispatch, the error handler attempts to reference cmdkey without it being defined. This throws a ReferenceError instead of generating the intended CouchDB fatal error array. This commit assigns cmd.shift() to a const variable cmdkey before evaluating the switch statement. --- share/server/dispatch-quickjs.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/share/server/dispatch-quickjs.js b/share/server/dispatch-quickjs.js index 5f69f6188ac..2f05118b313 100644 --- a/share/server/dispatch-quickjs.js +++ b/share/server/dispatch-quickjs.js @@ -154,7 +154,8 @@ globalThis.dispatch = function(line) { const cmd = JSON.parse(line); State.line_length = line.length; try { - switch (cmd.shift()) { + const cmdkey = cmd.shift(); + switch (cmdkey) { case "ddoc": DDoc.ddoc.apply(null, cmd); break; From 539b1a3a101e7b68078c7a0da15925c8c802e16b Mon Sep 17 00:00:00 2001 From: Karthikeya1500 Date: Thu, 26 Mar 2026 01:40:14 +0530 Subject: [PATCH 2/3] Add eunit test coverage for invalid quickjs commands --- src/couch/test/eunit/couch_js_tests.erl | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/couch/test/eunit/couch_js_tests.erl b/src/couch/test/eunit/couch_js_tests.erl index 29b303ffb5f..c5ad53eaaaf 100644 --- a/src/couch/test/eunit/couch_js_tests.erl +++ b/src/couch/test/eunit/couch_js_tests.erl @@ -38,7 +38,8 @@ couch_js_test_() -> ?TDEF(should_allow_js_string_mutations), ?TDEF(should_bump_timing_and_call_stats), ?TDEF(should_exit_on_internal_error, 60), - ?TDEF(should_use_bigint) + ?TDEF(should_use_bigint), + ?TDEF(should_handle_invalid_command) ]) } }. @@ -465,6 +466,11 @@ should_use_bigint(_) -> ?assertThrow({compilation_error, _}, prompt(Proc, [<<"add_fun">>, Src])) end. +should_handle_invalid_command(_) -> + Proc = couch_query_servers:get_os_process(<<"javascript">>), + BadCmd = [<<"bad">>, <<"foo">>], + ?assertThrow({unknown_command, <<"unknown command 'bad'">>}, prompt(Proc, BadCmd)). + sample_time(Stat) -> couch_stats:sample([couchdb, query_server, time, Stat]). From 78059acd03b2ff98c1c60a8bc031bcf9d5729b59 Mon Sep 17 00:00:00 2001 From: Karthikeya1500 Date: Thu, 26 Mar 2026 15:00:48 +0530 Subject: [PATCH 3/3] QuickJS: fix error reporting in query server Use the errstr(e) utility in handleError to ensure meaningful error messages are returned to the client for both named and unnamed errors. - Replace e.stack with errstr(e) for unnamed errors - Replace raw Error object with errstr(e) for named errors --- share/server/dispatch-quickjs.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/share/server/dispatch-quickjs.js b/share/server/dispatch-quickjs.js index 2f05118b313..a1210dcbcd9 100644 --- a/share/server/dispatch-quickjs.js +++ b/share/server/dispatch-quickjs.js @@ -142,10 +142,10 @@ function handleError(e) { respond(["error", e.error, e.reason]); return true; } else if (e.name) { - respond(["error", e.name, e]); + respond(["error", e.name, errstr(e)]); return true; } else { - respond(["error","unnamed_error", e.stack]); + respond(["error","unnamed_error", errstr(e)]); return true; } };