Skip to content

Commit

Permalink
feat: add rejected_message support for plugin request-validation (#5122)
Browse files Browse the repository at this point in the history
  • Loading branch information
leslie-tsang authored Sep 28, 2021
1 parent c3635d4 commit 76540a5
Show file tree
Hide file tree
Showing 4 changed files with 338 additions and 74 deletions.
33 changes: 13 additions & 20 deletions apisix/plugins/request-validation.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,20 @@
local core = require("apisix.core")
local plugin_name = "request-validation"
local ngx = ngx
local io = io
local io = io
local req_read_body = ngx.req.read_body
local req_get_body_data = ngx.req.get_body_data

local schema = {
type = "object",
properties = {
header_schema = {type = "object"},
body_schema = {type = "object"},
rejected_msg = {type = "string", minLength = 1, maxLength = 256}
},
anyOf = {
{
title = "Body schema",
properties = {
body_schema = {type = "object"}
},
required = {"body_schema"}
},
{
title = "Header schema",
properties = {
header_schema = {type = "object"}
},
required = {"header_schema"}
}
{required = {"header_schema"}},
{required = {"body_schema"}}
}
}

Expand Down Expand Up @@ -82,7 +75,7 @@ function _M.rewrite(conf)
local ok, err = core.schema.check(conf.header_schema, headers)
if not ok then
core.log.error("req schema validation failed", err)
return 400, err
return 400, conf.rejected_msg or err
end
end

Expand All @@ -94,11 +87,11 @@ function _M.rewrite(conf)
if not body then
local filename = ngx.req.get_body_file()
if not filename then
return 500
return 500, conf.rejected_msg
end
local fd = io.open(filename, 'rb')
if not fd then
return 500
return 500, conf.rejected_msg
end
body = fd:read('*a')
end
Expand All @@ -111,13 +104,13 @@ function _M.rewrite(conf)

if not req_body then
core.log.error('failed to decode the req body', error)
return 400, error
return 400, conf.rejected_msg or error
end

local ok, err = core.schema.check(conf.body_schema, req_body)
if not ok then
core.log.error("req schema validation failed", err)
return 400, err
return 400, conf.rejected_msg or err
end
end
end
Expand Down
43 changes: 37 additions & 6 deletions docs/en/latest/plugins/request-validation.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,13 @@ For more information on schema, refer to [JSON schema](https://github.com/api7/j

## Attributes

| Name | Type | Requirement | Default | Valid | Description |
| ------------- | ------ | ----------- | ------- | ----- | -------------------------- |
| header_schema | object | optional | | | schema for the header data |
| body_schema | object | optional | | | schema for the body data |
> Note that at least one of `header_schema` and `body_schema` must be filled in.
| Name | Type | Requirement | Default | Valid | Description |
| ---------------- | ------ | ----------- | ------- | ----- | -------------------------- |
| header_schema | object | optional | | | schema for the header data |
| body_schema | object | optional | | | schema for the body data |
| rejected_message | string | optional | | | the custom rejected message |

## How To Enable

Expand All @@ -60,7 +63,8 @@ curl http://127.0.0.1:9080/apisix/admin/routes/5 -H 'X-API-KEY: edd1c9f034335f13
"properties": {
"required_payload": {"type": "string"},
"boolean_payload": {"type": "boolean"}
}
},
"rejected_message": "customize reject message"
}
}
},
Expand All @@ -82,7 +86,7 @@ curl --header "Content-Type: application/json" \
http://127.0.0.1:9080/get
```

If the schema is violated the plugin will yield a `400` bad request.
If the schema is violated the plugin will yield a `400` bad request with the reject response.

## Disable Plugin

Expand Down Expand Up @@ -252,3 +256,30 @@ curl http://127.0.0.1:9080/apisix/admin/routes/5 -H 'X-API-KEY: edd1c9f034335f13
}
}
```

**Custom rejected message:**

```json
{
"uri": "/get",
"plugins": {
"request-validation": {
"body_schema": {
"type": "object",
"required": ["required_payload"],
"properties": {
"required_payload": {"type": "string"},
"boolean_payload": {"type": "boolean"}
},
"rejected_message": "customize reject message"
}
}
},
"upstream": {
"type": "roundrobin",
"nodes": {
"127.0.0.1:8080": 1
}
}
}
```
43 changes: 37 additions & 6 deletions docs/zh/latest/plugins/request-validation.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,13 @@ title: request-validation

## 属性

| Name | Type | Requirement | Default | Valid | Description |
| ------------- | ------ | ----------- | ------- | ----- | --------------------------------- |
| header_schema | object | 可选 | | | `header` 数据的 `schema` 数据结构 |
| body_schema | object | 可选 | | | `body` 数据的 `schema` 数据结构 |
> 注意, `header_schema``body_schema` 至少填写其中一个
| Name | Type | Requirement | Default | Valid | Description |
| ---------------- | ------ | ----------- | ------- | ----- | --------------------------------- |
| header_schema | object | 可选 | | | `header` 数据的 `schema` 数据结构 |
| body_schema | object | 可选 | | | `body` 数据的 `schema` 数据结构 |
| rejected_message | string | 可选 | | | 自定义拒绝信息 |

## 如何启用

Expand All @@ -59,7 +62,8 @@ curl http://127.0.0.1:9080/apisix/admin/routes/5 -H 'X-API-KEY: edd1c9f034335f13
"properties": {
"required_payload": {"type": "string"},
"boolean_payload": {"type": "boolean"}
}
},
"rejected_message": "customize reject message"
}
}
},
Expand All @@ -81,7 +85,7 @@ curl --header "Content-Type: application/json" \
http://127.0.0.1:9080/get
```

如果 `Schema` 验证失败,将返回 `400 bad request` 错误
如果 `Schema` 验证失败,将返回 `400` 状态码与相应的拒绝信息

## 禁用插件

Expand Down Expand Up @@ -250,3 +254,30 @@ curl http://127.0.0.1:9080/apisix/admin/routes/5 -H 'X-API-KEY: edd1c9f034335f13
}
}
```

**自定义拒绝信息:**

```json
{
"uri": "/get",
"plugins": {
"request-validation": {
"body_schema": {
"type": "object",
"required": ["required_payload"],
"properties": {
"required_payload": {"type": "string"},
"boolean_payload": {"type": "boolean"}
},
"rejected_message": "customize reject message"
}
}
},
"upstream": {
"type": "roundrobin",
"nodes": {
"127.0.0.1:8080": 1
}
}
}
```
Loading

0 comments on commit 76540a5

Please sign in to comment.