Firebase:如何保护未经登录发送的内容?

时间:2016-09-05 14:40:35

标签: firebase firebase-authentication firebase-security

我正在构建一个混合移动应用程序,Firebase作为我的后端。我希望让用户在墙上发布他们想要的任何消息而无需身份验证,但我对垃圾邮件的可能性感到担忧。我的意思是,如果用户不需要进行身份验证即可发布,我的安全规则基本上是空的,任何获取端点的人都可以发布无限量的内容。而且我看不出我能对付它做什么。

所以我知道anonymous auth,但我不确定它是否真的解决了这个问题。毕竟,端点仍然是开放的,只是在之前调用方法的必要性之后。我认为它增加了一点复杂性,但并不多。

我想知道是否有可能检查来电来源,以确保它来自我的应用程序而没有别的。或者,如果你有另一个想法让这更安全,我会对所有事情持开放态度。谢谢!

1 个答案:

答案 0 :(得分:1)

您可以结合使用客户端上的recaptcha和后端上的firebase cloud functions来实现此目的。

您将要添加到商店的消息与验证码一起发送到云功能。在云计算的功能,我们首先验证验证码。如果可以,我们将消息添加到商店。这个作品,因为经由云功能将项目添加到商店时,火力的身份验证规则都被忽略。

这是一个示例云功能:

const functions = require('firebase-functions')
const admin = require('firebase-admin')
const rp = require('request-promise')
const cors = require('cors')({
    origin: true,
});

admin.initializeApp();

exports.createUser = functions.https.onRequest(function (req, res) {
  cors(req, res, () => {

    // the body is a json of form {message: Message, captcha: string}
    const body = req.body;

    // here we verify whether the captcha is ok. We need a remote server for 
    // for this so you might need a paid plan
    rp({
        uri: 'https://recaptcha.google.com/recaptcha/api/siteverify',
        method: 'POST',
        formData: {
            secret: '<SECRET>',
            response: body.captcha
        },
        json: true
    }).then(result => {
        if (result.success) {
            // the captcha is ok! we can now send the message to the store
            admin.firestore()
                .collection('messages')
                .add(body.message)
                .then(writeResult => {
                    res.json({result: `Message with ID: ${writeResult.id} added.`});
                });
        } else {
            res.send({success: false, msg: "Recaptcha verification failed."})
        }
    }).catch(reason => {
        res.send({success: false, msg: "Recaptcha request failed."})
    })
  });
})

还有更多信息:https://firebase.googleblog.com/2017/08/guard-your-web-content-from-abuse-with.html