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

2024-04-29

我有以下代码可以验证谷歌验证码 v3在我的 Firebase 函数中导致CORS issue:

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" using $.ajax()。这是因为下面的 Firebase 函数日志让我想起了“无法访问外部网络”:

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

我的问题是:有没有办法让我的服务器端 reCAPTCHA 验证正常工作,而不会导致此 CORS 问题,这可以通过“未配置计费帐户”来防止?谢谢!

UPDATE:

抓住之后request()进行验证时出错,我收到以下错误:

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

另外,处理此错误后,不再有 CORS 问题,但 reCAPTCHA 仍然无法验证。知道是什么原因造成的吗?再次感谢!


目前确认上述问题已经解决启用计费 at the 谷歌云控制台 https://console.cloud.google.com/. It is 实际上不是 CORS 问题在 localhost 和 Firebase Functions/Hosting 之间(尽管 Chrome 浏览器返回了与 CORS 相关的错误消息),它实际上是从 Firebase 函数到 Google reCAPTCHA api 的 HTTP 请求在令牌验证过程中。由于结算帐户未链接到该函数所在的 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 验证的请求将成功(如果令牌有效),不会出现上述错误。然而,你的免费 Spark 等级Firebase 帐户将是自动升级 to 火焰计划- 现收现付。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 Firebase 函数中验证 reCAPTCHA v3 导致 CORS 问题 的相关文章

随机推荐