验证Firebase函数中的reCAPTCHA v3导致CORS问题。

问题描述 投票:0回答:1

我有以下代码可以验证 Google reCAPTCHA v3 在我的Firebase函数中,导致 CORS 问题。

const functions = require('firebase-functions');
const nodemailer = require("nodemailer");
const express = require("express");
const cors = require("cors");
const request = require('request');
const serverApi = express();

api.use(cors({ origin: true }));

function verifyCaptcha(token, returnData) {
    // Put your secret key here.
    var secretKey = functions.config().recaptcha.secretkey;

    var verificationUrl = "https://www.google.com/recaptcha/api/siteverify?secret=" + secretKey + "&response=" + token;

    // Note here: External network call to google.com
    request(verificationUrl, function (error, response, body) {
        body = JSON.parse(body);
        // Success will be true or false depending upon captcha validation.
        if (!body.success) {
            body['status'] = false;
            body['errSource'] = "recaptcha";
            body['message'] = "Failed to pass captcha verification.";

        } else {
            body['status'] = true;
            body['message'] = "Successfully passed captcha verification!";

        };
        console.log(`Google returns: ${JSON.stringify(body)}`);

        returnData(body);
    });
};

api.post("/api/service-name", (req, res) => {
    if (!req.body['g-recaptcha-response']) {
        return res.send({ "status": false, "errSource": "recaptcha", "message": "Client-side reCAPTCHA token not found." });
    };

    const recaptchaToken = req.body['g-recaptcha-response'];

    verifyCaptcha(recaptchaToken, function (result) {
        if (result.status == false) {
            return res.send(result);
        };

        // My business logics here.

    }); 
});

exports.api = functions.https.onRequest(api);

我注意到,在我的Firebase函数中删除了reCAPTCHA v3验证请求后,我的本地主机调用CORS问题就没有了。"/api/service-name" 使用 $.ajax(). 这是因为下面的Firebase Function日志让我想到了 "外部网络无法访问":

Billing account not configured. External network is not accessible and quotas are severely limited.
Configure billing account to remove these restrictions

我的问题是,有没有办法让我的服务器端reCAPTCHA验证工作,而不造成CORS问题? 是否有办法让我的服务器端reCAPTCHA验证正常工作,而不造成CORS问题,而这个问题可以通过 "账单账户未配置 "来防止?谢谢

更新。

在发现了 request() 错误,进行验证,我得到以下错误。

{errno: "EAI_AGAIN", code: "EAI_AGAIN", syscall: "getaddrinfo", hostname: "www.google.com", host: "www.google.com", …}

另外,在处理这个错误后,没有更多的CORS问题, 但reCAPTCHA仍然无法验证。知道是什么原因吗? 再次感谢

firebase google-cloud-functions recaptcha
1个回答
0
投票

现在确认上述问题已经解决,经过 启用计费谷歌云控制台. 它是 其实不是CORS的问题 在本地主机和Firebase FunctionsHosting之间(虽然Chrome浏览器返回的是CORS相关的错误信息),但实际上是本地主机和Firebase FunctionsHosting之间的。从Firebase函数到Google reCAPTCHA api的HTTP请求。 在token验证过程中。由于计费账户没有链接到函数所在的Firebase项目,从任何Firebase函数向任何 外部网络资源,包括Google reCAPTCHA。拒绝后会出现以下错误。

HTTP请求错误。

{errno: "EAI_AGAIN", code: "EAI_AGAIN", syscall: "getaddrinfo", hostname: "www.google.com", host: "www.google.com", …}

在GCP启用计费并将计费账户链接到特定的Firebase项目后,向Google reCAPTCHA验证的请求将成功(如果令牌有效),不会出现上述错误。然而,您的 免费的火花层 Firebase账户将被 自动升级的。烈火计划 -- 随用随付

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.