> For the complete documentation index, see [llms.txt](https://textav.gitbook.io/firebase-react-notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://textav.gitbook.io/firebase-react-notes/storage/security.md).

# Security

## NoSQL Schema

Recommended schema is having the userId somewhere in the file path, and updating the Security Rules like so:

```javascript
// Grants a user access to a node matching their user ID
service firebase.storage {
  match /b/{bucket}/o {
    // Files look like: "user/<UID>/path/to/file.txt"
    match /user/{userId}/{allPaths=**} {
      allow read, write: if request.auth.uid == userId;
    }
  }
}
```

## Sub-collections

If you use something like `{allPaths=**}`, this also includes subcollections. If you have set up conditionals based on the `response` object, you will need to ensure that the condition exists at every level. For example:

```
// Grants a user access to a node matching their user ID
service firebase.firestore {
  match /document/{database}/tables {
    match /user/{userId}/{allPaths=**} {
      allow read, write: if response.data.role == "ADMIN";
    }
  }
}
```

This will expect even subcollections that matched with `{allPaths=**}` to have a field `role`. In order to get around this you want to set a full path:

```
// Grants a user access to a node matching their user ID
service firebase.firestore {
  match /document/{database}/tables {
    match /user/{userId}/{allPaths=**} {
      allow read, write: if get(/document/$(database)/tables/user/$(request.auth.uid)).data.role == "ADMIN";
    }
  }
}
```

<https://medium.com/@khreniak/cloud-firestore-security-rules-basics-fac6b6bea18e>

{% embed url="<https://firebase.google.com/docs/storage/security/start>" %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://textav.gitbook.io/firebase-react-notes/storage/security.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
