@forklaunch/core 3.4.0 → 4.0.0

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.
Files changed (34) hide show
  1. package/lib/agent/index.js +43 -1
  2. package/lib/agent/index.js.map +1 -1
  3. package/lib/agent/index.mjs +43 -1
  4. package/lib/agent/index.mjs.map +1 -1
  5. package/lib/cache/interfaces/ttlCache.interface.d.ts +12 -0
  6. package/lib/cache/interfaces/ttlCache.interface.d.ts.map +1 -1
  7. package/lib/http/application/expressLikeApplication.d.ts.map +1 -1
  8. package/lib/http/bindRuntimeAuth.d.ts +27 -1
  9. package/lib/http/bindRuntimeAuth.d.ts.map +1 -1
  10. package/lib/http/discriminateAuthMethod.d.ts +2 -1
  11. package/lib/http/discriminateAuthMethod.d.ts.map +1 -1
  12. package/lib/http/hmacReplayProtection.d.ts +111 -0
  13. package/lib/http/hmacReplayProtection.d.ts.map +1 -0
  14. package/lib/http/index.d.ts +3 -0
  15. package/lib/http/index.d.ts.map +1 -1
  16. package/lib/http/index.js +739 -115
  17. package/lib/http/index.js.map +1 -1
  18. package/lib/http/index.mjs +682 -77
  19. package/lib/http/index.mjs.map +1 -1
  20. package/lib/http/instanceGateway.d.ts.map +1 -1
  21. package/lib/http/jwtAlgorithms.d.ts +44 -0
  22. package/lib/http/jwtAlgorithms.d.ts.map +1 -0
  23. package/lib/http/mcpGenerator/mcpGenerator.d.ts.map +1 -1
  24. package/lib/http/mcpGenerator/mcpHttpHandler.d.ts +57 -0
  25. package/lib/http/mcpGenerator/mcpHttpHandler.d.ts.map +1 -0
  26. package/lib/http/middleware/request/auth.middleware.d.ts.map +1 -1
  27. package/lib/http/middleware/request/cors.middleware.d.ts +45 -0
  28. package/lib/http/middleware/request/cors.middleware.d.ts.map +1 -1
  29. package/lib/http/telemetry/openTelemetryCollector.d.ts.map +1 -1
  30. package/lib/http/types/contractDetails.types.d.ts +42 -2
  31. package/lib/http/types/contractDetails.types.d.ts.map +1 -1
  32. package/lib/http/types/expressLikeOptions.d.ts +22 -1
  33. package/lib/http/types/expressLikeOptions.d.ts.map +1 -1
  34. package/package.json +29 -34
@@ -0,0 +1,111 @@
1
+ import type { TtlCache } from '../cache/interfaces/ttlCache.interface';
2
+ /**
3
+ * Replay protection shared by every HMAC verifier in the framework: the
4
+ * `hmac` route auth method (service-to-service calls) and platform event
5
+ * deliveries (`verifyPlatformEvent`).
6
+ *
7
+ * A signed request is accepted only when
8
+ * 1. its timestamp is within the allowed clock skew of now,
9
+ * 2. its signature matches (compared in constant time), and
10
+ * 3. its nonce has not been used before inside the freshness window.
11
+ *
12
+ * The nonce is claimed only after the signature has verified, so a caller
13
+ * without the key can never fill the store or burn somebody else's nonce.
14
+ */
15
+ /** How far a request timestamp may be from the server clock, either way. */
16
+ export declare const DEFAULT_HMAC_MAX_SKEW_MS: number;
17
+ /** How many nonces the default in-memory store remembers. */
18
+ export declare const DEFAULT_HMAC_NONCE_STORE_MAX_ENTRIES = 100000;
19
+ /**
20
+ * Constant-time comparison of two signatures. A length mismatch is a
21
+ * rejection, never a throw (`timingSafeEqual` throws on unequal lengths).
22
+ */
23
+ export declare function timingSafeEqualStrings(expected: string, received: string): boolean;
24
+ /** A usable skew window: a positive, finite number, else the default. */
25
+ export declare function resolveHmacMaxSkewMs(maxSkewMs?: number): number;
26
+ /**
27
+ * Whether a request timestamp is valid and within `maxSkewMs` of `nowMs`,
28
+ * in the past or the future. A missing or unparseable timestamp is stale.
29
+ */
30
+ export declare function isHmacTimestampFresh(timestamp: Date | undefined | null, nowMs: number, maxSkewMs?: number): boolean;
31
+ /**
32
+ * How long a nonce must be remembered: until the last moment its signature
33
+ * could still pass the freshness check. A timestamp from the future (within
34
+ * the skew) stays fresh for up to twice the window, so the lifetime is
35
+ * measured from the signed timestamp, not from when the request arrived.
36
+ */
37
+ export declare function hmacNonceTtlMs(timestamp: Date, nowMs: number, maxSkewMs: number): number;
38
+ /**
39
+ * The store key for a nonce. The key id is length-prefixed so that no two
40
+ * (key id, nonce) pairs can collide.
41
+ */
42
+ export declare function hmacNonceKey(keyId: string | undefined, nonce: string): string;
43
+ /**
44
+ * Where used nonces are remembered.
45
+ *
46
+ * `claim` records the nonce and answers whether this caller is the first to
47
+ * use it: `true` the first time, `false` for every later call until `ttlMs`
48
+ * has passed. It must be atomic (for Redis: `SET key 1 NX PX ttlMs`, which
49
+ * answers `OK` only for the first writer). If it throws, the request is
50
+ * rejected: storage failure fails closed.
51
+ */
52
+ export interface HmacNonceStore {
53
+ claim(key: string, ttlMs: number): boolean | Promise<boolean>;
54
+ }
55
+ /**
56
+ * The default nonce store: a bounded map in this process's memory.
57
+ *
58
+ * IMPORTANT: this protects a SINGLE PROCESS only. Each copy of the app has
59
+ * its own map, so when an app runs several copies (replicas, cluster
60
+ * workers, serverless instances) a captured request can be replayed once
61
+ * against every other copy inside the freshness window, and all memory is
62
+ * lost on restart. An app that runs more than one copy needs a shared store
63
+ * (pass `hmac.nonceStore`: a `TtlCache` with `putRecordIfAbsent` such as the
64
+ * Redis cache, or an `HmacNonceStore` over Redis `SET NX PX`) for full
65
+ * protection.
66
+ *
67
+ * Bounded: entries expire after their ttl, and when `maxEntries` unexpired
68
+ * nonces are held the oldest is dropped to make room. Only requests that
69
+ * carried a valid signature are ever stored, so only a holder of the key can
70
+ * fill it; past the bound the dropped nonces lose replay protection, which
71
+ * is one more reason high-volume apps should use a shared store.
72
+ */
73
+ export declare class InMemoryHmacNonceStore implements HmacNonceStore {
74
+ private readonly entries;
75
+ private readonly maxEntries;
76
+ private readonly sweepIntervalMs;
77
+ private lastSweepAt;
78
+ constructor(options?: {
79
+ maxEntries?: number;
80
+ sweepIntervalMs?: number;
81
+ });
82
+ /** Number of nonces currently held (expired ones may linger until swept). */
83
+ get size(): number;
84
+ claim(key: string, ttlMs: number, nowMs?: number): boolean;
85
+ private sweep;
86
+ }
87
+ /** The parts of a `TtlCache` a nonce store uses. */
88
+ export type HmacNonceCache = Pick<TtlCache, 'peekRecord' | 'putRecord' | 'putRecordIfAbsent'>;
89
+ /**
90
+ * Use one of the framework's `TtlCache` implementations (for example the
91
+ * Redis cache) as a nonce store shared by every copy of the app.
92
+ *
93
+ * When the cache implements `putRecordIfAbsent` (the Redis cache does, with
94
+ * `SET NX PX`) the claim is one atomic step and replay protection is
95
+ * complete: of any number of simultaneous copies of a request, exactly one
96
+ * is accepted.
97
+ *
98
+ * FALLBACK, for caches without `putRecordIfAbsent` only: a check followed by
99
+ * a write. That is NOT atomic. Two copies of the app handling the same
100
+ * replayed request at the very same instant can both pass. It still rejects
101
+ * every replay that arrives after the first request was recorded. Implement
102
+ * `putRecordIfAbsent` on the cache, or `HmacNonceStore` directly over an
103
+ * atomic primitive, to close that gap.
104
+ */
105
+ export declare function ttlCacheHmacNonceStore(cache: HmacNonceCache, keyPrefix?: string): HmacNonceStore;
106
+ /**
107
+ * The store to claim nonces in: the configured `HmacNonceStore`, a
108
+ * `TtlCache` wrapped as one, or the process-wide in-memory default.
109
+ */
110
+ export declare function resolveHmacNonceStore(configured?: HmacNonceStore | HmacNonceCache): HmacNonceStore;
111
+ //# sourceMappingURL=hmacReplayProtection.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hmacReplayProtection.d.ts","sourceRoot":"","sources":["../../src/http/hmacReplayProtection.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,wCAAwC,CAAC;AAEvE;;;;;;;;;;;;GAYG;AAEH,4EAA4E;AAC5E,eAAO,MAAM,wBAAwB,QAAgB,CAAC;AAEtD,6DAA6D;AAC7D,eAAO,MAAM,oCAAoC,SAAU,CAAC;AAE5D;;;GAGG;AACH,wBAAgB,sBAAsB,CACpC,QAAQ,EAAE,MAAM,EAChB,QAAQ,EAAE,MAAM,GACf,OAAO,CAUT;AAED,yEAAyE;AACzE,wBAAgB,oBAAoB,CAAC,SAAS,CAAC,EAAE,MAAM,GAAG,MAAM,CAM/D;AAED;;;GAGG;AACH,wBAAgB,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SAAS,SAAgB,GACxB,cAAc,CAmBhB;AAKD;;;GAGG;AACH,wBAAgB,qBAAqB,CACnC,UAAU,CAAC,EAAE,cAAc,GAAG,cAAc,GAC3C,cAAc,CAuBhB"}
@@ -14,6 +14,8 @@ export * from './whatsappClient';
14
14
  export * from './voiceClient';
15
15
  export * from './paymentsClient';
16
16
  export * from './discriminateAuthMethod';
17
+ export * from './hmacReplayProtection';
18
+ export * from './jwtAlgorithms';
17
19
  export * from './guards/isForklaunchRequest';
18
20
  export * from './guards/isForklaunchRouter';
19
21
  export * from './handlers/delete';
@@ -30,6 +32,7 @@ export * from './handlers/typedHandler';
30
32
  export * from './httpStatusCodes';
31
33
  export * from './interfaces/expressLikeRouter.interface';
32
34
  export * from './mcpGenerator/mcpGenerator';
35
+ export * from './mcpGenerator/mcpHttpHandler';
33
36
  export * from './middleware/request/createContext.middleware';
34
37
  export * from './middleware/response/enrichExpressLikeSend.middleware';
35
38
  export * from './openApiV3Generator/openApiV3Generator';
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/http/index.ts"],"names":[],"mappings":"AAAA,cAAc,mBAAmB,CAAC;AAClC,YAAY,EAAE,QAAQ,EAAE,MAAM,IAAI,CAAC;AACnC,cAAc,sCAAsC,CAAC;AACrD,cAAc,yBAAyB,CAAC;AACxC,cAAc,uBAAuB,CAAC;AACtC,cAAc,mBAAmB,CAAC;AAClC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,mBAAmB,CAAC;AAClC,cAAc,sBAAsB,CAAC;AACrC,cAAc,6BAA6B,CAAC;AAC5C,cAAc,eAAe,CAAC;AAC9B,cAAc,aAAa,CAAC;AAC5B,cAAc,kBAAkB,CAAC;AACjC,cAAc,eAAe,CAAC;AAC9B,cAAc,kBAAkB,CAAC;AACjC,cAAc,0BAA0B,CAAC;AACzC,cAAc,8BAA8B,CAAC;AAC7C,cAAc,6BAA6B,CAAC;AAC5C,cAAc,mBAAmB,CAAC;AAClC,cAA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