From e22a8d891a900ee25f08d2f111ec8bd69ff3c4bc Mon Sep 17 00:00:00 2001 From: Abhishek Choudhary Date: Mon, 13 Nov 2023 14:54:08 +0545 Subject: [PATCH 1/6] fix regex pattern --- apisix/core/utils.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apisix/core/utils.lua b/apisix/core/utils.lua index 70531e7c1fa9..f33c54b9d394 100644 --- a/apisix/core/utils.lua +++ b/apisix/core/utils.lua @@ -292,7 +292,7 @@ local resolve_var do local _ctx local n_resolved - local pat = [[(? Date: Mon, 13 Nov 2023 15:02:58 +0545 Subject: [PATCH 2/6] add test --- t/plugin/fault-injection2.t | 44 +++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/t/plugin/fault-injection2.t b/t/plugin/fault-injection2.t index 0ba23f536069..b77848ddaf96 100644 --- a/t/plugin/fault-injection2.t +++ b/t/plugin/fault-injection2.t @@ -140,3 +140,47 @@ GET /hello h1: v1 h2: 2 h3: /hello + + + +=== TEST 4: closing curly brace not should not be a part of variable +--- config + location /t { + content_by_lua_block { + local t = require("lib.test_admin").test + local code, body = t('/apisix/admin/routes/1', + ngx.HTTP_PUT, + [=[{ + "plugins": { + "fault-injection": { + "abort": { + "http_status": 200, + "body": "{\"count\": $arg_count}" + } + } + }, + "upstream": { + "nodes": { + "127.0.0.1:1980": 1 + }, + "type": "roundrobin" + }, + "uri": "/hello" + }]=] + ) + if code >= 300 then + ngx.status = code + end + ngx.say(body) + } + } +--- response_body +passed + + + +=== TEST 5: inject header +--- request +GET /hello?count=2 +--- response_body chomp +{"count": 2} From 77f7d1d5951450c96ba3cac8519c2ecdc4d701fd Mon Sep 17 00:00:00 2001 From: Abhishek Choudhary Date: Tue, 14 Nov 2023 07:27:50 +0545 Subject: [PATCH 3/6] reindex --- t/plugin/fault-injection2.t | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/t/plugin/fault-injection2.t b/t/plugin/fault-injection2.t index b77848ddaf96..bd28f6bfa5d2 100644 --- a/t/plugin/fault-injection2.t +++ b/t/plugin/fault-injection2.t @@ -143,7 +143,7 @@ h3: /hello -=== TEST 4: closing curly brace not should not be a part of variable +=== TEST 6: closing curly brace not should not be a part of variable --- config location /t { content_by_lua_block { @@ -179,7 +179,7 @@ passed -=== TEST 5: inject header +=== TEST 7: test route --- request GET /hello?count=2 --- response_body chomp From c276f5597eaf5b2101d7d8a3255dc086b1bde1dc Mon Sep 17 00:00:00 2001 From: Abhishek Choudhary Date: Tue, 14 Nov 2023 07:28:11 +0545 Subject: [PATCH 4/6] avoid evaluating capture with braces --- apisix/core/utils.lua | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/apisix/core/utils.lua b/apisix/core/utils.lua index f33c54b9d394..6c0abc2ad269 100644 --- a/apisix/core/utils.lua +++ b/apisix/core/utils.lua @@ -296,7 +296,11 @@ do local _escaper local function resolve(m) - local v = _ctx[m[1]] + local i = 1 + if m[1]:byte(1) == 123 then + i = 2 + end + local v = _ctx[m[i]] if v == nil then return "" end From f30fe75ce317d4d2e7279ec1910d9ee88caee624 Mon Sep 17 00:00:00 2001 From: Abhishek Choudhary Date: Tue, 14 Nov 2023 08:59:45 +0545 Subject: [PATCH 5/6] add comment --- apisix/core/utils.lua | 1 + 1 file changed, 1 insertion(+) diff --git a/apisix/core/utils.lua b/apisix/core/utils.lua index 6c0abc2ad269..a759135c6ccd 100644 --- a/apisix/core/utils.lua +++ b/apisix/core/utils.lua @@ -297,6 +297,7 @@ do local function resolve(m) local i = 1 + -- if first capture's first char is "{" evaluate the second capture if m[1]:byte(1) == 123 then i = 2 end From 533a626a7c4ec65ec62cfc7216a066d990783742 Mon Sep 17 00:00:00 2001 From: Abhishek Choudhary Date: Tue, 21 Nov 2023 19:38:55 +0545 Subject: [PATCH 6/6] optimize --- apisix/core/utils.lua | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/apisix/core/utils.lua b/apisix/core/utils.lua index a759135c6ccd..adca3d856d8c 100644 --- a/apisix/core/utils.lua +++ b/apisix/core/utils.lua @@ -296,12 +296,9 @@ do local _escaper local function resolve(m) - local i = 1 - -- if first capture's first char is "{" evaluate the second capture - if m[1]:byte(1) == 123 then - i = 2 - end - local v = _ctx[m[i]] + local variable = m[2] or m[3] + local v = _ctx[variable] + if v == nil then return "" end