Chancellor / firestore.rules.test.ts
VoidBreach's picture
Upload 62 files
35b8b9c verified
Raw History Blame Contribute Delete
5.84 kB
import { initializeTestEnvironment, RulesTestEnvironment } from '@firebase/rules-unit-testing';
import { assertFails, assertSucceeds } from '@firebase/rules-unit-testing';
import { readFileSync } from 'fs';
import { test, beforeAll, afterAll, beforeEach } from 'vitest';
import { doc, setDoc, updateDoc, deleteDoc } from 'firebase/firestore';
let testEnv: RulesTestEnvironment;
beforeAll(async () => {
// Load the rules file
const rules = readFileSync('firestore.rules', 'utf8');
// Initialize the test environment
testEnv = await initializeTestEnvironment({
projectId: 'demo-lyceum-protocols',
firestore: {
rules,
host: '127.0.0.1',
port: 8080,
},
});
});
beforeEach(async () => {
// Clear the database between tests
await testEnv.clearFirestore();
});
afterAll(async () => {
// Cleanup
await testEnv.cleanup();
});
const mockSystemSeed = {
id: 'seed-123',
x: 50,
y: 50,
affinity: 0.8,
recursiveDepth: 1,
bloomLevel: 10,
timestamp: Date.now(), // close enough
};
test('1. The Ghost Write: Reject arbitrary ID if user tries to mock or misalign', async () => {
// In our case, the ID in the payload must match the doc ID.
const alice = testEnv.authenticatedContext('alice', { email_verified: true });
const docRef = doc(alice.firestore(), 'systemSeeds', 'seed-123');
await assertFails(
setDoc(docRef, {
...mockSystemSeed,
id: 'hacked-id-999', // Payload ID differs from doc ID
}),
);
});
test('2. The Out-of-Bounds Seed: Reject x = 1000', async () => {
const alice = testEnv.authenticatedContext('alice', { email_verified: true });
const docRef = doc(alice.firestore(), 'systemSeeds', mockSystemSeed.id);
await assertFails(
setDoc(docRef, {
...mockSystemSeed,
x: 1000,
}),
);
});
test('3. The Infinite Affinity: Reject affinity = 999999', async () => {
const alice = testEnv.authenticatedContext('alice', { email_verified: true });
const docRef = doc(alice.firestore(), 'systemSeeds', mockSystemSeed.id);
await assertFails(
setDoc(docRef, {
...mockSystemSeed,
affinity: 999999,
}),
);
});
test('4. The Shadow Field: Reject injected isAdmin field', async () => {
const alice = testEnv.authenticatedContext('alice', { email_verified: true });
const docRef = doc(alice.firestore(), 'systemSeeds', mockSystemSeed.id);
await assertFails(
setDoc(docRef, {
...mockSystemSeed,
isAdmin: true,
}),
);
});
test('5. The Temporal Anchor: Reject creation with stale timestamp from 1970', async () => {
const alice = testEnv.authenticatedContext('alice', { email_verified: true });
const docRef = doc(alice.firestore(), 'systemSeeds', mockSystemSeed.id);
await assertFails(
setDoc(docRef, {
...mockSystemSeed,
timestamp: 0, // 1970
}),
);
});
test('6. The Denial of Wallet (ID Injection): Reject large ID string', async () => {
const alice = testEnv.authenticatedContext('alice', { email_verified: true });
const hugeId = 'A'.repeat(2000);
const docRef = doc(alice.firestore(), 'systemSeeds', hugeId);
await assertFails(
setDoc(docRef, {
...mockSystemSeed,
id: hugeId,
}),
);
});
test('7. The State Skip: (If progression rule applied) Update validation', async () => {
// Let"s do a valid create first
await testEnv.withSecurityRulesDisabled(async (context) => {
const db = context.firestore();
await setDoc(doc(db, 'systemSeeds', mockSystemSeed.id), mockSystemSeed);
});
const alice = testEnv.authenticatedContext('alice', { email_verified: true });
const docRef = doc(alice.firestore(), 'systemSeeds', mockSystemSeed.id);
// Valid update (e.g., increasing bloomLevel) is allowed, but we don't have explicit delta checks yet.
// We can at least test that arbitrary field additions fail on update.
await assertFails(
updateDoc(docRef, {
bloomLevel: 100,
x: 90, // Not inside allowed update fields (only bloomLevel, recursiveDepth, affinity, timestamp, label)
}),
);
});
test('8. The Identity Spoof: Attempting to create log as another user', async () => {
const bob = testEnv.authenticatedContext('bob', { email_verified: true });
const docRef = doc(bob.firestore(), 'recursive_growth_logs', 'log-1');
// We don"t have owner logic on logs yet, just isVerifiedScholar, but let"s assume a valid ID.
await assertSucceeds(setDoc(docRef, {}));
// Spoofing identity would mean Bob tries to claim auth uid is Alice, which rules-unit-testing prevents by design.
});
test('9. The Large Payload: Reject large string in label', async () => {
const alice = testEnv.authenticatedContext('alice', { email_verified: true });
const docRef = doc(alice.firestore(), 'systemSeeds', mockSystemSeed.id);
await assertFails(
setDoc(docRef, {
...mockSystemSeed,
label: 'A'.repeat(50 * 1024), // 50KB string, our limit is 512
}),
);
});
test('10. The Orphaned Write: Writing to non-existent collection', async () => {
const alice = testEnv.authenticatedContext('alice', { email_verified: true });
const docRef = doc(alice.firestore(), 'ghost_collection', 'item-1');
await assertFails(setDoc(docRef, { data: true }));
});
test('11. The PII Leak: Read restriction if we had private fields', async () => {
// Our schema is public.
});
test('12. The Unauthorized Purge: Attempt to delete seed', async () => {
// Let"s do a valid create first
await testEnv.withSecurityRulesDisabled(async (context) => {
const db = context.firestore();
await setDoc(doc(db, 'systemSeeds', mockSystemSeed.id), mockSystemSeed);
});
const alice = testEnv.authenticatedContext('alice', { email_verified: true });
const docRef = doc(alice.firestore(), 'systemSeeds', mockSystemSeed.id);
await assertFails(deleteDoc(docRef));
});