Firebase 函数仅授权来自 Firebase 托管应用程序的请求
Firebase functions authorize only requests from Firebase hosting app
我有一个简单的 Firebase 托管 Web 应用程序(基于 Vue 应用程序)调用 Firebase 函数(Google 云函数):
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"};
});
从安全角度看是否可以
- 仅允许从我的域名(Firebase 托管)进行此调用
myhostedapp.web.app
- 检查我的 JS 应用程序在请求期间提供的任何类型的身份验证(例如令牌)?
我已尝试访问 context.auth
属性 (see docs),但似乎需要某种服务帐户,并且在从 Firebase 托管 Web 应用程序调用时无法使用。
基本上我不希望公开访问我的函数(通过触发器 url 进行简单调用),因此任何关于保护 Firebase Hosting + Functions
的建议或最佳实践将不胜感激。
Firebase 刚刚发布了一项名为 App Check 的新功能,它正是这样做的:它允许您的项目中的 Cloud Functions 只能从在该项目中注册的应用程序中调用。
对于 web apps this happens through reCAPTCHA v3, which . Then once you enable enforcement 的 Cloud Functions 检查,它将拒绝来自其他来源的任何请求。
您通常希望将 App Check 与当前基于用户的方法相结合,这样您就可以轻松阻止来自网络应用程序外部的调用,同时仍确保经过身份验证的用户只能拨打他们获得授权的电话对于.
我有一个简单的 Firebase 托管 Web 应用程序(基于 Vue 应用程序)调用 Firebase 函数(Google 云函数):
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"};
});
从安全角度看是否可以
- 仅允许从我的域名(Firebase 托管)进行此调用
myhostedapp.web.app
- 检查我的 JS 应用程序在请求期间提供的任何类型的身份验证(例如令牌)?
我已尝试访问 context.auth
属性 (see docs),但似乎需要某种服务帐户,并且在从 Firebase 托管 Web 应用程序调用时无法使用。
基本上我不希望公开访问我的函数(通过触发器 url 进行简单调用),因此任何关于保护 Firebase Hosting + Functions
的建议或最佳实践将不胜感激。
Firebase 刚刚发布了一项名为 App Check 的新功能,它正是这样做的:它允许您的项目中的 Cloud Functions 只能从在该项目中注册的应用程序中调用。
对于 web apps this happens through reCAPTCHA v3, which . Then once you enable enforcement 的 Cloud Functions 检查,它将拒绝来自其他来源的任何请求。
您通常希望将 App Check 与当前基于用户的方法相结合,这样您就可以轻松阻止来自网络应用程序外部的调用,同时仍确保经过身份验证的用户只能拨打他们获得授权的电话对于.