@dokebi/websocket 0.4.2 → 0.4.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/AI.md CHANGED
@@ -143,7 +143,7 @@ stop: block new work → wait for received handlers and broadcasts being sent
143
143
 
144
144
  ## 7. Logs ([D-0047](https://git.nyx-zone.com/dokebi/framework/-/blob/main/docs/decisions/D-0047-logging-and-no-silent-errors.md))
145
145
 
146
- - Exceptions are never dropped silently. Failures other than those turned into close codes or error replies are `error`: a fixed English message + `code` + `error` (the full original error: name, whole message, stack and `Caused by: …` for each cause, address, port and errno text included; only URL credentials masked as `//***@`; cut only beyond 16 KB). Examples: handler failure `WebSocket message handler failed` (code from the error or `HANDLER_FAILED`, closed with 1011), socket error `SOCKET_ERROR`, broadcast write failure `BROADCAST_WRITE_FAILED`, presence handler `PRESENCE_HANDLER_FAILED`, lost hub events `HUB_EVENTS_FAILED` (every socket 1013), delayed cleanup `PRESENCE_CLEANUP_DEFERRED`. Registration failures are warn (closed with 4409 or 1013), slow consumers warn `SLOW_CONSUMER`. A reply or presence that could not be written to an already closed socket is warn `REPLY_DROPPED` (the client simply left; not a server failure; once per 10 seconds + `repeated`).
146
+ - Exceptions are never dropped silently. Failures other than those turned into close codes or error replies are `error`: a fixed English message + `code` + `error` (the full original error: name, whole message, stack and `Caused by: …` for each cause, address, port and errno text included; only URL credentials masked as `//***@`; cut only beyond 16 KB). Examples: handler failure `WebSocket message handler failed` (code from the error or `HANDLER_FAILED`, closed with 1011), socket error `SOCKET_ERROR`, broadcast write failure `BROADCAST_WRITE_FAILED`, presence handler `PRESENCE_HANDLER_FAILED`, lost hub events `HUB_EVENTS_FAILED` (every socket 1013), delayed cleanup `PRESENCE_CLEANUP_DEFERRED`. Registration failures are warn (closed with 4409 or 1013), slow consumers warn `SLOW_CONSUMER`. A reply or presence that could not be written to an already closed socket is warn `REPLY_DROPPED` (the client simply left; not a server failure; once per 10 seconds + `repeated`). That holds for the connection's own socket: a route's `message` that rejects with the `SOCKET_CLOSED` or `SLOW_CONSUMER` of a `send` to another connection (a relay awaiting a peer's `send`) while its own socket is open is a handler failure like any other (error, closed with 1011); catch it in `message` when a peer that left is expected (a `defineSocket` handler answers an error frame instead, as for every failure).
147
147
  - Places that run per message or socket write the same message and code once per `logRepeatWindowMs` (10 seconds) and group the rest as a `repeated` count (the remaining count is written at drain end too). Bodies and tokens are never logged.
148
148
  - Sealing (§4a): a refused frame is warn `WebSocket sealed frame refused; closed` (code `SEAL_FRAME_REFUSED`, `reason`, `error`; one line per reason per `logRepeatWindowMs` + `repeated`), codec creation failure error `SEAL_CODEC_FAILED` (1011), broadcast seal failure error `SEAL_ENCODE_FAILED` (1011), hello send failure warn `SEAL_HELLO_FAILED`. debug: provider present (`WebSocket seal provider`), the decision per upgrade (`WebSocket seal decision`: path, sealed, offered), `WebSocket seal hello sent`, 4415 (`WebSocket seal required; closed`), `sealed` on connection logs, the `sealed` count on broadcast logs.
149
149
  - debug: route registration (`WebSocket route registered`: path, `routeId`, version), connect, close, refused upgrades (`WebSocket upgrade unauthorized` for a 401, `WebSocket upgrade refused; authorization expired` for a 401 of an invalid lease, `WebSocket origin refused` for a 403, `WebSocket upgrade refused` for a 503; each with `path` and `routeId`, the route's `config.id` or null, D-0140), hub registration ms, room join, leave and refusal, presence changes (counts), kick, broadcast delivery (ID, connection count, `binary`; after checking `isEnabled`), ignored `ack-v1` from old clients (`ACK_IGNORED`, one line per 10 seconds + `repeated`), idle and heartbeat closes, drain start and end. Too many waiting messages (1013 `Too many messages`) is warn `TOO_MANY_MESSAGES`; presence changes merged behind a slow presence handler are warn `PRESENCE_MERGED` (once per 10 seconds + `repeated`).
@@ -182,7 +182,7 @@ stop: block new work → wait for received handlers and broadcasts being sent
182
182
 
183
183
  ## 9. Cautions
184
184
 
185
- - Never use `origins: '*'` on a cookie-authenticated route (Cross-Site WebSocket Hijacking). When a proxy rewrites Host, set web `trustProxy` accordingly or list the public origin in `origins`.
185
+ - Never use `origins: '*'` on a cookie-authenticated route (Cross-Site WebSocket Hijacking). Without `origins` the same Host passes with either scheme, except one case: an upgrade that came over **https** (the socket, or `X-Forwarded-Proto` from a proxy in web `trustProxy`) with an **`http://`** Origin of that host is 403 — an http page can be rewritten on the path and must not open the wss socket with the user's cookies. List `http://…` in `origins` if a page really has to. An upgrade that looks like http (a TLS proxy the app does not trust) is not held to this: name the proxy in `trustProxy`. When a proxy rewrites Host, set web `trustProxy` accordingly or list the public origin in `origins`.
186
186
  - Examples that trust a header value as the identity (example 23) are for tests only.
187
187
  - `hub.events` is consumed only by this plugin. No other code reads it.
188
188
  - Broadcasts are at most once. They can be missed across reconnects, leader changes and slow consumers, and resending after `OUTCOME_UNKNOWN` can deliver twice.
package/dist/gateway.js CHANGED
@@ -148,7 +148,7 @@ function parseOrigins(value) {
148
148
  * Browsers always send Origin on WebSocket upgrades, so a foreign page cannot reuse the user's
149
149
  * cookies (CSWSH) unless its origin is allowed. Clients without Origin are not browsers.
150
150
  */
151
- function originAllowed(origin, host, allowed) {
151
+ function originAllowed(origin, host, allowed, secure = false) {
152
152
  if (origin === undefined || allowed === '*')
153
153
  return true;
154
154
  if (typeof origin !== 'string')
@@ -166,6 +166,12 @@ function originAllowed(origin, host, allowed) {
166
166
  return true;
167
167
  if (typeof host !== 'string' || !host)
168
168
  return false;
169
+ // The same host over plain http is another origin for a request that came over https: an
170
+ // http page (which anyone on the path can rewrite) must not open the wss socket with the
171
+ // user's cookies. The other way round is not refused: behind a TLS proxy the app does not
172
+ // trust, the request looks like http while the page really is https.
173
+ if (secure && url.protocol === 'http:')
174
+ return false;
169
175
  try {
170
176
  return new URL(`${url.protocol}//${host}`).host === url.host;
171
177
  }
@@ -397,7 +403,7 @@ export class Gateway {
397
403
  // D-0140: dokebi-web records and checks `config.id` (describe, duplicates, route hooks).
398
404
  ...(config ? { config: { ...config } } : {}),
399
405
  preValidation: async (request, reply) => {
400
- if (!originAllowed(request.headers.origin, this.web.requestHost(request), origins)) {
406
+ if (!originAllowed(request.headers.origin, this.web.requestHost(request), origins, request.protocol === 'https')) {
401
407
  if (logger.isEnabled('debug'))
402
408
  logger.log('debug', 'WebSocket origin refused', {
403
409
  path: route.path,
@@ -1028,10 +1034,16 @@ export class Gateway {
1028
1034
  // failure. A slow consumer was logged where it was closed; a client that left is
1029
1035
  // counted here as warn, once per interval: the client is gone, nothing on this
1030
1036
  // server failed (OPS-002 soak: thousands of these hid the real cause at error).
1037
+ // Only when it is this connection's socket that is closing (every refusal of its own
1038
+ // send closed it first: closed, slow, revoked or failed). The same codes from a send
1039
+ // to another connection (a relay that awaits a peer's `send`) are a failure of this
1040
+ // handler: the branches below would otherwise leave this socket open with `failed`
1041
+ // set, taking and sending nothing more, with nothing logged (audit R9-g11-1).
1042
+ const own = socket.readyState !== OPEN;
1031
1043
  const code = error?.code;
1032
- if (code === 'SLOW_CONSUMER')
1044
+ if (own && code === 'SLOW_CONSUMER')
1033
1045
  return;
1034
- if (code === 'SOCKET_CLOSED') {
1046
+ if (own && code === 'SOCKET_CLOSED') {
1035
1047
  this.#throttle.log('warn', 'WebSocket reply dropped; socket closed', 'REPLY_DROPPED', error, { connection: id });
1036
1048
  return;
1037
1049
  }
package/dist/index.js CHANGED
@@ -59,7 +59,7 @@ class WebsocketRuntime {
59
59
  /** Factory input: `{ app, options? }`. Options default to `app.config['dokebi-websocket']`. */
60
60
  export const createPlugin = definePlugin({
61
61
  name: 'dokebi-websocket',
62
- version: '0.4.2',
62
+ version: '0.4.4',
63
63
  core: '^0.4.2',
64
64
  options,
65
65
  contributes: [realtime],
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dokebi/websocket",
3
- "version": "0.4.2",
3
+ "version": "0.4.4",
4
4
  "description": "WebSocket routes on @dokebi/web with rooms and broadcast across servers through @dokebi/websocket-hub.",
5
5
  "keywords": [
6
6
  "dokebi",
@@ -49,8 +49,8 @@
49
49
  "@dokebi/websocket-hub": "^0.4.0"
50
50
  },
51
51
  "devDependencies": {
52
- "@dokebi/web": "0.4.3",
53
- "@dokebi/websocket-hub": "0.4.2"
52
+ "@dokebi/web": "0.4.7",
53
+ "@dokebi/websocket-hub": "0.4.3"
54
54
  },
55
55
  "dokebi": {
56
56
  "schemaVersion": 2,