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

wenming pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/apisix.git


The following commit(s) were added to refs/heads/master by this push:
     new 790d322bd fix: traffic split plugin not validating upstream_id (#10008)
790d322bd is described below

commit 790d322bda6fc97b58527412d59be57512479fa5
Author: Abhishek Choudhary <[email protected]>
AuthorDate: Thu Aug 17 08:23:10 2023 +0545

    fix: traffic split plugin not validating upstream_id (#10008)
---
 apisix/plugins/traffic-split.lua | 14 ++++++++++++
 t/plugin/traffic-split2.t        | 47 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 61 insertions(+)

diff --git a/apisix/plugins/traffic-split.lua b/apisix/plugins/traffic-split.lua
index 18a6be6d0..1d621426a 100644
--- a/apisix/plugins/traffic-split.lua
+++ b/apisix/plugins/traffic-split.lua
@@ -233,6 +233,20 @@ function _M.access(conf, ctx)
     local match_passed = true
 
     for _, rule in ipairs(conf.rules) do
+        -- check if all upstream_ids are valid
+        if rule.weighted_upstreams then
+            for _, wupstream in ipairs(rule.weighted_upstreams) do
+                local ups_id = wupstream.upstream_id
+                if ups_id then
+                    local ups = upstream.get_by_id(ups_id)
+                    if not ups then
+                        return 500, "failed to fetch upstream info by "
+                                    .. "upstream id: " .. ups_id
+                    end
+                end
+            end
+        end
+
         if not rule.match then
             match_passed = true
             weighted_upstreams = rule.weighted_upstreams
diff --git a/t/plugin/traffic-split2.t b/t/plugin/traffic-split2.t
index 05b8047d3..5100337e4 100644
--- a/t/plugin/traffic-split2.t
+++ b/t/plugin/traffic-split2.t
@@ -752,3 +752,50 @@ GET /uri?id=1
 qr/host: 127.0.0.1/
 --- error_log
 proxy request to 127.0.0.1:1980
+
+
+
+=== TEST 20: invalid upstream_id should report failure
+--- config
+    location /t {
+        content_by_lua_block {
+            local json = require("toolkit.json")
+            local t = require("lib.test_admin").test
+
+            local data = {
+                uri = "/route",
+                plugins = {
+                    ["traffic-split"] = {
+                        rules = {
+                            {
+                                weighted_upstreams = {
+                                    {
+                                        upstream_id = "invalid-id",
+                                        weight = 1
+                                    }
+                                }
+                            },
+                        }
+                    }
+                },
+                upstream = {
+                    type = "roundrobin",
+                    nodes = {
+                        ["127.0.0.1:1980"] = 1
+                    }
+                }
+            }
+
+            code, body = t('/apisix/admin/routes/1',
+                ngx.HTTP_PATCH,
+                json.encode(data)
+            )
+            ngx.status, body = t('/route', ngx.HTTP_GET)
+            ngx.say(body)
+        }
+    }
+--- request
+GET /t
+--- error_code: 500
+--- error_log
+failed to find upstream by id: invalid-id

Reply via email to