问题
I am just getting started with Firebase security and probably I am missing something. I need to validate that after 'notification' the path(key) is 15 (later I will update 15 to be regex using matching method, but currently I am struggling enforcing this simple case):
{
"rules": {
".write": true,
"notifications": {
"$year": {
".validate": "newData.val() === '/15'",
}
}
}
}
I am testing in simulator with the following:
/notifications/15
Data I am posting:
{ "foo": "bar" }
The Firebase simulator returns:
Attempt to write {"foo":"bar"} to /notifications/15 with auth={"provider":"anonymous","uid":"a5e08499-c41d-47d0-9df4-64d9af802755"}
/:.write: "true"
=> true
/notifications/15:.validate: "newData.val() === '/15'"
=> false
Validation failed.
Write was denied.
回答1:
If you're writing a value to /notifications/15
then newData.val()
will be that value, not the key.
I don't think there is a way to test keys dynamically.
来源:https://stackoverflow.com/questions/32933181/validating-a-firebase-key