我不明白如何设置一天内上传文件的限制。我希望用户每天最多发布 10 张照片。在数据库端我放置了一个增量计数器。如果达到一定大小,则不允许用户发布其他内容。但在存储方面这是不可能的。攻击者可以不受限制地发布他想要的所有文件。有没有解决办法来防止这种情况发生?提前致谢。目前我的安全规则是:
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
}
}
}
最佳答案
嗯,有一种非常简单的方法可以做到这一点,而且有正确的方法。
只允许在特定时间段内上传一定数量的文件的黑客方法是使用某些数字属性来命名文件:例如 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()
}
从长远来看,解决方案是能够从存储中引用数据库数据,反之亦然。不幸的是,这个世界还没有到来,但我们正在努力实现这一目标。
关于firebase存储-防止垃圾邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39664404/