Android

[Android] 블로그 이전 - Firebase-Firestore 보안 규칙

pearhyunjin 2024. 6. 10. 09:29

 

2022 - 2023년도에 사용하던 블로그 내용을 이전해 온 기록입니다.

해당 블로그는 링크로 첨부하였으며

글 내용은 동일합니다.


 

Firebase 보안 규칙에 대해서 알아본다.

 

[ Android Studio - Java ]

[ Firebase - Firestore]


Firebase와 연동한 프로젝트 작업 중 Firestore에서 "Missing or insufficient permissions"라는 오류가 발생했다.

보안 규칙으로 인해 발생한 문제로, 간단하게 해결 가능한 오류이다.

오류를 해결하면서 Firestore 보안 규칙에 대해 정리해보려고 한다.

 


DB 선언

service cloud.firestore {
    match/database/{database}/documents
}

- service cloud.firestore -> 규칙의 범위를 cloud.firestore로 지정해 보안규칙과 다른 제품의 규칙간의 충돌을 방지

- 구체화시킬 path의 pattern을 매치한다. 프로젝트의 모든 cloud firestore DB가 일치하도록 지정한다.

+) 문서 지정 예시

    cities collection 내 seoul 이라는 특정 문서 지정 -> match/cities/seoul

    cities collection 내 모든 문서 지정 (하위 collection 적용 안됨) -> match/cities/{city}

    cities collection 내 모든 하위 범주 지정 -> match/cities/{document=**}

 


보안 모드

1. 잠금 모드

모든 읽기와 쓰기를 허용하지 않는 상태

allow read, write: if false;

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

 

2. 허용 모드 (테스트 모드)

모든 읽기와 쓰기를 허용하는 상태

allow read, write: if true;

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

 

3. 인증된 사용자에게만 허용

인증된 사용자에게만 읽기와 쓰기를 허용 -> 인증된 사용자 즉, 로그인한 유저 대상이므로 Firebase Authentication 이용

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

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

 

4. 컨텐츠 소유자에게만 허용

컨텐츠를 소유자만 읽기와 쓰기를 허용하는 상태

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

service cloud.firestore {
  match /databases/{database}/documents {
    // Allow only authenticated content owners access
    match /some_collection/{userId}/{documents=**} {
      allow read, write: if request.auth != null && request.auth.uid == userId;
    }
  }
}

 

5. 공개 / 비공개 설정

컨텐츠가 공개인지 비공개인지를 확인하고 읽기를 허용, 쓰기는 소유자만 허용하는 상태

allow read: if true

allow create: if request.auth.uid == request.resource.data.author_uid;

allow update, delete: if request.auth.uid == resource.data.author_uid;

service cloud.firestore {
  match /databases/{database}/documents {
    // Allow public read access, but only content owners can write
    match /some_collection/{document} {
      allow read: if true
      allow create: if request.auth.uid == request.resource.data.author_uid;
      allow update, delete: if request.auth.uid == resource.data.author_uid;
    }
  }
}

 

6. 역할 지정하기

데이터베이스 별로 지정자를 만들어 읽기와 쓰기를 허용하는 상태

service cloud.firestore {
  match /databases/{database}/documents {
    // For attribute-based access control, Check a boolean `admin` attribute
    allow write: if get(/databases/$(database)/documents/users/$(request.auth.uid)).data.admin == true;
    allow read: true;

    // Alterntatively, for role-based access, assign specific roles to users
    match /some_collection/{document} {
     allow read: if get(/databases/$(database)/documents/users/$(request.auth.uid)).data.role == "Reader"
     allow write: if get(/databases/$(database)/documents/users/$(request.auth.uid)).data.role == "Writer"
   }
  }
}

 


https://firebase.google.com/docs/rules/basics?hl=ko#cloud-firestore 

 

기본 보안 규칙  |  Firebase 보안 규칙

Google I/O 2023에서 Firebase의 주요 소식을 확인하세요. 자세히 알아보기 의견 보내기 기본 보안 규칙 컬렉션을 사용해 정리하기 내 환경설정을 기준으로 콘텐츠를 저장하고 분류하세요. Firebase 보안

firebase.google.com

 

https://seizemymoment.tistory.com/11

 

[Firebase] 파이어베이스 보안규칙 (Firestore Security Rules ) 작성 방법

파이어베이스에서 제공하는 보안규칙은 코드가 간단하며 보안규칙을 위해 인프라를 관리하거나 복잡한 서버측 인증 및 인증 코드를 작성할 필요 없다. 하지만 보안규칙을 적용하지 않으면 파

seizemymoment.tistory.com

 

 

 

 


 

https://baehj.tistory.com/136

 

[Android] Firebase-Firestore 보안 규칙

Firebase 보안 규칙에 대해서 알아본다. [ Android Studio - Java ] [ Firebase - Firestore] Firebase와 연동한 프로젝트 작업 중 Firestore에서 "Missing or insufficient permissions"라는 오류가 발생했다. 보안 규칙으로 인해

baehj.tistory.com