Skip to content

fix(Audit): Retrieving records via /api/v1/audit/ with a master API key results in error - #8275

Open
bardock-2393 wants to merge 1 commit into
Flagsmith:mainfrom
bardock-2393:fix/audit-log-master-api-key
Open

fix(Audit): Retrieving records via /api/v1/audit/ with a master API key results in error#8275
bardock-2393 wants to merge 1 commit into
Flagsmith:mainfrom
bardock-2393:fix/audit-log-master-api-key

Conversation

@bardock-2393

Copy link
Copy Markdown
Contributor

Thanks for submitting a PR! Please check the boxes below:

  • I have read the Contributing Guide.
  • I have added information to docs/ if required so people know about the feature.
  • I have filled in the "Changes" section below.
  • I have filled in the "How did you test this code" section below.

Changes

Listing audit log records with a Master API Key returns a 500 instead of the records. Anyone driving Flagsmith through the Terraform provider, a CI script, or a SIEM/compliance pipeline cannot read the audit log at all with the credential they already use for everything else — the endpoint fails before it gets as far as checking what the key is allowed to see.

The endpoint assumed the caller was always a signed-in user, and looked up their organisations by following a database relation that only exists for real user accounts. A Master API Key is not a user account, so that lookup fell over.

  • /api/v1/audit/ now returns audit logs for Master API Key callers instead of erroring.
  • Who can see what is unchanged: an admin key sees its own organisation's records, and a non-admin key sees nothing — the same rule already applied to admin and non-admin users.
  • The six Platform Hub endpoints had the same underlying assumption and were also erroring for Master API Keys. They are fixed by the same change, and covered by a test.
  • Removed two now-redundant type: ignore comments in the surrounding code.

Closes #6583

Review effort: 2/5

How did you test this code?

Added tests that fail on main and pass with this change:

  • /api/v1/audit/ with an admin Master API Key returns the organisation's records. Without the fix this reproduces the reported TypeError: Field 'id' expected a number but got <APIKeyUser> exactly.
  • /api/v1/audit/ with a non-admin Master API Key returns an empty list, matching the existing behaviour for non-admin users.
  • /api/v1/audit/ with an admin Master API Key belonging to a different organisation returns an empty list, so the fix does not leak records across organisations.
  • Platform Hub organisations endpoint with an admin Master API Key returns that organisation. Without the fix this raises AttributeError: 'APIKeyUser' object has no attribute 'get_admin_organisations'.

The existing tests covering user-authenticated access to this endpoint (regular user, and admin of another organisation) still pass unchanged, which is what confirms the visibility rules did not shift.

Also ran the full backend suite (4518 passed, 48 skipped — the only errors were InfluxDB connection failures from that service not running locally, in tests unrelated to this change), mypy in strict mode across the codebase, and pre-commit over the changed files.

…ey results in error

`AllAuditLogViewSet` assumed `request.user` was an `FFAdminUser` and filtered
audit logs by joining through `UserOrganisation`. Master API Key authentication
supplies an `APIKeyUser`, which is not a database row, so Django raised
`TypeError: Field 'id' expected a number but got <APIKeyUser>` and the endpoint
returned a 500.

Formalise `get_admin_organisations()` on `UserABC` and implement it for
`APIKeyUser`, returning the key's organisation when the key is an admin key and
nothing otherwise. `AllAuditLogViewSet` now resolves administered organisations
through that interface rather than assuming a concrete user model, which keeps
the existing org-admin visibility semantics for both authentication methods.

The same assumption broke the six Platform Hub endpoints, which already called
`get_admin_organisations()` on `request.user` and raised `AttributeError` for
master API keys. Those are fixed by the same change.
@bardock-2393
bardock-2393 requested a review from a team as a code owner August 12, 2026 17:28
@bardock-2393
bardock-2393 requested review from khvn26 and removed request for a team August 12, 2026 17:28
@vercel

vercel Bot commented Aug 12, 2026

Copy link
Copy Markdown

@bardock-2393 is attempting to deploy a commit to the Flagsmith Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai

coderabbitai Bot commented Aug 12, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 8e14232f-3679-4681-99cc-ccf588f71563

📥 Commits

Reviewing files that changed from the base of the PR and between 6c88198 and 5f59100.

📒 Files selected for processing (7)
  • api/api_keys/user.py
  • api/audit/views.py
  • api/tests/unit/audit/test_unit_audit_views.py
  • api/tests/unit/platform_hub/test_views.py
  • api/tests/unit/users/test_unit_users_models.py
  • api/users/abc.py
  • api/users/models.py

📝 Walkthrough

Walkthrough

Adds get_admin_organisations() to the user contract and implements it for API-key and administrator users. Audit views now use this method to restrict project and organisation visibility. Tests cover administrator, non-administrator, and cross-organisation master API keys for audit logs and platform-hub organisations. Type annotations are also updated.

Estimated code review effort: 3 (Moderate) | ~20 minutes

✨ Finishing Touches 💡 1
🛠️ Fix failing CI checks 💡
  • Create stacked PR
  • Commit on current branch

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@github-actions github-actions Bot added the api Issue related to the REST API label Aug 12, 2026
@codecov

codecov Bot commented Aug 12, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.59%. Comparing base (6c88198) to head (5f59100).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #8275      +/-   ##
==========================================
- Coverage   98.73%   98.59%   -0.15%     
==========================================
  Files        1567     1567              
  Lines       62379    62416      +37     
==========================================
- Hits        61591    61538      -53     
- Misses        788      878      +90     

☔ 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.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Retrieving records via /api/v1/audit/ with a master API key results in error

1 participant