Skip to content

[MEDIUM] — AdminGuard issues a fresh prisma.user.findUnique on every protected request instead of caching role lookups #10

@Alqku

Description

@Alqku

Severity: Medium
Type: Performance
Scope: users/guards/admin.guard.ts, Admin, Users
Labels: refactoring, help wanted

Description

AdminGuard.canActivate (src/users/guards/admin.guard.ts, lines ~10–26) loads the user via prisma.user.findUnique({ where: { walletAddress } }) for every admin-protected request, before comparing role !== 'ADMIN'. Under load, every admin dashboard, key-revocation, suspension, and KYC update adds one round trip to Postgres purely to verify a property already known at JWT issuance.

The query also runs after JwtAuthGuard, which has already authenticated the user, so the work is strictly redundant in the common case.

Recommendation

  • Embed role in the JWT payload at issuance (and include in the JwtStrategy.validate return value).
  • Have AdminGuard read request.user.role directly, with a RolesGuard (already present) enforcing it.
  • Optionally fall back to a Redis-cached role check (TTL ≈ 60s) to survive role changes within a short window.

Metadata

Metadata

Assignees

Labels

GrantFox OSSIssue tracked in GrantFox OSSMaybe RewardedIssue may be eligible for a GrantFox rewardOfficial CampaignAudit finding under the Official Campaignhelp wantedExtra attention is neededrefactoringCode restructuring without behavioural change

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions