leslie-tsang commented on code in PR #6512: URL: https://github.com/apache/apisix/pull/6512#discussion_r866419517
########## docs/zh/latest/plugins/recaptcha.md: ########## @@ -0,0 +1,122 @@ +--- +title: recaptcha +--- + +<!-- +# + +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +--> + +## 描述 + +通过向 Google reCAPTCHA 服务校验客户端传递的验证码来限制对上游服务的访问。插件支持自定义无效校验码的响应体。 + +> 注意,此插件只支持 Google reCAPTCHA v2 版本。 + +## 属性 + +| Name | Type | Requirement | Default | Valid | Description | +| --------- | ------------- | ----------- |----------------------------------------------------------------------------------------------------------------| ------------------------------------------------------------------------ |-------------------------------------| +| secret_key | string | 必须 | | | Google reCAPTCHA v2 的 secret key | +| parameter_source | string | 可选 | header | | 验证码参数的来源枚举值。当前仅支持 `header`, `query` | +| parameter_name | string | 可选 | captcha | | 验证码参数的名称 | +| response | object | 可选 | content_type = `application/json; charset=utf-8`; status_code = `400`; body = `{"message":"invalid captcha"}` | | 无效验证码的 HTTP 响应体 | +| ssl_verify | boolean | 可选 | true | | 验证 SSL 证书与主机名是否匹配 | Review Comment: > The ssl_verify does not just verify the hostname... Ditto ########## docs/zh/latest/plugins/recaptcha.md: ########## @@ -0,0 +1,122 @@ +--- +title: recaptcha +--- + +<!-- +# + +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +--> + +## 描述 + +通过向 Google reCAPTCHA 服务校验客户端传递的验证码来限制对上游服务的访问。插件支持自定义无效校验码的响应体。 + +> 注意,此插件只支持 Google reCAPTCHA v2 版本。 + +## 属性 + +| Name | Type | Requirement | Default | Valid | Description | +| --------- | ------------- | ----------- |----------------------------------------------------------------------------------------------------------------| ------------------------------------------------------------------------ |-------------------------------------| +| secret_key | string | 必须 | | | Google reCAPTCHA v2 的 secret key | +| parameter_source | string | 可选 | header | | 验证码参数的来源枚举值。当前仅支持 `header`, `query` | +| parameter_name | string | 可选 | captcha | | 验证码参数的名称 | +| response | object | 可选 | content_type = `application/json; charset=utf-8`; status_code = `400`; body = `{"message":"invalid captcha"}` | | 无效验证码的 HTTP 响应体 | +| ssl_verify | boolean | 可选 | true | | 验证 SSL 证书与主机名是否匹配 | + +插件的配置如下: + +```json +{ + "secret_key":"6LeIxAcTAAAAAGGXXXXXXXXXXXXXXXXXXX", + "parameter_source": "header", + "parameter_name": "captcha", + "response":{ + "content_type":"application/json; charset=utf-8", + "body":"{\"message\":\"invalid captcha\"}\n", + "status_code":400 + } +} +``` + +## 如何启用 + +下面是一个示例,在指定的 `route` 上开启了 `recaptcha` 插件: + +```shell +$ curl -i http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d ' +{ + "plugins": { + "recaptcha": { + "secret_key": "6LeIxAcTAAAAAGG-vFI1TnRWxMZNFuojJ4WifJWe", + "parameter_source": "header", + "parameter_name": "captcha", + "response": { + "content_type": "application/json; charset=utf-8", + "status_code": 400, + "body": "{\"message\":\"invalid captcha\"}\n" + } + } + }, + "upstream": { + "nodes": { + "127.0.0.1:1980": 1 + }, + "type": "roundrobin" + }, + "uri": "/login" +}' +``` + +## 测试插件 + +使用 `curl` 访问: + +```shell +$ curl -X POST 'http://127.0.0.1:9080/login' +{"message":"invalid captcha"} + +$ curl -X POST 'http://127.0.0.1:9080/login' -H 'captcha: the_invalid_captcha' +{"message":"invalid captcha"} + +$ curl -X POST 'http://127.0.0.1:9080/login' -H 'captcha: the_valid_captcha' Review Comment: Ditto ########## docs/en/latest/plugins/recaptcha.md: ########## @@ -0,0 +1,126 @@ +--- +title: recaptcha +--- + +<!-- +# + +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +--> + +## Description + +Restrict access to an upstream service by verifying request captcha token to the Google reCAPTCHA service. The Plugin supports customizing the invalid captcha response. + +> Note that the plugin only supports reCAPTCHA v2(verify requests with a challenge). + +## Attributes + +| Name | Type | Requirement | Default | Valid | Description | +| --------- | ------------- | ----------- |----------------------------------------------------------------------------------------------------------------| ------------------------------------------------------------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------- | +| secret_key | string | required | | | The secret key of the Google reCAPTCHA v2 service. | +| parameter_source | string | optional | header | | The enum of captcha parameter source. Only `header`, `query` are supported. | +| parameter_name | string | optional | captcha | | The name of captcha parameter. | +| response | object | optional | content_type = `application/json; charset=utf-8`; status_code = `400`; body = `{"message":"invalid captcha"}` | | The response for invalid recaptcha token. | +| ssl_verify | boolean | optional | true | | verify if SSL cert matches hostname. | + +The example configuration of plugin is + +```json +{ + "secret_key":"6LeIxAcTAAAAAGGXXXXXXXXXXXXXXXXXXX", + "parameter_source": "header", + "parameter_name": "captcha", + "response":{ + "content_type":"application/json; charset=utf-8", + "body":"{\"message\":\"invalid captcha\"}\n", + "status_code":400 + } +} +``` + +## How To Enable + +Here's an example, enable this plugin on the specified route: + +```shell +$ curl -i http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d ' +{ + "plugins": { + "recaptcha": { + "secret_key": "6LeIxAcTAAAAAGG-XXXXXXXXXXXXXX", + "parameter_source": "header", + "parameter_name": "captcha", + "response": { + "content_type": "application/json; charset=utf-8", + "status_code": 400, + "body": "{\"message\":\"invalid captcha\"}\n" + } + } + }, + "upstream": { + "nodes": { + "127.0.0.1:1980": 1 + }, + "type": "roundrobin" + }, + "uri": "/login" +}' +``` + +## Test Plugin + +Use curl to request: + +```shell +$ curl -X POST 'http://127.0.0.1:9080/login' +{"message":"invalid captcha"} + +$ curl -X POST 'http://127.0.0.1:9080/login' -H 'captcha: the_invalid_captcha' +{"message":"invalid captcha"} + +$ curl -X POST 'http://127.0.0.1:9080/login' -H 'captcha: the_valid_captcha' +<html> +<head><title>503 Service Temporarily Unavailable</title></head> +<body> +<center><h1>503 Service Temporarily Unavailable</h1></center> +<hr><center>openresty</center> +</body> +</html> +(Request has been proxied to the upstream service. Here 503 code is because we didn't set up an HTTP service on 127.0.0.1:1980) Review Comment: ```suggestion $ curl -I -X POST 'http://127.0.0.1:9080/login' -H 'captcha: the_valid_captcha' HTTP/1.1 200 OK ... ``` Would be better to add a normal upstream response case ? ########## apisix/plugins/recaptcha.lua: ########## @@ -0,0 +1,103 @@ +-- +-- Licensed to the Apache Software Foundation (ASF) under one or more +-- contributor license agreements. See the NOTICE file distributed with +-- this work for additional information regarding copyright ownership. +-- The ASF licenses this file to You under the Apache License, Version 2.0 +-- (the "License"); you may not use this file except in compliance with +-- the License. You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +-- See the License for the specific language governing permissions and +-- limitations under the License. +-- +local core = require("apisix.core") +local http = require("resty.http") +local ngx = ngx + +local schema = { + type = "object", + properties = { + secret_key = { type = "string" }, + parameter_source = { type = "string", default = "header", enum = { "header", "query" } }, + parameter_name = { type = "string", default = "captcha" }, + ssl_verify = { type = "boolean", default = true }, + response = { + type = "object", + properties = { + content_type = { type = "string", default = "application/json; charset=utf-8" }, + status_code = { type = "number", default = 400 }, + body = { type = "string", default = '{"message": "invalid captcha"}' } + } + }, + }, + required = { "secret_key" }, +} + +local recaptcha_url = "https://www.recaptcha.net" + +local _M = { + version = 0.1, + priority = 700, + name = "recaptcha", + schema = schema, +} + +function _M.check_schema(conf) + return core.schema.check(schema, conf) +end + + +local function retrieve_captcha(ctx, conf) + if conf.parameter_source == "header" then + return core.request.header(ctx, conf.parameter_name) + end + + if conf.parameter_source == "query" then + local uri_args = core.request.get_uri_args(ctx) or {} + return uri_args[conf.parameter_name] + end +end + + +function _M.access(conf, ctx) + local invalid_captcha = true + local captcha = retrieve_captcha(ctx, conf) + if captcha ~= nil and captcha ~= "" then + local httpc = http.new() + local secret = conf.secret_key + local remote_ip = core.request.get_remote_client_ip(ctx) + local res, err = httpc:request_uri(recaptcha_url .. "/recaptcha/api/siteverify", { + method = "POST", + body = ngx.encode_args({ secret = secret, response = captcha, remoteip = remote_ip }), + headers = { + ["Content-Type"] = "application/x-www-form-urlencoded", + }, + ssl_verify = conf.ssl_verify + }) + if not res then + core.log.error("request failed: ", err) + return 503 + end + core.log.debug("recaptcha verify result: ", res.body) + local recaptcha_result, err = core.json.decode(res.body) + if err then + core.log.error("failed to decode the recaptcha response json: ", err) + end + if recaptcha_result and recaptcha_result.success == true then + invalid_captcha = false + end + end + + if invalid_captcha then + core.response.set_header("Content-Type", conf.response.content_type) + return conf.response.status_code, core.utils.resolve_var(conf.response.body, ctx.var) Review Comment: It seems that the author wants to put some dynamic var in the response body, would it be better to use static ones? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
