rules_version = '2'; service cloud.firestore { match /databases/{database}/documents { // Default deny all access match /{document=**} { allow read, write: if false; } function isSignedIn() { return request.auth != null; } function isOwner(userId) { return isSignedIn() && request.auth.uid == userId; } function incoming() { return request.resource.data; } function existing() { return resource.data; } function isValidId(id) { return id is string && id.size() >= 1 && id.size() <= 128 && id.matches('^[a-zA-Z0-9_\\-]+$'); } function isValidMealEntry(data) { return data.userId is string && data.userId == request.auth.uid && data.mealType in ['BREAKFAST', 'LUNCH', 'SNACK', 'DINNER'] && data.mealTimestamp is int && data.mealTimestamp > 0 && data.hungerBefore is int && data.hungerBefore >= 1 && data.hungerBefore <= 10 && data.satietyAfter is int && data.satietyAfter >= 1 && data.satietyAfter <= 10 && (!('note' in data) || data.note == null || (data.note is string && data.note.size() <= 500)) && (!('foodItemsSerialized' in data) || data.foodItemsSerialized == null || (data.foodItemsSerialized is list && data.foodItemsSerialized.size() <= 30 && (data.foodItemsSerialized.size() == 0 || (data.foodItemsSerialized[0] is string && data.foodItemsSerialized[0].size() <= 200)))) && (!('photoUris' in data) || data.photoUris == null || (data.photoUris is list && data.photoUris.size() <= 4 && (data.photoUris.size() == 0 || (data.photoUris[0] is string && data.photoUris[0].size() <= 500)))) && (!('photosBase64' in data) || data.photosBase64 == null || (data.photosBase64 is list && data.photosBase64.size() <= 4 && (data.photosBase64.size() == 0 || (data.photosBase64[0] is string && data.photosBase64[0].size() <= 200000)))) && (!('updatedAt' in data) || data.updatedAt == null || (data.updatedAt is timestamp && data.updatedAt <= request.time)); } function isValidCustomFood(data) { return data.userId is string && data.userId == request.auth.uid && data.name is string && data.name.size() >= 1 && data.name.size() <= 100 && data.defaultUnit in ['g', 'ml', 'portion'] && (!('updatedAt' in data) || data.updatedAt == null || (data.updatedAt is timestamp && data.updatedAt <= request.time)); } match /users/{userId} { allow get: if isOwner(userId); match /meals/{mealId} { allow get, list: if isOwner(userId); allow create: if isOwner(userId) && isValidId(mealId) && incoming().keys().hasAll(['userId', 'mealType', 'mealTimestamp', 'hungerBefore', 'satietyAfter', 'createdAt']) && incoming().keys().hasOnly(['userId', 'mealType', 'mealTimestamp', 'hungerBefore', 'satietyAfter', 'note', 'foodItemsSerialized', 'photoUris', 'photosBase64', 'createdAt', 'updatedAt']) && incoming().createdAt is timestamp && incoming().createdAt <= request.time && isValidMealEntry(incoming()); allow update: if isOwner(userId) && isValidId(mealId) && incoming().userId == existing().userId && incoming().createdAt == existing().createdAt && isValidMealEntry(incoming()) && incoming().diff(existing()).affectedKeys().hasOnly(['mealType', 'mealTimestamp', 'hungerBefore', 'satietyAfter', 'note', 'foodItemsSerialized', 'photoUris', 'photosBase64', 'updatedAt']); allow delete: if isOwner(userId) && isValidId(mealId); } match /custom_foods/{foodId} { allow get, list: if isOwner(userId); allow create: if isOwner(userId) && isValidId(foodId) && incoming().keys().hasAll(['userId', 'name', 'defaultUnit', 'createdAt']) && incoming().keys().hasOnly(['userId', 'name', 'defaultUnit', 'createdAt', 'updatedAt']) && incoming().createdAt is timestamp && incoming().createdAt <= request.time && isValidCustomFood(incoming()); allow update: if isOwner(userId) && isValidId(foodId) && incoming().userId == existing().userId && incoming().createdAt == existing().createdAt && isValidCustomFood(incoming()) && incoming().diff(existing()).affectedKeys().hasOnly(['name', 'defaultUnit', 'updatedAt']); allow delete: if isOwner(userId) && isValidId(foodId); } match /settings/{settingId} { allow get, list: if isOwner(userId); allow create, update: if isOwner(userId) && isValidId(settingId) && incoming().userId == request.auth.uid && incoming().themeMode in ['DARK', 'LIGHT', 'SYSTEM'] && (!('updatedAt' in incoming()) || incoming().updatedAt is timestamp); allow delete: if isOwner(userId) && isValidId(settingId); } } } }