Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: traffic split plugin not validating upstream_id #10008

Merged
merged 8 commits into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading