为什么我的 firebase 存储安全规则没有执行并允许用户上传超过限制的图像?
Why are my firebase storage security rules are not enforcing and letting users upload images over the limit?
我已将我的存储安全规则设置为仅允许经过身份验证的用户使用以下规则 post 小于 3mb 的图像:
rules_version = '2';
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
// Only allow uploads of any image file that's less than 3MB
allow write: if request.resource.size < 3 * 1024 * 1024
&& request.resource.contentType.matches('image/.*');
allow read, write: if request.auth != null;
}
}
}
但是,我刚刚从我的客户端进行了测试,我能够上传一张 14mb 的图片。我已经给了足够的时间来设置安全规则。
我是如何绕过这条规则的?
您还有 allow read, write: if request.auth != null;
覆盖了前一个。尝试从第二行删除写入:
match /{allPaths=**} {
// Only allow uploads of any image file that's less than 3MB
allow write: if request.resource.size < 3 * 1024 * 1024
&& request.resource.contentType.matches('image/.*');
allow read: if request.auth != null;
}
请参阅here:
In the case where multiple allow expressions match a request, the access is allowed if any of the conditions is true
所以在你的情况下:
allow read, write: if request.auth != null;
让用户写入任意大小的文件。
我想这是你想要的规则:
allow write: if request.auth != null &&
request.resource.size < 3 * 1024 * 1024 &&
request.resource.contentType.matches('image/.*');
allow read: if request.auth != null;
虽然其他答案为您提供了可以使用的安全规则,但最好补充一点,您可以使用 Firebase rules playground 测试您的安全规则。使用此工具,您可以在针对事件(创建、读取、更新、删除)进行测试时准确判断每个规则的解决方案。
我测试了你的安全规则并收到了这个输出:
如您所见,虽然您的文件大小限制规则运行良好,但更广泛的 allow write: if request.auth != null;
规则仍在让您的请求通过。这是其他答案指出的内容以及 documentation:
中包含的内容
If any of the “allow” rules for the method are satisfied, the request is allowed. Additionally, if a broader rule grants access, rules grant access and ignore any more granular rules that might limit access.
我已将我的存储安全规则设置为仅允许经过身份验证的用户使用以下规则 post 小于 3mb 的图像:
rules_version = '2';
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
// Only allow uploads of any image file that's less than 3MB
allow write: if request.resource.size < 3 * 1024 * 1024
&& request.resource.contentType.matches('image/.*');
allow read, write: if request.auth != null;
}
}
}
但是,我刚刚从我的客户端进行了测试,我能够上传一张 14mb 的图片。我已经给了足够的时间来设置安全规则。
我是如何绕过这条规则的?
您还有 allow read, write: if request.auth != null;
覆盖了前一个。尝试从第二行删除写入:
match /{allPaths=**} {
// Only allow uploads of any image file that's less than 3MB
allow write: if request.resource.size < 3 * 1024 * 1024
&& request.resource.contentType.matches('image/.*');
allow read: if request.auth != null;
}
请参阅here:
In the case where multiple allow expressions match a request, the access is allowed if any of the conditions is true
所以在你的情况下:
allow read, write: if request.auth != null;
让用户写入任意大小的文件。
我想这是你想要的规则:
allow write: if request.auth != null &&
request.resource.size < 3 * 1024 * 1024 &&
request.resource.contentType.matches('image/.*');
allow read: if request.auth != null;
虽然其他答案为您提供了可以使用的安全规则,但最好补充一点,您可以使用 Firebase rules playground 测试您的安全规则。使用此工具,您可以在针对事件(创建、读取、更新、删除)进行测试时准确判断每个规则的解决方案。
我测试了你的安全规则并收到了这个输出:
如您所见,虽然您的文件大小限制规则运行良好,但更广泛的 allow write: if request.auth != null;
规则仍在让您的请求通过。这是其他答案指出的内容以及 documentation:
If any of the “allow” rules for the method are satisfied, the request is allowed. Additionally, if a broader rule grants access, rules grant access and ignore any more granular rules that might limit access.