-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
feat(hmac-auth): Add validate request body for hmac auth plugin #5038
Changes from all commits
2e461f2
1bc110c
8271bbd
70b4813
eddf479
304f3d5
d14aafa
6cbef7e
4623d2f
a4edde5
6863828
9891f46
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -51,6 +51,8 @@ The `consumer` then adds its key to request header to verify its request. | |
| signed_headers | array[string] | optional | | | Restrict the headers that are added to the encrypted calculation. After the specified, the client request can only specify the headers within this range. When this item is empty, all the headers specified by the client request will be added to the encrypted calculation | | ||
| keep_headers | boolean | optional | false | [ true, false ] | Whether it is necessary to keep the request headers of `X-HMAC-SIGNATURE`, `X-HMAC-ALGORITHM` and `X-HMAC-SIGNED-HEADERS` in the http request after successful authentication. true: means to keep the http request header, false: means to remove the http request header. | | ||
| encode_uri_params | boolean | optional | true | [ true, false ] | Whether to encode the uri parameter in the signature, for example: `params1=hello%2Cworld` is encoded, `params2=hello,world` is not encoded. true: means to encode the uri parameter in the signature, false: not to encode the uri parameter in the signature. | | ||
| validate_request_body | boolean | optional | false | [ true, false ] | Whether to check request body. | | ||
| max_req_body | integer | optional | 512 * 1024 | | Max allowed body size. | | ||
|
||
## How To Enable | ||
|
||
|
@@ -192,6 +194,18 @@ print(base64.b64encode(hash.digest())) | |
| --------- | -------------------------------------------- | | ||
| SIGNATURE | 8XV1GB7Tq23OJcoz6wjqTs4ZLxr9DiLoY4PxzScWGYg= | | ||
|
||
### Request body checking | ||
|
||
When `validate_request_body` is assigned to `true`, the plugin will check the request body. The plugin will calculate the hmac-sha value of the request body,and check against the `X-HMAC-DIGEST` header. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Better to mention the max_req_body limitation There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I add a note to that |
||
|
||
``` | ||
X-HMAC-DIGEST: base64(hmac-sha(<body>)) | ||
``` | ||
|
||
When there is no request body, you can set `X-HMAC-DIGEST` value to the hmac-sha of empty string. | ||
|
||
**Note:** The plugin will load the request body to memory to calculate the digest of the request body, which might cause high memory consumption with large bodies. You can limit the max allowed body size by the configuration of `max_req_body`(default=512KB), request body larger than that will be rejected. | ||
|
||
### Use the generated signature to try the request | ||
|
||
```shell | ||
|
@@ -262,12 +276,13 @@ plugin_attr: | |
date_key: X-APISIX-DATE | ||
access_key: X-APISIX-HMAC-ACCESS-KEY | ||
signed_headers_key: X-APISIX-HMAC-SIGNED-HEADERS | ||
body_digest_key: X-APISIX-HMAC-BODY-DIGEST | ||
``` | ||
|
||
**After customizing the header, request example:** | ||
|
||
```shell | ||
$ curl http://127.0.0.1:9080/index.html -H 'X-APISIX-HMAC-SIGNATURE: base64_encode(SIGNATURE)' -H 'X-APISIX-HMAC-ALGORITHM: ALGORITHM' -H 'X-APISIX-DATE: DATE' -H 'X-APISIX-HMAC-ACCESS-KEY: ACCESS_KEY' -H 'X-APISIX-HMAC-SIGNED-HEADERS: SIGNED_HEADERS' -i | ||
$ curl http://127.0.0.1:9080/index.html -H 'X-APISIX-HMAC-SIGNATURE: base64_encode(SIGNATURE)' -H 'X-APISIX-HMAC-ALGORITHM: ALGORITHM' -H 'X-APISIX-DATE: DATE' -H 'X-APISIX-HMAC-ACCESS-KEY: ACCESS_KEY' -H 'X-APISIX-HMAC-SIGNED-HEADERS: SIGNED_HEADERS' -H 'X-APISIX-HMAC-BODY-DIGEST: BODY_DIGEST' -i | ||
HTTP/1.1 200 OK | ||
Content-Type: text/html | ||
Content-Length: 13175 | ||
|
@@ -278,6 +293,29 @@ Accept-Ranges: bytes | |
<html lang="cn"> | ||
``` | ||
|
||
### Enable request body checking | ||
|
||
```shell | ||
$ curl -X "POST" "http://localhost:9080/index.html?age=36&name=james" \ | ||
-H 'X-HMAC-ACCESS-KEY: my-access-key' \ | ||
-H 'X-HMAC-SIGNATURE: lSWO4vcyVoZG5bn8miHudzABAeJQd8tqEHyM7RsjeiU=' \ | ||
-H 'X-HMAC-ALGORITHM: hmac-sha256' \ | ||
-H 'Date: Tue, 24 Aug 2021 03:19:21 GMT' \ | ||
-H 'X-HMAC-SIGNED-HEADERS: User-Agent;X-HMAC-DIGEST' \ | ||
-H 'User-Agent: curl/7.29.0' \ | ||
-H 'X-HMAC-DIGEST: L9b/+QMvhvnoUlSw5vq+kHPqnZiHGl61T8oavMVTaC4=' \ | ||
-H 'Content-Type: text/plain; charset=utf-8' \ | ||
-d "{\"hello\":\"world\"}" | ||
|
||
HTTP/1.1 200 OK | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Better to only show the first line? APISIX 2.2 doesn't support this feature. We can avoid confusing users by only show the first line. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Got it |
||
Content-Type: text/html; charset=utf-8 | ||
Transfer-Encoding: chunked | ||
Connection: keep-alive | ||
Date: Tue, 14 Sep 2021 03:28:14 GMT | ||
Server: APISIX/2.9 | ||
... | ||
``` | ||
|
||
## Disable Plugin | ||
|
||
When you want to disable the `hmac-auth` plugin, it is very simple, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -51,6 +51,8 @@ title: hmac-auth | |
| signed_headers | array[string] | 可选 | | | 限制加入加密计算的 headers ,指定后客户端请求只能在此范围内指定 headers ,此项为空时将把所有客户端请求指定的 headers 加入加密计算。如: ["User-Agent", "Accept-Language", "x-custom-a"] | | ||
| keep_headers | boolean | 可选 | false | [ true, false ] | 认证成功后的 http 请求中是否需要保留 `X-HMAC-SIGNATURE`、`X-HMAC-ALGORITHM` 和 `X-HMAC-SIGNED-HEADERS` 的请求头。true: 表示保留 http 请求头,false: 表示移除 http 请求头。 | | ||
| encode_uri_param | boolean | 可选 | true | [ true, false ] | 是否对签名中的 uri 参数进行编码,例如: `params1=hello%2Cworld` 进行了编码,`params2=hello,world` 没有进行编码。true: 表示对签名中的 uri 参数进行编码,false: 不对签名中的 uri 参数编码。 | | ||
| validate_request_body | boolean | 可选 | false | [ true, false ] | 是否对请求 body 做签名校验。| | ||
| max_req_body | integer | 可选 | 512 * 1024 | | 最大允许的 body 大小。| | ||
|
||
## 如何启用 | ||
|
||
|
@@ -186,6 +188,18 @@ print(base64.b64encode(hash.digest())) | |
| --------- | -------------------------------------------- | | ||
| SIGNATURE | 8XV1GB7Tq23OJcoz6wjqTs4ZLxr9DiLoY4PxzScWGYg= | | ||
|
||
### Body 校验 | ||
|
||
`validate_request_body` 设置为 true 时,插件将计算请求 body 的 `hmac-sha` 值,并与请求 headers 中的 `X-HMAC-DIGEST` 的值进行校验。 | ||
|
||
``` | ||
X-HMAC-DIGEST: base64(hmac-sha(<body>)) | ||
``` | ||
|
||
当没有请求 body 时,插件会计算长度为 0 的空字符串的 hmac-sha 值。 | ||
|
||
**注:**当开启 body 校验时,为了计算请求 body 的 `hmac-sha` 值,插件会把 body 加载到内存中,在请求 body 较大的情况下,可能会造成较高的内存消耗。插件提供了 `max_req_body`(默认值 512KB) 配置项来配置最大允许的 body 大小,body 超过此大小的请求会被拒绝。 | ||
|
||
### 使用生成好的签名进行请求尝试 | ||
|
||
```shell | ||
|
@@ -256,12 +270,13 @@ plugin_attr: | |
date_key: X-APISIX-DATE | ||
access_key: X-APISIX-HMAC-ACCESS-KEY | ||
signed_headers_key: X-APISIX-HMAC-SIGNED-HEADERS | ||
body_digest_key: X-APISIX-HMAC-BODY-DIGEST | ||
``` | ||
|
||
**自定义 header 后,请求示例:** | ||
|
||
```shell | ||
$ curl http://127.0.0.1:9080/index.html -H 'X-APISIX-HMAC-SIGNATURE: base64_encode(SIGNATURE)' -H 'X-APISIX-HMAC-ALGORITHM: ALGORITHM' -H 'X-APISIX-DATE: DATE' -H 'X-APISIX-HMAC-ACCESS-KEY: ACCESS_KEY' -H 'X-APISIX-HMAC-SIGNED-HEADERS: SIGNED_HEADERS' -i | ||
$ curl http://127.0.0.1:9080/index.html -H 'X-APISIX-HMAC-SIGNATURE: base64_encode(SIGNATURE)' -H 'X-APISIX-HMAC-ALGORITHM: ALGORITHM' -H 'X-APISIX-DATE: DATE' -H 'X-APISIX-HMAC-ACCESS-KEY: ACCESS_KEY' -H 'X-APISIX-HMAC-SIGNED-HEADERS: SIGNED_HEADERS' -H 'X-APISIX-HMAC-BODY-DIGEST: BODY_DIGEST' -i | ||
HTTP/1.1 200 OK | ||
Content-Type: text/html | ||
Content-Length: 13175 | ||
|
@@ -272,6 +287,29 @@ Accept-Ranges: bytes | |
<html lang="cn"> | ||
``` | ||
|
||
### 开启 body 校验 | ||
|
||
```shell | ||
$ curl -X "POST" "http://localhost:9080/index.html?age=36&name=james" \ | ||
-H 'X-HMAC-ACCESS-KEY: my-access-key' \ | ||
-H 'X-HMAC-SIGNATURE: lSWO4vcyVoZG5bn8miHudzABAeJQd8tqEHyM7RsjeiU=' \ | ||
-H 'X-HMAC-ALGORITHM: hmac-sha256' \ | ||
-H 'Date: Tue, 24 Aug 2021 03:19:21 GMT' \ | ||
-H 'X-HMAC-SIGNED-HEADERS: User-Agent;X-HMAC-DIGEST' \ | ||
-H 'User-Agent: curl/7.29.0' \ | ||
-H 'X-HMAC-DIGEST: L9b/+QMvhvnoUlSw5vq+kHPqnZiHGl61T8oavMVTaC4=' \ | ||
-H 'Content-Type: text/plain; charset=utf-8' \ | ||
-d "{\"hello\":\"world\"}" | ||
|
||
HTTP/1.1 200 OK | ||
Content-Type: text/html; charset=utf-8 | ||
Transfer-Encoding: chunked | ||
Connection: keep-alive | ||
Date: Tue, 14 Sep 2021 03:28:14 GMT | ||
Server: APISIX/2.9 | ||
... | ||
``` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you help to update the English doc? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I will |
||
|
||
## 禁用插件 | ||
|
||
当你想去掉 `hmac-auth` 插件的时候,很简单,在插件的配置中把对应的 `json` 配置删除即可,无须重启服务,即刻生效: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we could return here if no params.body_digest.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should check whether the params.body_digest exists or not.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
when validate_request_body == true and no params.body_digest, why not return false?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When there is no request body, the
X-HMAC-DIGEST
header can be omitted.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@arthur-zhang
OK. Thanks for your explanation. How about when
validate_request_body
is enabled,X-APISIX-HMAC-BODY-DIGEST
is required? Just pass empty strings for it when no body.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix it