From 18dea0bf57b8c3032dc6e6584251be1e36634d86 Mon Sep 17 00:00:00 2001 From: wanglihui Date: Wed, 4 Jun 2025 19:46:58 +0800 Subject: [PATCH 1/6] fix: plugin not add args, original args will lost bug#12286 --- apisix/plugins/ext-plugin/init.lua | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/apisix/plugins/ext-plugin/init.lua b/apisix/plugins/ext-plugin/init.lua index 2631afd36fb3..f433d391f107 100644 --- a/apisix/plugins/ext-plugin/init.lua +++ b/apisix/plugins/ext-plugin/init.lua @@ -705,11 +705,11 @@ local rpc_handlers = { end end - core.request.set_uri_args(ctx, args) + end + core.request.set_uri_args(ctx, args) - if path then - var.upstream_uri = path .. '?' .. var.args - end + if path then + var.upstream_uri = path .. '?' .. var.args end end From 59c5897bc5be3d38dd90d208245f92d4a5faa23f Mon Sep 17 00:00:00 2001 From: wanglihui Date: Thu, 31 Jul 2025 17:55:07 +0800 Subject: [PATCH 2/6] test: add tests for URI args processing in ext-plugin with various scenarios --- t/plugin/ext-plugin/uri-args-fix.t | 239 +++++++++++++++++ t/plugin/ext-plugin/uri-args-scope-fix.t | 327 +++++++++++++++++++++++ 2 files changed, 566 insertions(+) create mode 100644 t/plugin/ext-plugin/uri-args-fix.t create mode 100644 t/plugin/ext-plugin/uri-args-scope-fix.t diff --git a/t/plugin/ext-plugin/uri-args-fix.t b/t/plugin/ext-plugin/uri-args-fix.t new file mode 100644 index 000000000000..57c99cdc438b --- /dev/null +++ b/t/plugin/ext-plugin/uri-args-fix.t @@ -0,0 +1,239 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +use t::APISIX 'no_plan'; + +repeat_each(1); +no_long_string(); +no_root_location(); +no_shuffle(); +log_level("info"); + +add_block_preprocessor(sub { + my ($block) = @_; + + $block->set_value("stream_conf_enable", 1); + + if (!defined $block->extra_stream_config) { + my $stream_config = <<_EOC_; + server { + listen unix:\$TEST_NGINX_HTML_DIR/nginx.sock; + + content_by_lua_block { + local ext = require("lib.ext-plugin") + ext.go({}) + } + } + +_EOC_ + $block->set_value("extra_stream_config", $stream_config); + } + + my $unix_socket_path = $ENV{"TEST_NGINX_HTML_DIR"} . "/nginx.sock"; + my $cmd = $block->ext_plugin_cmd // "['sleep', '5s']"; + my $extra_yaml_config = <<_EOC_; +ext-plugin: + path_for_test: $unix_socket_path + cmd: $cmd +_EOC_ + + $block->set_value("extra_yaml_config", $extra_yaml_config); + + if (!$block->request) { + $block->set_value("request", "GET /t"); + } + + if (!$block->error_log) { + $block->set_value("no_error_log", "[error]\n[alert]"); + } +}); + +run_tests; + +__DATA__ + +=== TEST 1: add route with ext-plugin-pre-req +--- config + location /t { + content_by_lua_block { + local json = require("toolkit.json") + local t = require("lib.test_admin") + + local code, message, res = t.test('/apisix/admin/routes/1', + ngx.HTTP_PUT, + [[{ + "uri": "/hello", + "plugins": { + "ext-plugin-pre-req": { + } + }, + "upstream": { + "nodes": { + "127.0.0.1:1980": 1 + }, + "type": "roundrobin" + } + }]] + ) + + if code >= 300 then + ngx.status = code + ngx.say(message) + return + end + + ngx.say(message) + } + } +--- response_body +passed +--- no_error_log +[error] + +=== TEST 2: test URI args processing with rewrite - path and args +--- config + location /t { + content_by_lua_block { + local ext = require("lib.ext-plugin") + ext.go({rewrite = true}) + } + } +--- request +GET /hello?original=param&test=value +--- response_body +passed +--- no_error_log +[error] + +=== TEST 3: test URI args processing with rewrite - args only +--- config + location /t { + content_by_lua_block { + local ext = require("lib.ext-plugin") + ext.go({rewrite_args_only = true}) + } + } +--- request +GET /hello?original=param&test=value +--- response_body +passed +--- no_error_log +[error] + +=== TEST 4: test URI args processing with complex args modification +--- config + location /t { + content_by_lua_block { + local ext = require("lib.ext-plugin") + ext.go({rewrite_args = true}) + } + } +--- request +GET /hello?a=1&b=2&c=3 +--- response_body +passed +--- no_error_log +[error] + +=== TEST 5: test URI args processing with path rewrite and args +--- config + location /t { + content_by_lua_block { + local ext = require("lib.ext-plugin") + ext.go({rewrite = true}) + } + } +--- request +GET /hello?x=1&y=2&z=3 +--- response_body +passed +--- no_error_log +[error] + +=== TEST 6: test URI args processing with empty args +--- config + location /t { + content_by_lua_block { + local ext = require("lib.ext-plugin") + ext.go({rewrite_args_only = true}) + } + } +--- request +GET /hello +--- response_body +passed +--- no_error_log +[error] + +=== TEST 7: test URI args processing with multiple same name args +--- config + location /t { + content_by_lua_block { + local ext = require("lib.ext-plugin") + ext.go({rewrite_args = true}) + } + } +--- request +GET /hello?a=1&a=2&b=3 +--- response_body +passed +--- no_error_log +[error] + +=== TEST 8: test URI args processing with args containing special characters +--- config + location /t { + content_by_lua_block { + local ext = require("lib.ext-plugin") + ext.go({rewrite_args_only = true}) + } + } +--- request +GET /hello?param=value%20with%20spaces&encoded=%26%3D%3F +--- response_body +passed +--- no_error_log +[error] + +=== TEST 9: test URI args processing with path rewrite only +--- config + location /t { + content_by_lua_block { + local ext = require("lib.ext-plugin") + ext.go({rewrite = true}) + } + } +--- request +GET /hello?keep=this¶m=value +--- response_body +passed +--- no_error_log +[error] + +=== TEST 10: test URI args processing with args deletion +--- config + location /t { + content_by_lua_block { + local ext = require("lib.ext-plugin") + ext.go({rewrite_args = true}) + } + } +--- request +GET /hello?delete=me&keep=this&remove=too +--- response_body +passed +--- no_error_log +[error] diff --git a/t/plugin/ext-plugin/uri-args-scope-fix.t b/t/plugin/ext-plugin/uri-args-scope-fix.t new file mode 100644 index 000000000000..c0ff59d988e6 --- /dev/null +++ b/t/plugin/ext-plugin/uri-args-scope-fix.t @@ -0,0 +1,327 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +use t::APISIX; + +my $nginx_binary = $ENV{'TEST_NGINX_BINARY'} || 'nginx'; +my $version = eval { `$nginx_binary -V 2>&1` }; + +if ($version !~ m/\/apisix-nginx-module/) { + plan(skip_all => "apisix-nginx-module not installed"); +} else { + plan('no_plan'); +} + +repeat_each(1); +no_long_string(); +no_root_location(); +no_shuffle(); +log_level("info"); + +$ENV{"PATH"} = $ENV{PATH} . ":" . $ENV{TEST_NGINX_HTML_DIR}; + +add_block_preprocessor(sub { + my ($block) = @_; + + $block->set_value("stream_conf_enable", 1); + + if (!defined $block->extra_stream_config) { + my $stream_config = <<_EOC_; + server { + listen unix:\$TEST_NGINX_HTML_DIR/nginx.sock; + + content_by_lua_block { + local ext = require("lib.ext-plugin") + ext.go({}) + } + } + +_EOC_ + $block->set_value("extra_stream_config", $stream_config); + } + + my $unix_socket_path = $ENV{"TEST_NGINX_HTML_DIR"} . "/nginx.sock"; + my $orig_extra_yaml_config = $block->extra_yaml_config // ""; + my $cmd = $block->ext_plugin_cmd // "['sleep', '5s']"; + my $extra_yaml_config = <<_EOC_; +ext-plugin: + path_for_test: $unix_socket_path + cmd: $cmd +_EOC_ + $extra_yaml_config = $extra_yaml_config . $orig_extra_yaml_config; + + $block->set_value("extra_yaml_config", $extra_yaml_config); + + if (!$block->request) { + $block->set_value("request", "GET /t"); + } + + if (!$block->error_log) { + $block->set_value("no_error_log", "[error]\n[alert]"); + } +}); + +run_tests; + +__DATA__ + +=== TEST 1: add route with ext-plugin-pre-req +--- config + location /t { + content_by_lua_block { + local json = require("toolkit.json") + local t = require("lib.test_admin") + + local code, message, res = t.test('/apisix/admin/routes/1', + ngx.HTTP_PUT, + [[{ + "uri": "/hello", + "plugins": { + "ext-plugin-pre-req": { + } + }, + "upstream": { + "nodes": { + "127.0.0.1:1980": 1 + }, + "type": "roundrobin" + } + }]] + ) + + if code >= 300 then + ngx.status = code + ngx.say(message) + return + end + + ngx.say(message) + } + } +--- response_body +passed +--- no_error_log +[error] + +=== TEST 2: test URI args processing with path rewrite - verify args are always set +--- config + location /t { + content_by_lua_block { + local ext = require("lib.ext-plugin") + ext.go({rewrite = true}) + } + } +--- request +GET /hello?original=param&test=value +--- response_body +passed +--- no_error_log +[error] + +=== TEST 3: test URI args processing with args rewrite only - verify args are always set +--- config + location /t { + content_by_lua_block { + local ext = require("lib.ext-plugin") + ext.go({rewrite_args_only = true}) + } + } +--- request +GET /hello?original=param&test=value +--- response_body +passed +--- no_error_log +[error] + +=== TEST 4: test URI args processing with complex args modification - verify scope fix +--- config + location /t { + content_by_lua_block { + local ext = require("lib.ext-plugin") + ext.go({rewrite_args = true}) + } + } +--- request +GET /hello?a=1&b=2&c=3 +--- response_body +passed +--- no_error_log +[error] + +=== TEST 5: test URI args processing with path rewrite and args - verify upstream_uri is set +--- config + location /t { + content_by_lua_block { + local ext = require("lib.ext-plugin") + ext.go({rewrite = true}) + } + } +--- request +GET /hello?x=1&y=2&z=3 +--- response_body +passed +--- no_error_log +[error] + +=== TEST 6: test URI args processing with empty args - verify no errors +--- config + location /t { + content_by_lua_block { + local ext = require("lib.ext-plugin") + ext.go({rewrite_args_only = true}) + } + } +--- request +GET /hello +--- response_body +passed +--- no_error_log +[error] + +=== TEST 7: test URI args processing with multiple same name args - verify array handling +--- config + location /t { + content_by_lua_block { + local ext = require("lib.ext-plugin") + ext.go({rewrite_args = true}) + } + } +--- request +GET /hello?a=1&a=2&b=3 +--- response_body +passed +--- no_error_log +[error] + +=== TEST 8: test URI args processing with args deletion - verify nil handling +--- config + location /t { + content_by_lua_block { + local ext = require("lib.ext-plugin") + ext.go({rewrite_args = true}) + } + } +--- request +GET /hello?delete=me&keep=this&remove=too +--- response_body +passed +--- no_error_log +[error] + +=== TEST 9: test URI args processing with path rewrite only - verify upstream_uri construction +--- config + location /t { + content_by_lua_block { + local ext = require("lib.ext-plugin") + ext.go({rewrite = true}) + } + } +--- request +GET /hello?keep=this¶m=value +--- response_body +passed +--- no_error_log +[error] + +=== TEST 10: test URI args processing with args containing special characters - verify encoding +--- config + location /t { + content_by_lua_block { + local ext = require("lib.ext-plugin") + ext.go({rewrite_args_only = true}) + } + } +--- request +GET /hello?param=value%20with%20spaces&encoded=%26%3D%3F +--- response_body +passed +--- no_error_log +[error] + +=== TEST 11: test URI args processing with no rewrite - verify default behavior +--- config + location /t { + content_by_lua_block { + local ext = require("lib.ext-plugin") + ext.go({}) + } + } +--- request +GET /hello?original=param&test=value +--- response_body +passed +--- no_error_log +[error] + +=== TEST 12: test URI args processing with bad path rewrite - verify error handling +--- config + location /t { + content_by_lua_block { + local ext = require("lib.ext-plugin") + ext.go({rewrite_bad_path = true}) + } + } +--- request +GET /hello?param=value +--- response_body +passed +--- no_error_log +[error] + +=== TEST 13: test URI args processing with conditional args - verify scope fix +--- config + location /t { + content_by_lua_block { + local ext = require("lib.ext-plugin") + ext.go({rewrite_args = true}) + } + } +--- request +GET /hello?conditional=test&always=present +--- response_body +passed +--- no_error_log +[error] + +=== TEST 14: test URI args processing with mixed operations - verify all args are processed +--- config + location /t { + content_by_lua_block { + local ext = require("lib.ext-plugin") + ext.go({rewrite = true}) + } + } +--- request +GET /hello?add=new&modify=old&delete=remove&keep=preserve +--- response_body +passed +--- no_error_log +[error] + +=== TEST 15: test URI args processing with edge case - single arg +--- config + location /t { + content_by_lua_block { + local ext = require("lib.ext-plugin") + ext.go({rewrite_args_only = true}) + } + } +--- request +GET /hello?single=value +--- response_body +passed +--- no_error_log +[error] From cf5f6890b57a6d5ad9028f60cb9a3a4fdf11c0c6 Mon Sep 17 00:00:00 2001 From: wanglihui Date: Thu, 31 Jul 2025 18:18:14 +0800 Subject: [PATCH 3/6] test: add unit tests for ext-plugin URI args scope fix --- t/plugin/ext-plugin/uri-args-fix.t | 18 +++++++++++++++ t/plugin/ext-plugin/uri-args-scope-fix.t | 28 ++++++++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/t/plugin/ext-plugin/uri-args-fix.t b/t/plugin/ext-plugin/uri-args-fix.t index 57c99cdc438b..f16b12275544 100644 --- a/t/plugin/ext-plugin/uri-args-fix.t +++ b/t/plugin/ext-plugin/uri-args-fix.t @@ -103,6 +103,8 @@ passed --- no_error_log [error] + + === TEST 2: test URI args processing with rewrite - path and args --- config location /t { @@ -118,6 +120,8 @@ passed --- no_error_log [error] + + === TEST 3: test URI args processing with rewrite - args only --- config location /t { @@ -133,6 +137,8 @@ passed --- no_error_log [error] + + === TEST 4: test URI args processing with complex args modification --- config location /t { @@ -148,6 +154,8 @@ passed --- no_error_log [error] + + === TEST 5: test URI args processing with path rewrite and args --- config location /t { @@ -163,6 +171,8 @@ passed --- no_error_log [error] + + === TEST 6: test URI args processing with empty args --- config location /t { @@ -178,6 +188,8 @@ passed --- no_error_log [error] + + === TEST 7: test URI args processing with multiple same name args --- config location /t { @@ -193,6 +205,8 @@ passed --- no_error_log [error] + + === TEST 8: test URI args processing with args containing special characters --- config location /t { @@ -208,6 +222,8 @@ passed --- no_error_log [error] + + === TEST 9: test URI args processing with path rewrite only --- config location /t { @@ -223,6 +239,8 @@ passed --- no_error_log [error] + + === TEST 10: test URI args processing with args deletion --- config location /t { diff --git a/t/plugin/ext-plugin/uri-args-scope-fix.t b/t/plugin/ext-plugin/uri-args-scope-fix.t index c0ff59d988e6..722c71347752 100644 --- a/t/plugin/ext-plugin/uri-args-scope-fix.t +++ b/t/plugin/ext-plugin/uri-args-scope-fix.t @@ -116,6 +116,8 @@ passed --- no_error_log [error] + + === TEST 2: test URI args processing with path rewrite - verify args are always set --- config location /t { @@ -131,6 +133,8 @@ passed --- no_error_log [error] + + === TEST 3: test URI args processing with args rewrite only - verify args are always set --- config location /t { @@ -146,6 +150,8 @@ passed --- no_error_log [error] + + === TEST 4: test URI args processing with complex args modification - verify scope fix --- config location /t { @@ -161,6 +167,8 @@ passed --- no_error_log [error] + + === TEST 5: test URI args processing with path rewrite and args - verify upstream_uri is set --- config location /t { @@ -176,6 +184,8 @@ passed --- no_error_log [error] + + === TEST 6: test URI args processing with empty args - verify no errors --- config location /t { @@ -191,6 +201,8 @@ passed --- no_error_log [error] + + === TEST 7: test URI args processing with multiple same name args - verify array handling --- config location /t { @@ -206,6 +218,8 @@ passed --- no_error_log [error] + + === TEST 8: test URI args processing with args deletion - verify nil handling --- config location /t { @@ -221,6 +235,8 @@ passed --- no_error_log [error] + + === TEST 9: test URI args processing with path rewrite only - verify upstream_uri construction --- config location /t { @@ -236,6 +252,8 @@ passed --- no_error_log [error] + + === TEST 10: test URI args processing with args containing special characters - verify encoding --- config location /t { @@ -251,6 +269,8 @@ passed --- no_error_log [error] + + === TEST 11: test URI args processing with no rewrite - verify default behavior --- config location /t { @@ -266,6 +286,8 @@ passed --- no_error_log [error] + + === TEST 12: test URI args processing with bad path rewrite - verify error handling --- config location /t { @@ -281,6 +303,8 @@ passed --- no_error_log [error] + + === TEST 13: test URI args processing with conditional args - verify scope fix --- config location /t { @@ -296,6 +320,8 @@ passed --- no_error_log [error] + + === TEST 14: test URI args processing with mixed operations - verify all args are processed --- config location /t { @@ -311,6 +337,8 @@ passed --- no_error_log [error] + + === TEST 15: test URI args processing with edge case - single arg --- config location /t { From f438cec0d6409b598a7ef22ddd7963e318f2298f Mon Sep 17 00:00:00 2001 From: wanglihui Date: Wed, 6 Aug 2025 19:53:28 +0800 Subject: [PATCH 4/6] test: fix ext-plugin URI args scope test - use correct test pattern --- t/plugin/ext-plugin/uri-args-scope-fix.t | 231 ++++++++--------------- 1 file changed, 82 insertions(+), 149 deletions(-) diff --git a/t/plugin/ext-plugin/uri-args-scope-fix.t b/t/plugin/ext-plugin/uri-args-scope-fix.t index 722c71347752..9344550ed9ee 100644 --- a/t/plugin/ext-plugin/uri-args-scope-fix.t +++ b/t/plugin/ext-plugin/uri-args-scope-fix.t @@ -118,238 +118,171 @@ passed -=== TEST 2: test URI args processing with path rewrite - verify args are always set ---- config - location /t { - content_by_lua_block { - local ext = require("lib.ext-plugin") - ext.go({rewrite = true}) - } - } +=== TEST 2: test URI args processing with path rewrite --- request GET /hello?original=param&test=value ---- response_body -passed ---- no_error_log -[error] - - +--- extra_stream_config + server { + listen unix:$TEST_NGINX_HTML_DIR/nginx.sock; -=== TEST 3: test URI args processing with args rewrite only - verify args are always set ---- config - location /t { content_by_lua_block { local ext = require("lib.ext-plugin") - ext.go({rewrite_args_only = true}) + ext.go({rewrite = true}) } } ---- request -GET /hello?original=param&test=value --- response_body -passed ---- no_error_log -[error] +uri: /uri +host: 127.0.0.1 +x-real-ip: 127.0.0.1 -=== TEST 4: test URI args processing with complex args modification - verify scope fix ---- config - location /t { - content_by_lua_block { - local ext = require("lib.ext-plugin") - ext.go({rewrite_args = true}) - } - } +=== TEST 3: test URI args processing with args rewrite --- request -GET /hello?a=1&b=2&c=3 ---- response_body -passed ---- no_error_log -[error] - - +GET /hello?original=param&test=value +--- extra_stream_config + server { + listen unix:$TEST_NGINX_HTML_DIR/nginx.sock; -=== TEST 5: test URI args processing with path rewrite and args - verify upstream_uri is set ---- config - location /t { content_by_lua_block { local ext = require("lib.ext-plugin") - ext.go({rewrite = true}) + ext.go({rewrite_args_only = true}) } } ---- request -GET /hello?x=1&y=2&z=3 --- response_body -passed ---- no_error_log -[error] +uri: /hello +a: foo,bar +c: bar -=== TEST 6: test URI args processing with empty args - verify no errors ---- config - location /t { - content_by_lua_block { - local ext = require("lib.ext-plugin") - ext.go({rewrite_args_only = true}) - } - } +=== TEST 4: test URI args processing with complex args modification --- request -GET /hello ---- response_body -passed ---- no_error_log -[error] - - +GET /hello?a=1&b=2&c=3 +--- extra_stream_config + server { + listen unix:$TEST_NGINX_HTML_DIR/nginx.sock; -=== TEST 7: test URI args processing with multiple same name args - verify array handling ---- config - location /t { content_by_lua_block { local ext = require("lib.ext-plugin") ext.go({rewrite_args = true}) } } ---- request -GET /hello?a=1&a=2&b=3 --- response_body -passed ---- no_error_log -[error] +uri: /plugin_proxy_rewrite_args +a: foo,bar +c: bar -=== TEST 8: test URI args processing with args deletion - verify nil handling ---- config - location /t { - content_by_lua_block { - local ext = require("lib.ext-plugin") - ext.go({rewrite_args = true}) - } - } +=== TEST 5: test URI args processing with path rewrite and args --- request -GET /hello?delete=me&keep=this&remove=too ---- response_body -passed ---- no_error_log -[error] - - +GET /hello?x=1&y=2&z=3 +--- extra_stream_config + server { + listen unix:$TEST_NGINX_HTML_DIR/nginx.sock; -=== TEST 9: test URI args processing with path rewrite only - verify upstream_uri construction ---- config - location /t { content_by_lua_block { local ext = require("lib.ext-plugin") ext.go({rewrite = true}) } } ---- request -GET /hello?keep=this¶m=value --- response_body -passed ---- no_error_log -[error] +uri: /uri +host: 127.0.0.1 +x-real-ip: 127.0.0.1 -=== TEST 10: test URI args processing with args containing special characters - verify encoding ---- config - location /t { +=== TEST 6: test URI args processing with empty args +--- request +GET /hello +--- extra_stream_config + server { + listen unix:$TEST_NGINX_HTML_DIR/nginx.sock; + content_by_lua_block { local ext = require("lib.ext-plugin") ext.go({rewrite_args_only = true}) } } ---- request -GET /hello?param=value%20with%20spaces&encoded=%26%3D%3F --- response_body -passed ---- no_error_log -[error] +uri: /hello +a: foo,bar +c: bar -=== TEST 11: test URI args processing with no rewrite - verify default behavior ---- config - location /t { - content_by_lua_block { - local ext = require("lib.ext-plugin") - ext.go({}) - } - } +=== TEST 7: test URI args processing with multiple same name args --- request -GET /hello?original=param&test=value ---- response_body -passed ---- no_error_log -[error] - - +GET /hello?a=1&a=2&b=3 +--- extra_stream_config + server { + listen unix:$TEST_NGINX_HTML_DIR/nginx.sock; -=== TEST 12: test URI args processing with bad path rewrite - verify error handling ---- config - location /t { content_by_lua_block { local ext = require("lib.ext-plugin") - ext.go({rewrite_bad_path = true}) + ext.go({rewrite_args = true}) } } ---- request -GET /hello?param=value --- response_body -passed ---- no_error_log -[error] +uri: /plugin_proxy_rewrite_args +a: foo,bar +c: bar -=== TEST 13: test URI args processing with conditional args - verify scope fix ---- config - location /t { +=== TEST 8: test URI args processing with args deletion +--- request +GET /hello?delete=me&keep=this&remove=too +--- extra_stream_config + server { + listen unix:$TEST_NGINX_HTML_DIR/nginx.sock; + content_by_lua_block { local ext = require("lib.ext-plugin") ext.go({rewrite_args = true}) } } ---- request -GET /hello?conditional=test&always=present --- response_body -passed ---- no_error_log -[error] +uri: /plugin_proxy_rewrite_args +a: foo,bar +c: bar -=== TEST 14: test URI args processing with mixed operations - verify all args are processed ---- config - location /t { +=== TEST 9: test URI args processing with path rewrite only +--- request +GET /hello?keep=this¶m=value +--- extra_stream_config + server { + listen unix:$TEST_NGINX_HTML_DIR/nginx.sock; + content_by_lua_block { local ext = require("lib.ext-plugin") ext.go({rewrite = true}) } } ---- request -GET /hello?add=new&modify=old&delete=remove&keep=preserve --- response_body -passed ---- no_error_log -[error] +uri: /uri +host: 127.0.0.1 +x-real-ip: 127.0.0.1 -=== TEST 15: test URI args processing with edge case - single arg ---- config - location /t { +=== TEST 10: test URI args processing with args containing special characters +--- request +GET /hello?param=value%20with%20spaces&encoded=%26%3D%3F +--- extra_stream_config + server { + listen unix:$TEST_NGINX_HTML_DIR/nginx.sock; + content_by_lua_block { local ext = require("lib.ext-plugin") ext.go({rewrite_args_only = true}) } } ---- request -GET /hello?single=value --- response_body -passed ---- no_error_log -[error] +uri: /hello +a: foo,bar +c: bar From df8f6b7fa7f43e1182db077e96cd96b9769f65e9 Mon Sep 17 00:00:00 2001 From: wanglihui Date: Wed, 6 Aug 2025 19:56:42 +0800 Subject: [PATCH 5/6] test: fix uri-args-fix.t - use correct test pattern --- t/plugin/ext-plugin/uri-args-fix.t | 156 ++++++++++++++++------------- 1 file changed, 87 insertions(+), 69 deletions(-) diff --git a/t/plugin/ext-plugin/uri-args-fix.t b/t/plugin/ext-plugin/uri-args-fix.t index f16b12275544..3093361ec1ec 100644 --- a/t/plugin/ext-plugin/uri-args-fix.t +++ b/t/plugin/ext-plugin/uri-args-fix.t @@ -105,153 +105,171 @@ passed -=== TEST 2: test URI args processing with rewrite - path and args ---- config - location /t { +=== TEST 2: test URI args processing with path rewrite +--- request +GET /hello?original=param&test=value +--- extra_stream_config + server { + listen unix:$TEST_NGINX_HTML_DIR/nginx.sock; + content_by_lua_block { local ext = require("lib.ext-plugin") ext.go({rewrite = true}) } } ---- request -GET /hello?original=param&test=value --- response_body -passed ---- no_error_log -[error] +uri: /uri +host: 127.0.0.1 +x-real-ip: 127.0.0.1 -=== TEST 3: test URI args processing with rewrite - args only ---- config - location /t { +=== TEST 3: test URI args processing with args rewrite +--- request +GET /hello?original=param&test=value +--- extra_stream_config + server { + listen unix:$TEST_NGINX_HTML_DIR/nginx.sock; + content_by_lua_block { local ext = require("lib.ext-plugin") ext.go({rewrite_args_only = true}) } } ---- request -GET /hello?original=param&test=value --- response_body -passed ---- no_error_log -[error] +uri: /hello +a: foo,bar +c: bar === TEST 4: test URI args processing with complex args modification ---- config - location /t { +--- request +GET /hello?a=1&b=2&c=3 +--- extra_stream_config + server { + listen unix:$TEST_NGINX_HTML_DIR/nginx.sock; + content_by_lua_block { local ext = require("lib.ext-plugin") ext.go({rewrite_args = true}) } } ---- request -GET /hello?a=1&b=2&c=3 --- response_body -passed ---- no_error_log -[error] +uri: /plugin_proxy_rewrite_args +a: foo,bar +c: bar === TEST 5: test URI args processing with path rewrite and args ---- config - location /t { +--- request +GET /hello?x=1&y=2&z=3 +--- extra_stream_config + server { + listen unix:$TEST_NGINX_HTML_DIR/nginx.sock; + content_by_lua_block { local ext = require("lib.ext-plugin") ext.go({rewrite = true}) } } ---- request -GET /hello?x=1&y=2&z=3 --- response_body -passed ---- no_error_log -[error] +uri: /uri +host: 127.0.0.1 +x-real-ip: 127.0.0.1 === TEST 6: test URI args processing with empty args ---- config - location /t { +--- request +GET /hello +--- extra_stream_config + server { + listen unix:$TEST_NGINX_HTML_DIR/nginx.sock; + content_by_lua_block { local ext = require("lib.ext-plugin") ext.go({rewrite_args_only = true}) } } ---- request -GET /hello --- response_body -passed ---- no_error_log -[error] +uri: /hello +a: foo,bar +c: bar === TEST 7: test URI args processing with multiple same name args ---- config - location /t { +--- request +GET /hello?a=1&a=2&b=3 +--- extra_stream_config + server { + listen unix:$TEST_NGINX_HTML_DIR/nginx.sock; + content_by_lua_block { local ext = require("lib.ext-plugin") ext.go({rewrite_args = true}) } } ---- request -GET /hello?a=1&a=2&b=3 --- response_body -passed ---- no_error_log -[error] +uri: /plugin_proxy_rewrite_args +a: foo,bar +c: bar -=== TEST 8: test URI args processing with args containing special characters ---- config - location /t { +=== TEST 8: test URI args processing with args deletion +--- request +GET /hello?delete=me&keep=this&remove=too +--- extra_stream_config + server { + listen unix:$TEST_NGINX_HTML_DIR/nginx.sock; + content_by_lua_block { local ext = require("lib.ext-plugin") - ext.go({rewrite_args_only = true}) + ext.go({rewrite_args = true}) } } ---- request -GET /hello?param=value%20with%20spaces&encoded=%26%3D%3F --- response_body -passed ---- no_error_log -[error] +uri: /plugin_proxy_rewrite_args +a: foo,bar +c: bar === TEST 9: test URI args processing with path rewrite only ---- config - location /t { +--- request +GET /hello?keep=this¶m=value +--- extra_stream_config + server { + listen unix:$TEST_NGINX_HTML_DIR/nginx.sock; + content_by_lua_block { local ext = require("lib.ext-plugin") ext.go({rewrite = true}) } } ---- request -GET /hello?keep=this¶m=value --- response_body -passed ---- no_error_log -[error] +uri: /uri +host: 127.0.0.1 +x-real-ip: 127.0.0.1 -=== TEST 10: test URI args processing with args deletion ---- config - location /t { +=== TEST 10: test URI args processing with args containing special characters +--- request +GET /hello?param=value%20with%20spaces&encoded=%26%3D%3F +--- extra_stream_config + server { + listen unix:$TEST_NGINX_HTML_DIR/nginx.sock; + content_by_lua_block { local ext = require("lib.ext-plugin") - ext.go({rewrite_args = true}) + ext.go({rewrite_args_only = true}) } } ---- request -GET /hello?delete=me&keep=this&remove=too --- response_body -passed ---- no_error_log -[error] +uri: /hello +a: foo,bar +c: bar From 63636d736f683e2564d098636c491a809ede132e Mon Sep 17 00:00:00 2001 From: wanglihui Date: Tue, 12 Aug 2025 12:11:30 +0800 Subject: [PATCH 6/6] fix: correct upstream_uri construction and ensure URI args are properly set in ext-plugin --- apisix/plugins/ext-plugin/init.lua | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/apisix/plugins/ext-plugin/init.lua b/apisix/plugins/ext-plugin/init.lua index f433d391f107..80d6385c1613 100644 --- a/apisix/plugins/ext-plugin/init.lua +++ b/apisix/plugins/ext-plugin/init.lua @@ -640,7 +640,6 @@ local rpc_handlers = { local path = rewrite:Path() if path then path = core.utils.uri_safe_encode(path) - var.upstream_uri = path end local len = rewrite:HeadersLength() @@ -682,6 +681,7 @@ local rpc_handlers = { local len = rewrite:ArgsLength() if len > 0 then + local args = core.request.get_uri_args(ctx) local changed = {} for i = 1, len do local entry = rewrite:Args(i) @@ -689,7 +689,6 @@ local rpc_handlers = { local value = entry:Value() if value == nil then args[name] = nil - else if changed[name] then if type(args[name]) == "table" then @@ -700,16 +699,15 @@ local rpc_handlers = { else args[name] = entry:Value() end - changed[name] = true end end + core.request.set_uri_args(ctx, args) end - core.request.set_uri_args(ctx, args) if path then - var.upstream_uri = path .. '?' .. var.args + var.upstream_uri = path .. (var.is_args or '') .. (var.args or '') end end