【问题标题】:Firestore permission error in test database mode测试数据库模式下的 Firestore 权限错误
【发布时间】:2025-12-01 06:40:01
【问题描述】:

我知道关于 Firestore 权限错误的问题被问了很多,但我的情况可能是独一无二的。这是因为三个原因:

  1. 我的数据库处于测试模式,应该允许任何人访问(对吗?)
  2. 我在使用经过身份验证的 firebase 用户模拟我的应用时遇到错误
  3. 我可以写入数据——错误只是在读取数据时发生。

这是我的安全规则:

rules_version = '2';
service cloud.firestore {
    match /databases/{database}/documents {
        match /{document=**} {
          allow read, write: if
              request.time < timestamp.date(2021, 12, 31);
    }
  }
}

这里是Firestore的测试数据库模式的描述截图:

我得到的实际错误是这样的:

Error: Error Domain=FIRStorageErrorDomain Code=-13021 "User does not have permission 
to access gs://zimmerfour.appspot.com/senderID." UserInfo={object=senderID, 
ResponseBody={
    "error": {
    "code": 403,
    "message": "Permission denied. Please enable Firebase Storage for your bucket by visiting the Storage tab in the Firebase Console and ensure that you have sufficient permission to properly provision resources."

编辑:

我没有更改整体 Cloud Storage 规则。它们仍设置为默认值:

service firebase.storage {
match /b/{bucket}/o {
    match /{allPaths=**} {
       allow read, write: if request.auth != null;
      }
   }
}

【问题讨论】:

    标签: firebase google-cloud-firestore permissions firebase-security


    【解决方案1】:

    错误消息来自 Cloud Storage for Firebase(如 FIRStorageErrorDomain 和错误消息所示),在您的项目中受其自己的一组 security rules 保护。

    【讨论】:

    • 我正在使用默认的云存储规则,它应该适用于我的模拟用户(经过身份验证)。请注意,我还能够在收到此错误的同一会话中访问存储在我的云存储中的图像。我错过了什么?
    • 这确实应该是一个新帖子,但我会在这里尝试进一步提供帮助。您是否将存储桶与 Firebase 相关联?此错误消息似乎暗示可能是问题所在:“请通过访问 Firebase 控制台中的存储选项卡为您的存储桶启用 Firebase 存储,并确保您有足够的权限来正确配置资源。”
    • 我所指的规则属于我的 Firebase 应用的存储部分。 (即身份验证、实时、存储、Firestore)。您指的是一组不同的“存储”规则吗?
    【解决方案2】:

    试试这个方法,它对我有用:)

    // if you want to allow for all to read, write
    
    rules_version = '2';
    service cloud.firestore {
      match /databases/{database}/documents {
        match /{document=**} {
          allow read, write;
        }
      }
    }
    
    
    // if you want to allow for all to read, and authenticated users to write
    
    rules_version = '2';
    service cloud.firestore {
      match /databases/{database}/documents {
        match /{document=**} {
          allow read;
          allow write: if request.auth != null;
        }
      }
    }
    
    // if you want to allow for only authenticated users to write or read
    
    rules_version = '2';
    service cloud.firestore {
      match /databases/{database}/documents {
        match /{document=**} {
          allow read, write: if request.auth != null;
        }
      }
    }
    

    【讨论】: