-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for Firestore security rules (#2010)
This adds support for Google Cloud Firestore security rules.
- Loading branch information
1 parent
ebe363f
commit 9f72258
Showing
18 changed files
with
348 additions
and
5 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
Prism.languages['firestore-security-rules'] = Prism.languages.extend('clike', { | ||
'comment': /\/\/.*/, | ||
'keyword': /\b(?:allow|function|if|match|null|return|rules_version|service)\b/, | ||
'operator': /&&|\|\||[<>!=]=?|[-+*/%=]|\b(?:in|is)\b/, | ||
}); | ||
|
||
delete Prism.languages['firestore-security-rules']['class-name']; | ||
|
||
Prism.languages.insertBefore('firestore-security-rules', 'keyword', { | ||
'path': { | ||
pattern: /(^|[\s(),])(?:\/(?:[\w\xA0-\uFFFF]+|\{[\w\xA0-\uFFFF]+(?:=\*\*)?\}|\$\([\w\xA0-\uFFFF.]+\)))+/, | ||
lookbehind: true, | ||
greedy: true, | ||
inside: { | ||
'variable': { | ||
pattern: /\{[\w\xA0-\uFFFF]+(?:=\*\*)?\}|\$\([\w\xA0-\uFFFF.]+\)/, | ||
inside: { | ||
'operator': /=/, | ||
'keyword': /\*\*/, | ||
'punctuation': /[.$(){}]/ | ||
} | ||
}, | ||
'punctuation': /[/]/ | ||
} | ||
}, | ||
'method': { | ||
// to make the pattern shorter, the actual method names are omitted | ||
pattern: /(\ballow\s+)[a-z]+(?:\s*,\s*[a-z]+)*(?=\s*[:;])/, | ||
lookbehind: true, | ||
alias: 'builtin', | ||
inside: { | ||
'punctuation': /,/ | ||
} | ||
}, | ||
}); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<h2>Full example</h2> | ||
<pre><code>rules_version = '2'; | ||
service cloud.firestore { | ||
|
||
match /databases/{database}/documents { | ||
|
||
// Returns `true` if the requested post is 'published' | ||
// or the user authored the post | ||
function authorOrPublished() { | ||
return resource.data.published == true || request.auth.uid == resource.data.author; | ||
} | ||
|
||
match /{path=**}/posts/{post} { | ||
|
||
// Anyone can query published posts | ||
// Authors can query their unpublished posts | ||
allow list: if authorOrPublished(); | ||
|
||
// Anyone can retrieve a published post | ||
// Authors can retrieve an unpublished post | ||
allow get: if authorOrPublished(); | ||
} | ||
|
||
match /forums/{forumid}/posts/{postid} { | ||
// Only a post's author can write to a post | ||
allow write: if request.auth.uid == resource.data.author; | ||
} | ||
} | ||
|
||
match /databases/{database}/reviews { | ||
// Assign roles to all users and refine access based on user roles | ||
match /some_collection/{document} { | ||
allow read: if get(/databases/$(database)/reviews/users/$(request.auth.uid)).data.role == "Reader" | ||
allow write: if get(/databases/$(database)/reviews/users/$(request.auth.uid)).data.role == "Writer" | ||
} | ||
} | ||
}</code></pre> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
tests/languages/firestore-security-rules/comment_feature.test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// comment | ||
|
||
---------------------------------------------------- | ||
|
||
[ | ||
["comment", "// comment"] | ||
] | ||
|
||
---------------------------------------------------- | ||
|
||
Checks for comments. |
25 changes: 25 additions & 0 deletions
25
tests/languages/firestore-security-rules/keyword_feature.test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
allow | ||
function | ||
if | ||
match | ||
null | ||
return | ||
rules_version | ||
service | ||
|
||
---------------------------------------------------- | ||
|
||
[ | ||
["keyword", "allow"], | ||
["keyword", "function"], | ||
["keyword", "if"], | ||
["keyword", "match"], | ||
["keyword", "null"], | ||
["keyword", "return"], | ||
["keyword", "rules_version"], | ||
["keyword", "service"] | ||
] | ||
|
||
---------------------------------------------------- | ||
|
||
Checks for keywords. |
38 changes: 38 additions & 0 deletions
38
tests/languages/firestore-security-rules/method_feature.test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
allow read; | ||
allow read, write: if false; | ||
allow get, list, update; | ||
|
||
---------------------------------------------------- | ||
|
||
[ | ||
["keyword", "allow"], | ||
["method", [ | ||
"read" | ||
]], | ||
["punctuation", ";"], | ||
|
||
["keyword", "allow"], | ||
["method", [ | ||
"read", | ||
["punctuation", ","], | ||
" write" | ||
]], | ||
["punctuation", ":"], | ||
["keyword", "if"], | ||
["boolean", "false"], | ||
["punctuation", ";"], | ||
|
||
["keyword", "allow"], | ||
["method", [ | ||
"get", | ||
["punctuation", ","], | ||
" list", | ||
["punctuation", ","], | ||
" update" | ||
]], | ||
["punctuation", ";"] | ||
] | ||
|
||
---------------------------------------------------- | ||
|
||
Checks for methods. |
13 changes: 13 additions & 0 deletions
13
tests/languages/firestore-security-rules/number_feature.test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
123 | ||
1.23 | ||
|
||
---------------------------------------------------- | ||
|
||
[ | ||
["number", "123"], | ||
["number", "1.23"] | ||
] | ||
|
||
---------------------------------------------------- | ||
|
||
Checks for numbers. |
31 changes: 31 additions & 0 deletions
31
tests/languages/firestore-security-rules/operator_feature.test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
+ - * / % | ||
< > <= >= != == | ||
! && || | ||
|
||
in is | ||
|
||
---------------------------------------------------- | ||
|
||
[ | ||
["operator", "+"], | ||
["operator", "-"], | ||
["operator", "*"], | ||
["operator", "/"], | ||
["operator", "%"], | ||
["operator", "<"], | ||
["operator", ">"], | ||
["operator", "<="], | ||
["operator", ">="], | ||
["operator", "!="], | ||
["operator", "=="], | ||
["operator", "!"], | ||
["operator", "&&"], | ||
["operator", "||"], | ||
|
||
["operator", "in"], | ||
["operator", "is"] | ||
] | ||
|
||
---------------------------------------------------- | ||
|
||
Checks for operators. |
Oops, something went wrong.