Firebase安全哈希UID不';不匹配客户端计算

Firebase安全哈希UID不';不匹配客户端计算,firebase,google-cloud-firestore,firebase-security,Firebase,Google Cloud Firestore,Firebase Security,根据firebase文档,我实施了以下安全规则: match /docs/{hashID} { allow read, write: if hashing.md5(request.auth.uid.toUtf8()).toBase64() == hashID; } 在客户端,我使用计算用户的hashID并写入文档 firebase.firestore().collection("docs").doc(md5(user.uid)).writeSomething() 由于散列不匹配,它未通过

根据firebase文档,我实施了以下安全规则:

match /docs/{hashID} {
  allow read, write: if hashing.md5(request.auth.uid.toUtf8()).toBase64() == hashID;
}
在客户端,我使用计算用户的hashID并写入文档

firebase.firestore().collection("docs").doc(md5(user.uid)).writeSomething()
由于散列不匹配,它未通过安全规则


对于uid“crz6KyreRCM4A0Qvk9EfeXHBLF43”,我的客户md5给我“eee1f958a8c0a273f138bdee0167693d”,而Firebase规则游乐场给我“fbOLeadWz7YxnsGgfESBNg==”。我已使用验证客户端md5值是否正确。我在安全规则中做错了什么?

您不必要地调用了
toBase64()。这是为了将字节数组转换成字符串,然后可以对字符串进行散列。由于您在这里完全处理字符串,因此不需要对任何内容进行base64编码。

解决了!
node-md5
输出是一个十六进制字符串,因此以下规则是合适的:

match /docs/{hashID} {
  allow read, write: if hashing.md5(request.auth.uid.toUtf8()).toHexString() == hashID;
}
此外,Firebase的
toHexString()
在所有CAP中返回一个结果,因此我需要在客户端执行此操作

firebase.firestore().collection("docs").doc(md5(user.uid).toUpperCase()).writeSomething()

我的哈希ID是一个字符串,而
哈希。md5
返回字节。当我取出
toBase64
时,我得到一个错误:子表达式不可比较,因此此比较将始终返回false。我还尝试了
toHexString()
,但这给了我另一个不匹配的值。也许你想调用
hashID
字符串?运气不好。我想知道我是否给node-md5和hashing.md5提供了相同的输入。我没有办法看到request.auth.uid.toUtf8()的中间值。
Ah!我在操场上的uid输入错误!在我修复之后,修复其余部分是很简单的。我们可以在客户端执行
btoa(md5(user.uid,{asString:true}))
以使base64规则正常工作