【问题标题】:Firebase Permission denied Error on Firebase emulatorFirebase 模拟器上的 Firebase 权限被拒绝错误
【发布时间】:2020-09-11 00:53:48
【问题描述】:

我引用此 tutorial 来了解 Firestore 安全规则。我已经从存储库中提取了代码,它与视频的代码相匹配。 我将setup 代码更改为运行firestore.rules 而不是firestore-test.rules,并尝试按照相同的目录结构运行firebase emulators:startjest ./spec,我未能通过"should allow delete when user is admin""should not allow delete for normal user" 的测试以及原因它失败是由于通配符中的写入规则。有谁知道怎么回事?

collections.spec.js

const { setup, teardown } = require("./helpers");


describe("General Safety Rules", () => {
  afterEach(async () => {
    await teardown();
  });

  test("should deny a read to the posts collection", async () => {
    const db = await setup();
    const postsRef = db.collection("posts");
    await expect(postsRef.get()).toDeny();
  });

  test("should deny a write to users even when logged in", async () => {
    const db = await setup({
      uid: "danefilled"
    });

    const usersRef = db.collection("users");
    await expect(usersRef.add({ data: "something" })).toDeny();
  });
});

describe("Posts Rules", () => {
  afterEach(async () => {
    await teardown();
  });

  test("should allow update when user owns post", async () => {
    const mockData = {
      "posts/id1": {
        userId: "danefilled"
      },
      "posts/id2": {
        userId: "not_filledstacks"
      }
    };

    const mockUser = {
      uid: "danefilled"
    };

    const db = await setup(mockUser, mockData);

    const postsRef = db.collection("posts");

    await expect(
      postsRef.doc("id1").update({ updated: "new_value" })
    ).toAllow();

    await expect(postsRef.doc("id2").update({ updated: "new_value" })).toDeny();
  });

  test("should allow delete when user owns post", async () => {
    const mockData = {
      "posts/id1": {
        userId: "danefilled"
      },
      "posts/id2": {
        userId: "not_filledstacks"
      }
    };

    const mockUser = {
      uid: "danefilled"
    };

    const db = await setup(mockUser, mockData);

    const postsRef = db.collection("posts");

    await expect(postsRef.doc("id1").delete()).toAllow();

    await expect(postsRef.doc("id2").delete()).toDeny();
  });

  test("should allow delete when user is admin", async () => {
    const mockData = {
      "users/filledstacks": {
        userRole: "Admin"
      },
      "posts/id1": {
        userId: "not_matching1"
      },
      "posts/id2": {
        userId: "not_matching2"
      }
    };

    const mockUser = {
      uid: "filledstacks"
    };

    const db = await setup(mockUser, mockData);

    const postsRef = db.collection("posts");

    await expect(postsRef.doc("id1").delete()).toAllow();
  });

  test("should not allow delete for normal user", async () => {
    const mockData = {
      "users/filledstacks": {
        userRole: "User"
      },
      "posts/id1": {
        userId: "not_matching1"
      },
      "posts/id2": {
        userId: "not_matching2"
      }
    };

    const mockUser = {
      uid: "filledstacks"
    };

    const db = await setup(mockUser, mockData);

    const postsRef = db.collection("posts");

    await expect(postsRef.doc("id1").delete()).toDeny();
  });

  test("should allow adding a post when logged in", async () => {
    const db = await setup({
      uid: "userId"
    });

    const postsRef = db.collection("posts");
    await expect(postsRef.add({ title: "new_post" })).toAllow();
  });

  test("should deny adding a post when not logged in", async () => {
    const db = await setup();
    const postsRef = db.collection("posts");
    await expect(postsRef.add({ title: "new post" })).toDeny();
  });
});

firestore.rules

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {



    // lock down the db
    match /{document=**} {
      allow read: if false;
      allow write: if false;
    }

    match /posts/{postId} {
      allow update: if userOwnsPost();
      allow delete: if userOwnsPost() || userIsAdmin();
      allow create: if loggedIn();
    }

    function loggedIn() {
      return request.auth.uid != null;
    }

    function userIsAdmin() {
      return getUserData().userRole == 'Admin';
    }

    function getUserData() {
      return get(/databases/$(database)/documents/users/$(request.auth.uid)).data
    }

    function userOwnsPost() {
      return resource.data.userId == request.auth.uid;
    }
  }
}

来自终端的错误跟踪

FirebaseError: 7 PERMISSION_DENIED: 
false for 'create' @ L10


  ● Posts Rules › should not allow delete for normal user

FirebaseError: 7 PERMISSION_DENIED: 
false for 'create' @ L10

  at new FirestoreError (/Users/../../../../../../../../../Resources/rules/node_modules/@firebase/firestore/src/util/error.ts:166:5)
  at ClientDuplexStream.<anonymous> (/Users/../../../../../../../../../Resources/rules/node_modules/@firebase/firestore/src/platform_node/grpc_connection.ts:240:13)
  at ClientDuplexStream._emitStatusIfDone (/Users/../../../../../../../../../Resources/rules/node_modules/grpc/src/client.js:234:12)
  at ClientDuplexStream._receiveStatus (/Users/../../../../../../../../../Resources/rules/node_modules/grpc/src/client.js:211:8)
  at Object.onReceiveStatus (/Users/../../../../../../../../../Resources/rules/node_modules/grpc/src/client_interceptors.js:1311:15)
  at InterceptingListener._callNext (/Users/../../../../../../../../../Resources/rules/node_modules/grpc/src/client_interceptors.js:568:42)
  at InterceptingListener.onReceiveStatus (/Users/../../../../../../../../../Resources/rules/node_modules/grpc/src/client_interceptors.js:618:8)
  at /Users/../../../../../../../../../Resources/rules/node_modules/grpc/src/client_interceptors.js:1127:18

【问题讨论】:

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


    【解决方案1】:

    我实际上是按照相同的教程开始使用 firebase 模拟器并收到相同类型的错误消息。我的问题是,当你启动模拟器时,它会自动查找你的firestore.rules 文件并加载规则。因此,当您添加 mockData 时,规则已经适用。

    为了使您的测试代码正常工作,请将您的 firebase.json 中的 Firestore 规则文件的设置更改为不存在的文件(或允许所有读/写的规则文件)或添加 mockData 作为setup 函数中的管理员,例如:

    module.exports.setup = async (auth, data) => {
      const projectId = `rules-spec-${Date.now()}`;
      const app = firebase.initializeTestApp({
        projectId,
        auth
      });
    
      const db = app.firestore();
    
      // Initialize admin app
      const adminApp = firebase.initializeAdminApp({
        projectId
      });
    
      const adminDB = adminApp.firestore();
    
      // Write mock documents before rules using adminApp
      if (data) {
        for (const key in data) {
          const ref = adminDB.doc(key);
          await ref.set(data[key]);
        }
      }
    
      // Apply rules
      await firebase.loadFirestoreRules({
        projectId,
        rules: fs.readFileSync('firestore.rules', 'utf8')
      });
    
      return db;
    };
    

    希望这会有所帮助。

    另见this question

    【讨论】:

      【解决方案2】:

      对于那些目前在 firestore 8.6.1(或同等版本)遇到此问题的用户,这里讨论了一个错误: https://github.com/firebase/firebase-tools/issues/3258#issuecomment-814402977

      修复方法是降级到 firestore 8.3.1,或者如果您在未来阅读本文并且 firestore >= 9.9.0 已发布,请升级到该版本。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-09-25
        • 1970-01-01
        • 2016-10-30
        • 1970-01-01
        • 2017-07-01
        • 2020-02-15
        • 2017-12-26
        相关资源
        最近更新 更多