Cloud Firestore와 Realtime Database는 모두 정보 보안 및 액세스 제어를 관리하기 위해 특별히 만든 강력하고 간결한 규칙 언어를 사용합니다. 하지만 규칙이 길어지고 복잡해지면 규칙 동작의 오류를 디버깅하는 데 도움을 받아야 할 수 있습니다.
Firebase 에뮬레이터에는 규칙 적용 범위 보고서를 생성하는 기능이 포함되어 있어 오류를 재현할 때 각 하위 표현식이 평가한 내용을 정확하게 확인할 수 있습니다. 이 보고서에는 기존의 '선 적용 범위' 기법 같이 각 테스트 사례가 규칙을 얼마나 자주 사용했는지에 대한 정보도 제공합니다.
보고서 생성
일련의 테스트를 실행한 후 각 보안 규칙이 평가된 방식을 보여주는 테스트 범위 보고서에 액세스할 수 있습니다.
이 보고서를 가져오려면 실행 중 에뮬레이터에서 노출된 엔드포인트를 쿼리하세요. 브라우저 버전에서는 다음 URL을 사용하세요.
테스트 보고서를 쉽게 생성하려면 Cloud Firestore 및 Realtime Database용 GitHub에서 제공하는 에뮬레이터 빠른 시작을 사용하세요.
이 빠른 시작 가이드에서는 에뮬레이터를 올바르게 설치하고 초기화한 후 규칙 모음 예시에서 샘플 테스트를 생성하는 과정을 설명합니다.
Cloud Firestore를 사용하여 사용자가 버튼을 얼마나 클릭했는지 계산하는 앱을 예로 살펴보겠습니다. 이 앱은 다음 규칙을 사용합니다.
위의 예시에는 규칙이 문서 생성과 문서 업데이트를 구분하지 않는다는 문제가 있습니다. 결과적으로 문서가 존재하지 않으면 쓰기가 허용되지 않고 문서가 존재하지 않기 때문에 문서를 생성할 수 없는 것입니다. '쓰기'를 두 가지의 더 구체적인 작업('생성' 및 '업데이트')으로 구분하면 이 문제는 해결됩니다.
[[["이해하기 쉬움","easyToUnderstand","thumb-up"],["문제가 해결됨","solvedMyProblem","thumb-up"],["기타","otherUp","thumb-up"]],[["필요한 정보가 없음","missingTheInformationINeed","thumb-down"],["너무 복잡함/단계 수가 너무 많음","tooComplicatedTooManySteps","thumb-down"],["오래됨","outOfDate","thumb-down"],["번역 문제","translationIssue","thumb-down"],["샘플/코드 문제","samplesCodeIssue","thumb-down"],["기타","otherDown","thumb-down"]],["최종 업데이트: 2025-07-25(UTC)"],[],[],null,["\u003cbr /\u003e\n\nCloud Firestore and Realtime Database both rely on powerful, concise rules languages\nspecifically created to govern information security and access control. However,\nas rules get longer and more complex, you might need some help debugging errors\nin their behavior.\n\nThe Firebase Emulators include the ability to generate rule coverage reports, so you\ncan see see exactly what each subexpression evaluated to when you reproduce\nan error. The reports also provide information about how frequently each test\ncase used a rule, like traditional \"line coverage\" techniques.\n\nGenerate a report\n\nAfter running a suite of tests, you can access test\ncoverage reports that show how each of your security rules was evaluated.\n\nTo get the reports, query an exposed endpoint on the emulator while\nit's running. For a browser-friendly version, use the following URL: \n\nCloud Firestore \n\n```scdoc\nhttp://localhost:8080/emulator/v1/projects/\u003cdatabase_name\u003e:ruleCoverage.html\n \n```\n\nRealtime Database \n\n```scdoc\nhttp://localhost:9000/.inspect/coverage?ns=\u003cdatabase_name\u003e\n \n```\n\nThis breaks your rules into expressions and subexpressions that you can\nmouseover for more information, including number of evaluations and values\nreturned. For the raw JSON version of this data, include the following URL\nin your query: \n\nCloud Firestore \n\n```scdoc\nhttp://localhost:8080/emulator/v1/projects/\u003cdatabase_name\u003e:ruleCoverage\n \n```\n\nRealtime Database \n\n```scdoc\nhttp://localhost:9000/.inspect/coverage.json?ns=\u003cdatabase_name\u003e\n \n```\n\nDebugging example rules\n\nTo easily generate a test report, use the emulator quickstarts available on\nGitHub for [Cloud Firestore](https://github.com/firebase/quickstart-testing/) and [Realtime Database](https://github.com/firebase/quickstart-testing/).\nThese quickstarts guide you through properly installing\nand initializing the emulators, then generating sample tests from an example\nset of rules.\n\nConsider an example app using Cloud Firestore that counts how many times users\nclick a button. The app employs the following rules: \n\nCloud Firestore \n\n```css+lasso\n service cloud.firestore {\n match /databases/{database}/documents {\n match /counters/{counter} {\n allow read;\n allow write: if request.resource.data.value == resource.data.value +1;\n }\n }\n }\n \n```\n\nTo debug the errors in the rules shown above, use the following sample\nJavaScript test: \n\n const counter0 = db.collection(\"counters\").doc(\"0\");\n await firebase.assertSucceeds(counter0.set({value: 0}));\n\nThe emulator generates a report available at the URL noted above: \n\n```scdoc\nhttp://localhost:8080/emulator/v1/projects/\u003cdatabase_name\u003e:ruleCoverage.html\n```\n\nThe report shows the following undefined and null-value errors:\n\nThe problem with this specific example is that the rules don't differentiate\nbetween creating the document and updating the document. Consequently, the\nwrite isn't allowed if the document doesn't exist, and the document can't be\ncreated because it doesn't exist. Differentiating the \"write\" into two\nmore specific operations --- \"create\" and \"update\" --- solves the problem. \n\nCloud Firestore \n\n```css+lasso\n service cloud.firestore {\n match /databases/{database}/documents {\n match /counters/{counter} {\n allow read;\n allow create: if request.resource.data.value == 0;\n allow update: if request.resource.data.value == resource.data.value +1;\n }\n }\n }\n \n```\n\nThe generated report shows how frequently each rule was used and what was\nreturned."]]