【问题标题】:Firebase security hashing UID doesn't match client computedFirebase 安全哈希 UID 与客户端计算的不匹配
【发布时间】:2020-05-02 19:51:39
【问题描述】:

按照 firebase 文档here,我实施了以下安全规则:

match /docs/{hashID} {
  allow read, write: if hashing.md5(request.auth.uid.toUtf8()).toBase64() == hashID;
}

在客户端,我使用 node-md5 计算用户的 hashID 并写入文档

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

由于哈希值不匹配,它未能通过安全规则。

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

【问题讨论】:

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


    【解决方案1】:

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

    match /docs/{hashID} {
      allow read, write: if hashing.md5(request.auth.uid.toUtf8()).toHexString() == hashID;
    }
    

    此外,Firebase 的 toHexString() 返回全部大写的结果,所以我需要在客户端执行此操作

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

    【讨论】:

    • 或者我们可以在客户端执行btoa(md5(user.uid, {asString: true})) 以使base64 规则起作用
    【解决方案2】:

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

    【讨论】:

    • 我的 hashID 是一个字符串,而 hashing.md5 返回字节。当我取出toBase64 时,我得到错误:子表达式不可比较,所以这个比较总是返回false。我也试过toHexString(),但这给了我另一个不匹配的值。
    • 也许您想改为在 hashID 字符串上调用 toUtf8()
    • 不走运。我想知道我是否给 node-md5 和 hashing.md5 提供了相同的输入。我没有办法看到request.auth.uid.toUtf8()的中间值
    • 啊!我在规则游乐场中的 uid 有错字!在我修复之后,修复其余部分很简单。很快就会公布答案
    猜你喜欢
    • 2018-09-11
    • 1970-01-01
    • 1970-01-01
    • 2018-04-08
    • 2021-03-30
    • 2021-06-06
    • 2022-07-06
    • 1970-01-01
    • 2019-07-17
    相关资源
    最近更新 更多