fix(proxy-mirror): pattern validation (#6529)

This commit is contained in:
罗泽轩 2022-03-08 11:37:02 +08:00 committed by GitHub
parent cb2ba6819f
commit 61a590843a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 51 additions and 58 deletions

View File

@ -25,8 +25,7 @@ local schema = {
properties = {
host = {
type = "string",
pattern = [[^http(s)?:\/\/[a-zA-Z0-9][-a-zA-Z0-9]{0,62}]]
.. [[(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+(:[0-9]{1,5})?$]],
pattern = [=[^http(s)?:\/\/([\da-zA-Z.-]+|\[[\da-fA-F:]+\])(:\d+)?$]=],
},
path = {
type = "string",

View File

@ -56,6 +56,14 @@ add_block_preprocessor(sub {
_EOC_
$block->set_value("http_config", $http_config);
if (!$block->request) {
$block->set_value("request", "GET /t");
}
if ((!defined $block->error_log) && (!defined $block->no_error_log)) {
$block->set_value("no_error_log", "[error]");
}
});
run_tests;
@ -91,13 +99,9 @@ __DATA__
ngx.say(body)
}
}
--- request
GET /t
--- error_code: 400
--- response_body eval
qr/failed to check the configuration of plugin proxy-mirror/
--- no_error_log
[error]
@ -130,13 +134,9 @@ qr/failed to check the configuration of plugin proxy-mirror/
ngx.say(body)
}
}
--- request
GET /t
--- error_code: 400
--- response_body eval
qr/failed to check the configuration of plugin proxy-mirror/
--- no_error_log
[error]
@ -169,13 +169,9 @@ qr/failed to check the configuration of plugin proxy-mirror/
ngx.say(body)
}
}
--- request
GET /t
--- error_code: 400
--- response_body eval
qr/failed to check the configuration of plugin proxy-mirror/
--- no_error_log
[error]
@ -208,13 +204,9 @@ qr/failed to check the configuration of plugin proxy-mirror/
ngx.say(body)
}
}
--- request
GET /t
--- error_code: 200
--- response_body
passed
--- no_error_log
[error]
@ -247,13 +239,9 @@ passed
ngx.say(body)
}
}
--- request
GET /t
--- error_code: 400
--- response_body eval
qr/failed to check the configuration of plugin proxy-mirror/
--- no_error_log
[error]
@ -286,13 +274,9 @@ qr/failed to check the configuration of plugin proxy-mirror/
ngx.say(body)
}
}
--- request
GET /t
--- error_code: 200
--- response_body
passed
--- no_error_log
[error]
@ -336,13 +320,9 @@ uri: /hello
ngx.say(body)
}
}
--- request
GET /t
--- error_code: 200
--- response_body
passed
--- no_error_log
[error]
@ -399,13 +379,9 @@ name: jake
ngx.say(body)
}
}
--- request
GET /t
--- error_code: 200
--- response_body
passed
--- no_error_log
[error]
@ -438,13 +414,9 @@ host: 127.0.0.2
ngx.say(body)
}
}
--- request
GET /t
--- error_code: 200
--- response_body
passed
--- no_error_log
[error]
@ -478,13 +450,9 @@ passed
ngx.print(body)
}
}
--- request
GET /t
--- error_code: 400
--- response_body
{"error_msg":"failed to check the configuration of plugin proxy-mirror err: property \"sample_ratio\" validation failed: expected 10 to be at most 1"}
--- no_error_log
[error]
@ -518,13 +486,9 @@ GET /t
ngx.say(body)
}
}
--- request
GET /t
--- error_code: 200
--- response_body
passed
--- no_error_log
[error]
@ -569,8 +533,6 @@ qr/uri: \/hello\?sample_ratio=1/
ngx.say(body)
}
}
--- request
GET /t
--- error_code: 200
--- response_body
passed
@ -596,10 +558,6 @@ passed
end
}
}
--- request
GET /t
--- no_error_log
[error]
--- error_log_like eval
qr/(uri: \/hello\?sample_ratio=0\.5){75,125}/
@ -635,8 +593,6 @@ qr/(uri: \/hello\?sample_ratio=0\.5){75,125}/
ngx.say(body)
}
}
--- request
GET /t
--- response_body
passed
@ -693,12 +649,50 @@ uri: /a?a=1
end
}
}
--- request
GET /t
--- grep_error_log eval
qr/property \\"path\\" validation failed: failed to match pattern/
--- grep_error_log_out
property \"path\" validation failed: failed to match pattern
property \"path\" validation failed: failed to match pattern
--- no_error_log
[error]
=== TEST 22: sanity check (host)
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
for _, p in ipairs({
"http://a",
"http://ab.com",
"http://[::1]",
"http://[::1]:202",
}) do
local code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PUT,
[[{
"plugins": {
"proxy-mirror": {
"host": "]] .. p .. [["
}
},
"upstream": {
"nodes": {
"127.0.0.1:1980": 1
},
"type": "roundrobin"
},
"uri": "/hello"
}]]
)
ngx.log(ngx.WARN, body)
end
}
}
--- grep_error_log eval
qr/(passed|property \\"host\\" validation failed: failed to match pattern)/
--- grep_error_log_out
passed
passed
passed
passed