@bymax-one/nest-auth 1.4.12 → 1.4.13
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/CHANGELOG.md +38 -1
- package/README.md +7 -0
- package/dist/server/index.cjs +46 -20
- package/dist/server/index.d.cts +15 -3
- package/dist/server/index.d.ts +15 -3
- package/dist/server/index.mjs +47 -21
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -18,6 +18,42 @@ what moves, and that note is the compatibility contract until strict SemVer begi
|
|
|
18
18
|
|
|
19
19
|
## [Unreleased]
|
|
20
20
|
|
|
21
|
+
## [1.4.13] - 2026-11-01
|
|
22
|
+
|
|
23
|
+
### Apply to a derived backend
|
|
24
|
+
|
|
25
|
+
- **A client address that is not an address is no longer a rate-limit key, and no longer reaches a
|
|
26
|
+
hook, a session or an audit event.** With `rateLimit.clientIpSource: 'trusted-proxy'` the limiter
|
|
27
|
+
keyed on `req.ip`, and behind a hop that forwards the client's own `X-Forwarded-For` unchanged
|
|
28
|
+
Express returns the first entry its `trust proxy` setting does not trust, which is an arbitrary
|
|
29
|
+
string when that entry is not an address. A client that varied it on every request got a fresh
|
|
30
|
+
budget each time. One function now resolves
|
|
31
|
+
the address for the limiter and for every place that records it: `req.ip`, then the rest of
|
|
32
|
+
`req.ips` (the trusted hops, nearest the client first), then the socket peer, taking the first
|
|
33
|
+
that is an address. A header entry that carries an IPv6 zone index is not one, while the socket peer's zone,
|
|
34
|
+
which a link-local peer reports, is cut. An unparsable client entry is therefore
|
|
35
|
+
charged to the nearest trusted hop, so every caller that sends one shares that hop's bucket; a
|
|
36
|
+
chain with no address in it at all falls back to the shared unknown bucket, as before.
|
|
37
|
+
**What consumers see differently:** `HookContext.ip`, which every hook receives, and the `ip` of
|
|
38
|
+
the `onApiKeyRequest` audit event used to carry the raw `req.ip`; they now carry an address, or an empty string when none can be read. The `ip`
|
|
39
|
+
stored on a session and shown by the sessions list and the new-session email follows the same
|
|
40
|
+
value. In `clientIpSource: 'peer'` the limiter still keys on the socket; the recorded address
|
|
41
|
+
still comes from `req.ip` when the application sets `trust proxy`, and is validated the same
|
|
42
|
+
way.
|
|
43
|
+
**Not changed:**
|
|
44
|
+
`'trusted-proxy'` is only as honest as the hops in front of the app. Behind such a proxy each hop
|
|
45
|
+
appends the address it saw, so the client is the rightmost entry that is not itself a trusted
|
|
46
|
+
proxy. A hop that forwards the client's own `X-Forwarded-For` unchanged lets the client choose
|
|
47
|
+
its address even when every entry is a valid one, and a client that varies a valid address still
|
|
48
|
+
gets a fresh budget each time. Behind
|
|
49
|
+
such a hop use `'peer'`, or make the hop append or overwrite the header.
|
|
50
|
+
|
|
51
|
+
### Fixed
|
|
52
|
+
|
|
53
|
+
- **The per-address rate limit could be bypassed behind a proxy that forwards `X-Forwarded-For`
|
|
54
|
+
unchanged.** See the Apply note above.
|
|
55
|
+
- **A client-written non-address string could reach hook payloads and API-key audit events.** See the Apply note above.
|
|
56
|
+
|
|
21
57
|
## [1.4.12] - 2026-11-01
|
|
22
58
|
|
|
23
59
|
### Apply to a derived backend
|
|
@@ -4185,7 +4221,8 @@ ever installable.
|
|
|
4185
4221
|
- Phase 4 password-reset tests cover: both `token` and `otp` flows, mutual exclusivity validation, `verifiedToken` exchange, resend cooldown, anti-enumeration (no error on unknown email), and session invalidation on reset
|
|
4186
4222
|
- Phase 5 tests cover: platform login with MFA path and brute-force lockout, `JwtPlatformGuard` cross-context rejection, `PlatformRolesGuard` hierarchy enforcement, OAuth CSRF state lifecycle, `onOAuthLogin` hook resolution strategies, and invitation role-authorization + acceptance single-use enforcement
|
|
4187
4223
|
|
|
4188
|
-
[Unreleased]: https://github.com/bymaxone/nest-auth/compare/v1.4.
|
|
4224
|
+
[Unreleased]: https://github.com/bymaxone/nest-auth/compare/v1.4.13...HEAD
|
|
4225
|
+
[1.4.13]: https://github.com/bymaxone/nest-auth/compare/v1.4.12...v1.4.13
|
|
4189
4226
|
[1.4.12]: https://github.com/bymaxone/nest-auth/compare/v1.4.11...v1.4.12
|
|
4190
4227
|
[1.4.11]: https://github.com/bymaxone/nest-auth/compare/v1.4.10...v1.4.11
|
|
4191
4228
|
[1.4.10]: https://github.com/bymaxone/nest-auth/compare/v1.4.9...v1.4.10
|
package/README.md
CHANGED
|
@@ -961,6 +961,13 @@ All options are configurable via `registerAsync()`. Here are the key configurati
|
|
|
961
961
|
> `'trusted-proxy'` without a proxy lets the caller choose their own key. Both look like a
|
|
962
962
|
> working limiter at runtime. Pass `rateLimit.enabled: false` if the limits are enforced at the
|
|
963
963
|
> edge instead.
|
|
964
|
+
>
|
|
965
|
+
> `'trusted-proxy'` is only as honest as the hops in front of the app. Behind such a proxy each
|
|
966
|
+
> hop appends the address it saw, so the client is the rightmost entry that is not itself a
|
|
967
|
+
> trusted proxy. A hop that forwards the client's own `X-Forwarded-For` unchanged lets the client
|
|
968
|
+
> choose its address, so a client that varies it gets a fresh budget every time; behind such a hop
|
|
969
|
+
> use `'peer'`, or make the hop append or overwrite the header. An entry that is not an address at
|
|
970
|
+
> all is never a key: the limit is charged to the nearest trusted hop instead.
|
|
964
971
|
|
|
965
972
|
> [!TIP]
|
|
966
973
|
> **Binding tokens to an issuer and an audience.** `jwt.issuer` and `jwt.audience` are off by
|
package/dist/server/index.cjs
CHANGED
|
@@ -2889,6 +2889,31 @@ function isWsTicketSnapshot(value) {
|
|
|
2889
2889
|
return typeof record["sub"] === "string" && typeof record["role"] === "string" && typeof record["status"] === "string" && typeof record["mfaEnabled"] === "boolean" && typeof record["mfaVerified"] === "boolean" && typeof record["jti"] === "string" && record["jti"] !== "" && typeof record["epoch"] === "number" && Number.isSafeInteger(record["epoch"]) && record["epoch"] >= 0 && (record["tenantId"] === void 0 || typeof record["tenantId"] === "string");
|
|
2890
2890
|
}
|
|
2891
2891
|
__name(isWsTicketSnapshot, "isWsTicketSnapshot");
|
|
2892
|
+
var IPV6 = 6;
|
|
2893
|
+
function isHeaderAddress(candidate) {
|
|
2894
|
+
return typeof candidate === "string" && !candidate.includes("%") && net.isIP(candidate) !== 0;
|
|
2895
|
+
}
|
|
2896
|
+
__name(isHeaderAddress, "isHeaderAddress");
|
|
2897
|
+
function peerAddress(remoteAddress) {
|
|
2898
|
+
if (typeof remoteAddress !== "string") return "";
|
|
2899
|
+
const zoneIndex = remoteAddress.indexOf("%");
|
|
2900
|
+
const bare = zoneIndex === -1 ? remoteAddress : remoteAddress.slice(0, zoneIndex);
|
|
2901
|
+
const family = net.isIP(bare);
|
|
2902
|
+
if (zoneIndex !== -1) return family === IPV6 ? bare : "";
|
|
2903
|
+
return family === 0 ? "" : bare;
|
|
2904
|
+
}
|
|
2905
|
+
__name(peerAddress, "peerAddress");
|
|
2906
|
+
function resolveClientAddress(request) {
|
|
2907
|
+
const hops = request.ips ?? [];
|
|
2908
|
+
for (const candidate of [
|
|
2909
|
+
request.ip,
|
|
2910
|
+
...hops
|
|
2911
|
+
]) {
|
|
2912
|
+
if (isHeaderAddress(candidate)) return candidate;
|
|
2913
|
+
}
|
|
2914
|
+
return peerAddress(request.socket?.remoteAddress);
|
|
2915
|
+
}
|
|
2916
|
+
__name(resolveClientAddress, "resolveClientAddress");
|
|
2892
2917
|
var IPV6_GROUPS = 8;
|
|
2893
2918
|
var IPV6_PREFIX_GROUPS = 4;
|
|
2894
2919
|
function rateLimitBucket(address) {
|
|
@@ -3009,8 +3034,9 @@ exports.AuthRateLimitGuard = class AuthRateLimitGuard {
|
|
|
3009
3034
|
* The address this request is counted against.
|
|
3010
3035
|
*
|
|
3011
3036
|
* `'peer'` — the default — reads the socket directly, so `X-Forwarded-For` cannot influence
|
|
3012
|
-
* it. `'trusted-proxy'` uses
|
|
3013
|
-
* forwarding headers it admits
|
|
3037
|
+
* it. `'trusted-proxy'` uses the resolved client address, which starts from `req.ip` (honouring
|
|
3038
|
+
* the app's `trust proxy` setting and the forwarding headers it admits) and never returns a
|
|
3039
|
+
* string that is not an address, so a caller cannot mint a bucket by writing one.
|
|
3014
3040
|
*
|
|
3015
3041
|
* The default is the strict one because the two failure modes are not symmetric: keying on
|
|
3016
3042
|
* the peer address behind a proxy over-counts, which is visible and recoverable, while
|
|
@@ -3023,7 +3049,7 @@ exports.AuthRateLimitGuard = class AuthRateLimitGuard {
|
|
|
3023
3049
|
*/
|
|
3024
3050
|
clientIp(request) {
|
|
3025
3051
|
if (this.options.rateLimit.clientIpSource === "trusted-proxy") {
|
|
3026
|
-
return
|
|
3052
|
+
return resolveClientAddress(request) || UNKNOWN_CLIENT;
|
|
3027
3053
|
}
|
|
3028
3054
|
const peer = request.socket?.remoteAddress;
|
|
3029
3055
|
return typeof peer === "string" && peer !== "" ? peer : UNKNOWN_CLIENT;
|
|
@@ -6622,7 +6648,7 @@ exports.AuthService = class _AuthService {
|
|
|
6622
6648
|
*/
|
|
6623
6649
|
async register(dto, req) {
|
|
6624
6650
|
const tenantId = await this.resolveTenantId(dto.tenantId, req);
|
|
6625
|
-
const ip = req
|
|
6651
|
+
const ip = resolveClientAddress(req);
|
|
6626
6652
|
const userAgent = String(req.headers["user-agent"] ?? "");
|
|
6627
6653
|
dto = {
|
|
6628
6654
|
...dto,
|
|
@@ -6715,7 +6741,7 @@ exports.AuthService = class _AuthService {
|
|
|
6715
6741
|
*/
|
|
6716
6742
|
async login(dto, req) {
|
|
6717
6743
|
const tenantId = await this.resolveTenantId(dto.tenantId, req);
|
|
6718
|
-
const ip = req
|
|
6744
|
+
const ip = resolveClientAddress(req);
|
|
6719
6745
|
const userAgent = String(req.headers["user-agent"] ?? "");
|
|
6720
6746
|
dto = {
|
|
6721
6747
|
...dto,
|
|
@@ -7794,7 +7820,7 @@ var AuthController = class {
|
|
|
7794
7820
|
*/
|
|
7795
7821
|
async refresh(req, res) {
|
|
7796
7822
|
const rawRefreshToken = this.tokenDelivery.extractRefreshToken(req) ?? "";
|
|
7797
|
-
const ip = req
|
|
7823
|
+
const ip = resolveClientAddress(req);
|
|
7798
7824
|
const userAgent = String(req.headers["user-agent"] ?? "");
|
|
7799
7825
|
const rotated = await this.authService.refresh(rawRefreshToken, ip, userAgent);
|
|
7800
7826
|
const authResult = {
|
|
@@ -9286,7 +9312,7 @@ var InvitationController = class {
|
|
|
9286
9312
|
* @returns Auth response with tokens delivered per the configured `tokenDelivery` mode.
|
|
9287
9313
|
*/
|
|
9288
9314
|
async accept(dto, req, res) {
|
|
9289
|
-
const ip = req
|
|
9315
|
+
const ip = resolveClientAddress(req);
|
|
9290
9316
|
const userAgent = String(req.headers["user-agent"] ?? "");
|
|
9291
9317
|
const headers = req.headers;
|
|
9292
9318
|
const result = await this.invitationService.acceptInvitation(dto, ip, userAgent, headers);
|
|
@@ -11029,7 +11055,7 @@ var MfaController = class {
|
|
|
11029
11055
|
* @throws `MFA_INVALID_CODE` if the submitted TOTP code is invalid.
|
|
11030
11056
|
*/
|
|
11031
11057
|
async verifyEnable(user, dto, req) {
|
|
11032
|
-
const ip = req
|
|
11058
|
+
const ip = resolveClientAddress(req);
|
|
11033
11059
|
const userAgent = String(req.headers["user-agent"] ?? "");
|
|
11034
11060
|
await this.mfaService.verifyAndEnable(user.sub, dto.code, ip, userAgent, "dashboard", user.tenantId);
|
|
11035
11061
|
}
|
|
@@ -11062,7 +11088,7 @@ var MfaController = class {
|
|
|
11062
11088
|
* @throws `MFA_INVALID_CODE` if the submitted code is incorrect.
|
|
11063
11089
|
*/
|
|
11064
11090
|
async challenge(dto, req, res) {
|
|
11065
|
-
const ip = req
|
|
11091
|
+
const ip = resolveClientAddress(req);
|
|
11066
11092
|
const userAgent = String(req.headers["user-agent"] ?? "");
|
|
11067
11093
|
const cookieToken = readMfaTempCookie(req);
|
|
11068
11094
|
const mfaTempToken = dto.mfaTempToken ?? cookieToken;
|
|
@@ -11101,7 +11127,7 @@ var MfaController = class {
|
|
|
11101
11127
|
* @throws `MFA_INVALID_CODE` if the submitted TOTP code is incorrect.
|
|
11102
11128
|
*/
|
|
11103
11129
|
async disable(user, dto, req) {
|
|
11104
|
-
const ip = req
|
|
11130
|
+
const ip = resolveClientAddress(req);
|
|
11105
11131
|
const userAgent = String(req.headers["user-agent"] ?? "");
|
|
11106
11132
|
await this.mfaService.disable(user.sub, dto.code, ip, userAgent, "dashboard", user.tenantId);
|
|
11107
11133
|
}
|
|
@@ -11125,7 +11151,7 @@ var MfaController = class {
|
|
|
11125
11151
|
* @returns The fresh plain-text recovery codes, one-time display.
|
|
11126
11152
|
*/
|
|
11127
11153
|
async regenerateRecoveryCodes(user, dto, req) {
|
|
11128
|
-
const ip = req
|
|
11154
|
+
const ip = resolveClientAddress(req);
|
|
11129
11155
|
const userAgent = String(req.headers["user-agent"] ?? "");
|
|
11130
11156
|
return this.mfaService.regenerateRecoveryCodes(user.sub, dto.code, ip, userAgent, "dashboard", user.tenantId);
|
|
11131
11157
|
}
|
|
@@ -12963,7 +12989,7 @@ var PlatformAuthController = class {
|
|
|
12963
12989
|
* @param req - Incoming request (provides IP and User-Agent for session tracking).
|
|
12964
12990
|
*/
|
|
12965
12991
|
async login(dto, req) {
|
|
12966
|
-
const ip = req
|
|
12992
|
+
const ip = resolveClientAddress(req);
|
|
12967
12993
|
const userAgent = String(req.headers["user-agent"] ?? "").slice(0, 512);
|
|
12968
12994
|
const result = await this.platformAuthService.login(dto, ip, userAgent);
|
|
12969
12995
|
if (isMfaChallenge2(result)) {
|
|
@@ -12992,7 +13018,7 @@ var PlatformAuthController = class {
|
|
|
12992
13018
|
* @throws `PLATFORM_AUTH_REQUIRED` when the submitted MFA token has `context='dashboard'`.
|
|
12993
13019
|
*/
|
|
12994
13020
|
async mfaChallenge(dto, req) {
|
|
12995
|
-
const ip = req
|
|
13021
|
+
const ip = resolveClientAddress(req);
|
|
12996
13022
|
const userAgent = String(req.headers["user-agent"] ?? "").slice(0, 512);
|
|
12997
13023
|
if (dto.mfaTempToken === void 0 || dto.mfaTempToken.length === 0) {
|
|
12998
13024
|
throw new AuthException(AUTH_ERROR_CODES.MFA_TEMP_TOKEN_INVALID);
|
|
@@ -13056,7 +13082,7 @@ var PlatformAuthController = class {
|
|
|
13056
13082
|
*/
|
|
13057
13083
|
async refresh(req) {
|
|
13058
13084
|
const rawRefreshToken = this.tokenDelivery.extractPlatformRefreshToken(req) ?? "";
|
|
13059
|
-
const ip = req
|
|
13085
|
+
const ip = resolveClientAddress(req);
|
|
13060
13086
|
const userAgent = String(req.headers["user-agent"] ?? "").slice(0, 512);
|
|
13061
13087
|
const rotated = await this.platformAuthService.refresh(rawRefreshToken, ip, userAgent);
|
|
13062
13088
|
const admin = await this.platformAuthService.getMe(rotated.session.userId);
|
|
@@ -13243,7 +13269,7 @@ var PlatformMfaController = class {
|
|
|
13243
13269
|
* @throws `MFA_INVALID_CODE` if the submitted TOTP code is invalid.
|
|
13244
13270
|
*/
|
|
13245
13271
|
async verifyEnable(user, dto, req) {
|
|
13246
|
-
const ip = req
|
|
13272
|
+
const ip = resolveClientAddress(req);
|
|
13247
13273
|
const userAgent = String(req.headers["user-agent"] ?? "");
|
|
13248
13274
|
await this.mfaService.verifyAndEnable(user.sub, dto.code, ip, userAgent, "platform");
|
|
13249
13275
|
}
|
|
@@ -13261,7 +13287,7 @@ var PlatformMfaController = class {
|
|
|
13261
13287
|
* @throws `MFA_INVALID_CODE` if the submitted TOTP code is incorrect.
|
|
13262
13288
|
*/
|
|
13263
13289
|
async disable(user, dto, req) {
|
|
13264
|
-
const ip = req
|
|
13290
|
+
const ip = resolveClientAddress(req);
|
|
13265
13291
|
const userAgent = String(req.headers["user-agent"] ?? "");
|
|
13266
13292
|
await this.mfaService.disable(user.sub, dto.code, ip, userAgent, "platform");
|
|
13267
13293
|
}
|
|
@@ -13281,7 +13307,7 @@ var PlatformMfaController = class {
|
|
|
13281
13307
|
* @returns The fresh plain-text recovery codes, one-time display.
|
|
13282
13308
|
*/
|
|
13283
13309
|
async regenerateRecoveryCodes(user, dto, req) {
|
|
13284
|
-
const ip = req
|
|
13310
|
+
const ip = resolveClientAddress(req);
|
|
13285
13311
|
const userAgent = String(req.headers["user-agent"] ?? "");
|
|
13286
13312
|
return this.mfaService.regenerateRecoveryCodes(user.sub, dto.code, ip, userAgent, "platform");
|
|
13287
13313
|
}
|
|
@@ -13920,7 +13946,7 @@ exports.ApiKeyGuard = class _ApiKeyGuard {
|
|
|
13920
13946
|
...partial,
|
|
13921
13947
|
keyId: key?.id ?? null,
|
|
13922
13948
|
label: key?.label ?? null,
|
|
13923
|
-
ip: request
|
|
13949
|
+
ip: resolveClientAddress(request)
|
|
13924
13950
|
};
|
|
13925
13951
|
const context = this.hookContext(request);
|
|
13926
13952
|
const run = /* @__PURE__ */ __name(() => hook.call(this.hooks, event, context), "run");
|
|
@@ -13945,7 +13971,7 @@ exports.ApiKeyGuard = class _ApiKeyGuard {
|
|
|
13945
13971
|
hookContext(request) {
|
|
13946
13972
|
const headers = Object.fromEntries(Object.entries(request.headers).filter(([name]) => name !== this.options.apiKeys.header));
|
|
13947
13973
|
return {
|
|
13948
|
-
ip: request
|
|
13974
|
+
ip: resolveClientAddress(request),
|
|
13949
13975
|
userAgent: request.headers["user-agent"] ?? "",
|
|
13950
13976
|
sanitizedHeaders: sanitizeHeaders(headers)
|
|
13951
13977
|
};
|
|
@@ -14955,7 +14981,7 @@ var OAuthController = class _OAuthController {
|
|
|
14955
14981
|
* payload, or `undefined` when a redirect was issued.
|
|
14956
14982
|
*/
|
|
14957
14983
|
async callback(provider, query, req, res) {
|
|
14958
|
-
const ip = (req
|
|
14984
|
+
const ip = resolveClientAddress(req).slice(0, 64);
|
|
14959
14985
|
const userAgent = String(req.headers["user-agent"] ?? "").slice(0, 512);
|
|
14960
14986
|
const stateCookie = readOAuthStateCookie(req);
|
|
14961
14987
|
if (stateCookie !== void 0) {
|
package/dist/server/index.d.cts
CHANGED
|
@@ -455,7 +455,18 @@ interface IUserRepository {
|
|
|
455
455
|
* - `'peer'` — the socket peer address, read directly from the connection. Never consults
|
|
456
456
|
* `X-Forwarded-For`, so a spoofed header cannot buy an attacker a fresh budget.
|
|
457
457
|
* - `'trusted-proxy'` — Express's `req.ip`, which honours the app's `trust proxy` setting and
|
|
458
|
-
* therefore the forwarding headers it admits.
|
|
458
|
+
* therefore the forwarding headers it admits. When that entry is not an address the nearest
|
|
459
|
+
* trusted hop is used instead, and a non-address string is never the key.
|
|
460
|
+
*
|
|
461
|
+
* `'trusted-proxy'` is only as honest as the hops in front of the app. Behind such a proxy each
|
|
462
|
+
* hop appends the address it saw, so the client is the rightmost entry that is not itself a
|
|
463
|
+
* trusted proxy. A hop that forwards the client's own `X-Forwarded-For` unchanged lets the client
|
|
464
|
+
* choose its address, and a client that varies a valid address gets a fresh budget every time;
|
|
465
|
+
* behind such a hop use `'peer'`, or make the hop append or overwrite the header.
|
|
466
|
+
*
|
|
467
|
+
* Whichever source keys the limit, the address recorded on a session, passed in a hook's context
|
|
468
|
+
* and carried by an API-key audit event is the same validated one: an address, or an empty
|
|
469
|
+
* string when none can be read.
|
|
459
470
|
*/
|
|
460
471
|
type ClientIpSource = 'peer' | 'trusted-proxy';
|
|
461
472
|
/**
|
|
@@ -5623,8 +5634,9 @@ declare class AuthRateLimitGuard implements CanActivate {
|
|
|
5623
5634
|
* The address this request is counted against.
|
|
5624
5635
|
*
|
|
5625
5636
|
* `'peer'` — the default — reads the socket directly, so `X-Forwarded-For` cannot influence
|
|
5626
|
-
* it. `'trusted-proxy'` uses
|
|
5627
|
-
* forwarding headers it admits
|
|
5637
|
+
* it. `'trusted-proxy'` uses the resolved client address, which starts from `req.ip` (honouring
|
|
5638
|
+
* the app's `trust proxy` setting and the forwarding headers it admits) and never returns a
|
|
5639
|
+
* string that is not an address, so a caller cannot mint a bucket by writing one.
|
|
5628
5640
|
*
|
|
5629
5641
|
* The default is the strict one because the two failure modes are not symmetric: keying on
|
|
5630
5642
|
* the peer address behind a proxy over-counts, which is visible and recoverable, while
|
package/dist/server/index.d.ts
CHANGED
|
@@ -455,7 +455,18 @@ interface IUserRepository {
|
|
|
455
455
|
* - `'peer'` — the socket peer address, read directly from the connection. Never consults
|
|
456
456
|
* `X-Forwarded-For`, so a spoofed header cannot buy an attacker a fresh budget.
|
|
457
457
|
* - `'trusted-proxy'` — Express's `req.ip`, which honours the app's `trust proxy` setting and
|
|
458
|
-
* therefore the forwarding headers it admits.
|
|
458
|
+
* therefore the forwarding headers it admits. When that entry is not an address the nearest
|
|
459
|
+
* trusted hop is used instead, and a non-address string is never the key.
|
|
460
|
+
*
|
|
461
|
+
* `'trusted-proxy'` is only as honest as the hops in front of the app. Behind such a proxy each
|
|
462
|
+
* hop appends the address it saw, so the client is the rightmost entry that is not itself a
|
|
463
|
+
* trusted proxy. A hop that forwards the client's own `X-Forwarded-For` unchanged lets the client
|
|
464
|
+
* choose its address, and a client that varies a valid address gets a fresh budget every time;
|
|
465
|
+
* behind such a hop use `'peer'`, or make the hop append or overwrite the header.
|
|
466
|
+
*
|
|
467
|
+
* Whichever source keys the limit, the address recorded on a session, passed in a hook's context
|
|
468
|
+
* and carried by an API-key audit event is the same validated one: an address, or an empty
|
|
469
|
+
* string when none can be read.
|
|
459
470
|
*/
|
|
460
471
|
type ClientIpSource = 'peer' | 'trusted-proxy';
|
|
461
472
|
/**
|
|
@@ -5623,8 +5634,9 @@ declare class AuthRateLimitGuard implements CanActivate {
|
|
|
5623
5634
|
* The address this request is counted against.
|
|
5624
5635
|
*
|
|
5625
5636
|
* `'peer'` — the default — reads the socket directly, so `X-Forwarded-For` cannot influence
|
|
5626
|
-
* it. `'trusted-proxy'` uses
|
|
5627
|
-
* forwarding headers it admits
|
|
5637
|
+
* it. `'trusted-proxy'` uses the resolved client address, which starts from `req.ip` (honouring
|
|
5638
|
+
* the app's `trust proxy` setting and the forwarding headers it admits) and never returns a
|
|
5639
|
+
* string that is not an address, so a caller cannot mint a bucket by writing one.
|
|
5628
5640
|
*
|
|
5629
5641
|
* The default is the strict one because the two failure modes are not symmetric: keying on
|
|
5630
5642
|
* the peer address behind a proxy over-counts, which is visible and recoverable, while
|
package/dist/server/index.mjs
CHANGED
|
@@ -5,7 +5,7 @@ import { randomInt, scrypt as scrypt$1, randomBytes, timingSafeEqual, randomUUID
|
|
|
5
5
|
import { Throttle } from '@nestjs/throttler';
|
|
6
6
|
import { Transform, plainToInstance } from 'class-transformer';
|
|
7
7
|
import { IsEmail, MaxLength, IsString, MinLength, IsOptional, IsNotEmpty, Matches, Length, ValidateIf } from 'class-validator';
|
|
8
|
-
import { isIPv6 } from 'net';
|
|
8
|
+
import { isIPv6, isIP } from 'net';
|
|
9
9
|
import { promisify } from 'util';
|
|
10
10
|
|
|
11
11
|
var __defProp = Object.defineProperty;
|
|
@@ -2887,6 +2887,31 @@ function isWsTicketSnapshot(value) {
|
|
|
2887
2887
|
return typeof record["sub"] === "string" && typeof record["role"] === "string" && typeof record["status"] === "string" && typeof record["mfaEnabled"] === "boolean" && typeof record["mfaVerified"] === "boolean" && typeof record["jti"] === "string" && record["jti"] !== "" && typeof record["epoch"] === "number" && Number.isSafeInteger(record["epoch"]) && record["epoch"] >= 0 && (record["tenantId"] === void 0 || typeof record["tenantId"] === "string");
|
|
2888
2888
|
}
|
|
2889
2889
|
__name(isWsTicketSnapshot, "isWsTicketSnapshot");
|
|
2890
|
+
var IPV6 = 6;
|
|
2891
|
+
function isHeaderAddress(candidate) {
|
|
2892
|
+
return typeof candidate === "string" && !candidate.includes("%") && isIP(candidate) !== 0;
|
|
2893
|
+
}
|
|
2894
|
+
__name(isHeaderAddress, "isHeaderAddress");
|
|
2895
|
+
function peerAddress(remoteAddress) {
|
|
2896
|
+
if (typeof remoteAddress !== "string") return "";
|
|
2897
|
+
const zoneIndex = remoteAddress.indexOf("%");
|
|
2898
|
+
const bare = zoneIndex === -1 ? remoteAddress : remoteAddress.slice(0, zoneIndex);
|
|
2899
|
+
const family = isIP(bare);
|
|
2900
|
+
if (zoneIndex !== -1) return family === IPV6 ? bare : "";
|
|
2901
|
+
return family === 0 ? "" : bare;
|
|
2902
|
+
}
|
|
2903
|
+
__name(peerAddress, "peerAddress");
|
|
2904
|
+
function resolveClientAddress(request) {
|
|
2905
|
+
const hops = request.ips ?? [];
|
|
2906
|
+
for (const candidate of [
|
|
2907
|
+
request.ip,
|
|
2908
|
+
...hops
|
|
2909
|
+
]) {
|
|
2910
|
+
if (isHeaderAddress(candidate)) return candidate;
|
|
2911
|
+
}
|
|
2912
|
+
return peerAddress(request.socket?.remoteAddress);
|
|
2913
|
+
}
|
|
2914
|
+
__name(resolveClientAddress, "resolveClientAddress");
|
|
2890
2915
|
var IPV6_GROUPS = 8;
|
|
2891
2916
|
var IPV6_PREFIX_GROUPS = 4;
|
|
2892
2917
|
function rateLimitBucket(address) {
|
|
@@ -3007,8 +3032,9 @@ var AuthRateLimitGuard = class {
|
|
|
3007
3032
|
* The address this request is counted against.
|
|
3008
3033
|
*
|
|
3009
3034
|
* `'peer'` — the default — reads the socket directly, so `X-Forwarded-For` cannot influence
|
|
3010
|
-
* it. `'trusted-proxy'` uses
|
|
3011
|
-
* forwarding headers it admits
|
|
3035
|
+
* it. `'trusted-proxy'` uses the resolved client address, which starts from `req.ip` (honouring
|
|
3036
|
+
* the app's `trust proxy` setting and the forwarding headers it admits) and never returns a
|
|
3037
|
+
* string that is not an address, so a caller cannot mint a bucket by writing one.
|
|
3012
3038
|
*
|
|
3013
3039
|
* The default is the strict one because the two failure modes are not symmetric: keying on
|
|
3014
3040
|
* the peer address behind a proxy over-counts, which is visible and recoverable, while
|
|
@@ -3021,7 +3047,7 @@ var AuthRateLimitGuard = class {
|
|
|
3021
3047
|
*/
|
|
3022
3048
|
clientIp(request) {
|
|
3023
3049
|
if (this.options.rateLimit.clientIpSource === "trusted-proxy") {
|
|
3024
|
-
return
|
|
3050
|
+
return resolveClientAddress(request) || UNKNOWN_CLIENT;
|
|
3025
3051
|
}
|
|
3026
3052
|
const peer = request.socket?.remoteAddress;
|
|
3027
3053
|
return typeof peer === "string" && peer !== "" ? peer : UNKNOWN_CLIENT;
|
|
@@ -6620,7 +6646,7 @@ var AuthService = class _AuthService {
|
|
|
6620
6646
|
*/
|
|
6621
6647
|
async register(dto, req) {
|
|
6622
6648
|
const tenantId = await this.resolveTenantId(dto.tenantId, req);
|
|
6623
|
-
const ip = req
|
|
6649
|
+
const ip = resolveClientAddress(req);
|
|
6624
6650
|
const userAgent = String(req.headers["user-agent"] ?? "");
|
|
6625
6651
|
dto = {
|
|
6626
6652
|
...dto,
|
|
@@ -6713,7 +6739,7 @@ var AuthService = class _AuthService {
|
|
|
6713
6739
|
*/
|
|
6714
6740
|
async login(dto, req) {
|
|
6715
6741
|
const tenantId = await this.resolveTenantId(dto.tenantId, req);
|
|
6716
|
-
const ip = req
|
|
6742
|
+
const ip = resolveClientAddress(req);
|
|
6717
6743
|
const userAgent = String(req.headers["user-agent"] ?? "");
|
|
6718
6744
|
dto = {
|
|
6719
6745
|
...dto,
|
|
@@ -7792,7 +7818,7 @@ var AuthController = class {
|
|
|
7792
7818
|
*/
|
|
7793
7819
|
async refresh(req, res) {
|
|
7794
7820
|
const rawRefreshToken = this.tokenDelivery.extractRefreshToken(req) ?? "";
|
|
7795
|
-
const ip = req
|
|
7821
|
+
const ip = resolveClientAddress(req);
|
|
7796
7822
|
const userAgent = String(req.headers["user-agent"] ?? "");
|
|
7797
7823
|
const rotated = await this.authService.refresh(rawRefreshToken, ip, userAgent);
|
|
7798
7824
|
const authResult = {
|
|
@@ -9284,7 +9310,7 @@ var InvitationController = class {
|
|
|
9284
9310
|
* @returns Auth response with tokens delivered per the configured `tokenDelivery` mode.
|
|
9285
9311
|
*/
|
|
9286
9312
|
async accept(dto, req, res) {
|
|
9287
|
-
const ip = req
|
|
9313
|
+
const ip = resolveClientAddress(req);
|
|
9288
9314
|
const userAgent = String(req.headers["user-agent"] ?? "");
|
|
9289
9315
|
const headers = req.headers;
|
|
9290
9316
|
const result = await this.invitationService.acceptInvitation(dto, ip, userAgent, headers);
|
|
@@ -11027,7 +11053,7 @@ var MfaController = class {
|
|
|
11027
11053
|
* @throws `MFA_INVALID_CODE` if the submitted TOTP code is invalid.
|
|
11028
11054
|
*/
|
|
11029
11055
|
async verifyEnable(user, dto, req) {
|
|
11030
|
-
const ip = req
|
|
11056
|
+
const ip = resolveClientAddress(req);
|
|
11031
11057
|
const userAgent = String(req.headers["user-agent"] ?? "");
|
|
11032
11058
|
await this.mfaService.verifyAndEnable(user.sub, dto.code, ip, userAgent, "dashboard", user.tenantId);
|
|
11033
11059
|
}
|
|
@@ -11060,7 +11086,7 @@ var MfaController = class {
|
|
|
11060
11086
|
* @throws `MFA_INVALID_CODE` if the submitted code is incorrect.
|
|
11061
11087
|
*/
|
|
11062
11088
|
async challenge(dto, req, res) {
|
|
11063
|
-
const ip = req
|
|
11089
|
+
const ip = resolveClientAddress(req);
|
|
11064
11090
|
const userAgent = String(req.headers["user-agent"] ?? "");
|
|
11065
11091
|
const cookieToken = readMfaTempCookie(req);
|
|
11066
11092
|
const mfaTempToken = dto.mfaTempToken ?? cookieToken;
|
|
@@ -11099,7 +11125,7 @@ var MfaController = class {
|
|
|
11099
11125
|
* @throws `MFA_INVALID_CODE` if the submitted TOTP code is incorrect.
|
|
11100
11126
|
*/
|
|
11101
11127
|
async disable(user, dto, req) {
|
|
11102
|
-
const ip = req
|
|
11128
|
+
const ip = resolveClientAddress(req);
|
|
11103
11129
|
const userAgent = String(req.headers["user-agent"] ?? "");
|
|
11104
11130
|
await this.mfaService.disable(user.sub, dto.code, ip, userAgent, "dashboard", user.tenantId);
|
|
11105
11131
|
}
|
|
@@ -11123,7 +11149,7 @@ var MfaController = class {
|
|
|
11123
11149
|
* @returns The fresh plain-text recovery codes, one-time display.
|
|
11124
11150
|
*/
|
|
11125
11151
|
async regenerateRecoveryCodes(user, dto, req) {
|
|
11126
|
-
const ip = req
|
|
11152
|
+
const ip = resolveClientAddress(req);
|
|
11127
11153
|
const userAgent = String(req.headers["user-agent"] ?? "");
|
|
11128
11154
|
return this.mfaService.regenerateRecoveryCodes(user.sub, dto.code, ip, userAgent, "dashboard", user.tenantId);
|
|
11129
11155
|
}
|
|
@@ -12961,7 +12987,7 @@ var PlatformAuthController = class {
|
|
|
12961
12987
|
* @param req - Incoming request (provides IP and User-Agent for session tracking).
|
|
12962
12988
|
*/
|
|
12963
12989
|
async login(dto, req) {
|
|
12964
|
-
const ip = req
|
|
12990
|
+
const ip = resolveClientAddress(req);
|
|
12965
12991
|
const userAgent = String(req.headers["user-agent"] ?? "").slice(0, 512);
|
|
12966
12992
|
const result = await this.platformAuthService.login(dto, ip, userAgent);
|
|
12967
12993
|
if (isMfaChallenge2(result)) {
|
|
@@ -12990,7 +13016,7 @@ var PlatformAuthController = class {
|
|
|
12990
13016
|
* @throws `PLATFORM_AUTH_REQUIRED` when the submitted MFA token has `context='dashboard'`.
|
|
12991
13017
|
*/
|
|
12992
13018
|
async mfaChallenge(dto, req) {
|
|
12993
|
-
const ip = req
|
|
13019
|
+
const ip = resolveClientAddress(req);
|
|
12994
13020
|
const userAgent = String(req.headers["user-agent"] ?? "").slice(0, 512);
|
|
12995
13021
|
if (dto.mfaTempToken === void 0 || dto.mfaTempToken.length === 0) {
|
|
12996
13022
|
throw new AuthException(AUTH_ERROR_CODES.MFA_TEMP_TOKEN_INVALID);
|
|
@@ -13054,7 +13080,7 @@ var PlatformAuthController = class {
|
|
|
13054
13080
|
*/
|
|
13055
13081
|
async refresh(req) {
|
|
13056
13082
|
const rawRefreshToken = this.tokenDelivery.extractPlatformRefreshToken(req) ?? "";
|
|
13057
|
-
const ip = req
|
|
13083
|
+
const ip = resolveClientAddress(req);
|
|
13058
13084
|
const userAgent = String(req.headers["user-agent"] ?? "").slice(0, 512);
|
|
13059
13085
|
const rotated = await this.platformAuthService.refresh(rawRefreshToken, ip, userAgent);
|
|
13060
13086
|
const admin = await this.platformAuthService.getMe(rotated.session.userId);
|
|
@@ -13241,7 +13267,7 @@ var PlatformMfaController = class {
|
|
|
13241
13267
|
* @throws `MFA_INVALID_CODE` if the submitted TOTP code is invalid.
|
|
13242
13268
|
*/
|
|
13243
13269
|
async verifyEnable(user, dto, req) {
|
|
13244
|
-
const ip = req
|
|
13270
|
+
const ip = resolveClientAddress(req);
|
|
13245
13271
|
const userAgent = String(req.headers["user-agent"] ?? "");
|
|
13246
13272
|
await this.mfaService.verifyAndEnable(user.sub, dto.code, ip, userAgent, "platform");
|
|
13247
13273
|
}
|
|
@@ -13259,7 +13285,7 @@ var PlatformMfaController = class {
|
|
|
13259
13285
|
* @throws `MFA_INVALID_CODE` if the submitted TOTP code is incorrect.
|
|
13260
13286
|
*/
|
|
13261
13287
|
async disable(user, dto, req) {
|
|
13262
|
-
const ip = req
|
|
13288
|
+
const ip = resolveClientAddress(req);
|
|
13263
13289
|
const userAgent = String(req.headers["user-agent"] ?? "");
|
|
13264
13290
|
await this.mfaService.disable(user.sub, dto.code, ip, userAgent, "platform");
|
|
13265
13291
|
}
|
|
@@ -13279,7 +13305,7 @@ var PlatformMfaController = class {
|
|
|
13279
13305
|
* @returns The fresh plain-text recovery codes, one-time display.
|
|
13280
13306
|
*/
|
|
13281
13307
|
async regenerateRecoveryCodes(user, dto, req) {
|
|
13282
|
-
const ip = req
|
|
13308
|
+
const ip = resolveClientAddress(req);
|
|
13283
13309
|
const userAgent = String(req.headers["user-agent"] ?? "");
|
|
13284
13310
|
return this.mfaService.regenerateRecoveryCodes(user.sub, dto.code, ip, userAgent, "platform");
|
|
13285
13311
|
}
|
|
@@ -13918,7 +13944,7 @@ var ApiKeyGuard = class _ApiKeyGuard {
|
|
|
13918
13944
|
...partial,
|
|
13919
13945
|
keyId: key?.id ?? null,
|
|
13920
13946
|
label: key?.label ?? null,
|
|
13921
|
-
ip: request
|
|
13947
|
+
ip: resolveClientAddress(request)
|
|
13922
13948
|
};
|
|
13923
13949
|
const context = this.hookContext(request);
|
|
13924
13950
|
const run = /* @__PURE__ */ __name(() => hook.call(this.hooks, event, context), "run");
|
|
@@ -13943,7 +13969,7 @@ var ApiKeyGuard = class _ApiKeyGuard {
|
|
|
13943
13969
|
hookContext(request) {
|
|
13944
13970
|
const headers = Object.fromEntries(Object.entries(request.headers).filter(([name]) => name !== this.options.apiKeys.header));
|
|
13945
13971
|
return {
|
|
13946
|
-
ip: request
|
|
13972
|
+
ip: resolveClientAddress(request),
|
|
13947
13973
|
userAgent: request.headers["user-agent"] ?? "",
|
|
13948
13974
|
sanitizedHeaders: sanitizeHeaders(headers)
|
|
13949
13975
|
};
|
|
@@ -14953,7 +14979,7 @@ var OAuthController = class _OAuthController {
|
|
|
14953
14979
|
* payload, or `undefined` when a redirect was issued.
|
|
14954
14980
|
*/
|
|
14955
14981
|
async callback(provider, query, req, res) {
|
|
14956
|
-
const ip = (req
|
|
14982
|
+
const ip = resolveClientAddress(req).slice(0, 64);
|
|
14957
14983
|
const userAgent = String(req.headers["user-agent"] ?? "").slice(0, 512);
|
|
14958
14984
|
const stateCookie = readOAuthStateCookie(req);
|
|
14959
14985
|
if (stateCookie !== void 0) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bymax-one/nest-auth",
|
|
3
|
-
"version": "1.4.
|
|
3
|
+
"version": "1.4.13",
|
|
4
4
|
"description": "Full-stack authentication and authorization package for NestJS, React and Next.js — JWT, MFA, OAuth, sessions, multi-tenant SaaS ready",
|
|
5
5
|
"author": "Bymax One <support@bymax.one>",
|
|
6
6
|
"license": "MIT",
|