Skip to content

Speed up the project access check for units - #1897

Open
samanrassam wants to merge 1 commit into
devfrom
HMS-2714
Open

samanrassam wants to merge 1 commit into
devfrom
HMS-2714

Conversation

@samanrassam

@samanrassam samanrassam commented Nov 27, 2026 •

Copy link
Copy Markdown

Pull Request Template

Before Marking as Ready for Review

  • Add relevant information to the sections below (Summary etc)
  • Rebase or merge the latest dev (or other targeted branch)
  • Update documentation if needed
  • Add an entry to the SPRINTLOG.md if needed
  • Choose an appropriate label. See here for information on the labelling options
  • The code follows the style guidelines
  • Perform a self-review: read the diff as if reviewing someone else's code
  • I have commented the code, particularly in hard-to-understand areas
  • Verify that all checks and tests have passed

If the target branch is master:

Summary

Bulk uploads from large units caused CPU spikes on the backend, because the project access check loaded every project in the user's unit on each request, so once for every uploaded file. The check now only looks at the project's unit, the user's ProjectUsers row or the Super Admin role. This gives the same answer as before without loading any project lists. The error is also unchanged (403, "Project access denied."), so the CLI is not affected. With ~3k projects in the unit, the load took a median of 39 ms on dev and 1 ms on this branch.

utils.py:
verify_project_access used to check project in current_user.projects. For a unit user that list is every project the unit has ever had, inactive ones included, and for a Super Admin it is every project in DDS. It now compares project.unit_id with the user's unit_id for Unit Admins and Unit Personnel, looks up the one ProjectUsers row for the project and user for Researchers (Project Owners included). We have the same behavior as before with always allowing Super Admins. unit.projects is defined by Project.unit_id, so the unit check gives the same result as the old list. Any other role is denied.

project_schemas.py:
verify_project_access here was an identical copy of the one in utils.py, and it was the one uploads went through, via ProjectRequiredSchema. It is removed and ProjectRequiredSchema now calls the utils.py version, so the fix applies to every endpoint and there is only one access check to keep up to date. This duplication was added in #1240.

test_utils.py:
test_verify_project_access_no_unit checks that a unit user is denied access to a project without a unit (unit_id is NULL). This is the one case where comparing unit_id could give a different answer than the old list, and no existing test covered it. Unit users inside and outside the unit, researchers in and not in the project, and Super Admins are already covered by existing tests in tests/test_utils.py, tests/test_project_info.py and tests/test_project_listing.py.

Related Issue/Ticket

HMS-2714, from the investigation in HMS-2712.

Testing

Locally the full test suite passes (946 passed) and formatter is clean.

I also counted the queries ProjectRequiredSchema().load() runs on dev and on this branch. As a unit user it no longer loads the unit and its projects, as a Super Admin it no longer loads all projects, and as a researcher it looks up one ProjectUsers row instead of all of theirs. With ~3k projects in the unit, the load took a median of 39 ms on dev and 1 ms on this branch.

Reviewer Notes

It was brought up during a discussion that the password reset mechanism might revoke access that interferes with the ID check in this PR.
A password reset only deletes the user's ProjectUserKeys and key pair, not their ProjectUsers rows or unit_id, so this check gives the same answer before and after a reset. Downloads after a reset are still stopped by verify_project_user_key.


Once all boxes are checked, mark the PR as Ready for Review and tag at least one team member as the initial reviewer.

… ID check once, instead of iterating through all projects). Removed extranous check in project_schemas.py and added one test.
@samanrassam samanrassam changed the title Implemented fix detected in HMS-2712 in utils.py (verifying access by ID check once, instead of iterating through all projects). Removed extranous check in project_schemas.py and added one test. Speed up the project access check for units Nov 27, 2026
@samanrassam samanrassam added the type: bug Something isn't working - for release template label Nov 27, 2026
@codecov

codecov Bot commented Nov 27, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.89%. Comparing base (cc15483) to head (fea21cd).

Additional details and impacted files
@@           Coverage Diff           @@
##              dev    #1897   +/-   ##
=======================================
  Coverage   92.88%   92.89%           
=======================================
  Files          30       30           
  Lines        5077     5080    +3     
=======================================
+ Hits         4716     4719    +3     
  Misses        361      361           

☔ View full report in Codecov by Harness.
📢 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.

@samanrassam
samanrassam marked this pull request as ready for review November 28, 2026 00:29
@samanrassam
samanrassam requested a review from a team as a code owner November 28, 2026 00:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type: bug Something isn't working - for release template

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant