限制可写入路径的记录数(引用安全规则中的其他路径)

Dan*_*nze 8 firebase firebase-security

假设我的Firebase集合如下所示:

{
  "max":5
  "things":{}
}
Run Code Online (Sandbox Code Playgroud)

我如何使用max我的安全规则中的值来限制数量things?

{
  "rules": {
    "things": {
      ".validate": "newData.val().length <= max"
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

Kat*_*ato 16

使用现有属性是使用root或parent完成的,非常简单.

{
  "rules": {
    "things": {
      // assuming value is being stored as an integer
      ".validate": "newData.val() <= root.child('max')"
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

但是,确定记录数并强制执行此操作比编写安全规则要复杂一些:

  • 由于.length对象上没有,我们需要存储有多少记录
  • 我们需要以安全/实时的方式更新该号码
  • 我们需要知道我们相对于该计数器添加的记录的数量

天真的方法

假设限制很小(例如5条记录),一个穷人的方法就是简单地在安全规则中枚举它们:

{
  "rules": {
    "things": {
      ".write": "newData.hasChildren()", // is an object
      "thing1": { ".validate": true },
      "thing2": { ".validate": true },
      "thing3": { ".validate": true },
      "thing4": { ".validate": true },
      "thing5": { ".validate": true },
      "$other": { ".validate": false
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

一个真实的例子

像这样的数据结构有效:

/max/<number>
/things_counter/<number>
/things/$record_id/{...data...}
Run Code Online (Sandbox Code Playgroud)

因此,每次添加记录时,计数器必须递增.

var fb = new Firebase(URL);
fb.child('thing_counter').transaction(function(curr) {
   // security rules will fail this if it exceeds max
   // we could also compare to max here and return undefined to cancel the trxn
   return (curr||0)+1;
}, function(err, success, snap) {
   // if the counter updates successfully, then write the record
   if( err ) { throw err; }
   else if( success ) {
      var ref = fb.child('things').push({hello: 'world'}, function(err) {
         if( err ) { throw err; }
         console.log('created '+ref.name());
      });
   }
});
Run Code Online (Sandbox Code Playgroud)

每次删除记录时,计数器必须递减.

var recordId = 'thing123';
var fb = new Firebase(URL);
fb.child('thing_counter').transaction(function(curr) {
   if( curr === 0 ) { return undefined; } // cancel if no records exist
   return (curr||0)-1;
}, function(err, success, snap) {
   // if the counter updates successfully, then write the record
   if( err ) { throw err; }
   else if( success ) {
      var ref = fb.child('things/'+recordId).remove(function(err) {
         if( err ) { throw err; }
         console.log('removed '+recordId);
      });
   }
});
Run Code Online (Sandbox Code Playgroud)

现在谈到安全规则:

{
  "rules": {
    "max": { ".write": false },

    "thing_counter": {
      ".write": "newData.exists()", // no deletes
      ".validate": "newData.isNumber() && newData.val() >= 0 && newData.val() <= root.child('max').val()"
    },

    "things": {
      ".write": "root.child('thing_counter').val() < root.child('max').val()"
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

请注意,这不会强制用户在更新记录之前写入thing_counter,因此虽然适合限制记录数量,但它不适合强制执行游戏规则或防止作弊.

其他资源和想法

如果您想要游戏级安全性,请查看此小提琴,其中详细说明了如何使用增量ID创建记录,包括强制执行计数器所需的安全规则.您可以将其与上述规则结合使用,以对增量ID强制执行最大值,并确保在写入记录之前更新计数器.

此外,请确保您没有过度考虑这一点,并且有一个合理的用例来限制记录的数量,而不仅仅是为了满足健康的担忧.简单地在数据结构上强制执行穷人的配额是很复杂的.