【问题标题】:Firebase rules doesn't work on server request but work on SimulatorFirebase 规则不适用于服务器请求,但适用于模拟器
【发布时间】:2016-07-16 22:52:42
【问题描述】:
{
    "rules": {
        "verifications": {
             // Can only request verification code if it's been one minute than previous request.
            "$phoneNumber": {
                ".validate": "!data.exists() || (newData.child('timestamp').val() > data.child('timestamp').val() + 60000)"
            }
        },
        ".read": true,
        ".write": true
    }
}

此规则适用于 Firebase 模拟器,如果时间戳不晚 1 分钟,则写入过程会被拒绝。但是当我尝试从服务器写入时,它通过了规则并且允许写入过程。

代码:

var data = {
    timestamp: 1468664575179
};

var phoneNumber = '+14253452';

firebase.database().ref(`verifications/${phoneNumber}`).set(data, function(err) {
    console.log(err);
});

我想知道为什么 Firebase 模拟器上的写入过程被拒绝,但当请求来自服务器时允许。

这并没有给出原因: Firebase Security Rules work in simulator, but not in code

【问题讨论】:

  • 显示一个数据库结构示例。感觉ref('verifications/${+14253452}')不对。
  • 其实是${phoneNumber},phoneNumber的值为'+14253452'

标签: javascript firebase firebase-realtime-database firebase-security


【解决方案1】:

这取决于您如何初始化连接。

如果您的服务器在不受限制的服务帐户下运行,它将以管理权限运行并绕过安全规则。

// Initialize the app with a service account, granting admin privileges
firebase.initializeApp({
  databaseURL: "https://databaseName.firebaseio.com",
  serviceAccount: "path/to/serviceAccountCredentials.json"
});

请参阅authentication section of the Firebase server docs。

文档的同一部分解释了如何通过在初始化时向服务器进程提供 uid 来覆盖此行为:

// Initialize the app with a custom auth variable, limiting the server's access
firebase.initializeApp({
  databaseURL: "https://databaseName.firebaseio.com",
  serviceAccount: "path/to/serviceAccountCredentials.json",
  databaseAuthVariableOverride: {
    uid: "my-service-worker"
  }
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-02
    • 2020-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-15
    • 1970-01-01
    相关资源
    最近更新 更多