Q: Firebase Database, Cloud Functions, Rules How to restrict write access to /lastmodified table in database named /functions-project-12345 only to cloud function, read & write access to table /chat to everyone
/functions-project-12345
    /lastmodified: 1234567890
    /chat
        /key-123456
            username: "Mat"
            text: "Hey Bob!"
        /key-123457
            username: "Bob"
            text: "Hey Mat"


A: Cloud Functions run with administrative privileges, which means they bypass the security rules of your database. Knowing that, it is quite simple to secure the database to your requirements:
{
  "rules": {
    ".write": false,
    "chat": {
      ".write": true
    }
  }
}


A: Have you seen chapter Authenticate with limited privileges here: https://firebase.google.com/docs/database/admin/start ?
It might be what you need.
Basically you need to use Admin SDK and initialize it with a specific uid and service key. Otherwise (namely, if you access database via reference from event which triggered the function), your function will have the same uid (and same access rights) as the client who created the event.
