@couch-kit/host 1.7.11 → 1.7.12

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.
@@ -0,0 +1,28 @@
1
+ /** Error codes surfaced when a client ACTION is rejected before dispatch. */
2
+ export type ActionRejectionCode = "FORBIDDEN_ACTION" | "NOT_JOINED";
3
+ /** Outcome of authorizing an inbound client ACTION message. */
4
+ export type ActionAuthorization = {
5
+ kind: "allow";
6
+ playerId: string;
7
+ } | {
8
+ kind: "reject";
9
+ code: ActionRejectionCode;
10
+ message: string;
11
+ };
12
+ /**
13
+ * Decide whether an inbound client ACTION may be dispatched.
14
+ *
15
+ * Rejects, in order:
16
+ * - **Internal action types** — clients must not be able to inject framework
17
+ * actions (`__HYDRATE__`, `__PLAYER_JOINED__`, etc.) to forge state.
18
+ * - **Un-joined sockets** — a socket with no resolved player ID never completed
19
+ * a JOIN, so its actions have no owner and must not mutate state.
20
+ *
21
+ * The decision is pure so it can be unit-tested without a WebSocket or React.
22
+ *
23
+ * @param actionType - The `type` field of the client's action payload.
24
+ * @param resolvedPlayerId - The player ID cached for the socket at JOIN time, or
25
+ * `undefined` if the socket has not joined.
26
+ */
27
+ export declare function authorizeClientAction(actionType: string, resolvedPlayerId: string | undefined): ActionAuthorization;
28
+ //# sourceMappingURL=action-authorization.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"action-authorization.d.ts","sourceRoot":"","sources":["../src/action-authorization.ts"],"names":[],"mappings":"AAEA,6EAA6E;AAC7E,MAAM,MAAM,mBAAmB,GAAG,kBAAkB,GAAG,YAAY,CAAC;AAEpE,+DAA+D;AAC/D,MAAM,MAAM,mBAAmB,GAC3B;IAAE,IAAI,EAAE,OAAO,CAAC;IAAC,QAAQ,EAAE,MAAM,CAAA;CAAE,GACnC;IAAE,IAAI,EAAE,QAAQ,CAAC;IAAC,IAAI,EAAE,mBAAmB,CAAC;IAAC,OAAO,EAAE,MAAM,CAAA;CAAE,CAAC;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C;IAC/C,cAAc,CAAC,EAAE,CAAC,QAAQ,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,KAAK,IAAI,CAAC;IAC1D,wCAAwC;IACxC,YAAY,CAAC,EAAE,CAAC,QAAQ,EAAE,MAAM,KAAK,IAAI,CAAC;IAC1C,yCAAyC;IACzC,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,IAAI,CAAC;CAClC;AAED,UAAU,oBAAoB,CAAC,CAAC,SAAS,UAAU,EAAE,CAAC,SAAS,OAAO;IACpE,KAAK,EAAE,CAAC,CAAC;IACT,QAAQ,EAAE,CAAC,MAAM,EAAE,CAAC,KAAK,IAAI,CAAC;IAC9B,SAAS,EAAE,MAAM,GAAG,IAAI,CAAC;IACzB,WAAW,EAAE,KAAK,GAAG,IAAI,CAAC;CAC3B;AAQD;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,gBAAgB,CAAC,CAAC,SAAS,UAAU,EAAE,CAAC,SAAS,OAAO,EAAE,EACxE,QAAQ,EACR,MAAM,GACP,EAAE;IACD,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAC;IAC1B,MAAM,EAAE,cAAc,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;CAC9B,qBAqWA;AAED;;;;;;;;;GASG;AACH,wBAAgB,WAAW,CAAC,CAAC,SAAS,UAAU,EAAE,CAAC,SAAS,OAAO,KAK/C,oBAAoB,CAAC,CAAC,EAAE,CAAC,CAAC,CAC7C"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@couch-kit/host",
3
- "version": "1.7.11",
3
+ "version": "1.7.12",
4
4
  "publishConfig": {
5
5
  "access": "public",
6
6
  "provenance": true
@@ -0,0 +1,55 @@
1
+ import { InternalActionTypes } from "@couch-kit/core";
2
+
3
+ /** Error codes surfaced when a client ACTION is rejected before dispatch. */
4
+ export type ActionRejectionCode = "FORBIDDEN_ACTION" | "NOT_JOINED";
5
+
6
+ /** Outcome of authorizing an inbound client ACTION message. */
7
+ export type ActionAuthorization =
8
+ | { kind: "allow"; playerId: string }
9
+ | { kind: "reject"; code: ActionRejectionCode; message: string };
10
+
11
+ const INTERNAL_ACTION_TYPES = new Set<string>([
12
+ InternalActionTypes.HYDRATE,
13
+ InternalActionTypes.PLAYER_JOINED,
14
+ InternalActionTypes.PLAYER_LEFT,
15
+ InternalActionTypes.PLAYER_RECONNECTED,
16
+ InternalActionTypes.PLAYER_REMOVED,
17
+ ]);
18
+
19
+ /**
20
+ * Decide whether an inbound client ACTION may be dispatched.
21
+ *
22
+ * Rejects, in order:
23
+ * - **Internal action types** — clients must not be able to inject framework
24
+ * actions (`__HYDRATE__`, `__PLAYER_JOINED__`, etc.) to forge state.
25
+ * - **Un-joined sockets** — a socket with no resolved player ID never completed
26
+ * a JOIN, so its actions have no owner and must not mutate state.
27
+ *
28
+ * The decision is pure so it can be unit-tested without a WebSocket or React.
29
+ *
30
+ * @param actionType - The `type` field of the client's action payload.
31
+ * @param resolvedPlayerId - The player ID cached for the socket at JOIN time, or
32
+ * `undefined` if the socket has not joined.
33
+ */
34
+ export function authorizeClientAction(
35
+ actionType: string,
36
+ resolvedPlayerId: string | undefined,
37
+ ): ActionAuthorization {
38
+ if (INTERNAL_ACTION_TYPES.has(actionType)) {
39
+ return {
40
+ kind: "reject",
41
+ code: "FORBIDDEN_ACTION",
42
+ message: "Internal action types cannot be dispatched by clients",
43
+ };
44
+ }
45
+
46
+ if (!resolvedPlayerId) {
47
+ return {
48
+ kind: "reject",
49
+ code: "NOT_JOINED",
50
+ message: "You must JOIN before dispatching actions",
51
+ };
52
+ }
53
+
54
+ return { kind: "allow", playerId: resolvedPlayerId };
55
+ }
package/src/provider.tsx CHANGED
@@ -27,6 +27,7 @@ import {
27
27
  HostSessionManager,
28
28
  type JoinSessionPayload,
29
29
  } from "./session-manager";
30
+ import { authorizeClientAction } from "./action-authorization";
30
31
  import {
31
32
  BroadcastScheduler,
32
33
  DEFAULT_STATE_THROTTLE_MS,
@@ -283,28 +284,25 @@ export function GameHostProvider<S extends IGameState, A extends IAction>({
283
284
  }
284
285
 
285
286
  case MessageTypes.ACTION: {
286
- // Only accept actions with a user-defined type string,
287
- // reject internal action types to prevent injection.
288
287
  const actionPayload = message.payload as A;
289
- if (
290
- actionPayload.type === InternalActionTypes.HYDRATE ||
291
- actionPayload.type === InternalActionTypes.PLAYER_JOINED ||
292
- actionPayload.type === InternalActionTypes.PLAYER_LEFT ||
293
- actionPayload.type === InternalActionTypes.PLAYER_RECONNECTED ||
294
- actionPayload.type === InternalActionTypes.PLAYER_REMOVED
295
- ) {
288
+
289
+ // Authorize before doing any work: reject client-injected internal
290
+ // action types and actions from sockets that never completed a JOIN.
291
+ const resolvedPlayerId =
292
+ sessionManager.current.getPlayerIdForSocket(socketId);
293
+ const auth = authorizeClientAction(
294
+ actionPayload.type,
295
+ resolvedPlayerId,
296
+ );
297
+ if (auth.kind === "reject") {
296
298
  if (configRef.current.debug)
297
299
  console.warn(
298
- `[GameHost] Rejected internal action from ${socketId}:`,
300
+ `[GameHost] Rejected action from ${socketId} (${auth.code}):`,
299
301
  actionPayload.type,
300
302
  );
301
303
  server.send(socketId, {
302
304
  type: MessageTypes.ERROR,
303
- payload: {
304
- code: "FORBIDDEN_ACTION",
305
- message:
306
- "Internal action types cannot be dispatched by clients",
307
- },
305
+ payload: { code: auth.code, message: auth.message },
308
306
  });
309
307
  return;
310
308
  }
@@ -323,10 +321,7 @@ export function GameHostProvider<S extends IGameState, A extends IAction>({
323
321
  return;
324
322
  }
325
323
 
326
- // Use cached playerId (populated at JOIN time)
327
- const resolvedPlayerId =
328
- sessionManager.current.getPlayerIdForSocket(socketId);
329
- dispatch({ ...actionPayload, playerId: resolvedPlayerId });
324
+ dispatch({ ...actionPayload, playerId: auth.playerId });
330
325
  actionQueue.current.push(actionPayload);
331
326
  break;
332
327
  }