Firestore 安全规则:request.time“未定义对象”

sie*_*fix 4 firebase-security google-cloud-firestore

我正在尝试根据AngularFirebase 网站上的示例中给出的request.time创建安全规则。

我的功能是

function isThrottled() {
    return request.time < resource.data.lastUpdate + duration.value(1, 'm')
}
Run Code Online (Sandbox Code Playgroud)

我试图去的地方 allow update: if isThrottled() == false

但是,当我尝试使用此规则更新文档时,由于未在对象上定义时间而失败。

错误:simulator.rules 行 [169],列 [12]。对象上的属性时间未定义。

不应该每个请求都有一个timeTimeStamp附加到它吗?这与我初始化 Cloud Functions 或客户端应用程序的方式有关吗?

截图如下:

在此处输入图片说明 在此处输入图片说明

编辑

其余更新安全规则的片段是:

service cloud.firestore {
  match /databases/{db}/documents {
    match /users/{userId} {
      match /username/{id} {
        allow update: if isSelf(userId)
                      && usernameAvailable(incomingData().username)
                      && incomingData().username is string
                      && incomingData().username.size() <= 25
                      && incomingFields().size() == 1
                      && isThrottled() == false;
      }
    }

    function incomingData() {
      return request.resource.data
    }
    function isThrottled() {
        return request.time < resource.data.lastUpdate + duration.value(1, 'm')
        }
    function incomingFields() {
        return incomingData().keys()
    }
    function isSelf(userId) {
        return userId == currentUser().uid;
    }
    function usernameAvailable(username) {
        return !exists(/databases/$(db)/documents/usernames/$(username));
    }


  }
}
Run Code Online (Sandbox Code Playgroud)

username集合是各下一个子集合user文档(在users根集合。每个username文件只有1个场称为username该用户可以更新)。

cry*_*sxd 17

这可能对您的情况特别没有用,但是在检查令牌对象上的自定义声明时我遇到了同样的错误。

在访问该字段之前,您可以使用它in来检查对象上是否存在该属性。如果未定义代理,此代码会生成错误:

allow write: if request.auth != null && request.auth.token.agent == true;
Run Code Online (Sandbox Code Playgroud)

如果未定义代理,此代码工作正常:

allow write: if request.auth != null && "agent" in request.auth.token && request.auth.token.agent == true;
Run Code Online (Sandbox Code Playgroud)