You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by sp...@apache.org on 2022/06/14 01:27:56 UTC

[apisix] branch master updated: fix(api-response): check response header format (#7238)

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

spacewander 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 75107ab40 fix(api-response): check response header format (#7238)
75107ab40 is described below

commit 75107ab40e7f2a4e74d4881776f6143ac31a8228
Author: 罗泽轩 <sp...@gmail.com>
AuthorDate: Tue Jun 14 09:27:46 2022 +0800

    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 5ccf44040..eabca140a 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 87c1f1d58..4469b5a31 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 6672f8d4f..0e00517b2 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 e1eccfb2b..c63d87dba 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]