@bymax-one/nest-realtime 1.2.25 → 1.2.27
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 +36 -1
- package/README.md +10 -10
- package/dist/internal/index.cjs +55 -2
- package/dist/internal/index.d.cts +4 -4
- package/dist/internal/index.d.ts +4 -4
- package/dist/internal/index.mjs +55 -2
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,39 @@ Versioning is **not** strict [SemVer](https://semver.org/spec/v2.0.0.html) while
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
+
## [1.2.27] - 2026-11-21
|
|
11
|
+
|
|
12
|
+
### Fixed
|
|
13
|
+
|
|
14
|
+
- **`roles` is read with a bound, and a `roles` that cannot be read is refused in every shape.** A revoked proxy over an array made `Array.isArray` throw a raw `TypeError` before the guard; the check now sits inside it, so the refusal is `REALTIME_AUTH_FAILED` with the host's error as the cause. The copy stops at 1,024 roles: an authentication result with more is refused with `REALTIME_AUTH_FAILED`, and an iterator that never ends is no longer read to exhaustion. The bound is there to cap the cost of the copy, not to say how many roles a user may hold; exactly 1,024 is admitted.
|
|
15
|
+
- **The rejection of a promise a `tenantResolver` returns is absorbed whatever kind of promise it is.** The check was `instanceof Promise`, which a promise from another realm fails, and the handler was the promise's own `catch`, which a subclass can override; either left an unhandled rejection that ended the process. The handler is now attached with the intrinsic `then`, which accepts a real promise of any realm and refuses every other value without reading a property of it. A proxy over a promise, whose own methods the intrinsic cannot reach, is given its own `catch` when the intrinsic refuses it, so its rejection is absorbed too; a thenable that is not a promise is still never asked for its `then`. A promise whose `constructor` or species cannot be read cannot be given a handler by either path.
|
|
16
|
+
|
|
17
|
+
### Documentation
|
|
18
|
+
|
|
19
|
+
- **What a `tenantResolver` that returns something other than a native promise gets.** A thenable that is not a promise is never asked for its `then`, because asking could run a lazy query, so its own rejection is not absorbed; a promise whose `constructor` cannot be read cannot be given a handler either. Both are refused as a blank tenant.
|
|
20
|
+
- **`roles` that is not an array is kept as it is.** A string, a `Set` or an array-like proxy is not copied, so it is read later by whoever uses it.
|
|
21
|
+
- **A `websocket.cors` key outside `CorsConfig`.** Only the keys the library names (`origin`, `credentials`, `methods`) and the block's own enumerable keys are carried; any other key defined as a getter on a prototype or as non-enumerable is not.
|
|
22
|
+
|
|
23
|
+
### Tests
|
|
24
|
+
|
|
25
|
+
- Rows for `websocket.cors` with `origin: false`, a `RegExp`, a function and a proxy over an empty target that answers by name, over a real Socket.IO server.
|
|
26
|
+
|
|
27
|
+
#### Apply to a derived backend
|
|
28
|
+
|
|
29
|
+
An authenticator that returns more than 1,024 roles is now refused; none is expected to.
|
|
30
|
+
|
|
31
|
+
## [1.2.26] - 2026-11-21
|
|
32
|
+
|
|
33
|
+
### Fixed
|
|
34
|
+
|
|
35
|
+
- **An origin allowlist in `websocket.cors` is no longer dropped when it is defined as a class getter, a non-enumerable property or a prototype property.** The block was copied by its own enumerable keys, so `origin` went missing, Socket.IO fell back to its default and answered every origin with `Access-Control-Allow-Origin: *`. The block is now read by the names of `CorsConfig` whenever it is a non-array object, however it reports its type: a block with a `Symbol.toStringTag`, or a strict proxy, is read the same way. A block whose names cannot be read is refused with `REALTIME_INVALID_OPTIONS` and the host's error as the cause, never answered with a wildcard.
|
|
36
|
+
- **`roles` whose read throws is refused at admission.** A proxy or getter on `roles` was first read when the connection event or a hook needed it, so the host's error reached the framework raw. The array is now read once at admission into a frozen copy, and a read that throws is refused with `REALTIME_AUTH_FAILED` and the host's error as the cause. A `roles` value that is not an array is left as it is.
|
|
37
|
+
- **An `async` `tenantResolver` no longer leaves an unhandled rejection.** The resolver is synchronous; a promise is refused as a tenant that is not a string, and its rejection is now absorbed.
|
|
38
|
+
|
|
39
|
+
#### Apply to a derived backend
|
|
40
|
+
|
|
41
|
+
Nothing to change. A host that defined `websocket.cors.origin` as a getter, non-enumerable or on a prototype now gets the allowlist it wrote; check that it is the list you intend, since until now such a server answered every origin.
|
|
42
|
+
|
|
10
43
|
## [1.2.25] - 2026-11-21
|
|
11
44
|
|
|
12
45
|
### Fixed
|
|
@@ -1286,7 +1319,9 @@ First published release.
|
|
|
1286
1319
|
rather than by the manual sweep that raised the NestJS floors — that sweep
|
|
1287
1320
|
asked about NestJS and never put the same question to the other ten peers.
|
|
1288
1321
|
|
|
1289
|
-
[Unreleased]: https://github.com/bymaxone/nest-realtime/compare/v1.2.
|
|
1322
|
+
[Unreleased]: https://github.com/bymaxone/nest-realtime/compare/v1.2.27...HEAD
|
|
1323
|
+
[1.2.27]: https://github.com/bymaxone/nest-realtime/compare/v1.2.26...v1.2.27
|
|
1324
|
+
[1.2.26]: https://github.com/bymaxone/nest-realtime/compare/v1.2.25...v1.2.26
|
|
1290
1325
|
[1.2.25]: https://github.com/bymaxone/nest-realtime/compare/v1.2.24...v1.2.25
|
|
1291
1326
|
[1.2.24]: https://github.com/bymaxone/nest-realtime/compare/v1.2.23...v1.2.24
|
|
1292
1327
|
[1.2.23]: https://github.com/bymaxone/nest-realtime/compare/v1.2.22...v1.2.23
|
package/README.md
CHANGED
|
@@ -638,16 +638,16 @@ When integrating `@bymax-one/nest-realtime` in production, verify each of the fo
|
|
|
638
638
|
|
|
639
639
|
## 🛡️ Security Table
|
|
640
640
|
|
|
641
|
-
| Layer | Implementation
|
|
642
|
-
| --------------------- |
|
|
643
|
-
| Credential validation | Delegated entirely to your `IConnectionAuthenticator`
|
|
644
|
-
| Auth library imports | None: an ESLint rule fails lint on a string literal naming an auth package in `src`
|
|
645
|
-
| Tenant isolation | Room-scoped (`tenant:{id}`), resolved from the authenticated result
|
|
646
|
-
| Connection revocation | `disconnect(connectionId)`, cross-instance; rejects if it cannot reach it
|
|
647
|
-
| Credential expiry | Interval re-authentication; a failure drops the connection, after an event if `onFailure: 'event'`
|
|
648
|
-
| Connection flooding | FIFO eviction per user: oldest closed, new admitted, never a 429
|
|
649
|
-
| SSE transport headers | `X-Accel-Buffering: no` and `Cache-Control: no-cache, no-transform`
|
|
650
|
-
| WebSocket origin | Explicit `cors` config
|
|
641
|
+
| Layer | Implementation |
|
|
642
|
+
| --------------------- | ----------------------------------------------------------------------------------------------------- |
|
|
643
|
+
| Credential validation | Delegated entirely to your `IConnectionAuthenticator` |
|
|
644
|
+
| Auth library imports | None: an ESLint rule fails lint on a string literal naming an auth package in `src` |
|
|
645
|
+
| Tenant isolation | Room-scoped (`tenant:{id}`), resolved from the authenticated result |
|
|
646
|
+
| Connection revocation | `disconnect(connectionId)`, cross-instance; rejects if it cannot reach it |
|
|
647
|
+
| Credential expiry | Interval re-authentication; a failure drops the connection, after an event if `onFailure: 'event'` |
|
|
648
|
+
| Connection flooding | FIFO eviction per user: oldest closed, new admitted, never a 429 |
|
|
649
|
+
| SSE transport headers | `X-Accel-Buffering: no` and `Cache-Control: no-cache, no-transform` |
|
|
650
|
+
| WebSocket origin | Explicit `cors` config: `origin`, `credentials` and `methods` are read by name and given to Socket.IO |
|
|
651
651
|
|
|
652
652
|
---
|
|
653
653
|
|
package/dist/internal/index.cjs
CHANGED
|
@@ -79,6 +79,11 @@ var WEBSOCKET_KEYS = {
|
|
|
79
79
|
emitConnectionEvent: true,
|
|
80
80
|
redisAdapter: true
|
|
81
81
|
};
|
|
82
|
+
var CORS_KEYS = {
|
|
83
|
+
origin: true,
|
|
84
|
+
credentials: true,
|
|
85
|
+
methods: true
|
|
86
|
+
};
|
|
82
87
|
var REAUTHENTICATION_KEYS = {
|
|
83
88
|
intervalSeconds: true,
|
|
84
89
|
onFailure: true,
|
|
@@ -122,6 +127,13 @@ function dropUnset(options) {
|
|
|
122
127
|
const value = kept[group];
|
|
123
128
|
if (isPlainObject(value)) kept[group] = readSet(value, keys);
|
|
124
129
|
}
|
|
130
|
+
const websocket = kept.websocket;
|
|
131
|
+
if (isPlainObject(websocket)) {
|
|
132
|
+
const cors = websocket.cors;
|
|
133
|
+
if (typeof cors === "object" && cors !== null && !Array.isArray(cors)) {
|
|
134
|
+
websocket.cors = readSet(cors, CORS_KEYS);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
125
137
|
return kept;
|
|
126
138
|
}
|
|
127
139
|
|
|
@@ -848,15 +860,45 @@ function refuseReservedRoom(operation, roomId) {
|
|
|
848
860
|
}
|
|
849
861
|
|
|
850
862
|
// src/server/utils/admit-rooms.ts
|
|
863
|
+
var MAX_ROLES = 1024;
|
|
851
864
|
function refuse2(detail) {
|
|
852
865
|
throw realtimeError(
|
|
853
866
|
REALTIME_ERROR_CODES.AUTH_FAILED,
|
|
854
867
|
`the authenticator returned rooms that are not valid: ${detail}. Fix the authenticator; the limits are bounds against abuse and apply to this field only.`
|
|
855
868
|
);
|
|
856
869
|
}
|
|
870
|
+
function admitRoles(roles) {
|
|
871
|
+
const admitted = [];
|
|
872
|
+
let overflow = false;
|
|
873
|
+
try {
|
|
874
|
+
if (!Array.isArray(roles)) return roles;
|
|
875
|
+
for (const role of roles) {
|
|
876
|
+
if (admitted.length === MAX_ROLES) {
|
|
877
|
+
overflow = true;
|
|
878
|
+
break;
|
|
879
|
+
}
|
|
880
|
+
admitted.push(role);
|
|
881
|
+
}
|
|
882
|
+
} catch (error) {
|
|
883
|
+
throw realtimeError(
|
|
884
|
+
REALTIME_ERROR_CODES.AUTH_FAILED,
|
|
885
|
+
"the authenticator returned roles that could not be read: a getter or a proxy on them throws",
|
|
886
|
+
{ cause: error }
|
|
887
|
+
);
|
|
888
|
+
}
|
|
889
|
+
if (overflow) {
|
|
890
|
+
throw realtimeError(
|
|
891
|
+
REALTIME_ERROR_CODES.AUTH_FAILED,
|
|
892
|
+
`the authenticator returned more than ${MAX_ROLES} roles: the limit is ${MAX_ROLES}`
|
|
893
|
+
);
|
|
894
|
+
}
|
|
895
|
+
return Object.freeze(admitted);
|
|
896
|
+
}
|
|
857
897
|
function admitRooms(auth) {
|
|
898
|
+
const roles = admitRoles(auth.roles);
|
|
858
899
|
const { rooms } = auth;
|
|
859
|
-
|
|
900
|
+
const kept = roles === auth.roles ? auth : { ...auth, roles };
|
|
901
|
+
if (rooms === void 0) return kept;
|
|
860
902
|
if (!Array.isArray(rooms)) refuse2("`rooms` is not an array");
|
|
861
903
|
const admitted = [];
|
|
862
904
|
for (const room of rooms) {
|
|
@@ -877,7 +919,7 @@ function admitRooms(auth) {
|
|
|
877
919
|
const reserved = reservedRoomPrefix(room);
|
|
878
920
|
if (reserved !== void 0) refuse2(`a room name starts with the reserved prefix "${reserved}"`);
|
|
879
921
|
}
|
|
880
|
-
return { ...
|
|
922
|
+
return { ...kept, rooms: Object.freeze(admitted) };
|
|
881
923
|
}
|
|
882
924
|
|
|
883
925
|
// src/server/utils/assert-identity.ts
|
|
@@ -888,9 +930,20 @@ function resolveAuthTenant(options, auth) {
|
|
|
888
930
|
const own = auth.tenantId;
|
|
889
931
|
const kept = keepByName(auth);
|
|
890
932
|
const resolved = options.tenantResolver?.(auth) ?? own;
|
|
933
|
+
absorbRejection(resolved);
|
|
891
934
|
if (resolved === void 0 || resolved === null) return kept;
|
|
892
935
|
return { ...kept, tenantId: resolved };
|
|
893
936
|
}
|
|
937
|
+
function absorbRejection(value) {
|
|
938
|
+
try {
|
|
939
|
+
Promise.prototype.then.call(value, void 0, () => void 0);
|
|
940
|
+
} catch {
|
|
941
|
+
try {
|
|
942
|
+
if (value instanceof Promise) value.catch(() => void 0);
|
|
943
|
+
} catch {
|
|
944
|
+
}
|
|
945
|
+
}
|
|
946
|
+
}
|
|
894
947
|
function keepByName(auth) {
|
|
895
948
|
const kept = {};
|
|
896
949
|
for (const key of ["userId", "roles", "rooms", "metadata"]) {
|
|
@@ -2187,7 +2187,7 @@ declare class RedisOfflineQueue implements IOfflineQueueStorage {
|
|
|
2187
2187
|
*
|
|
2188
2188
|
* @param options - The module options, read for an optional `tenantResolver`.
|
|
2189
2189
|
* @param auth - The authentication result the consumer's authenticator returned.
|
|
2190
|
-
* @returns
|
|
2190
|
+
* @returns A copy of the result with `tenantId` resolved, or absent when there is none.
|
|
2191
2191
|
*/
|
|
2192
2192
|
declare function resolveAuthTenant(options: Pick<BymaxRealtimeModuleOptions, 'tenantResolver'>, auth: AuthenticationResult): AuthenticationResult;
|
|
2193
2193
|
/**
|
|
@@ -2227,9 +2227,9 @@ declare function assertIdentityIsNotBlank(auth: AuthenticationResult): void;
|
|
|
2227
2227
|
* await, so reading the original there would join whatever it holds by then.
|
|
2228
2228
|
*
|
|
2229
2229
|
* @param auth - The resolved authentication result.
|
|
2230
|
-
* @returns `auth` itself when it has no `rooms`; otherwise a result with the
|
|
2231
|
-
* frozen copy of
|
|
2232
|
-
* @throws Error carrying `REALTIME_AUTH_FAILED` when `rooms` is present and invalid.
|
|
2230
|
+
* @returns `auth` itself when it has no `rooms` and no array `roles`; otherwise a result with the
|
|
2231
|
+
* same fields and a frozen copy of each of those it has.
|
|
2232
|
+
* @throws Error carrying `REALTIME_AUTH_FAILED` when `rooms` is present and invalid, or `roles` cannot be read.
|
|
2233
2233
|
*/
|
|
2234
2234
|
declare function admitRooms(auth: AuthenticationResult): AuthenticationResult;
|
|
2235
2235
|
|
package/dist/internal/index.d.ts
CHANGED
|
@@ -2187,7 +2187,7 @@ declare class RedisOfflineQueue implements IOfflineQueueStorage {
|
|
|
2187
2187
|
*
|
|
2188
2188
|
* @param options - The module options, read for an optional `tenantResolver`.
|
|
2189
2189
|
* @param auth - The authentication result the consumer's authenticator returned.
|
|
2190
|
-
* @returns
|
|
2190
|
+
* @returns A copy of the result with `tenantId` resolved, or absent when there is none.
|
|
2191
2191
|
*/
|
|
2192
2192
|
declare function resolveAuthTenant(options: Pick<BymaxRealtimeModuleOptions, 'tenantResolver'>, auth: AuthenticationResult): AuthenticationResult;
|
|
2193
2193
|
/**
|
|
@@ -2227,9 +2227,9 @@ declare function assertIdentityIsNotBlank(auth: AuthenticationResult): void;
|
|
|
2227
2227
|
* await, so reading the original there would join whatever it holds by then.
|
|
2228
2228
|
*
|
|
2229
2229
|
* @param auth - The resolved authentication result.
|
|
2230
|
-
* @returns `auth` itself when it has no `rooms`; otherwise a result with the
|
|
2231
|
-
* frozen copy of
|
|
2232
|
-
* @throws Error carrying `REALTIME_AUTH_FAILED` when `rooms` is present and invalid.
|
|
2230
|
+
* @returns `auth` itself when it has no `rooms` and no array `roles`; otherwise a result with the
|
|
2231
|
+
* same fields and a frozen copy of each of those it has.
|
|
2232
|
+
* @throws Error carrying `REALTIME_AUTH_FAILED` when `rooms` is present and invalid, or `roles` cannot be read.
|
|
2233
2233
|
*/
|
|
2234
2234
|
declare function admitRooms(auth: AuthenticationResult): AuthenticationResult;
|
|
2235
2235
|
|
package/dist/internal/index.mjs
CHANGED
|
@@ -77,6 +77,11 @@ var WEBSOCKET_KEYS = {
|
|
|
77
77
|
emitConnectionEvent: true,
|
|
78
78
|
redisAdapter: true
|
|
79
79
|
};
|
|
80
|
+
var CORS_KEYS = {
|
|
81
|
+
origin: true,
|
|
82
|
+
credentials: true,
|
|
83
|
+
methods: true
|
|
84
|
+
};
|
|
80
85
|
var REAUTHENTICATION_KEYS = {
|
|
81
86
|
intervalSeconds: true,
|
|
82
87
|
onFailure: true,
|
|
@@ -120,6 +125,13 @@ function dropUnset(options) {
|
|
|
120
125
|
const value = kept[group];
|
|
121
126
|
if (isPlainObject(value)) kept[group] = readSet(value, keys);
|
|
122
127
|
}
|
|
128
|
+
const websocket = kept.websocket;
|
|
129
|
+
if (isPlainObject(websocket)) {
|
|
130
|
+
const cors = websocket.cors;
|
|
131
|
+
if (typeof cors === "object" && cors !== null && !Array.isArray(cors)) {
|
|
132
|
+
websocket.cors = readSet(cors, CORS_KEYS);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
123
135
|
return kept;
|
|
124
136
|
}
|
|
125
137
|
|
|
@@ -846,15 +858,45 @@ function refuseReservedRoom(operation, roomId) {
|
|
|
846
858
|
}
|
|
847
859
|
|
|
848
860
|
// src/server/utils/admit-rooms.ts
|
|
861
|
+
var MAX_ROLES = 1024;
|
|
849
862
|
function refuse2(detail) {
|
|
850
863
|
throw realtimeError(
|
|
851
864
|
REALTIME_ERROR_CODES.AUTH_FAILED,
|
|
852
865
|
`the authenticator returned rooms that are not valid: ${detail}. Fix the authenticator; the limits are bounds against abuse and apply to this field only.`
|
|
853
866
|
);
|
|
854
867
|
}
|
|
868
|
+
function admitRoles(roles) {
|
|
869
|
+
const admitted = [];
|
|
870
|
+
let overflow = false;
|
|
871
|
+
try {
|
|
872
|
+
if (!Array.isArray(roles)) return roles;
|
|
873
|
+
for (const role of roles) {
|
|
874
|
+
if (admitted.length === MAX_ROLES) {
|
|
875
|
+
overflow = true;
|
|
876
|
+
break;
|
|
877
|
+
}
|
|
878
|
+
admitted.push(role);
|
|
879
|
+
}
|
|
880
|
+
} catch (error) {
|
|
881
|
+
throw realtimeError(
|
|
882
|
+
REALTIME_ERROR_CODES.AUTH_FAILED,
|
|
883
|
+
"the authenticator returned roles that could not be read: a getter or a proxy on them throws",
|
|
884
|
+
{ cause: error }
|
|
885
|
+
);
|
|
886
|
+
}
|
|
887
|
+
if (overflow) {
|
|
888
|
+
throw realtimeError(
|
|
889
|
+
REALTIME_ERROR_CODES.AUTH_FAILED,
|
|
890
|
+
`the authenticator returned more than ${MAX_ROLES} roles: the limit is ${MAX_ROLES}`
|
|
891
|
+
);
|
|
892
|
+
}
|
|
893
|
+
return Object.freeze(admitted);
|
|
894
|
+
}
|
|
855
895
|
function admitRooms(auth) {
|
|
896
|
+
const roles = admitRoles(auth.roles);
|
|
856
897
|
const { rooms } = auth;
|
|
857
|
-
|
|
898
|
+
const kept = roles === auth.roles ? auth : { ...auth, roles };
|
|
899
|
+
if (rooms === void 0) return kept;
|
|
858
900
|
if (!Array.isArray(rooms)) refuse2("`rooms` is not an array");
|
|
859
901
|
const admitted = [];
|
|
860
902
|
for (const room of rooms) {
|
|
@@ -875,7 +917,7 @@ function admitRooms(auth) {
|
|
|
875
917
|
const reserved = reservedRoomPrefix(room);
|
|
876
918
|
if (reserved !== void 0) refuse2(`a room name starts with the reserved prefix "${reserved}"`);
|
|
877
919
|
}
|
|
878
|
-
return { ...
|
|
920
|
+
return { ...kept, rooms: Object.freeze(admitted) };
|
|
879
921
|
}
|
|
880
922
|
|
|
881
923
|
// src/server/utils/assert-identity.ts
|
|
@@ -886,9 +928,20 @@ function resolveAuthTenant(options, auth) {
|
|
|
886
928
|
const own = auth.tenantId;
|
|
887
929
|
const kept = keepByName(auth);
|
|
888
930
|
const resolved = options.tenantResolver?.(auth) ?? own;
|
|
931
|
+
absorbRejection(resolved);
|
|
889
932
|
if (resolved === void 0 || resolved === null) return kept;
|
|
890
933
|
return { ...kept, tenantId: resolved };
|
|
891
934
|
}
|
|
935
|
+
function absorbRejection(value) {
|
|
936
|
+
try {
|
|
937
|
+
Promise.prototype.then.call(value, void 0, () => void 0);
|
|
938
|
+
} catch {
|
|
939
|
+
try {
|
|
940
|
+
if (value instanceof Promise) value.catch(() => void 0);
|
|
941
|
+
} catch {
|
|
942
|
+
}
|
|
943
|
+
}
|
|
944
|
+
}
|
|
892
945
|
function keepByName(auth) {
|
|
893
946
|
const kept = {};
|
|
894
947
|
for (const key of ["userId", "roles", "rooms", "metadata"]) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bymax-one/nest-realtime",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.27",
|
|
4
4
|
"description": "Realtime backend → frontend communication for NestJS — dual-transport SSE (default) and WebSocket (opt-in) with a unified server-side API.",
|
|
5
5
|
"author": "Bymax One <support@bymax.one>",
|
|
6
6
|
"license": "MIT",
|