@nathapp/nestjs-auth 2.1.4 → 3.0.0-rc.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CLAUDE.md +29 -0
- package/dist/auth-provider.js.map +1 -1
- package/dist/auth.module.d.ts +17 -0
- package/dist/auth.module.js +109 -0
- package/dist/auth.module.js.map +1 -0
- package/dist/authentication/auth.module.d.ts +49 -0
- package/dist/authentication/auth.module.js +252 -0
- package/dist/authentication/auth.module.js.map +1 -0
- package/dist/authentication/index.d.ts +8 -0
- package/dist/authentication/index.js +25 -0
- package/dist/authentication/index.js.map +1 -0
- package/dist/authorization/authorization.module.d.ts +32 -0
- package/dist/authorization/authorization.module.js +145 -0
- package/dist/authorization/authorization.module.js.map +1 -0
- package/dist/authorization/index.d.ts +5 -0
- package/dist/authorization/index.js +22 -0
- package/dist/authorization/index.js.map +1 -0
- package/dist/casl/casl-ability-factory.js.map +1 -1
- package/dist/constant/jwt-constant.js.map +1 -1
- package/dist/constant/module-constant.js.map +1 -1
- package/dist/decorator/is-public.decorator.js.map +1 -1
- package/dist/decorator/permission.decorator.js.map +1 -1
- package/dist/decorator/principal.decorator.js.map +1 -1
- package/dist/decorator/refresh-token.decorator.js.map +1 -1
- package/dist/guard/jwt-auth.guard.js +1 -8
- package/dist/guard/jwt-auth.guard.js.map +1 -1
- package/dist/guard/jwt-refresh.guard.d.ts +1 -0
- package/dist/guard/jwt-refresh.guard.js +8 -2
- package/dist/guard/jwt-refresh.guard.js.map +1 -1
- package/dist/guard/permission-auth.guard.js +1 -1
- package/dist/guard/permission-auth.guard.js.map +1 -1
- package/dist/index.d.ts +3 -0
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -1
- package/dist/module/jwt-module.js.map +1 -1
- package/dist/permission/permission.provider.js +25 -3
- package/dist/permission/permission.provider.js.map +1 -1
- package/dist/strategy/base-jwt-strategy-provider.d.ts +19 -0
- package/dist/strategy/base-jwt-strategy-provider.js +54 -0
- package/dist/strategy/base-jwt-strategy-provider.js.map +1 -0
- package/dist/strategy/index.d.ts +4 -3
- package/dist/strategy/index.js +3 -1
- package/dist/strategy/index.js.map +1 -1
- package/dist/strategy/jwt-refresh-strategy-provider.d.ts +4 -17
- package/dist/strategy/jwt-refresh-strategy-provider.js +5 -38
- package/dist/strategy/jwt-refresh-strategy-provider.js.map +1 -1
- package/dist/strategy/jwt-refresh-strategy.js.map +1 -1
- package/dist/strategy/jwt-strategy-provider.d.ts +4 -18
- package/dist/strategy/jwt-strategy-provider.js +5 -38
- package/dist/strategy/jwt-strategy-provider.js.map +1 -1
- package/dist/strategy/jwt-strategy.js.map +1 -1
- package/dist/strategy/strategy-provider.d.ts +2 -10
- package/dist/strategy/strategy-provider.js.map +1 -1
- package/dist/strategy/type-interface.d.ts +11 -0
- package/dist/utils/utils.d.ts +1 -1
- package/dist/utils/utils.js +6 -6
- package/dist/utils/utils.js.map +1 -1
- package/docs/20260214-fix-plan-auth-module-split.md +148 -0
- package/docs/20260214-fix-plan-auth-security.md +130 -0
- package/docs/20260214-review-auth-enterprise.md +186 -0
- package/docs/20260214-review-auth.md +300 -0
- package/docs/20260215-review-auth-post-fix.md +519 -0
- package/package.json +3 -3
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
# @nathapp/nestjs-auth — Enterprise Code Review (Post-Fix)
|
|
2
|
+
|
|
3
|
+
**Date:** 2026-02-14
|
|
4
|
+
**Branch:** `feat/monorepo-enterprise-upgrade`
|
|
5
|
+
**Reviewer:** Subrina
|
|
6
|
+
**Version:** 2.1.5
|
|
7
|
+
|
|
8
|
+
---
|
|
9
|
+
|
|
10
|
+
## Architectural Context
|
|
11
|
+
|
|
12
|
+
> **@nathapp/nestjs-auth is a library — NOT an application.**
|
|
13
|
+
> It provides hooks and abstractions. Business logic belongs in consuming apps.
|
|
14
|
+
|
|
15
|
+
| Concern | In scope (library) | Out of scope (consumer app) |
|
|
16
|
+
|---|---|---|
|
|
17
|
+
| JWT signing, verification, validation | ✅ | |
|
|
18
|
+
| Guard abstractions (`JwtAuthGuard`, `PermissionAuthGuard`) | ✅ | |
|
|
19
|
+
| Provider interfaces (`AuthProvider`, `PermissionProvider`) | ✅ | |
|
|
20
|
+
| CASL integration hooks | ✅ | |
|
|
21
|
+
| Guard registration as global guards | | ✅ `nestjs-app` (`NathApplication`) |
|
|
22
|
+
| `JwtRefreshGuard` auto-registration | | ✅ Only auth-api needs it |
|
|
23
|
+
| Token revocation / blacklisting | | ✅ Business logic |
|
|
24
|
+
| Refresh token rotation | | ✅ Business logic |
|
|
25
|
+
| Multi-tenant support | | ✅ Business logic |
|
|
26
|
+
| Session management | | ✅ Business logic |
|
|
27
|
+
|
|
28
|
+
---
|
|
29
|
+
|
|
30
|
+
## Executive Summary
|
|
31
|
+
|
|
32
|
+
| Metric | Value |
|
|
33
|
+
|---|---|
|
|
34
|
+
| **Overall Grade** | **B** (up from B-) |
|
|
35
|
+
| Source Files | 28 `.ts` files (~1,282 lines) |
|
|
36
|
+
| Test Files | 10 `.ts` files (98 tests passing) |
|
|
37
|
+
| Security Posture | **Improved** — SEC-1, BUG-3, BUG-4 fixed, audit logging added |
|
|
38
|
+
|
|
39
|
+
### What's Fixed Since Last Review
|
|
40
|
+
1. ✅ **SEC-1:** `@RefreshToken()` no longer bypasses `JwtAuthGuard`
|
|
41
|
+
2. ✅ **BUG-3:** `isAuthority()` operator precedence fixed
|
|
42
|
+
3. ✅ **BUG-4:** `PermissionAuthGuard` now passes ALL CASL permissions
|
|
43
|
+
4. ✅ **SEC-7:** Auth failure audit logging in both guards
|
|
44
|
+
5. ✅ Deprecated constants tagged with `@deprecated`
|
|
45
|
+
6. ✅ New unit tests for JwtAuthGuard, PermissionAuthGuard (multi-CASL), utils
|
|
46
|
+
|
|
47
|
+
---
|
|
48
|
+
|
|
49
|
+
## 1. Architecture Analysis
|
|
50
|
+
|
|
51
|
+
### What's Good ✅
|
|
52
|
+
|
|
53
|
+
- **Clean provider abstraction** — `AuthProvider`, `PermissionProvider`, `StrategyProvider`, `CaslAbilityFactory` are all swappable
|
|
54
|
+
- **Module composition** — `JwtModule.forRoot()` / `forRootAsync()` follows NestJS conventions with `useClass`/`useFactory`/`useExisting`
|
|
55
|
+
- **Guard layering** — `JwtAuthGuard` → `PermissionAuthGuard` is correct and clean post-fix
|
|
56
|
+
- **`nestjs-app` handles guard registration** — intentional, not a gap (guards registered in `NathApplication.useAppGlobalGuards()`)
|
|
57
|
+
- **Refresh guard NOT auto-registered** — intentional, only auth-api needs it
|
|
58
|
+
|
|
59
|
+
### What Needs Improvement
|
|
60
|
+
|
|
61
|
+
| Issue | Description | Effort |
|
|
62
|
+
|---|---|---|
|
|
63
|
+
| **DRY violation** | `JwtStrategyProvider` and `JwtRefreshStrategyProvider` share ~80% identical code (constructor, `getStrategyOptions`, `sign`, `verify`). Extract `BaseJwtStrategyProvider`. | Medium |
|
|
64
|
+
| **DRY violation** | `JwtStrategy` and `JwtRefreshStrategy` have identical `validate` arg-routing logic. | Low |
|
|
65
|
+
| **CASL factory not injectable** | `CaslAbilityFactory` is injected via constructor in `DefaultPermissionProvider`, not via DI. Swapping requires a custom `PermissionProvider`. | Medium |
|
|
66
|
+
| **`PermissionAuthGuard` default allow** | Returns `true` when no `@RequiredPermission()` set. Consider optional "deny by default" mode. | Low |
|
|
67
|
+
|
|
68
|
+
---
|
|
69
|
+
|
|
70
|
+
## 2. Security Posture
|
|
71
|
+
|
|
72
|
+
### What's Solid ✅
|
|
73
|
+
|
|
74
|
+
| Check | Status |
|
|
75
|
+
|---|---|
|
|
76
|
+
| JWT signature validation | ✅ passport-jwt handles |
|
|
77
|
+
| Token expiration (`ignoreExpiration: false`) | ✅ Default |
|
|
78
|
+
| `@Public()` is only auth bypass | ✅ Post-fix |
|
|
79
|
+
| Revocation check in access token flow | ✅ Via `IPrincipal.revoked` |
|
|
80
|
+
| Blacklist check | ✅ Via `IPrincipal.blacklisted` |
|
|
81
|
+
| IP binding | ✅ Via `bindIp` option |
|
|
82
|
+
| Duplicate login detection | ✅ Via `AuthProvider.isDuplicateLogin()` |
|
|
83
|
+
| Auth failure logging | ✅ Post-fix |
|
|
84
|
+
|
|
85
|
+
### What Needs Improvement (library scope only)
|
|
86
|
+
|
|
87
|
+
| # | Issue | Description | Effort |
|
|
88
|
+
|---|---|---|---|
|
|
89
|
+
| **S1** | `iss`/`aud` claim validation not exposed | passport-jwt supports `issuer` and `audience` in `StrategyOptions`, but `JwtStrategyProvider` doesn't pass them through from `JwtOptions`. Easy to add. | Low |
|
|
90
|
+
| **S2** | `DefaultJwtRefreshStrategyProvider.validate()` has no checks | Unlike access token, refresh validation does zero server-side checks. Should at minimum call `authProvider.getPrincipal()` for revocation check. | Low |
|
|
91
|
+
| **S3** | `ignoreExpiration` settable via env without restriction | No guard rail to restrict to dev-only. Add a warning log if enabled in production. | Low |
|
|
92
|
+
| **S4** | Secrets via `process.env` not `ConfigService` | `DefaultJwtModuleOptionsFactory` reads some values from `process.env` directly, bypassing `ConfigService` validation. | Low |
|
|
93
|
+
|
|
94
|
+
---
|
|
95
|
+
|
|
96
|
+
## 3. Code Quality
|
|
97
|
+
|
|
98
|
+
### What Needs Cleanup
|
|
99
|
+
|
|
100
|
+
| # | Issue | Effort |
|
|
101
|
+
|---|---|---|
|
|
102
|
+
| **C1** | Commented-out code in `permission.decorator.ts` | Trivial |
|
|
103
|
+
| **C2** | Duplicate `abstract validate` signatures in `StrategyProvider` + `JwtStrategyProvider` (TypeScript uses only the last) | Trivial |
|
|
104
|
+
| **C3** | `IStrategyProvider` interface redundant with `StrategyProvider` abstract class | Trivial |
|
|
105
|
+
| **C4** | Unused `SetMetadata` import in `principal.decorator.ts` | Trivial |
|
|
106
|
+
| **C5** | Deprecated constants still exported (`JWT_PROVIDER`, `STRATEGY_PROVIDER`, `JWT_API_STRATEGY_NAME`, `JWT_GRPC_STRATEGY_NAME`) | Low |
|
|
107
|
+
| **C6** | 15+ `any` usages — define `JwtPayload` interface for type safety | Low |
|
|
108
|
+
| **C7** | `createJwtProvider` exported but is internal utility | Trivial |
|
|
109
|
+
|
|
110
|
+
---
|
|
111
|
+
|
|
112
|
+
## 4. Test Coverage
|
|
113
|
+
|
|
114
|
+
### Current: 98 tests passing
|
|
115
|
+
|
|
116
|
+
| Area | Coverage | Gap |
|
|
117
|
+
|---|---|---|
|
|
118
|
+
| `JwtAuthGuard` | ✅ ~90% | — |
|
|
119
|
+
| `PermissionAuthGuard` | ✅ ~90% | — |
|
|
120
|
+
| `SimpleAuthProvider` | ✅ ~100% | — |
|
|
121
|
+
| `DefaultJwtStrategyProvider` | ✅ ~95% | — |
|
|
122
|
+
| `DefaultPermissionProvider` | ⚠️ ~75% | CASL path untested |
|
|
123
|
+
| `JwtModule` (forRoot/forRootAsync) | ✅ ~85% | — |
|
|
124
|
+
| `Utils` | ✅ ~95% | — |
|
|
125
|
+
| **`JwtRefreshGuard`** | ❌ 0% | No tests at all |
|
|
126
|
+
| **`JwtRefreshStrategy`** | ❌ 0% | No tests at all |
|
|
127
|
+
| **`JwtRefreshStrategyProvider`** | ❌ 0% | No tests at all |
|
|
128
|
+
| **`CaslAbilityFactory`** | ❌ 0% | No tests at all |
|
|
129
|
+
| **`DefaultJwtModuleOptionsFactory`** | ❌ 0% | No tests at all |
|
|
130
|
+
|
|
131
|
+
---
|
|
132
|
+
|
|
133
|
+
## 5. Prioritized Improvements
|
|
134
|
+
|
|
135
|
+
### 🔴 Must-Have for 3.0
|
|
136
|
+
|
|
137
|
+
| # | Category | What | Effort | Why |
|
|
138
|
+
|---|---|---|---|---|
|
|
139
|
+
| 1 | Security | Expose `issuer`/`audience` in `JwtOptions` → pass to `StrategyOptions` | Low | OWASP JWT best practice |
|
|
140
|
+
| 2 | Security | Add `authProvider.getPrincipal()` revocation check to `DefaultJwtRefreshStrategyProvider.validate()` | Low | Refresh tokens are long-lived |
|
|
141
|
+
| 3 | Testing | Unit tests for `JwtRefreshGuard`, `JwtRefreshStrategy`, `JwtRefreshStrategyProvider` | Medium | 0% coverage on security code |
|
|
142
|
+
| 4 | Testing | Unit tests for CASL flow (`CaslAbilityFactory` → `DefaultPermissionProvider` CASL branch) | Medium | 0% coverage |
|
|
143
|
+
| 5 | Architecture | Extract `BaseJwtStrategyProvider` (DRY up access/refresh) | Medium | ~80% code duplication |
|
|
144
|
+
| 6 | Type Safety | Define `JwtPayload` interface, replace `any` in provider signatures | Low | Enterprise code quality |
|
|
145
|
+
| 7 | Cleanup | Remove dead code: commented code, duplicate signatures, unused imports, redundant interface | Trivial | Code hygiene |
|
|
146
|
+
| 8 | Docs | README.md with usage examples + 2.x → 3.0 migration guide (document `@RefreshToken()` change) | Medium | DX |
|
|
147
|
+
|
|
148
|
+
### 🟡 Should-Have (Post 3.0)
|
|
149
|
+
|
|
150
|
+
| # | Category | What | Effort |
|
|
151
|
+
|---|---|---|---|
|
|
152
|
+
| 9 | Architecture | Make `CaslAbilityFactory` injectable via `JwtModule` options | Medium |
|
|
153
|
+
| 10 | Security | Add `ignoreExpiration` production warning | Low |
|
|
154
|
+
| 11 | Security | Use `ConfigService` consistently for secrets | Low |
|
|
155
|
+
| 12 | Architecture | Optional "deny by default" mode for `PermissionAuthGuard` | Low |
|
|
156
|
+
| 13 | Testing | E2E tests for refresh token flow | Medium |
|
|
157
|
+
| 14 | Testing | `DefaultJwtModuleOptionsFactory` env-var parsing tests | Low |
|
|
158
|
+
| 15 | Docs | Add JSDoc (`@param`, `@returns`, `@throws`) to all public methods | Medium |
|
|
159
|
+
|
|
160
|
+
### 🟢 Nice-to-Have (Future)
|
|
161
|
+
|
|
162
|
+
| # | Category | What | Effort |
|
|
163
|
+
|---|---|---|---|
|
|
164
|
+
| 16 | Architecture | GraphQL execution context support | Medium |
|
|
165
|
+
| 17 | Architecture | WebSocket/gRPC context (complete TODOs) | Medium |
|
|
166
|
+
| 18 | Security | Structured audit log events (not just string logs) | Medium |
|
|
167
|
+
| 19 | DX | Remove deprecated constants from public exports | Low |
|
|
168
|
+
|
|
169
|
+
---
|
|
170
|
+
|
|
171
|
+
## Summary
|
|
172
|
+
|
|
173
|
+
**nestjs-auth is a solid JWT+CASL auth library** for the Nathapp ecosystem. The provider abstraction is clean, and business logic (revocation, rotation, multi-tenant) is correctly left to consumer apps.
|
|
174
|
+
|
|
175
|
+
**For 3.0**, the main work is:
|
|
176
|
+
1. Close the refresh token validation gap (S2)
|
|
177
|
+
2. Add `iss`/`aud` support (S1)
|
|
178
|
+
3. DRY up strategy providers
|
|
179
|
+
4. Fill test coverage gaps (refresh + CASL)
|
|
180
|
+
5. Cleanup + docs
|
|
181
|
+
|
|
182
|
+
**Estimated effort for all Must-Haves: ~3-4 hours of focused work.**
|
|
183
|
+
|
|
184
|
+
---
|
|
185
|
+
|
|
186
|
+
*Review generated 2026-02-14. Next review after 3.0 release.*
|
|
@@ -0,0 +1,300 @@
|
|
|
1
|
+
# @nathapp/nestjs-auth — Deep Code Review
|
|
2
|
+
|
|
3
|
+
**Date:** 2026-02-14
|
|
4
|
+
**Branch:** `feat/monorepo-enterprise-upgrade`
|
|
5
|
+
**Reviewer:** Subrina (automated)
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
## Summary
|
|
10
|
+
|
|
11
|
+
| Metric | Value |
|
|
12
|
+
|---|---|
|
|
13
|
+
| Source files | 28 `.ts` files |
|
|
14
|
+
| Test files | 8 `.ts` files |
|
|
15
|
+
| Dependencies | `passport-jwt`, `@nathapp/nestjs-common`, `@nathapp/auth` |
|
|
16
|
+
| Peer deps | `@casl/ability`, `@nestjs/passport` |
|
|
17
|
+
| Version | 2.1.5 |
|
|
18
|
+
| **Overall Grade** | **B-** — Solid architecture with meaningful security concerns |
|
|
19
|
+
|
|
20
|
+
**Architecture:** Provider-based JWT auth built on Passport.js with CASL permission integration. Clean separation: `JwtStrategyProvider` (access), `JwtRefreshStrategyProvider` (refresh), `AuthProvider` (principal resolution), `PermissionProvider` (authorization). Module registration via `forRoot`/`forRootAsync` dynamic modules. Guards (`JwtAuthGuard`, `JwtRefreshGuard`, `PermissionAuthGuard`) designed for global registration.
|
|
21
|
+
|
|
22
|
+
---
|
|
23
|
+
|
|
24
|
+
## 1. Security Issues (Critical)
|
|
25
|
+
|
|
26
|
+
### 🔴 SEC-1: `@RefreshToken()` decorator bypasses ALL authentication in `JwtAuthGuard`
|
|
27
|
+
|
|
28
|
+
**File:** `lib/guard/jwt-auth.guard.ts` lines 27-30
|
|
29
|
+
**Severity:** CRITICAL
|
|
30
|
+
|
|
31
|
+
```typescript
|
|
32
|
+
if (RefreshToken) {
|
|
33
|
+
return true; // ← Completely skips JWT validation
|
|
34
|
+
}
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
The `@RefreshToken()` decorator causes `JwtAuthGuard` to return `true` without any authentication check. If `JwtRefreshGuard` is not explicitly applied to the route, the endpoint is **completely unprotected**. An attacker who discovers a `@RefreshToken()`-decorated endpoint that lacks `JwtRefreshGuard` gets unauthenticated access.
|
|
38
|
+
|
|
39
|
+
**Also:** The variable `RefreshToken` shadows the imported `REFRESH_TOKEN` constant name — confusing and error-prone.
|
|
40
|
+
|
|
41
|
+
**Fix:** Remove the `RefreshToken` bypass from `JwtAuthGuard`. Instead, refresh endpoints should explicitly use `@UseGuards(JwtRefreshGuard)` and `@Public()` to opt out of the global JWT guard. Or at minimum, verify the refresh guard is actually applied.
|
|
42
|
+
|
|
43
|
+
### 🔴 SEC-2: `PermissionAuthGuard` returns `true` when no `@RequiredPermission()` is set
|
|
44
|
+
|
|
45
|
+
**File:** `lib/guard/permission-auth.guard.ts` lines 25-27
|
|
46
|
+
|
|
47
|
+
```typescript
|
|
48
|
+
if (requirePermission.length === 0) {
|
|
49
|
+
return true;
|
|
50
|
+
}
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
This is by design (only enforce when permissions are declared), but combined with SEC-1, any `@RefreshToken()` endpoint without explicit permission requirements is fully open.
|
|
54
|
+
|
|
55
|
+
### 🟡 SEC-3: `DefaultPermissionProvider` returns `undefined` for unhandled permission types
|
|
56
|
+
|
|
57
|
+
**File:** `lib/permission/permission.provider.ts`
|
|
58
|
+
In `checkPermission()`, when `permission` is an array (CASL) the method returns `Promise<boolean>`. But there's no `else` clause after the `if/else if` — if `permission` is neither `string` nor `Array`, the function returns `undefined` (falsy, so effectively denied — but implicit).
|
|
59
|
+
|
|
60
|
+
### 🟡 SEC-4: `SimpleAuthProvider` trusts JWT payload blindly
|
|
61
|
+
|
|
62
|
+
**File:** `lib/auth-provider.ts`
|
|
63
|
+
`SimpleAuthProvider.getPrincipal()` sets `blacklisted: false` and `revoked: false` unconditionally from JWT claims. This is the default provider. If consumers forget to replace it with a database-backed provider, revocation/blacklisting is impossible. The `authorities` field is also taken directly from the JWT payload — an attacker who can forge tokens controls their own permissions.
|
|
64
|
+
|
|
65
|
+
**Mitigation:** This is documented as "Simple" but there should be a loud warning or the default should require explicit opt-in.
|
|
66
|
+
|
|
67
|
+
### 🟡 SEC-5: `ignoreExpiration` option exists and defaults to `false`
|
|
68
|
+
|
|
69
|
+
**File:** `lib/strategy/type-interface.ts`, `DefaultJwtModuleOptionsFactory`
|
|
70
|
+
Good that it defaults to `false`, but the option exists and can be enabled via env var `JWT_ACCESS_IGNORE_EXPIRATION=true`. This should be restricted to development only. No guard rail exists.
|
|
71
|
+
|
|
72
|
+
### 🟡 SEC-6: Secrets read directly from `process.env` in `DefaultJwtModuleOptionsFactory`
|
|
73
|
+
|
|
74
|
+
**File:** `lib/module/jwt-module.ts` lines 224-248
|
|
75
|
+
JWT secrets are read from `process.env` directly (not `configService.get()`) — inconsistent with the `parseOption` method above which uses `configService`. If `ConfigService` is configured with custom env file paths or validation, secrets would bypass that.
|
|
76
|
+
|
|
77
|
+
### 🟡 SEC-7: `handleRequest` in guards logs no audit trail
|
|
78
|
+
|
|
79
|
+
**Files:** `jwt-auth.guard.ts`, `jwt-refresh.guard.ts`
|
|
80
|
+
Auth failures throw `AppException` but don't log the attempt. Failed authentication attempts should be logged for security monitoring.
|
|
81
|
+
|
|
82
|
+
---
|
|
83
|
+
|
|
84
|
+
## 2. Possible Bugs
|
|
85
|
+
|
|
86
|
+
### 🔴 BUG-1: `resolveJwtRefreshOption` mutates the input object
|
|
87
|
+
|
|
88
|
+
**File:** `lib/module/jwt-module.ts` — `resolveJwtRefreshOption()`
|
|
89
|
+
|
|
90
|
+
```typescript
|
|
91
|
+
if(refreshOption.isBase64AsymmetricKey === undefined) {
|
|
92
|
+
refreshOption.isBase64AsymmetricKey = jwtOption.isBase64AsymmetricKey;
|
|
93
|
+
}
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
This mutates `refreshOption` in place. If the same options object is reused (common in testing), this causes side effects.
|
|
97
|
+
|
|
98
|
+
### 🟡 BUG-2: `sanitizeJwtModuleOptions` uses shallow spread
|
|
99
|
+
|
|
100
|
+
**File:** `lib/module/jwt-module.ts`
|
|
101
|
+
`{ ...defaultOptions, ...options }` is a shallow merge. Nested objects like `jwtOptions` won't be deep-merged with defaults.
|
|
102
|
+
|
|
103
|
+
### 🟡 BUG-3: `isAuthority` type guard has operator precedence issue
|
|
104
|
+
|
|
105
|
+
**File:** `lib/utils/utils.ts`
|
|
106
|
+
|
|
107
|
+
```typescript
|
|
108
|
+
return ('id' in val && typeof val.id === 'string' || typeof val.id === 'number') && 'name' in val && typeof val.name === 'string';
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
Due to operator precedence, this evaluates as:
|
|
112
|
+
```
|
|
113
|
+
('id' in val && typeof val.id === 'string') || (typeof val.id === 'number' && 'name' in val && typeof val.name === 'string')
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
Objects with `id: string` but no `name` property would return `true`. Should be:
|
|
117
|
+
```typescript
|
|
118
|
+
return 'id' in val && (typeof val.id === 'string' || typeof val.id === 'number') && 'name' in val && typeof val.name === 'string';
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
### 🟡 BUG-4: `PermissionAuthGuard` single vs array CASL permission handling
|
|
122
|
+
|
|
123
|
+
**File:** `lib/guard/permission-auth.guard.ts` lines 32-39
|
|
124
|
+
|
|
125
|
+
```typescript
|
|
126
|
+
if (typeof firstPermission === 'string') {
|
|
127
|
+
return this.permissionProvider.checkPermission(context, firstPermission);
|
|
128
|
+
} else if (isRequiredCaslPermission(firstPermission)) {
|
|
129
|
+
return this.permissionProvider.checkPermission(context, [firstPermission] as RequiredCaslPermission[]);
|
|
130
|
+
} else {
|
|
131
|
+
return this.permissionProvider.checkPermission(context, requirePermission as RequiredCaslPermission[]);
|
|
132
|
+
}
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
When a single CASL permission is passed, only the first permission is checked; the rest are silently ignored. `@RequiredPermission(['create', 'Invoice'], ['read', 'Invoice'])` — the second permission would be ignored if the first one matches `isRequiredCaslPermission`.
|
|
136
|
+
|
|
137
|
+
**Wait** — re-reading: if `firstPermission` is a single `RequiredCaslPermission` (tuple), it wraps it in an array. But if `firstPermission` is itself an array of tuples (nested), it passes the full `requirePermission`. The logic is fragile and depends on how `@RequiredPermission()` is called. With `@RequiredPermission(['create', 'Invoice'], ['read', 'Invoice'])`, `requirePermission` = `[['create', 'Invoice'], ['read', 'Invoice']]`, `firstPermission` = `['create', 'Invoice']` which is `isRequiredCaslPermission` → wraps as `[['create', 'Invoice']]` → **drops the second permission**. This is a **real bug**.
|
|
138
|
+
|
|
139
|
+
### 🟡 BUG-5: `Principal` decorator doesn't handle non-HTTP contexts
|
|
140
|
+
|
|
141
|
+
**File:** `lib/decorator/principal.decorator.ts`
|
|
142
|
+
Uses `ctx.switchToHttp().getRequest()` — fails silently for WebSocket or gRPC contexts. Returns `undefined`.
|
|
143
|
+
|
|
144
|
+
### 🟡 BUG-6: `DefaultPermissionProvider.checkPermission` returns `false` instead of throwing for RPC/WS
|
|
145
|
+
|
|
146
|
+
The TODO comments for RPC and WS suggest `user = context.switchToRpc().getData()?.user` which may not be the correct way to extract the user in all RPC frameworks (e.g., gRPC metadata).
|
|
147
|
+
|
|
148
|
+
---
|
|
149
|
+
|
|
150
|
+
## 3. Type Safety
|
|
151
|
+
|
|
152
|
+
### 🟡 TYPE-1: Pervasive `any` usage
|
|
153
|
+
|
|
154
|
+
| Location | Issue |
|
|
155
|
+
|---|---|
|
|
156
|
+
| `IPrincipal.extra` | `any` |
|
|
157
|
+
| `AuthProvider.getPrincipal(jwtPayload: any)` | Payload untyped |
|
|
158
|
+
| `AuthProvider.isDuplicateLogin(..., jwtPayload: any)` | Payload untyped |
|
|
159
|
+
| `StrategyProvider.validate(req: Req, payload: any)` | `Req = any`, payload `any` |
|
|
160
|
+
| `CaslPermissionSubject` | `= any` |
|
|
161
|
+
| `handleRequest(err, user, info)` | All 3 params untyped |
|
|
162
|
+
| `JwtModuleAsyncOptions.inject` | `any[]` |
|
|
163
|
+
|
|
164
|
+
### 🟡 TYPE-2: Duplicate `validate` method signatures in `JwtStrategyProvider`
|
|
165
|
+
|
|
166
|
+
**File:** `lib/strategy/jwt-strategy-provider.ts`
|
|
167
|
+
|
|
168
|
+
```typescript
|
|
169
|
+
abstract validate(req: Req, payload: any): IPrincipal | Promise<IPrincipal>;
|
|
170
|
+
abstract validate<T>(req: Req, payload: any): T | Promise<T>;
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
Two abstract declarations — TypeScript only uses the last one. The first is dead code. Same issue in `StrategyProvider` and `IStrategyProvider`.
|
|
174
|
+
|
|
175
|
+
### 🟡 TYPE-3: `isCaslPermission` doesn't check types of `subject` and `action`
|
|
176
|
+
|
|
177
|
+
**File:** `lib/utils/utils.ts`
|
|
178
|
+
Only checks `'subject' in val && 'action' in val` — any object with those two keys matches. Should validate `action` is a valid `CaslPermissionAction`.
|
|
179
|
+
|
|
180
|
+
---
|
|
181
|
+
|
|
182
|
+
## 4. Code Cleanliness
|
|
183
|
+
|
|
184
|
+
### CLEAN-1: Dead commented code
|
|
185
|
+
`lib/decorator/permission.decorator.ts` — large block of commented-out code (alternative `RequiredPermission` implementation).
|
|
186
|
+
|
|
187
|
+
### CLEAN-2: Typo in function name
|
|
188
|
+
`lib/utils/utils.ts` — `isAysmmetricAlgo` should be `isAsymmetricAlgo`.
|
|
189
|
+
|
|
190
|
+
### CLEAN-3: Variable shadowing
|
|
191
|
+
`lib/guard/jwt-auth.guard.ts` — `const RefreshToken` shadows the imported `REFRESH_TOKEN` constant.
|
|
192
|
+
|
|
193
|
+
### CLEAN-4: Unused import
|
|
194
|
+
`lib/decorator/principal.decorator.ts` — imports `SetMetadata` but doesn't use it.
|
|
195
|
+
|
|
196
|
+
### CLEAN-5: Barrel file re-exports are verbose
|
|
197
|
+
`lib/constant/index.ts` imports and re-exports individually instead of using `export *`.
|
|
198
|
+
|
|
199
|
+
### CLEAN-6: Inconsistent naming
|
|
200
|
+
- `jwtOptions` (property) vs `JwtOptions` (type) vs `JWT_OPTIONS` (injection token) — fine
|
|
201
|
+
- `refreshJwtOptions` vs `JwtRefreshStrategyProvider` — prefix inconsistency (refresh-first vs jwt-first)
|
|
202
|
+
|
|
203
|
+
---
|
|
204
|
+
|
|
205
|
+
## 5. Architecture & Extensibility
|
|
206
|
+
|
|
207
|
+
### ✅ Strengths
|
|
208
|
+
- Clean provider abstraction — easy to swap `AuthProvider`, `PermissionProvider`, `CaslAbilityFactory`
|
|
209
|
+
- `forRoot`/`forRootAsync` follows NestJS conventions
|
|
210
|
+
- CASL integration is well-separated via `CaslAbilityFactory`
|
|
211
|
+
- Base64 asymmetric key support is a nice enterprise feature
|
|
212
|
+
- `ProviderClassOrAsyncOptions` pattern enables flexible DI
|
|
213
|
+
|
|
214
|
+
### ⚠️ Concerns
|
|
215
|
+
|
|
216
|
+
**ARCH-1:** Guards are not self-contained modules. Consumers must manually wire `JwtAuthGuard` and `PermissionAuthGuard` as global guards. The `JwtModule` doesn't register them — this could lead to misconfiguration.
|
|
217
|
+
|
|
218
|
+
**ARCH-2:** `JwtStrategy` and `JwtRefreshStrategy` are `@Injectable()` but not registered in `JwtModule` providers. They must be manually added by consumers (visible in e2e test). This is error-prone.
|
|
219
|
+
|
|
220
|
+
**ARCH-3:** `PermissionProvider` is not provided by `JwtModule`. It's a loose dependency that consumers must wire themselves. Consider adding it to the module or creating a separate `AuthorizationModule`.
|
|
221
|
+
|
|
222
|
+
**ARCH-4:** The package doesn't export `JwtNestJsOptions` in a discoverable way — it's nested under `strategy/type-interface.ts` but available via barrel exports.
|
|
223
|
+
|
|
224
|
+
**ARCH-5:** No GraphQL context support. `getType() === 'graphql'` is mentioned in comments but not implemented.
|
|
225
|
+
|
|
226
|
+
---
|
|
227
|
+
|
|
228
|
+
## 6. Test Coverage Gaps
|
|
229
|
+
|
|
230
|
+
### What's tested ✅
|
|
231
|
+
- `SimpleAuthProvider` — principal mapping, duplicate login
|
|
232
|
+
- `DefaultJwtStrategyProvider` — all validation paths (revoked, blacklisted, IP binding, duplicate login)
|
|
233
|
+
- `DefaultPermissionProvider` — string permissions, authority objects, edge cases
|
|
234
|
+
- `PermissionAuthGuard` — reflector integration
|
|
235
|
+
- `JwtModule.forRoot` — symmetric + asymmetric + base64 key configs
|
|
236
|
+
- `JwtModule.forRootAsync` — useClass, custom providers, factory
|
|
237
|
+
- E2E — full request lifecycle with guards, public/protected/permission endpoints
|
|
238
|
+
|
|
239
|
+
### What's NOT tested ❌
|
|
240
|
+
|
|
241
|
+
| Gap | Risk |
|
|
242
|
+
|---|---|
|
|
243
|
+
| `JwtAuthGuard` unit tests | HIGH — `@RefreshToken()` bypass not explicitly tested |
|
|
244
|
+
| `JwtRefreshGuard` | HIGH — no tests at all |
|
|
245
|
+
| `JwtRefreshStrategy.validate()` — passReqToCallback arg routing | MEDIUM |
|
|
246
|
+
| `JwtStrategy.validate()` — passReqToCallback arg routing | MEDIUM |
|
|
247
|
+
| `JwtCaslAbilityFactory` | MEDIUM — CASL permission extraction from JWT |
|
|
248
|
+
| `BaseCaslAbilityFactory` | MEDIUM |
|
|
249
|
+
| CASL-based `@RequiredPermission()` with tuples | HIGH — the multi-permission bug (BUG-4) would be caught |
|
|
250
|
+
| `DefaultJwtModuleOptionsFactory` | LOW — env-based config |
|
|
251
|
+
| `createJwtProvider` with base64 keys | LOW — covered implicitly by module tests |
|
|
252
|
+
| `isRequiredCaslPermission` / `isCaslPermissionAction` utils | LOW |
|
|
253
|
+
| `Principal` decorator | LOW |
|
|
254
|
+
| `RefreshToken` decorator | LOW |
|
|
255
|
+
| WebSocket / RPC context in `DefaultPermissionProvider` | MEDIUM |
|
|
256
|
+
| `resolveJwtRefreshOption` fallback logic | LOW |
|
|
257
|
+
|
|
258
|
+
---
|
|
259
|
+
|
|
260
|
+
## 7. Public API Surface
|
|
261
|
+
|
|
262
|
+
### Exports analysis
|
|
263
|
+
The package exports **everything** via barrel files. This includes:
|
|
264
|
+
- Internal constants (`JWT_OPTIONS`, `AUTH_PROVIDER`, `JWT_PROVIDER`, `STRATEGY_PROVIDER`)
|
|
265
|
+
- Abstract classes that are implementation details (`StrategyProvider`, `BaseCaslAbilityFactory`)
|
|
266
|
+
- Type utilities (`isAuthority`, `isCaslPermission`, `isRequiredCaslPermission`, `isCaslPermissionAction`)
|
|
267
|
+
|
|
268
|
+
### Over-exports
|
|
269
|
+
- `JWT_PROVIDER` is defined but never used anywhere in the codebase
|
|
270
|
+
- `STRATEGY_PROVIDER` is defined but never used
|
|
271
|
+
- `JWT_API_STRATEGY_NAME` and `JWT_GRPC_STRATEGY_NAME` are defined but have no corresponding strategy implementations
|
|
272
|
+
- `IStrategyProvider` interface + `StrategyProvider` abstract class — redundant, only `StrategyProvider` is used
|
|
273
|
+
|
|
274
|
+
### Missing exports
|
|
275
|
+
- No re-export of `@nathapp/auth` types that consumers might need (e.g., `JwtProvider`)
|
|
276
|
+
- `createJwtProvider` is exported but probably shouldn't be (internal utility)
|
|
277
|
+
|
|
278
|
+
### Breaking change risks
|
|
279
|
+
- `IPrincipal` interface changes would break all `AuthProvider` implementations
|
|
280
|
+
- `PermissionType` union type changes would break `PermissionProvider` implementations
|
|
281
|
+
- Removing the `@RefreshToken()` bypass from `JwtAuthGuard` (the fix for SEC-1) is a breaking change for existing consumers
|
|
282
|
+
|
|
283
|
+
---
|
|
284
|
+
|
|
285
|
+
## Priority Action Items
|
|
286
|
+
|
|
287
|
+
| # | Severity | Issue | Description | Effort |
|
|
288
|
+
|---|---|---|---|---|
|
|
289
|
+
| 1 | 🔴 Critical | SEC-1 | `@RefreshToken()` bypasses all auth in `JwtAuthGuard` | Medium |
|
|
290
|
+
| 2 | 🔴 High | BUG-4 | Multi-CASL-permission `@RequiredPermission()` drops permissions | Low |
|
|
291
|
+
| 3 | 🔴 High | BUG-3 | `isAuthority` operator precedence bug | Low |
|
|
292
|
+
| 4 | 🟡 Medium | SEC-4 | `SimpleAuthProvider` trusts JWT payload — add warnings/docs | Low |
|
|
293
|
+
| 5 | 🟡 Medium | SEC-6 | Use `configService` consistently for secrets | Low |
|
|
294
|
+
| 6 | 🟡 Medium | TYPE-2 | Remove duplicate abstract `validate` signatures | Low |
|
|
295
|
+
| 7 | 🟡 Medium | Test gaps | Add unit tests for `JwtAuthGuard`, `JwtRefreshGuard`, CASL flow | Medium |
|
|
296
|
+
| 8 | 🟡 Medium | ARCH-2 | Register `JwtStrategy`/`JwtRefreshStrategy` in `JwtModule` | Medium |
|
|
297
|
+
| 9 | 🟢 Low | CLEAN-2 | Fix `isAysmmetricAlgo` typo | Trivial |
|
|
298
|
+
| 10 | 🟢 Low | CLEAN-1 | Remove dead commented code | Trivial |
|
|
299
|
+
| 11 | 🟢 Low | CLEAN-4 | Remove unused `SetMetadata` import | Trivial |
|
|
300
|
+
| 12 | 🟢 Low | Over-exports | Remove unused constants (`JWT_PROVIDER`, `STRATEGY_PROVIDER`) | Low |
|