【问题标题】:Firebase functions authorize only requests from Firebase hosting appFirebase 函数仅授权来自 Firebase 托管应用的请求
【发布时间】:2021-08-11 09:41:34
【问题描述】:

我有一个简单的 Firebase 托管网络应用程序(基于 Vue 应用程序),它调用 Firebase 函数(谷歌云函数):

import firebase from "firebase/app";
import "firebase/functions";


firebase.initializeApp(firebaseConfig);

let functions = firebase.app().functions("us-west4");
let testFunction = functions.httpsCallable("testFunction");

及对应函数index.js文件:

const functions = require("firebase-functions");

exports.testFunction = functions.region("us-west4").https.onCall(async (data, context) => {
  console.log("Very important things here");
  return {"response": "data"};
});

从安全角度来看是不是可以

  1. 仅允许来自我的域名(Firebase 托管)myhostedapp.web.app 的此调用
  2. 检查我的 JS 应用在请求期间提供的任何类型的身份验证(例如令牌)?

我已尝试访问 context.auth 属性 (see docs),但似乎需要某种服务帐户,并且在从 Firebase 托管 Web 应用程序调用时无法使用。

基本上,我不希望我的函数可公开访问(通过触发 url 进行简单调用),因此任何保护 Firebase Hosting + Functions 的建议或最佳实践将不胜感激。

【问题讨论】:

  • 关于云功能的“允许未经身份验证”部分,请查看this answer,了解为什么需要这样的信息。注意:我还没有用新的App Check 的信息更新它。

标签: firebase firebase-authentication google-cloud-functions firebase-security firebase-hosting


【解决方案1】:

Firebase 刚刚发布了一个名为 App Check 的新功能,它正是这样做的:它允许您项目中的 Cloud Functions 只能从在该项目中注册的应用调用。

对于web apps,这是通过 reCAPTCHA v3 发生的,它 .然后,一旦您 enable enforcement 对 Cloud Functions 进行检查,它将拒绝来自其他来源的任何请求。

您通常希望将 App Check 与当前基于用户的方法结合使用,以便您可以轻松阻止来自 Web 应用程序外部的呼叫,同时仍确保经过身份验证的用户只能拨打他们获得授权的呼叫。

【讨论】:

    猜你喜欢
    • 2021-11-16
    • 2021-04-26
    • 2018-10-15
    • 2020-04-20
    • 2017-08-18
    • 1970-01-01
    • 2021-09-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多