Skip to content

fix: prevent unauthorised access from group permissions - #5865

Closed
matthewelwell wants to merge 15 commits into
mainfrom
fix/prevent-unauthorised-access-from-group-permissions
Closed

fix: prevent unauthorised access from group permissions#5865
matthewelwell wants to merge 15 commits into
mainfrom
fix/prevent-unauthorised-access-from-group-permissions

Conversation

@matthewelwell

Copy link
Copy Markdown
Contributor

Changes

This PR prevents users that were part of groups in organisations that they no longer belong to from accessing the resources in that organisation.

From testing, the only way that this was possible in the first place was if the user was removed from the organisation in one of the following non-standard ways:

  1. The user was removed via django admin
  2. The user was removed as part of the logic to remove all users but the first one when a subscription is cancelled or downgraded

Note that this PR also includes a (tested) migration to remove any users from groups in organisations they do not belong to.

How did you test this code?

Added a lot of new unit tests.

@matthewelwell
matthewelwell requested a review from a team as a code owner July 30, 2025 18:15
@matthewelwell
matthewelwell requested review from khvn26 and removed request for a team July 30, 2025 18:15
@vercel

vercel Bot commented Jul 30, 2025

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
flagsmith-frontend-preview ✅ Ready (Inspect) Visit Preview 💬 Add feedback Aug 5, 2025 0:10am
flagsmith-frontend-staging ✅ Ready (Inspect) Visit Preview 💬 Add feedback Aug 5, 2025 0:10am
1 Skipped Deployment
Name Status Preview Comments Updated (UTC)
docs ⬜️ Ignored (Inspect) Visit Preview Aug 5, 2025 0:10am

@github-actions github-actions Bot added api Issue related to the REST API fix labels Jul 30, 2025
@github-actions

github-actions Bot commented Jul 30, 2025

Copy link
Copy Markdown
Contributor

Docker builds report

Image Build Status Security report
ghcr.io/flagsmith/flagsmith-api-test:pr-5865 Finished ✅ Skipped
ghcr.io/flagsmith/flagsmith-e2e:pr-5865 Finished ✅ Skipped
ghcr.io/flagsmith/flagsmith-api:pr-5865 Finished ✅ Results
ghcr.io/flagsmith/flagsmith-frontend:pr-5865 Finished ✅ Results
ghcr.io/flagsmith/flagsmith:pr-5865 Finished ✅ Results
ghcr.io/flagsmith/flagsmith-private-cloud:pr-5865 Finished ✅ Results

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.

I'm not entirely convinced that the extra checks that I've added here are the most optimal way of adding this logic, but I couldn't find a single entrypoint to the permissions service which would allow me to add it.

Keen to get @gagantrivedi 's opinion on this topic.

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.

Yeah, doesn't look like there is a better way

queryset = user_permission_group_membership_model.objects.exclude(
ffadminuser__organisations=F("userpermissiongroup__organisation")
)
logger.info("Removing orphan group memberships.", num_orphan_memberships=queryset.count())

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.

I've purposefully added one such record to the staging database. We should verify in the logs on staging that this migration behaves as we expect before running the migration in production.

gagantrivedi
gagantrivedi previously approved these changes Jul 31, 2025
@matthewelwell
matthewelwell force-pushed the fix/prevent-unauthorised-access-from-group-permissions branch from c77fb26 to 46eeb6f Compare August 4, 2025 16:19
@github-actions github-actions Bot added fix and removed fix labels Aug 4, 2025
@matthewelwell
matthewelwell changed the base branch from main to test/combine-staff-and-test-user-fixtures August 4, 2025 16:21
@github-actions github-actions Bot added fix and removed fix labels Aug 5, 2025
@codecov

codecov Bot commented Aug 5, 2025

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 99.28571% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 97.83%. Comparing base (8b20652) to head (02cf3df).

Files with missing lines Patch % Lines
api/permissions/permission_service.py 93.33% 1 Missing ⚠️
Additional details and impacted files
@@                            Coverage Diff                             @@
##           test/combine-staff-and-test-user-fixtures    #5865   +/-   ##
==========================================================================
  Coverage                                      97.82%   97.83%           
==========================================================================
  Files                                           1258     1260    +2     
  Lines                                          44846    44920   +74     
==========================================================================
+ Hits                                           43872    43946   +74     
  Misses                                           974      974           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Base automatically changed from test/combine-staff-and-test-user-fixtures to main August 5, 2025 12:45
@matthewelwell

Copy link
Copy Markdown
Contributor Author

Superseded by #5893

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api Issue related to the REST API fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants