Skip to content

fix: enforce current database role and reject deleted users - #429

Open
7-hello007 wants to merge 6 commits into
mainfrom
7-hello007/fix/harden-auth-deleted-users-stale-roles
Open

fix: enforce current database role and reject deleted users#429
7-hello007 wants to merge 6 commits into
mainfrom
7-hello007/fix/harden-auth-deleted-users-stale-roles

Conversation

@7-hello007

@7-hello007 7-hello007 commented Jul 25, 2026

Copy link
Copy Markdown
Collaborator

fix: enforce current database role and reject deleted users

Summary

This PR hardens the authentication middleware (auth.js) to ensure every protected request validates the current database user and current stored role, rather than trusting stale JWT claims.

Changes

  • auth.js now queries DB for current user (isDeleted, role)
  • loadUser.js syncs req.user.role with DB role
  • Rejects soft-deleted users → 401 'User account is no longer active.'
  • Rejects non-existent users → 401 'User account is no longer available.'
  • Uses current DB role instead of JWT role (stale role is ignored)
  • Added 6 automated security tests (tests/auth.staleRole.test.js)

Security Rationale

  • Database is the source of truth for user state and role
  • Role changes must take effect immediately
  • Soft deletion must immediately revoke access
  • JWT is a session identifier, not a permissions cache
  • No sensitive fields (password) attached to req.user

Evidence

  • All 6 security tsts passed
  • Before/after evidence captured in toProveProblem/

Before changing:
image
After changing:
image

Changes:

Reason: Running authentication tests on the development database (secureshift_local) could corrupt test data or produce unreliable results.

  • tests/auth.staleRole.test.js now requires the MONGO_TEST_URI environment variable to be set before running
  • The test fails safely with a clear error message when MONGO_TEST_URI is not set
image 屏幕截图 2026-07-28 173419

The tests now require MONGO_TEST_URI to be set before running, or they will fail safely with a clear error message.
image

- auth.js now queries DB for current user (isDeleted, role)
- loadUser.js syncs req.user.role with DB role
- Rejects soft-deleted users → 401 'User account is no longer active.'
- Rejects non-existent users → 401 'User account is no longer available.'
- Uses current DB role instead of JWT role
- Added 6 automated security tests (tests/auth.staleRole.test.js)

Security rationale:
- Database is the source of truth for user state and role
- Role changes take effect immediately
- Soft deletion immediately revokes access
- No sensitive fields attached to req.user

Evidence:
- All 6 security tests passed
- Before/after evidence captured in toProveProblem/
- tests/auth.staleRole.test.js now requires MONGO_TEST_URI
- Fails safely when MONGO_TEST_URI is not set
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant