Fix missing username in uWSGI logs when using API Token authenticatio…#14527
Fix missing username in uWSGI logs when using API Token authenticatio…#14527tejas0077 wants to merge 4 commits intoDefectDojo:bugfixfrom
Conversation
|
This pull request modifies sensitive files (dojo/middleware.py and dojo/remote_user.py), and the scanner flagged these edits as errors under the "Configured Codepaths Edit" rule; review/update of .dryrunsecurity.yaml may be required to allow these changes or to confirm the authors. The findings are non-blocking but marked at a failing risk threshold and should be reviewed for security/approval before merging.
🔴 Configured Codepaths Edit in
|
| Vulnerability | Configured Codepaths Edit |
|---|---|
| Description | Sensitive edits detected for this file. Sensitive file paths and allowed authors can be configured in .dryrunsecurity.yaml. |
🔴 Configured Codepaths Edit in dojo/middleware.py (drs_d88ef6d0)
| Vulnerability | Configured Codepaths Edit |
|---|---|
| Description | Sensitive edits detected for this file. Sensitive file paths and allowed authors can be configured in .dryrunsecurity.yaml. |
🔴 Configured Codepaths Edit in dojo/remote_user.py (drs_e4825f1c)
| Vulnerability | Configured Codepaths Edit |
|---|---|
| Description | Sensitive edits detected for this file. Sensitive file paths and allowed authors can be configured in .dryrunsecurity.yaml. |
🔴 Configured Codepaths Edit in dojo/middleware.py (drs_e2ff8542)
| Vulnerability | Configured Codepaths Edit |
|---|---|
| Description | Sensitive edits detected for this file. Sensitive file paths and allowed authors can be configured in .dryrunsecurity.yaml. |
We've notified @mtesauro.
Comment to provide feedback on these findings.
Report false positive: @dryrunsecurity fp [FINDING ID] [FEEDBACK]
Report low-impact: @dryrunsecurity nit [FINDING ID] [FEEDBACK]
Example: @dryrunsecurity fp drs_90eda195 This code is not user-facing
All finding details can be found in the DryRun Security Dashboard.
Maffooch
left a comment
There was a problem hiding this comment.
This is partially implemented here: https://github.com/DefectDojo/django-DefectDojo/blob/c89ac6087b553301e95214eb59d31a657641b8f6/dojo/remote_user.py
Please update this middleware instead of adding a new one
|
Thanks @Maffooch! I'll update the existing |
|
Thanks @Maffooch! I've moved the logic into the existing |
@Maffooch I don't see how REMOTE_USER authentication is related to apikey authentication? |
|
Hi @valentijnscholten, you raise a valid point. RemoteUserMiddleware only runs when Looking at the codebase again, I see there's already an existing middleware in @Maffooch could you clarify where the right place would be to handle this for API token auth specifically? |
|
@valentijnscholten is right — @tejas0077 the right place is Please revert the changes to |
I'm not following. What has REMOTE_USER todo with tokens? |
|
Hi @Maffooch and @valentijnscholten, I've updated the fix based on your feedback. Reverted the changes to remote_user.py and instead added request.META["REMOTE_USER"] = str(request.user) in LoginRequiredMiddleware in dojo/middleware.py, right next to the existing uwsgi.set_logvar call. This ensures the username is logged correctly for API token auth requests without touching the remote user authentication flow. |
Description
When using API Token authentication, uWSGI logs show a dash (-) instead
of the username, making it impossible to trace which user made which API
request. This breaks audit trails and forensic analysis.
Web interface requests correctly log the username, but API token requests do not.
Fix: Added ApiTokenUsernameLoggingMiddleware that sets REMOTE_USER in the
request metadata after authentication is complete, so uWSGI can log the
correct username regardless of the authentication method used.
Fixes #13751
Test results
Manually traced the middleware execution. The middleware runs after
AuthenticationMiddleware so the user is always authenticated before
we attempt to set REMOTE_USER.
Documentation
No documentation changes needed.