Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion firestore.rules
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ service cloud.firestore {
}

allow read: if isAdmin()
|| (request.auth != null && request.auth.uid == userId)
|| isUser(userId)
|| isOwnerOrClinicianOfSameOrganization();

allow write: if isAdmin()
Expand Down
29 changes: 29 additions & 0 deletions functions/src/tests/rules/userCollections.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,15 @@ describe("firestore.rules: users/{userId}/{collectionName}/{documentId}", () =>
const clinicianId = "mockClinician";
const patientId = "mockPatient";
const userId = "mockUser";
const disabledUserId = "disabledMockUser";

let testEnvironment: RulesTestEnvironment;
let adminFirestore: firebase.firestore.Firestore;
let ownerFirestore: firebase.firestore.Firestore;
let clinicianFirestore: firebase.firestore.Firestore;
let patientFirestore: firebase.firestore.Firestore;
let userFirestore: firebase.firestore.Firestore;
let disabledUserFirestore: firebase.firestore.Firestore;

beforeAll(async () => {
testEnvironment = await initializeTestEnvironment({
Expand Down Expand Up @@ -80,6 +82,14 @@ describe("firestore.rules: users/{userId}/{collectionName}/{documentId}", () =>
userFirestore = testEnvironment
.authenticatedContext(userId, {})
.firestore();

disabledUserFirestore = testEnvironment
.authenticatedContext(disabledUserId, {
type: UserType.patient,
organization: organizationId,
disabled: true,
})
.firestore();
});

beforeEach(async () => {
Expand All @@ -99,6 +109,11 @@ describe("firestore.rules: users/{userId}/{collectionName}/{documentId}", () =>
.doc(`users/${patientId}`)
.set({ type: UserType.patient, organization: organizationId });
await firestore.doc(`users/${userId}`).set({});
await firestore.doc(`users/${disabledUserId}`).set({
type: UserType.patient,
organization: organizationId,
disabled: true,
});
});
});

Expand Down Expand Up @@ -143,4 +158,18 @@ describe("firestore.rules: users/{userId}/{collectionName}/{documentId}", () =>
await assertFails(userFirestore.collection(patientPath).get());
await assertSucceeds(userFirestore.collection(userPath).get());
});

it("disabled users cannot access their own nested collections", async () => {
const disabledUserPath = `users/${disabledUserId}/allergyIntolerances`;

// Disabled users should not be able to read their own nested collections
await assertFails(disabledUserFirestore.collection(disabledUserPath).get());

// Admin can still access disabled user's collections
await assertSucceeds(adminFirestore.collection(disabledUserPath).get());

// Owners and clinicians can still access disabled user's collections
await assertSucceeds(ownerFirestore.collection(disabledUserPath).get());
await assertSucceeds(clinicianFirestore.collection(disabledUserPath).get());
});
});
Loading