-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(scopes): Enforce scope hierarchy #54510
Conversation
Codecov Report
@@ Coverage Diff @@
## master #54510 +/- ##
===========================================
+ Coverage 55.22% 78.60% +23.37%
===========================================
Files 5069 5080 +11
Lines 218768 218661 -107
Branches 37052 37020 -32
===========================================
+ Hits 120825 171881 +51056
+ Misses 94455 41212 -53243
- Partials 3488 5568 +2080
|
"scopes": [ | ||
"event:read", | ||
"project:read", | ||
], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fix: refactor test to work with alphabetical order
@@ -64,7 +64,7 @@ def setUp(self): | |||
super().setUp() | |||
|
|||
self.sentry_app = self.create_sentry_app( | |||
name="external_app", organization=self.org, scopes=("org:write", "team:admin") | |||
name="external_app", organization=self.org, scopes=("org:read", "team:read") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fix: refactor test to avoid implicit hierarchy
@@ -79,4 +79,4 @@ def test_create_token(self): | |||
|
|||
# verify token was created properly | |||
assert api_token.expires_at == today | |||
assert api_token.scope_list == ["org:write", "team:admin"] | |||
assert api_token.scope_list == ["org:read", "team:read"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fix: refactor test to avoid implicit hierarchy
@@ -325,7 +325,7 @@ def test_valid_params_id_token_additional_scopes(self): | |||
data = json.loads(resp.content) | |||
token = ApiToken.objects.get(token=data["access_token"]) | |||
|
|||
assert token.get_scopes() == ["openid", "profile", "email"] | |||
assert token.get_scopes() == ["email", "openid", "profile"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: change ordering b/c response scopes will be in alphabetical order
Note
ref(token): Update create user token page to use dropdowns #54651
Implementation
To model the hierarchy I used a simple dict of scope -> all granted scopes. I chose this approach b/c it's very simple and easy to enforce in the code.
We use a pre-save signal on the
ApiKey
andApiToken
models to enforce scope hierarchy. The basic scope hierarchy for each resource is:When updating one of these models, there is no way to know if the scopes are enforced without fetching it from the DB. To avoid this trip, we always iterate through the hierarchy mapping.