ingenium 0.0.1 → 0.0.2

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/src/ws/types.ts CHANGED
@@ -21,12 +21,49 @@ export type WebSocket = WsWebSocket
21
21
  */
22
22
  export type WebSocketHandler = (socket: WsWebSocket, ctx: IngeniumContext) => void | Promise<void>
23
23
 
24
+ /**
25
+ * Custom Origin verifier. Receives the request's `Origin` header (or
26
+ * `undefined` when absent, e.g. a non-browser client) plus the raw upgrade
27
+ * request. Return `true` to allow the upgrade, `false` to reject it with a
28
+ * `403` handshake.
29
+ */
30
+ export type WebSocketOriginVerifier = (
31
+ origin: string | undefined,
32
+ req: IncomingMessage,
33
+ ) => boolean
34
+
35
+ /**
36
+ * Origin allowlist policy for a WebSocket upgrade. WS handlers run OUTSIDE the
37
+ * normal middleware pipeline, so the only built-in defense against
38
+ * Cross-Site WebSocket Hijacking (CSWSH) is this option — browsers attach the
39
+ * victim's cookies to cross-origin upgrades, so without an Origin check any
40
+ * external page can open an authenticated socket.
41
+ *
42
+ * - `true` — same-origin only: the `Origin` host must equal the request `Host`.
43
+ * - `string` / `string[]` — exact `Origin` allowlist (compared verbatim).
44
+ * - function — custom predicate (see {@link WebSocketOriginVerifier}).
45
+ *
46
+ * Omitting the option preserves backward compatibility (no enforcement) but
47
+ * emits a one-time dev warning, because the safe default for a browser-facing
48
+ * socket is to restrict origins.
49
+ */
50
+ export type WebSocketOriginOption =
51
+ | boolean
52
+ | string
53
+ | string[]
54
+ | WebSocketOriginVerifier
55
+
24
56
  /** Per-handler options forwarded to `WebSocketServer({ noServer: true, ... })`. */
25
57
  export interface WebSocketHandlerOptions {
26
58
  /** Max payload size (bytes) for incoming frames. */
27
59
  maxPayload?: number
28
60
  /** Enable permessage-deflate. Defaults to false (matches `ws` default). */
29
61
  perMessageDeflate?: boolean
62
+ /**
63
+ * Origin allowlist for the upgrade handshake — the built-in CSWSH defense.
64
+ * See {@link WebSocketOriginOption} for semantics and why it matters.
65
+ */
66
+ origin?: WebSocketOriginOption
30
67
  }
31
68
 
32
69
  /** Internal: a registered handler entry. */