firebase 存储-防止垃圾邮件
firebase storage-prevent spam
我不明白如何设置一天上传文件的限制。我希望用户每天最多发布 10 张照片。在数据库端,我放了一个增量计数器。如果它达到一定大小,则不允许用户 post 其他内容。但在存储方面这是不可能的。攻击者可以不受限制地发布他想要的所有文件。有没有办法防止这种情况发生?提前致谢。目前我的安全规则是:
service firebase.storage {
match /b/projectid/o {
match /Photo/{user}/{photo}/image.jpg {
allow write: if request.auth != null &&
request.auth.uid == user && (
request.resource.size < 5 * 1024 * 1024 && photo.size() < 32 ||
request.resource == null);
allow read: if request.auth != null &&
request.auth.uid == user
}
}
}
好吧,有一个非常简单的方法,也有正确的方法。
只允许在特定时间段内上传特定数量的文件的 hacky 方法是用一些数字 属性 命名文件:比如 users/{userid}/0.jpg
到 users/{userid}/9.jpg
(10 张照片)。
您可以编写如下规则来检查:
// Match all filenames like 0.jpg
match /users/{userId}/{photoId} {
allow write: if photoId.matches('^\d\.jpg$')
}
如果您需要比数量级更多的粒度,您可以这样做:
// Match all filenames like YYY.jpg where YYY is a number less than XXX
match /users/{userId}/{photoId} {
allow write: if int(photoId.split('\.')[0]) < XXX
}
虽然这只解决了一半的问题:我们可以限制文件的数量,但如果用户只想上传文件怎么办?幸运的是,我们可以编写一个规则来防止最终用户覆盖他们的文件(尽管我们必须排除删除),或者在给定的时间段内。让我们一起探索:
// Allow files to be overwritten once a day, written if there's nothing there, or deleted as often as desired
match /users/{userId}/{photoId} {
allow write: if request.time > resource.timeCreated + duration.value(1, "d") || resource.size == 0 || request.resource.size == 0
}
这些可以组合成函数:
function isAllowedPhotoId(photoId) {
return int(photoId.split('\.')[0]) < XXX
}
function canOverwritePhoto() {
return request.time > resource.timeCreated + duration.value(1, "d") || resource.size == 0 || request.resource.size == 0
}
match /users/{userId}/{photoId} {
allow write: if isAllowedPhotoId(photoId) && canOverwritePhoto()
}
从长远来看,解决方案是能够从存储中引用数据库数据,反之亦然。不幸的是,那个世界还没有到来,但我们正在努力实现它。
我不明白如何设置一天上传文件的限制。我希望用户每天最多发布 10 张照片。在数据库端,我放了一个增量计数器。如果它达到一定大小,则不允许用户 post 其他内容。但在存储方面这是不可能的。攻击者可以不受限制地发布他想要的所有文件。有没有办法防止这种情况发生?提前致谢。目前我的安全规则是:
service firebase.storage {
match /b/projectid/o {
match /Photo/{user}/{photo}/image.jpg {
allow write: if request.auth != null &&
request.auth.uid == user && (
request.resource.size < 5 * 1024 * 1024 && photo.size() < 32 ||
request.resource == null);
allow read: if request.auth != null &&
request.auth.uid == user
}
}
}
好吧,有一个非常简单的方法,也有正确的方法。
只允许在特定时间段内上传特定数量的文件的 hacky 方法是用一些数字 属性 命名文件:比如 users/{userid}/0.jpg
到 users/{userid}/9.jpg
(10 张照片)。
您可以编写如下规则来检查:
// Match all filenames like 0.jpg
match /users/{userId}/{photoId} {
allow write: if photoId.matches('^\d\.jpg$')
}
如果您需要比数量级更多的粒度,您可以这样做:
// Match all filenames like YYY.jpg where YYY is a number less than XXX
match /users/{userId}/{photoId} {
allow write: if int(photoId.split('\.')[0]) < XXX
}
虽然这只解决了一半的问题:我们可以限制文件的数量,但如果用户只想上传文件怎么办?幸运的是,我们可以编写一个规则来防止最终用户覆盖他们的文件(尽管我们必须排除删除),或者在给定的时间段内。让我们一起探索:
// Allow files to be overwritten once a day, written if there's nothing there, or deleted as often as desired
match /users/{userId}/{photoId} {
allow write: if request.time > resource.timeCreated + duration.value(1, "d") || resource.size == 0 || request.resource.size == 0
}
这些可以组合成函数:
function isAllowedPhotoId(photoId) {
return int(photoId.split('\.')[0]) < XXX
}
function canOverwritePhoto() {
return request.time > resource.timeCreated + duration.value(1, "d") || resource.size == 0 || request.resource.size == 0
}
match /users/{userId}/{photoId} {
allow write: if isAllowedPhotoId(photoId) && canOverwritePhoto()
}
从长远来看,解决方案是能够从存储中引用数据库数据,反之亦然。不幸的是,那个世界还没有到来,但我们正在努力实现它。