Firebase安全性 - 如何停止覆盖

时间:2022-07-26 07:26:34

here is my datastructure:

这是我的数据结构:

{
   "items" : {
      "item0" : {
         "uniqueID" : 5,
         "uniqueID" : 31,
         "uniqueID" : 88
      },
      "item1" : {
         "uniqueID" : 22,
         "uniqueID" : 3
      }
   }
}

I would like for the user to be able to:

我希望用户能够:

1-create a new item

1 - 创建一个新项目

2-create a new uniqueID (entry in an item)

2 - 创建一个新的唯一ID(项目中的条目)

I would like to stop the user from:

我想阻止用户:

3-deleting anything

3 - 删除任何东西

4-creating anything other than mentioned above

4 - 创建除上述之外的任何内容

here is my incorrect attempt:

这是我不正确的尝试:

{
    "rules": {
        ".read": true,
        ".write": false,
        "items": {
          ".write": "!data.exists()"
        }
    }
}

I believe the ".write": false should accomplish task 3 and 4, and that ".write": "!data.exists()" should accomplish task 1 and 2, but this solution stops any data from being written to the db

我相信“.write”:false应该完成任务3和4,并且“。write”:“!data.exists()”应该完成任务1和2,但是这个解决方案会阻止任何数据写入db

Write operations that should fail:

编写失败的操作:

var killItem0 = {};
killItem0['item0'] = 'all item 0 entries are now replaced by this text';
REF.child('items/').update(killItem0); //this replaces all data held by item0

1 个解决方案

#1


2  

I think something like this should work:

我认为这样的事情应该有效:

{
"rules": {
  ".read": true,
  "items": {
      "$itemID": {
          "$uniqueID": {
             //check if data is already there and new data in a number (going off your example data for the number part)
             ".write": "!data.exists() && newData.isNumber()"
            }
        }
    }
}
}

#1


2  

I think something like this should work:

我认为这样的事情应该有效:

{
"rules": {
  ".read": true,
  "items": {
      "$itemID": {
          "$uniqueID": {
             //check if data is already there and new data in a number (going off your example data for the number part)
             ".write": "!data.exists() && newData.isNumber()"
            }
        }
    }
}
}