Skip to content

@firebase/rules-unit-testing v2: Null value error. for 'get' when accessing resource in security rules #5520

@sceee

Description

@sceee

[REQUIRED] Describe your environment

  • Operating System version: Windows 10
  • Browser version: N/A
  • Firebase SDK version: 9
  • Firebase Product: firestore

[REQUIRED] Describe the problem

After migrating to @firebase/rules-unit-testing v2, some tests fail that passed before using v1.

The issue seems to be lying down somewhere here in these rules:

match /test/{id} {
      function getRole(rsc) {
        // Read from the "members" map in the resource (rsc).
        return int(rsc.data.members[request.auth.uid]);
      }

      function isOneOfRoles(rsc, array) {
        // Determine if the user is one of an array of roles
        return (getRole(rsc) in array);
      }

    allow read: if isOneOfRoles(resource, [1, 2]); // <-- This is the line mentioned in the error message
}

After migrating to v2, a test performing a get request to a resource in this collection fails with false for 'get' @ LX, Null value error. for 'get' @ LXX (the last line mentioned here is the line marked above)

Using the admin SDK I ensured that the data is there and the members map is correctly filled:

{
    members: { user: 2 },
}

So it seems for whatever reason this code int(rsc.data.members[request.auth.uid]) no longer executes correctly after updating to v2 and returns NULL.
I guess (but could not verify using the debug statement as it prints nothing) that request.auth.uid is null or at least not what I set using the authenticatedContext(...) call.

Steps to reproduce:

  1. Create a document test/123 with the following data:
{
    members: {
        user: 2
    }
}
  1. Have the above mentioned security rules checking that user is in the members map with value 1 or 2.
  2. Execute test code below

Relevant Code:

const userDB = firebaseTestEnvironment.authenticatedContext('user', { email: 'user@example.com' }).firestore()
...
// Test code
await assertSucceeds(userDB.collection('test').doc('123').get()) // Fails with the above error

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions