From 75107ab40e7f2a4e74d4881776f6143ac31a8228 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BD=97=E6=B3=BD=E8=BD=A9?= Date: Tue, 14 Jun 2022 09:27:46 +0800 Subject: [PATCH] fix(api-response): check response header format (#7238) --- apisix/plugins/api-breaker.lua | 3 ++- docs/en/latest/plugins/api-breaker.md | 2 +- docs/zh/latest/plugins/api-breaker.md | 2 +- t/plugin/api-breaker.t | 33 +++++++++++++++++++++++++++ 4 files changed, 37 insertions(+), 3 deletions(-) diff --git a/apisix/plugins/api-breaker.lua b/apisix/plugins/api-breaker.lua index 5ccf4404082a..eabca140af11 100644 --- a/apisix/plugins/api-breaker.lua +++ b/apisix/plugins/api-breaker.lua @@ -53,7 +53,8 @@ local schema = { type = "string", minLength = 1 } - } + }, + required = {"key", "value"}, } }, max_breaker_sec = { diff --git a/docs/en/latest/plugins/api-breaker.md b/docs/en/latest/plugins/api-breaker.md index 87c1f1d58cf5..4469b5a31d40 100644 --- a/docs/en/latest/plugins/api-breaker.md +++ b/docs/en/latest/plugins/api-breaker.md @@ -43,7 +43,7 @@ In an unhealthy state, if the Upstream service responds with a status code from |-------------------------|----------------|----------|---------|-----------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | break_response_code | integer | True | | [200, ..., 599] | HTTP error code to return when Upstream is unhealthy. | | break_response_body | string | False | | | Body of the response message to return when Upstream is unhealthy. | -| break_response_headers | array[object] | False | | | Headers of the response message to return when Upstream is unhealthy. Can only be configured when the `break_response_body` attribute is configured. The values can contain Nginx variables. For example, `$remote_addr` and `$balancer_ip`. | +| break_response_headers | array[object] | False | | [{"key":"header_name","value":"can contain Nginx $var"}] | Headers of the response message to return when Upstream is unhealthy. Can only be configured when the `break_response_body` attribute is configured. The values can contain APISIX variables. For example, we can use `{"key":"X-Client-Addr","value":"$remote_addr:$remote_port"}`. | | max_breaker_sec | integer | False | 300 | >=3 | Maximum time in seconds for circuit breaking. | | unhealthy.http_statuses | array[integer] | False | [500] | [500, ..., 599] | Status codes of Upstream to be considered unhealthy. | | unhealthy.failures | integer | False | 3 | >=1 | Number of consecutive failures for the Upstream service to be considered unhealthy. | diff --git a/docs/zh/latest/plugins/api-breaker.md b/docs/zh/latest/plugins/api-breaker.md index 6672f8d4fa44..0e00517b259a 100644 --- a/docs/zh/latest/plugins/api-breaker.md +++ b/docs/zh/latest/plugins/api-breaker.md @@ -45,7 +45,7 @@ title: api-breaker | ----------------------- | -------------- | ------ | ---------- | --------------- | -------------------------------- | | break_response_code | integer | 必须 | 无 | [200, ..., 599] | 不健康返回错误码 | | break_response_body | string | 可选 | 无 | | 不健康返回报文 | -| break_response_headers | array[object] | 可选 | 无 | | 不健康返回报文头,这里可以设置多个。这个值能够以 `$var` 的格式包含 Nginx 变量,比如 `$remote_addr $balancer_ip`。该字段仅在 `break_response_body` 被配置时生效 | +| break_response_headers | array[object] | 可选 | 无 | [{"key":"header_name","value":"can contain Nginx $var"}] | 不健康返回报文头,这里可以设置多个。该字段仅在 `break_response_body` 被配置时生效。这个值能够以 `$var` 的格式包含 APISIX 变量,比如 `{"key":"X-Client-Addr","value":"$remote_addr:$remote_port"}`。 | | max_breaker_sec | integer | 可选 | 300 | >=3 | 最大熔断持续时间 | | unhealthy.http_statuses | array[integer] | 可选 | {500} | [500, ..., 599] | 不健康时候的状态码 | | unhealthy.failures | integer | 可选 | 3 | >=1 | 触发不健康状态的连续错误请求次数 | diff --git a/t/plugin/api-breaker.t b/t/plugin/api-breaker.t index e1eccfb2b6b1..c63d87dba072 100644 --- a/t/plugin/api-breaker.t +++ b/t/plugin/api-breaker.t @@ -655,3 +655,36 @@ phase_func(): breaker_time: 10 --- response_body {"500":4,"502":16} --- timeout: 25 + + + +=== TEST 20: reject invalid schema +--- config + location /t { + content_by_lua_block { + local t = require("lib.test_admin").test + for _, case in ipairs({ + {input = { + break_response_code = 200, + break_response_headers = {{["content-type"] = "application/json"}} + }}, + }) do + local code, body = t('/apisix/admin/global_rules/1', + ngx.HTTP_PUT, + { + id = "1", + plugins = { + ["api-breaker"] = case.input + } + } + ) + ngx.print(require("toolkit.json").decode(body).error_msg) + end + } + } +--- request +GET /t +--- response_body eval +qr/failed to check the configuration of plugin api-breaker err: property \"break_response_headers\" validation failed: failed to validate item 1: property \"(key|value)\" is required/ +--- no_error_log +[error]