代码之家  ›  专栏  ›  技术社区  ›  ABS CEDAR

Flutter Firebase安全规则不起作用

  •  0
  • ABS CEDAR  · 技术社区  · 2 年前

    我正试图更改的安全规则

    match /{document=**} {
       allow read, write;       
    }
    

    以仅允许经过身份验证的用户。

    我将此规则更改为:

    allow read, write: if request.auth != null;
    

    它在“规则游戏场”中工作,但当我运行该应用程序时,它会出现以下错误:

    [WriteStream]: (c1114d) Stream closed with status: Status{code=PERMISSION_DENIED,     description=Missing or insufficient permissions., cause=null}.
    
    FirebaseFirestoreHostApi.documentReferenceSet     
    (package:cloud_firestore_platform_interface/src/pigeon/messages.pigeon.dart:1009:7)
    
    <asynchronous suspension>
    
    MethodChannelDocumentReference.set       
      (package:cloud_firestore_platform_interface/src/method_channel/method_channel_document_reference.   dart:35:7)
     <asynchronous suspension>
    

    行动:

    我的DB在我的一些集合中的集合中有集合,所以,我将规则更改为:

    如果集合“count”只有文档,则使用此格式:

    match  /count/{document=**} {  
      allow read;
      allow write: if request.auth.uid !=null;       
    }
    

    如果集合(注释)具有包含文档的子集合的文档,则转换为此格式

    match  /comments/{comments} {  
      allow read;
      allow write: if request.auth.uid !=null;
      
        match  /comments/{comments} {  
             allow read;
             allow write: if request.auth.uid !=null;
        }                                               
      }
    

    如果以上 if request.auth.uid !=null; 被替换为 true; 一切都很完美,但它并不安全,因为即使是未经身份验证的用户也可以写入数据库。

    我使用的是安卓工作室和Flutter。

    1 回复  |  直到 2 年前
        1
  •  0
  •   Sahil Totala    2 年前

    首先,您需要确认用户已通过Firebase身份验证登录,如果您想保护所有Firestore文档的安全,您可以使用此规则只允许授权用户访问数据

    service cloud.firestore {
      match /databases/{database}/documents {
        match /{document=**} {
          allow read, write: if request.auth != null;
        }
      }
    }