Skip to content

Commit

Permalink
fix: traffic split plugin not validating upstream_id (#10008)
Browse files Browse the repository at this point in the history
  • Loading branch information
shreemaan-abhishek committed Aug 17, 2023
1 parent 18135e5 commit 790d322
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
14 changes: 14 additions & 0 deletions apisix/plugins/traffic-split.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
47 changes: 47 additions & 0 deletions t/plugin/traffic-split2.t
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 790d322

Please sign in to comment.