Skip to content

fix issue where storage init does not get rules from console - #9809

Merged
aalej merged 5 commits into
mainfrom
aalej_storage-init
Jan 27, 2026
Merged

aalej merged 5 commits into
mainfrom
aalej_storage-init

Conversation

@aalej

@aalej aalej commented Jan 26, 2026 •

Copy link
Copy Markdown
Contributor

Description

Fixes #8170

Scenarios Tested

firebase init storage --project demo-project creates a storage.rules file like(uses the default rules template):

rules_version = '2';

// Craft rules based on data in your Firestore database
// allow write: if firestore.get(
//    /databases/(default)/documents/users/$(request.auth.uid)).data.isAdmin;
service firebase.storage {
  match /b/{bucket}/o {
    match /{allPaths=**} {
      allow read, write: if false;
    }
  }
}

firebase init storage --project amalej-8170 creates a storage.rules file like(downloads the rules from storage):

rules_version = '2';

// Craft rules based on data in your Firestore database
// allow write: if firestore.get(
//    /databases/(default)/documents/users/$(request.auth.uid)).data.isAdmin;


// this is the rules from project `amalej-8170`
// this is updated again

service firebase.storage {
  match /b/{bucket}/o {
    match /{allPaths=**} {
      allow read, write: if false;
    }
  }
}

Sample Commands

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @aalej, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request resolves an issue where the firebase init storage command would not correctly retrieve and apply existing Storage Security Rules from the Firebase console. The changes introduce a mechanism to fetch these rules, ensuring that the local storage.rules file accurately reflects the deployed configuration. This enhancement improves the developer experience by maintaining consistency between local development setup and the Firebase project's existing security policies.

Highlights

  • Enhanced Ruleset Retrieval: The getLatestRulesetName function in src/gcp/rules.ts now supports an optional resourceName parameter, allowing for more specific retrieval of rulesets, such as those tied to a particular storage bucket.
  • Automatic Storage Rules Download: During firebase init storage, the tool will now attempt to download existing Storage Security Rules from the Firebase console for the specified project. If rules are found, they will be used as the default content for the storage.rules file, preventing the creation of a generic default file when custom rules already exist.
  • Refactored Storage Initialization Logic: The core storage initialization logic has been refactored, moving src/init/features/storage.ts to src/init/features/storage/index.ts and introducing a new module src/init/features/storage/rules.ts to encapsulate the logic for fetching rules from the console.
  • Improved Test Coverage: New tests have been added in src/init/features/storage/index.spec.ts to specifically cover the scenario where existing Storage Security Rules are downloaded and used during the initialization process.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request addresses an issue where firebase init storage would not fetch existing rules from the console, instead always creating a default rules file. The changes correctly implement fetching rules for the project's default storage bucket and using them if they exist. This is achieved by modifying getLatestRulesetName to accept a resource name, and adding new logic in the storage initialization feature to perform the fetch. The tests have also been updated to cover this new behavior.

My main feedback is to improve the error handling around fetching the rules. Currently, any failure during the fetch process will cause the init command to crash. I've suggested a change to catch these errors and gracefully fall back to the default rules, which will provide a better user experience.

Comment on lines +33 to +39
if (setup.projectId) {
const downloadedRules = await getRulesFromConsole(setup.projectId);
if (downloadedRules) {
info.rules = downloadedRules;
logger.info(`Downloaded the existing Storage Security Rules from the Firebase console`);
}
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The call to getRulesFromConsole can reject for various reasons (e.g., network issues, permissions, or if the ruleset on the backend is in an unexpected state). If it rejects, it will crash the firebase init process, providing a poor user experience. It would be more robust to wrap this call in a try...catch block. If fetching the rules fails, you can log the error for debugging purposes and gracefully fall back to using the default rules template. This ensures the init process can continue successfully even if existing rules can't be retrieved.

Suggested change
if (setup.projectId) {
const downloadedRules = await getRulesFromConsole(setup.projectId);
if (downloadedRules) {
info.rules = downloadedRules;
logger.info(`Downloaded the existing Storage Security Rules from the Firebase console`);
}
}
if (setup.projectId) {
try {
const downloadedRules = await getRulesFromConsole(setup.projectId);
if (downloadedRules) {
info.rules = downloadedRules;
logger.info(`Downloaded the existing Storage Security Rules from the Firebase console`);
}
} catch (e: any) {
logger.debug("Could not retrieve storage rules, falling back to default rules.", e);
// We can ignore this error and fall back to the default rules.
}
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we might actually want to crash in these scenarios?

For example, if a user already has an existing local config but they want to get the rules from the Storage console, they run firebase init storage. In this scenario, if we default back to the default rules template, we would overwrite their existing one with a default rules template.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree, i think this is fine as is. Swallowing this error is a bit aggressive.

@aalej
aalej requested a review from joehan January 26, 2026 11:58
@aalej
aalej enabled auto-merge (squash) January 27, 2026 07:57
@aalej
aalej merged commit 6f4e922 into main Jan 27, 2026
@aalej
aalej deleted the aalej_storage-init branch January 27, 2026 08:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Storage security rules is always overwritten when I call firebase init storage

2 participants