|
| 1 | + |
| 2 | +rules_version = '2'; |
| 3 | + |
| 4 | +service cloud.firestore { |
| 5 | + match /databases/{database}/documents { |
| 6 | + |
| 7 | + // ====================================== |
| 8 | + // RÈGLES POUR LES CAMPAGNES |
| 9 | + // ====================================== |
| 10 | + match /campaigns/{campaignId} { |
| 11 | + // Lecture : uniquement le propriétaire |
| 12 | + allow read: if request.auth != null && |
| 13 | + request.auth.uid == resource.data.userId; |
| 14 | + |
| 15 | + // Création : utilisateur authentifié + structure valide |
| 16 | + allow create: if request.auth != null && |
| 17 | + request.auth.uid == request.resource.data.userId && |
| 18 | + isValidCampaign(request.resource.data); |
| 19 | + |
| 20 | + // Mise à jour : uniquement le propriétaire + structure valide |
| 21 | + allow update: if request.auth != null && |
| 22 | + request.auth.uid == resource.data.userId && |
| 23 | + request.auth.uid == request.resource.data.userId && |
| 24 | + isValidCampaign(request.resource.data); |
| 25 | + |
| 26 | + // Suppression : uniquement le propriétaire |
| 27 | + allow delete: if request.auth != null && |
| 28 | + request.auth.uid == resource.data.userId; |
| 29 | + } |
| 30 | + |
| 31 | + // ====================================== |
| 32 | + // RÈGLES POUR LES AFFILIÉS |
| 33 | + // ====================================== |
| 34 | + match /affiliates/{affiliateId} { |
| 35 | + // Lecture : propriétaire de la campagne associée |
| 36 | + allow read: if request.auth != null && |
| 37 | + request.auth.uid == resource.data.userId; |
| 38 | + |
| 39 | + // Création : utilisateur authentifié + campagne valide + structure valide |
| 40 | + allow create: if request.auth != null && |
| 41 | + request.auth.uid == request.resource.data.userId && |
| 42 | + isValidAffiliate(request.resource.data) && |
| 43 | + isOwnerOfCampaign(request.resource.data.campaignId); |
| 44 | + |
| 45 | + // Mise à jour : propriétaire + structure valide |
| 46 | + allow update: if request.auth != null && |
| 47 | + request.auth.uid == resource.data.userId && |
| 48 | + request.auth.uid == request.resource.data.userId && |
| 49 | + isValidAffiliate(request.resource.data); |
| 50 | + |
| 51 | + // Suppression : propriétaire |
| 52 | + allow delete: if request.auth != null && |
| 53 | + request.auth.uid == resource.data.userId; |
| 54 | + } |
| 55 | + |
| 56 | + // ====================================== |
| 57 | + // RÈGLES POUR LES CLICS (Lecture seule pour les propriétaires) |
| 58 | + // ====================================== |
| 59 | + match /clicks/{clickId} { |
| 60 | + // Lecture : propriétaire de la campagne associée |
| 61 | + allow read: if request.auth != null && |
| 62 | + isOwnerOfCampaign(resource.data.campaignId); |
| 63 | + |
| 64 | + // Création : limitée aux fonctions serveur (via service account) |
| 65 | + allow create: if false; // Sera géré par Cloud Functions |
| 66 | + |
| 67 | + // Pas de mise à jour ou suppression directe |
| 68 | + allow update, delete: if false; |
| 69 | + } |
| 70 | + |
| 71 | + // ====================================== |
| 72 | + // RÈGLES POUR LES CONVERSIONS (Très restrictives) |
| 73 | + // ====================================== |
| 74 | + match /conversions/{conversionId} { |
| 75 | + // Lecture : propriétaire de la campagne |
| 76 | + allow read: if request.auth != null && |
| 77 | + isOwnerOfCampaign(resource.data.campaignId); |
| 78 | + |
| 79 | + // Création : uniquement via Cloud Functions |
| 80 | + allow create: if false; // Sera géré par Cloud Functions sécurisées |
| 81 | + |
| 82 | + // Mise à jour : uniquement pour vérification (propriétaire) |
| 83 | + allow update: if request.auth != null && |
| 84 | + isOwnerOfCampaign(resource.data.campaignId) && |
| 85 | + onlyUpdatingVerificationStatus(request.resource.data, resource.data); |
| 86 | + |
| 87 | + // Suppression : propriétaire seulement |
| 88 | + allow delete: if request.auth != null && |
| 89 | + isOwnerOfCampaign(resource.data.campaignId); |
| 90 | + } |
| 91 | + |
| 92 | + // ====================================== |
| 93 | + // RÈGLES POUR LES LIENS COURTS |
| 94 | + // ====================================== |
| 95 | + match /shortLinks/{linkId} { |
| 96 | + // Lecture : propriétaire de la campagne ou accès public pour redirection |
| 97 | + allow read: if request.auth != null && |
| 98 | + isOwnerOfCampaign(resource.data.campaignId); |
| 99 | + |
| 100 | + // Lecture publique limitée (juste pour la redirection) |
| 101 | + allow get: if true; // Nécessaire pour le tracking public |
| 102 | + |
| 103 | + // Création : propriétaire de la campagne + structure valide |
| 104 | + allow create: if request.auth != null && |
| 105 | + isOwnerOfCampaign(request.resource.data.campaignId) && |
| 106 | + isValidShortLink(request.resource.data); |
| 107 | + |
| 108 | + // Mise à jour : propriétaire (pour compteur de clics) |
| 109 | + allow update: if request.auth != null && |
| 110 | + isOwnerOfCampaign(resource.data.campaignId) && |
| 111 | + onlyUpdatingClickCount(request.resource.data, resource.data); |
| 112 | + |
| 113 | + // Suppression : propriétaire |
| 114 | + allow delete: if request.auth != null && |
| 115 | + isOwnerOfCampaign(resource.data.campaignId); |
| 116 | + } |
| 117 | + |
| 118 | + // ====================================== |
| 119 | + // FONCTIONS DE VALIDATION |
| 120 | + // ====================================== |
| 121 | + |
| 122 | + // Vérifier si l'utilisateur est propriétaire d'une campagne |
| 123 | + function isOwnerOfCampaign(campaignId) { |
| 124 | + return exists(/databases/$(database)/documents/campaigns/$(campaignId)) && |
| 125 | + get(/databases/$(database)/documents/campaigns/$(campaignId)).data.userId == request.auth.uid; |
| 126 | + } |
| 127 | + |
| 128 | + // Validation structure campagne |
| 129 | + function isValidCampaign(data) { |
| 130 | + return data.keys().hasAll(['name', 'description', 'targetUrl', 'userId', 'isActive']) && |
| 131 | + data.name is string && data.name.size() > 0 && data.name.size() <= 100 && |
| 132 | + data.description is string && data.description.size() <= 500 && |
| 133 | + data.targetUrl is string && data.targetUrl.matches('https?://.*') && |
| 134 | + data.userId is string && |
| 135 | + data.isActive is bool && |
| 136 | + (!data.keys().hasAny(['createdAt', 'updatedAt']) || |
| 137 | + (data.createdAt is timestamp && data.updatedAt is timestamp)); |
| 138 | + } |
| 139 | + |
| 140 | + // Validation structure affilié |
| 141 | + function isValidAffiliate(data) { |
| 142 | + return data.keys().hasAll(['name', 'email', 'commissionRate', 'campaignId', 'userId', 'trackingCode', 'isActive']) && |
| 143 | + data.name is string && data.name.size() > 0 && data.name.size() <= 100 && |
| 144 | + data.email is string && data.email.matches('.*@.*\\..*') && |
| 145 | + data.commissionRate is number && data.commissionRate >= 0 && data.commissionRate <= 100 && |
| 146 | + data.campaignId is string && |
| 147 | + data.userId is string && |
| 148 | + data.trackingCode is string && data.trackingCode.size() > 10 && |
| 149 | + data.isActive is bool; |
| 150 | + } |
| 151 | + |
| 152 | + // Validation structure lien court |
| 153 | + function isValidShortLink(data) { |
| 154 | + return data.keys().hasAll(['shortCode', 'campaignId', 'affiliateId', 'targetUrl']) && |
| 155 | + data.shortCode is string && data.shortCode.size() == 6 && |
| 156 | + data.campaignId is string && |
| 157 | + data.affiliateId is string && |
| 158 | + data.targetUrl is string && data.targetUrl.matches('https?://.*') && |
| 159 | + data.clickCount is number && data.clickCount >= 0; |
| 160 | + } |
| 161 | + |
| 162 | + // Vérifier que seul le statut de vérification change |
| 163 | + function onlyUpdatingVerificationStatus(newData, oldData) { |
| 164 | + return newData.diff(oldData).affectedKeys().hasOnly(['verified']) && |
| 165 | + newData.verified is bool; |
| 166 | + } |
| 167 | + |
| 168 | + // Vérifier que seul le compteur de clics change |
| 169 | + function onlyUpdatingClickCount(newData, oldData) { |
| 170 | + return newData.diff(oldData).affectedKeys().hasOnly(['clickCount']) && |
| 171 | + newData.clickCount is number && |
| 172 | + newData.clickCount >= oldData.clickCount; |
| 173 | + } |
| 174 | + |
| 175 | + // ====================================== |
| 176 | + // PROTECTION CONTRE LES ATTAQUES |
| 177 | + // ====================================== |
| 178 | + |
| 179 | + // Bloquer toute autre collection non définie |
| 180 | + match /{document=**} { |
| 181 | + allow read, write: if false; |
| 182 | + } |
| 183 | + } |
| 184 | +} |
0 commit comments