[GitHub] [apisix] vm-001 commented on a change in pull request #6512: feat: support google reCAPTCHA
vm-001 commented on a change in pull request #6512: URL: https://github.com/apache/apisix/pull/6512#discussion_r831944763 ## File path: t/plugin/recaptcha.t ## @@ -0,0 +1,241 @@ +# +# 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. +# +use t::APISIX 'no_plan'; + +log_level('info'); +repeat_each(1); +no_long_string(); +no_root_location(); + +add_block_preprocessor(sub { +my ($block) = @_; + +if (! $block->request) { +$block->set_value("request", "GET /t"); +} + +if (! $block->no_error_log && ! $block->error_log) { +$block->set_value("no_error_log", "[error]\n[alert]"); +} +}); + +run_tests; + + +__DATA__ + +=== TEST 1: sanity +--- config +location /t { +content_by_lua_block { +local plugin = require("apisix.plugins.recaptcha") +local ok, err = plugin.check_schema({ +# https://developers.google.com/recaptcha/docs/faq#id-like-to-run-automated-tests-with-recaptcha +secret_key = "6LeIxAcTAGG-vFI1TnRWxMZNFuojJ4WifJWe", # Google automated-tests secret key +parameter_source = "header", +parameter_name = "captcha", +response = { +content_type = "application/json; charset=utf-8", +status_code = 400, +body = "{\"message\":\"invalid captcha\"}\n" +}, +ssl_verify = false, +}) +if not ok then +ngx.say(err) +end + +ngx.say("done") +} +} +--- response_body +done + + + +== TEST 2: invalid secret_key +--- config +location /t { +content_by_lua_block { +local plugin = require("apisix.plugins.recaptcha") +local ok, err = plugin.check_schema({ +secret_key = nil, +}) +if not ok then +ngx.say(err) +end + +ngx.say("done") +} +} +--- response_body +property "secret_key" is required +done + + + +=== TEST 2: add plugin +--- config +location /t { +content_by_lua_block { +local t = require("lib.test_admin").test +local code, body = t('/apisix/admin/routes/1', +ngx.HTTP_PUT, +[[{ + "plugins": { + "recaptcha": { + "secret_key": "6LeIxAcTAGG-vFI1TnRWxMZNFuojJ4WifJWe", + "parameter_source": "header", + "parameter_name": "captcha", + "response": { +"content_type": "application/json; charset=utf-8", +"status_code": 400, +"body": "{\"message\":\"invalid captcha\"}\n" + }, + "ssl_verify": false + } + }, + "upstream": { + "nodes": { + "127.0.0.1:1980": 1 + }, + "type": "roundrobin" + }, + "uri": "/index" + }]] +) + +if code >= 300 then +ngx.status = code +end +ngx.say(body) +} +} +--- response_body +passed + + + +=== TEST 3: add plugin on routes +--- config + location /t { + content_by_lua_block { + local t = require("lib.test_admin").test + local code, body = t('/apisix/admin/routes/1', +ngx.HTTP_PUT, +[=[{ + "plugins": { + "recaptcha": { + "secret_key": "6LeIxAcTAGG-vFI1TnRWxMZNFuojJ4WifJWe", + "parameter_source": "header", + "parameter_name": "captcha", + "response": { +"content_type": "application/json; charset=utf-8", +
[GitHub] [apisix] vm-001 commented on a change in pull request #6512: feat: support google reCAPTCHA
vm-001 commented on a change in pull request #6512: URL: https://github.com/apache/apisix/pull/6512#discussion_r831704775 ## File path: apisix/plugins/recaptcha.lua ## @@ -0,0 +1,102 @@ +-- +-- 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 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, schema_type) +return core.schema.check(schema, conf) +end + +local function retrieve_captcha(ctx, conf) +local captcha +if conf.parameter_source == "header" then +captcha = core.request.header(ctx, conf.parameter_name) +elseif conf.parameter_source == "query" then +local uri_args = core.request.get_uri_args(ctx) or {} +captcha = uri_args[conf.parameter_name] +end +return captcha +end + +function _M.access(conf, ctx) +local path = ctx.var.uri +local method = core.request.get_method() + +core.log.debug("path: ", path, ", method: ", method, ", conf: ", core.json.encode(conf)) + +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 = "secret=" .. secret .. "=" .. captcha .. "=" .. remote_ip, +headers = { +["Content-Type"] = "application/x-www-form-urlencoded", +}, +ssl_verify = conf.ssl_verify +}) +if err then +core.log.error("request failed: ", err) +return 503 +end +core.log.debug("recaptcha veirfy result: ", res.body) +local recaptcha_result = core.json.decode(res.body) +if recaptcha_result.success == true then +invalid_captcha = false +end Review comment: No. if request didn't pass the captcha parameter in the header(by default) also cause invalid captcha. and it will be handled in the last in function. -- 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: notifications-unsubscr...@apisix.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org
[GitHub] [apisix] vm-001 commented on a change in pull request #6512: feat: support google reCAPTCHA
vm-001 commented on a change in pull request #6512: URL: https://github.com/apache/apisix/pull/6512#discussion_r831702939 ## File path: docs/zh/latest/plugins/recaptcha.md ## @@ -0,0 +1,112 @@ +--- +title: recaptcha +--- + + + +## 描述 + +通过向 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 响应体 | Review comment: Sure. -- 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: notifications-unsubscr...@apisix.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org
[GitHub] [apisix] vm-001 commented on a change in pull request #6512: feat: support google reCAPTCHA
vm-001 commented on a change in pull request #6512: URL: https://github.com/apache/apisix/pull/6512#discussion_r830712828 ## File path: docs/zh/latest/plugins/recaptcha.md ## @@ -0,0 +1,112 @@ +--- +title: recaptcha +--- + + + +## 描述 + +通过向 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 响应体 | Review comment: The response is an object type and it has three properties. And I just want to make sure it looks good. Has APISIX had any guidance on this? Or any suggestions from you? -- 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: notifications-unsubscr...@apisix.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org
[GitHub] [apisix] vm-001 commented on a change in pull request #6512: feat: support google reCAPTCHA
vm-001 commented on a change in pull request #6512: URL: https://github.com/apache/apisix/pull/6512#discussion_r830707616 ## File path: apisix/plugins/recaptcha.lua ## @@ -0,0 +1,102 @@ +-- +-- 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 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, schema_type) +return core.schema.check(schema, conf) +end + +local function retrieve_captcha(ctx, conf) +local captcha +if conf.parameter_source == "header" then +captcha = core.request.header(ctx, conf.parameter_name) +elseif conf.parameter_source == "query" then +local uri_args = core.request.get_uri_args(ctx) or {} +captcha = uri_args[conf.parameter_name] +end +return captcha +end + +function _M.access(conf, ctx) +local path = ctx.var.uri +local method = core.request.get_method() + +core.log.debug("path: ", path, ", method: ", method, ", conf: ", core.json.encode(conf)) + +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 = "secret=" .. secret .. "=" .. captcha .. "=" .. remote_ip, +headers = { +["Content-Type"] = "application/x-www-form-urlencoded", +}, +ssl_verify = conf.ssl_verify +}) +if err then Review comment: ``` if not res then core.log.error("request failed: ", err) return 503 end ``` I see. -- 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: notifications-unsubscr...@apisix.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org
[GitHub] [apisix] vm-001 commented on a change in pull request #6512: feat: support google reCAPTCHA
vm-001 commented on a change in pull request #6512: URL: https://github.com/apache/apisix/pull/6512#discussion_r830707336 ## File path: apisix/plugins/recaptcha.lua ## @@ -0,0 +1,102 @@ +-- +-- 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 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, schema_type) +return core.schema.check(schema, conf) +end + +local function retrieve_captcha(ctx, conf) +local captcha +if conf.parameter_source == "header" then +captcha = core.request.header(ctx, conf.parameter_name) +elseif conf.parameter_source == "query" then +local uri_args = core.request.get_uri_args(ctx) or {} +captcha = uri_args[conf.parameter_name] +end +return captcha +end + +function _M.access(conf, ctx) +local path = ctx.var.uri +local method = core.request.get_method() + +core.log.debug("path: ", path, ", method: ", method, ", conf: ", core.json.encode(conf)) + +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 = "secret=" .. secret .. "=" .. captcha .. "=" .. remote_ip, +headers = { +["Content-Type"] = "application/x-www-form-urlencoded", +}, +ssl_verify = conf.ssl_verify +}) +if err then +core.log.error("request failed: ", err) +return 503 +end +core.log.debug("recaptcha veirfy result: ", res.body) +local recaptcha_result = core.json.decode(res.body) +if recaptcha_result.success == true then +invalid_captcha = false +end Review comment: ```lua function _M.access(conf, ctx) local invalid_captcha = true local captcha = retrieve_captcha(ctx, conf) if captcha == nil or captcha == "" then -- duplicated ! core.response.set_header("Content-Type", conf.response.content_type) return conf.response.status_code, core.utils.resolve_var(conf.response.body, ctx.var) end 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("faield to decode the recaptcha response json: ", err) end if recaptcha_result.success ~= true then -- duplicated !