با مجموعهها، منظم بمانید
ذخیره و طبقهبندی محتوا براساس اولویتهای شما.
Firebase Security Rules
plat_ios
plat_android
plat_web
plat_flutter
plat_node
از قوانین امنیتی Firebase انعطاف پذیر و قابل توسعه ما برای ایمن سازی داده های خود در Cloud Firestore ، Firebase Realtime Database و Cloud Storage استفاده کنید.
Firebase Security Rules بین داده های شما و کاربران مخرب قرار می گیرد. میتوانید قوانین ساده یا پیچیدهای بنویسید که از دادههای برنامه شما تا سطحی از جزئیات که برنامه خاص شما نیاز دارد محافظت کند.
Firebase Security Rules از زبانهای پیکربندی انعطافپذیر و قابل توسعه برای تعیین اینکه کاربران شما میتوانند برای Realtime Database ، Cloud Firestore و Cloud Storage به چه دادههایی دسترسی داشته باشند، استفاده میکنند. Firebase Realtime Database Security Rules از JSON در تعاریف قوانین استفاده می کنند، در حالی که Cloud Firestore Security Rules و Firebase Security Rules برای Cloud Storage از زبان منحصر به فردی استفاده می کنند که برای تطبیق ساختارهای پیچیده تری برای قوانین خاص ساخته شده است.
درباره نحوه تنظیم Rules برای محصولات Firebase خاصی که در برنامه خود استفاده میکنید، و اینکه چگونه رفتار Rules در محصولات Firebase متفاوت است، بیشتر بیاموزید.
شروع کنید
قابلیت های کلیدی
انعطاف پذیری | قوانین سفارشی بنویسید که برای ساختار و رفتار برنامه شما منطقی باشد. Rules از زبان هایی استفاده می کنند که به شما امکان می دهد از داده های خود برای مجوز دسترسی استفاده کنید. |
دانه بندی | قوانین شما می تواند به اندازه نیاز شما گسترده یا محدود باشد. |
امنیت مستقل | از آنجایی که Rules خارج از برنامه شما (در کنسول Firebase یا Firebase CLI) تعریف شدهاند، کلاینتها مسئول اجرای امنیت نیستند، اشکالات دادهها را به خطر نمیاندازند و دادههای شما همیشه محافظت میشوند. |
چگونه کار می کنند؟
Firebase Security Rules با تطبیق یک الگو با مسیرهای پایگاه داده و سپس اعمال شرایط سفارشی برای اجازه دسترسی به داده ها در آن مسیرها کار می کنند. همه Rules در محصولات Firebase دارای یک جزء تطبیق مسیر و یک عبارت شرطی هستند که اجازه دسترسی خواندن یا نوشتن را می دهد. باید برای هر محصول Firebase که در برنامه خود استفاده می کنید، Rules تعریف کنید.
برای Cloud Firestore و Cloud Storage ، Rules از نحو زیر استفاده میکند:
service <<name>> {
// Match the resource path.
match <<path>> {
// Allow the request if the following conditions are true.
allow <<methods>> : if <<condition>>
}
}
برای Realtime Database ، Rules مبتنی بر JSON از نحو زیر استفاده می کنند:
{
"rules": {
"<<path>>": {
// Allow the request if the condition for each method is true.
".read": <<condition>>,
".write": <<condition>>
}
}
}
Rules به عنوان عبارات OR
، نه عبارات AND
اعمال می شوند. در نتیجه، اگر چندین قاعده با یک مسیر مطابقت داشته باشند، و هر یک از شرایط منطبق اجازه دسترسی را بدهد، Rules به دادههای آن مسیر دسترسی میدهد. بنابراین، اگر یک قانون گسترده اجازه دسترسی به داده ها را می دهد، نمی توانید با یک قانون خاص تر محدودیت ایجاد کنید. با این حال، می توانید با اطمینان از همپوشانی بیش از حد Rules خود، از این مشکل جلوگیری کنید. پرچم Firebase Security Rules در مسیرهای منطبق با شما به عنوان هشدارهای کامپایلر همپوشانی دارد.
Firebase Security Rules همچنین میتواند از Authentication برای اعطای مجوزهای مبتنی بر کاربر استفاده کند و شرایطی که شما تعیین میکنید میتواند بسیار ابتدایی یا بسیار پیچیده باشد. قبل از شروع نوشتن Rules ، درباره زبان و رفتار Rules بیشتر بدانید.
مسیر پیاده سازی
| SDK های محصول را یکپارچه کنید | Cloud Firestore ، Cloud Storage یا Realtime Database برای برنامه خود تنظیم کنید. |
| Firebase Security Rules خود را بنویسید | درباره نحوه عملکرد Rules و تنظیم برخی از Rules اساسی بیشتر بدانید |
| Firebase Security Rules خود را آزمایش کنید | از Realtime Database و شبیه سازهای Cloud Firestore برای آزمایش رفتار برنامه و تأیید قوانین خود قبل از استقرار آنها در تولید استفاده کنید. |
| Firebase Security Rules خود را اجرا کنید | از کنسول Firebase یا Firebase CLI برای استقرار قوانین خود در تولید استفاده کنید. |
مراحل بعدی
جز در مواردی که غیر از این ذکر شده باشد،محتوای این صفحه تحت مجوز Creative Commons Attribution 4.0 License است. نمونه کدها نیز دارای مجوز Apache 2.0 License است. برای اطلاع از جزئیات، به خطمشیهای سایت Google Developers مراجعه کنید. جاوا علامت تجاری ثبتشده Oracle و/یا شرکتهای وابسته به آن است.
تاریخ آخرین بهروزرسانی 2025-07-25 بهوقت ساعت هماهنگ جهانی.
[null,null,["تاریخ آخرین بهروزرسانی 2025-07-25 بهوقت ساعت هماهنگ جهانی."],[],[],null,["Firebase Security Rules \nplat_ios plat_android plat_web plat_flutter plat_node \nUse our flexible, extensible Firebase Security Rules to\nsecure your data in Cloud Firestore, Firebase Realtime Database, and\nCloud Storage.\n\nFirebase Security Rules stand between your data and malicious users. You can write simple or\ncomplex rules that protect your app's data to the level of granularity that\nyour specific app requires.\n\nFirebase Security Rules leverage\nextensible, flexible configuration languages to define what data your users\ncan access for Realtime Database, Cloud Firestore, and Cloud Storage.\nFirebase Realtime Database Security Rules leverage JSON in rule definitions, while\nCloud Firestore Security Rules and Firebase Security Rules for Cloud Storage leverage a unique\nlanguage built to accommodate more complex rules-specific structures.\n\nLearn more about how to set up Rules for the specific Firebase products\nyou use in your app, and how Rules behavior differs across Firebase\nproducts.\n\n[Get started](/docs/rules/get-started)\n\nKey capabilities\n\n|----------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| Flexibility | Write custom rules that make sense for your app's structure and behavior. Rules use languages that allow you to leverage your own data to authorize access. |\n| Granularity | Your rules can be as broad or as narrow as you need. |\n| Independent security | Because Rules are defined outside of your app (in the Firebase console or Firebase CLI), clients aren't responsible for enforcing security, bugs don't compromise data, and your data is always protected. |\n\nHow do they work?\n\nFirebase Security Rules work by matching a pattern against database paths, and then applying\ncustom conditions to allow access to data at those paths. All Rules\nacross Firebase products have a path-matching component and a conditional\nstatement allowing read or write access. You must define Rules for\neach Firebase product you use in your app.\n\nFor Cloud Firestore and Cloud Storage, Rules use the following\nsyntax: \n\n service \u003c\u003cname\u003e\u003e {\n // Match the resource path.\n match \u003c\u003cpath\u003e\u003e {\n // Allow the request if the following conditions are true.\n allow \u003c\u003cmethods\u003e\u003e : if \u003c\u003ccondition\u003e\u003e\n }\n }\n\nFor Realtime Database, JSON-based Rules use the following syntax: \n\n {\n \"rules\": {\n \"\u003c\u003cpath\u003e\u003e\": {\n // Allow the request if the condition for each method is true.\n \".read\": \u003c\u003ccondition\u003e\u003e,\n \".write\": \u003c\u003ccondition\u003e\u003e\n }\n }\n }\n\nRules are applied as `OR` statements, not `AND` statements.\nConsequently, if multiple rules match a path, and any of the matched\nconditions grants access, Rules grant access to the data at that\npath. Therefore, if a broad rule grants access to data, you can't restrict with\na more specific rule. You can, however, avoid this problem by making sure your\nRules don't overlap too much. Firebase Security Rules flag overlaps in your\nmatched paths as compiler warnings.\n\nFirebase Security Rules can also leverage Authentication to grant user-based permissions, and the\nconditions you set can be very basic or incredibly complex. Learn more\nabout Rules [language](/docs/rules/rules-language) and [behavior](/docs/rules/rules-behavior)\nbefore you start writing Rules.\n\nImplementation path\n\n|---|-------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------|\n| | Integrate the product SDKs | Set up [Cloud Firestore](/docs/firestore), [Cloud Storage](/docs/storage), or [Realtime Database](/docs/database) for your app. |\n| | Write your Firebase Security Rules | Learn more about [how Rules work](/docs/rules/rules-behavior) and [set up some basic Rules](/docs/rules/basics) |\n| | Test your Firebase Security Rules | Use the Realtime Database and Cloud Firestore emulators to test your app's behavior and validate your rules before you deploy them to production. |\n| | Deploy your Firebase Security Rules | Use the Firebase console or the Firebase CLI to deploy your rules to production. |\n\nNext steps\n\n- [Understand the Firebase Security Rules language](/docs/rules/rules-language).\n- Learn more about [how Firebase Security Rules work](/docs/rules/rules-behavior).\n- Explore the [common mistakes you should avoid](/docs/rules/insecure-rules)."]]