Skip to content
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

ref(flags): limit scopes for secret updates #82897

Merged
merged 6 commits into from
Jan 8, 2025
Merged

Conversation

michellewzhang
Copy link
Member

closes https://github.com/getsentry/team-replay/issues/522

changes the secret endpoint permissions so that only managers & owners can update a secret (anyone can post) -- only exception is the original creator of the secret can always update their secret, regardless of their scope.

@michellewzhang michellewzhang requested a review from a team as a code owner January 3, 2025 21:58
@github-actions github-actions bot added the Scope: Backend Automatically applied to PRs that change backend components label Jan 3, 2025
Copy link

codecov bot commented Jan 7, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

✅ All tests successful. No failed tests found.

Additional details and impacted files
@@             Coverage Diff             @@
##           master   #82897       +/-   ##
===========================================
+ Coverage   56.21%   87.61%   +31.40%     
===========================================
  Files        9416     9416               
  Lines      536122   535938      -184     
  Branches    21120    20959      -161     
===========================================
+ Hits       301375   469570   +168195     
+ Misses     234387    66003   -168384     
- Partials      360      365        +5     

Comment on lines 86 to 100
# these scopes can always update or post secrets
has_update_or_post_access = request.access.has_scope(
"org:write"
) or request.access.has_scope("org:admin")

try:
secret = FlagWebHookSigningSecretModel.objects.filter(
organization_id=organization.id
).get(provider=validator.validated_data["provider"])
# allow update access if the user created the secret
if request.user.id == secret.created_by:
has_update_or_post_access = True
except FlagWebHookSigningSecretModel.DoesNotExist:
# anyone can post a new secret
has_update_or_post_access = True
Copy link
Member

Choose a reason for hiding this comment

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

This section confused me a little and is probably brittle. I think we can simplify and make it more understandable.

I would make two variables: has_permission and is_creator. And then implement as:

has_permission = request.access.has_scope(...)

try:
    ...
    is_creator = request.user.id == secret.created_by
except:
    is_creator = True

Finally in the call-site:

if is_creator or has_permission:
    ...

Copy link
Member Author

Choose a reason for hiding this comment

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

updated!

return Response(
"You must be an organization owner or manager, or the creator of this secret in order to perform this action.",
status=403,
)

return Response(status=201)
Copy link
Member

@cmanallen cmanallen Jan 7, 2025

Choose a reason for hiding this comment

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

Nit: if we can only return this response if the boolean condition evaluates to true then we should move the response into the boolean itself. It's less confusing IMO if we know the function terminates at this conditional.

Edit: move into the if branch. Moving in to the boolean doesn't make any sense lol.

Copy link
Member Author

Choose a reason for hiding this comment

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

updated this as well

@michellewzhang michellewzhang merged commit 32ef888 into master Jan 8, 2025
49 checks passed
@michellewzhang michellewzhang deleted the mz/fix-secret-auth branch January 8, 2025 18:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Scope: Backend Automatically applied to PRs that change backend components
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants