mppx 0.6.27 → 0.6.28

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 (39) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/dist/Store.d.ts +32 -9
  3. package/dist/Store.d.ts.map +1 -1
  4. package/dist/Store.js +42 -10
  5. package/dist/Store.js.map +1 -1
  6. package/dist/proxy/internal/Headers.d.ts +13 -1
  7. package/dist/proxy/internal/Headers.d.ts.map +1 -1
  8. package/dist/proxy/internal/Headers.js +14 -1
  9. package/dist/proxy/internal/Headers.js.map +1 -1
  10. package/dist/stripe/server/Charge.d.ts +31 -1
  11. package/dist/stripe/server/Charge.d.ts.map +1 -1
  12. package/dist/stripe/server/Charge.js +88 -11
  13. package/dist/stripe/server/Charge.js.map +1 -1
  14. package/dist/tempo/server/Charge.d.ts +6 -0
  15. package/dist/tempo/server/Charge.d.ts.map +1 -1
  16. package/dist/tempo/server/Charge.js +7 -2
  17. package/dist/tempo/server/Charge.js.map +1 -1
  18. package/dist/tempo/server/Session.d.ts +6 -0
  19. package/dist/tempo/server/Session.d.ts.map +1 -1
  20. package/dist/tempo/server/Session.js +1 -1
  21. package/dist/tempo/server/Session.js.map +1 -1
  22. package/dist/tempo/server/Subscription.d.ts +6 -0
  23. package/dist/tempo/server/Subscription.d.ts.map +1 -1
  24. package/dist/tempo/server/Subscription.js +1 -1
  25. package/dist/tempo/server/Subscription.js.map +1 -1
  26. package/package.json +1 -1
  27. package/src/Store.test-d.ts +58 -0
  28. package/src/Store.test.ts +77 -0
  29. package/src/Store.ts +155 -74
  30. package/src/proxy/internal/Headers.test.ts +18 -0
  31. package/src/proxy/internal/Headers.ts +14 -1
  32. package/src/stripe/server/Charge.test.ts +215 -1
  33. package/src/stripe/server/Charge.ts +150 -20
  34. package/src/tempo/server/Charge.test.ts +22 -1
  35. package/src/tempo/server/Charge.ts +16 -2
  36. package/src/tempo/server/Session.ts +9 -1
  37. package/src/tempo/server/Subscription.ts +7 -1
  38. package/src/tempo/session/ChannelStore.test.ts +21 -0
  39. package/src/tempo/subscription/Store.test.ts +55 -0
package/src/Store.ts CHANGED
@@ -84,11 +84,55 @@ export type AtomicStore<itemMap extends StoreItemMap = StoreItemMap> = Store<
84
84
  AtomicActions<itemMap>
85
85
  >
86
86
 
87
+ /** Options shared by store constructors. */
88
+ export type Options = {
89
+ /** Prefix prepended to every backing store key. */
90
+ keyPrefix?: string | undefined
91
+ }
92
+
93
+ const keyPrefixCache = new WeakMap<Store, Map<string, Store | AtomicStore>>()
94
+
87
95
  /** Creates a {@link Store} from an existing implementation. */
88
- export function from<store extends Store>(store: store): store
89
- export function from<store extends AtomicStore>(store: store): store
90
- export function from(store: Store | AtomicStore) {
91
- return store
96
+ export function from<store extends AtomicStore<any>>(store: store, options?: Options): store
97
+ export function from<store extends Store<any>>(store: store, options?: Options): store
98
+ export function from(store: Store | AtomicStore, options?: Options) {
99
+ return withKeyPrefix(store, options?.keyPrefix)
100
+ }
101
+
102
+ function withKeyPrefix(store: Store | AtomicStore, keyPrefix = ''): Store | AtomicStore {
103
+ if (!keyPrefix) return store
104
+
105
+ const cached = keyPrefixCache.get(store)?.get(keyPrefix)
106
+ if (cached) return cached
107
+
108
+ const backing = store as Store
109
+ const prefixedKey = (key: string) => `${keyPrefix}${key}`
110
+ const prefixed = from({
111
+ async get(key: string) {
112
+ return backing.get(prefixedKey(key)) as Promise<unknown>
113
+ },
114
+ async put(key: string, value: unknown) {
115
+ await backing.put(prefixedKey(key), value)
116
+ },
117
+ async delete(key: string) {
118
+ await backing.delete(prefixedKey(key))
119
+ },
120
+ ...('update' in store
121
+ ? {
122
+ async update<result>(
123
+ key: string,
124
+ fn: (current: unknown | null) => Change<unknown, result>,
125
+ ) {
126
+ return (store as AtomicStore).update(prefixedKey(key), fn as never)
127
+ },
128
+ }
129
+ : {}),
130
+ } satisfies Store | AtomicStore)
131
+
132
+ const cachedByPrefix = keyPrefixCache.get(store) ?? new Map<string, Store | AtomicStore>()
133
+ cachedByPrefix.set(keyPrefix, prefixed)
134
+ keyPrefixCache.set(store, cachedByPrefix)
135
+ return prefixed
92
136
  }
93
137
 
94
138
  function wrapJsonUpdate(
@@ -113,26 +157,37 @@ function wrapJsonUpdate(
113
157
  }
114
158
 
115
159
  /** Wraps a Cloudflare KV namespace. */
116
- export function cloudflare(kv: cloudflare.AtomicParameters): AtomicStore
117
- export function cloudflare(kv: cloudflare.Parameters): Store
118
- export function cloudflare(kv: cloudflare.Parameters): Store {
119
- return from({
120
- async get(key) {
121
- const raw = await kv.get(key)
122
- if (raw == null) return null as any
123
- return Json.parse(raw as string)
160
+ export function cloudflare(
161
+ kv: cloudflare.AtomicParameters,
162
+ options?: cloudflare.Options,
163
+ ): AtomicStore
164
+ export function cloudflare(kv: cloudflare.Parameters, options?: cloudflare.Options): Store
165
+ export function cloudflare(kv: cloudflare.Parameters, options?: cloudflare.Options): Store {
166
+ return from(
167
+ {
168
+ async get(key: string) {
169
+ const raw = await kv.get(key)
170
+ if (raw == null) return null as any
171
+ return Json.parse(raw as string)
172
+ },
173
+ async put(key: string, value: unknown) {
174
+ await kv.put(key, Json.stringify(value))
175
+ },
176
+ async delete(key: string) {
177
+ await kv.delete(key)
178
+ },
179
+ ...wrapJsonUpdate(kv.update),
124
180
  },
125
- async put(key, value) {
126
- await kv.put(key, Json.stringify(value))
127
- },
128
- async delete(key) {
129
- await kv.delete(key)
130
- },
131
- ...wrapJsonUpdate(kv.update),
132
- })
181
+ options,
182
+ )
133
183
  }
134
184
 
135
185
  export declare namespace cloudflare {
186
+ export type Options = {
187
+ /** Prefix prepended to every backing store key. */
188
+ keyPrefix?: string | undefined
189
+ }
190
+
136
191
  export type Parameters = {
137
192
  get: (key: string) => Promise<unknown>
138
193
  put: (key: string, value: string) => Promise<void>
@@ -149,51 +204,69 @@ export declare namespace cloudflare {
149
204
  }
150
205
 
151
206
  /** In-memory store backed by a `Map`. JSON-roundtrips values to match production behavior. */
152
- export function memory(): AtomicStore {
207
+ export function memory(options?: memory.Options): AtomicStore {
153
208
  const store = new Map<string, string>()
154
- return from({
155
- async get(key) {
156
- const raw = store.get(key)
157
- if (raw === undefined) return null as any
158
- return Json.parse(raw)
209
+ return from(
210
+ {
211
+ async get(key: string) {
212
+ const raw = store.get(key)
213
+ if (raw === undefined) return null as any
214
+ return Json.parse(raw)
215
+ },
216
+ async put(key: string, value: unknown) {
217
+ store.set(key, Json.stringify(value))
218
+ },
219
+ async delete(key: string) {
220
+ store.delete(key)
221
+ },
222
+ async update<result>(key: string, fn: (current: unknown | null) => Change<unknown, result>) {
223
+ const current = store.has(key) ? (Json.parse(store.get(key)!) as never) : null
224
+ const change = fn(current)
225
+ if (change.op === 'set') store.set(key, Json.stringify(change.value))
226
+ if (change.op === 'delete') store.delete(key)
227
+ return change.result
228
+ },
159
229
  },
160
- async put(key, value) {
161
- store.set(key, Json.stringify(value))
162
- },
163
- async delete(key) {
164
- store.delete(key)
165
- },
166
- async update(key, fn) {
167
- const current = store.has(key) ? (Json.parse(store.get(key)!) as never) : null
168
- const change = fn(current)
169
- if (change.op === 'set') store.set(key, Json.stringify(change.value))
170
- if (change.op === 'delete') store.delete(key)
171
- return change.result
172
- },
173
- })
230
+ options,
231
+ )
232
+ }
233
+
234
+ export declare namespace memory {
235
+ export type Options = {
236
+ /** Prefix prepended to every backing store key. */
237
+ keyPrefix?: string | undefined
238
+ }
174
239
  }
175
240
 
176
241
  /** Wraps a standard Redis client (ioredis, node-redis, Valkey). */
177
- export function redis(client: redis.AtomicParameters): AtomicStore
178
- export function redis(client: redis.Parameters): Store
179
- export function redis(client: redis.Parameters): Store {
180
- return from({
181
- async get(key) {
182
- const raw = await client.get(key)
183
- if (raw == null) return null as any
184
- return Json.parse(raw)
185
- },
186
- async put(key, value) {
187
- await client.set(key, Json.stringify(value))
188
- },
189
- async delete(key) {
190
- await client.del(key)
242
+ export function redis(client: redis.AtomicParameters, options?: redis.Options): AtomicStore
243
+ export function redis(client: redis.Parameters, options?: redis.Options): Store
244
+ export function redis(client: redis.Parameters, options?: redis.Options): Store {
245
+ return from(
246
+ {
247
+ async get(key: string) {
248
+ const raw = await client.get(key)
249
+ if (raw == null) return null as any
250
+ return Json.parse(raw)
251
+ },
252
+ async put(key: string, value: unknown) {
253
+ await client.set(key, Json.stringify(value))
254
+ },
255
+ async delete(key: string) {
256
+ await client.del(key)
257
+ },
258
+ ...wrapJsonUpdate(client.update),
191
259
  },
192
- ...wrapJsonUpdate(client.update),
193
- })
260
+ options,
261
+ )
194
262
  }
195
263
 
196
264
  export declare namespace redis {
265
+ export type Options = {
266
+ /** Prefix prepended to every backing store key. */
267
+ keyPrefix?: string | undefined
268
+ }
269
+
197
270
  export type Parameters = {
198
271
  get: (key: string) => Promise<string | null>
199
272
  set: (key: string, value: string) => Promise<unknown>
@@ -210,28 +283,36 @@ export declare namespace redis {
210
283
  }
211
284
 
212
285
  /** Wraps an Upstash Redis instance (e.g. Vercel KV). */
213
- export function upstash(redis: upstash.AtomicParameters): AtomicStore
214
- export function upstash(redis: upstash.Parameters): Store
215
- export function upstash(redis: upstash.Parameters): Store {
216
- return from({
217
- async get(key) {
218
- return (await redis.get(key)) as any
219
- },
220
- async put(key, value) {
221
- await redis.set(key, value)
286
+ export function upstash(redis: upstash.AtomicParameters, options?: upstash.Options): AtomicStore
287
+ export function upstash(redis: upstash.Parameters, options?: upstash.Options): Store
288
+ export function upstash(redis: upstash.Parameters, options?: upstash.Options): Store {
289
+ return from(
290
+ {
291
+ async get(key: string) {
292
+ return (await redis.get(key)) as any
293
+ },
294
+ async put(key: string, value: unknown) {
295
+ await redis.set(key, value)
296
+ },
297
+ async delete(key: string) {
298
+ await redis.del(key)
299
+ },
300
+ ...(redis.update
301
+ ? {
302
+ update: redis.update as Update,
303
+ }
304
+ : {}),
222
305
  },
223
- async delete(key) {
224
- await redis.del(key)
225
- },
226
- ...(redis.update
227
- ? {
228
- update: redis.update as Update,
229
- }
230
- : {}),
231
- })
306
+ options,
307
+ )
232
308
  }
233
309
 
234
310
  export declare namespace upstash {
311
+ export type Options = {
312
+ /** Prefix prepended to every backing store key. */
313
+ keyPrefix?: string | undefined
314
+ }
315
+
235
316
  export type Parameters = {
236
317
  get: (key: string) => Promise<unknown>
237
318
  set: (key: string, value: unknown) => Promise<unknown>
@@ -98,4 +98,22 @@ describe('scrubResponse', () => {
98
98
  expect(result.statusText).toBe('Created')
99
99
  expect(await result.text()).toBe('hello')
100
100
  })
101
+
102
+ // Regression: an upstream service must never be able to issue a cookie
103
+ // under the proxy's origin. Otherwise a compromised or attacker-influenced
104
+ // upstream can session-fixate (`Set-Cookie: session=evil; Domain=…`) every
105
+ // sibling subdomain of the proxy. See the docblock on `scrubResponse`.
106
+ test('behavior: strips set-cookie so upstream cannot set cookies on proxy origin', () => {
107
+ const response = new Response('body', {
108
+ headers: [
109
+ ['Set-Cookie', '__Secure-session=evil; Domain=.example.com; Secure; HttpOnly'],
110
+ ['Set-Cookie', 'tracking=1; Path=/'],
111
+ ['Content-Type', 'application/json'],
112
+ ],
113
+ })
114
+ const result = Headers.scrubResponse(response)
115
+ expect(result.headers.has('set-cookie')).toBe(false)
116
+ expect(result.headers.getSetCookie?.() ?? []).toEqual([])
117
+ expect(result.headers.get('content-type')).toBe('application/json')
118
+ })
101
119
  })
@@ -29,11 +29,24 @@ export function scrub(headers: Headers): Headers {
29
29
  return scrubbed
30
30
  }
31
31
 
32
- /** Strips `content-encoding` and `content-length` from an upstream response so the proxy can re-stream it. */
32
+ /**
33
+ * Strips re-streaming headers (`content-encoding`, `content-length`) and
34
+ * security-sensitive headers (`set-cookie`) from an upstream response.
35
+ *
36
+ * `set-cookie` is dropped because a paid API proxy must never let an upstream
37
+ * service set cookies in the user's browser under the proxy's origin. If a
38
+ * compromised, misbehaving, or attacker-influenced upstream returned
39
+ * `Set-Cookie: session=evil; Domain=.example.com`, the browser would honor it
40
+ * for every sibling subdomain of the proxy — turning any future path-confusion
41
+ * or open-redirect bug in the surrounding deployment into a session-fixation
42
+ * primitive. Proxied services authenticate via bearer tokens / signed
43
+ * payloads, never cookies, so dropping `set-cookie` is purely defensive.
44
+ */
33
45
  export function scrubResponse(response: Response): Response {
34
46
  const headers = new Headers(response.headers)
35
47
  headers.delete('content-encoding')
36
48
  headers.delete('content-length')
49
+ headers.delete('set-cookie')
37
50
  return new Response(response.body, {
38
51
  status: response.status,
39
52
  statusText: response.statusText,
@@ -4,13 +4,18 @@ import { afterEach, describe, expect, test, vi } from 'vp/test'
4
4
  import * as Http from '~test/Http.js'
5
5
 
6
6
  import type { StripeClient } from '../internal/types.js'
7
+ import type { charge as StripeCharge } from './Charge.js'
7
8
 
8
9
  const realm = 'api.example.com'
9
10
  const secretKey = 'test-secret-key'
10
11
 
11
12
  let httpServer: Awaited<ReturnType<typeof Http.createServer>> | undefined
12
13
 
13
- afterEach(() => httpServer?.close())
14
+ afterEach(() => {
15
+ httpServer?.close()
16
+ httpServer = undefined
17
+ vi.restoreAllMocks()
18
+ })
14
19
 
15
20
  function createMockStripeClient(
16
21
  overrides?: Partial<{ status: string; id: string; throws: boolean }>,
@@ -126,6 +131,215 @@ describe('stripe.charge with client', () => {
126
131
  expect(params.metadata.mpp_is_mpp).toBe('true')
127
132
  })
128
133
 
134
+ test('behavior: applies Connect settlement parameters in client call', async () => {
135
+ const { client, create } = createMockStripeClient()
136
+
137
+ const server = Mppx.create({
138
+ methods: [
139
+ stripe.charge({
140
+ client,
141
+ connect({ request }) {
142
+ expect(request.amount).toBe('100')
143
+ return {
144
+ applicationFeeAmount: 12,
145
+ onBehalfOf: 'acct_merchant',
146
+ stripeAccount: 'acct_connected',
147
+ transferData: { amount: 88, destination: 'acct_destination' },
148
+ transferGroup: 'order_123',
149
+ }
150
+ },
151
+ networkId: 'internal',
152
+ paymentMethodTypes: ['card'],
153
+ }),
154
+ ],
155
+ realm,
156
+ secretKey,
157
+ })
158
+
159
+ const handle = server.charge({ amount: '1', currency: 'usd', decimals: 2 })
160
+ const firstResult = await handle(new Request('https://example.com'))
161
+ expect(firstResult.status).toBe(402)
162
+ if (firstResult.status !== 402) throw new Error()
163
+
164
+ const challenge = Challenge.fromResponse(firstResult.challenge)
165
+ expect(challenge.request).not.toHaveProperty('connect')
166
+ expect(challenge.request.methodDetails).not.toHaveProperty('applicationFeeAmount')
167
+ expect(challenge.request.methodDetails).not.toHaveProperty('stripeAccount')
168
+
169
+ const credential = Credential.from({
170
+ challenge,
171
+ payload: { spt: 'spt_test_token' },
172
+ })
173
+
174
+ const result = await handle(
175
+ new Request('https://example.com', {
176
+ headers: { Authorization: Credential.serialize(credential) },
177
+ }),
178
+ )
179
+
180
+ expect(result.status).toBe(200)
181
+ const [params, options] = create.mock.calls[0]!
182
+ expect(params).toMatchObject({
183
+ application_fee_amount: 12,
184
+ on_behalf_of: 'acct_merchant',
185
+ transfer_data: { amount: 88, destination: 'acct_destination' },
186
+ transfer_group: 'order_123',
187
+ })
188
+ expect(options).toMatchObject({ stripeAccount: 'acct_connected' })
189
+ })
190
+
191
+ test('behavior: applies Connect settlement parameters in secretKey call', async () => {
192
+ const fetchMock = vi.spyOn(globalThis, 'fetch').mockResolvedValueOnce(
193
+ new Response(JSON.stringify({ id: 'pi_fetch_123', status: 'succeeded' }), {
194
+ status: 200,
195
+ }),
196
+ )
197
+
198
+ const server = Mppx.create({
199
+ methods: [
200
+ stripe.charge({
201
+ connect: {
202
+ applicationFeeAmount: 12,
203
+ onBehalfOf: 'acct_merchant',
204
+ stripeAccount: 'acct_connected',
205
+ transferData: { amount: 88, destination: 'acct_destination' },
206
+ transferGroup: 'order_123',
207
+ },
208
+ networkId: 'internal',
209
+ paymentMethodTypes: ['card'],
210
+ secretKey,
211
+ }),
212
+ ],
213
+ realm,
214
+ secretKey,
215
+ })
216
+
217
+ const handle = server.charge({ amount: '1', currency: 'usd', decimals: 2 })
218
+ const firstResult = await handle(new Request('https://example.com'))
219
+ expect(firstResult.status).toBe(402)
220
+ if (firstResult.status !== 402) throw new Error()
221
+
222
+ const credential = Credential.from({
223
+ challenge: Challenge.fromResponse(firstResult.challenge),
224
+ payload: { spt: 'spt_test_token' },
225
+ })
226
+ const result = await handle(
227
+ new Request('https://example.com', {
228
+ headers: { Authorization: Credential.serialize(credential) },
229
+ }),
230
+ )
231
+
232
+ expect(result.status).toBe(200)
233
+ expect(fetchMock).toHaveBeenCalledOnce()
234
+ const [input, init] = fetchMock.mock.calls[0]!
235
+ expect(input).toBe('https://api.stripe.com/v1/payment_intents')
236
+ const headers = new Headers(init?.headers)
237
+ expect(headers.get('Stripe-Account')).toBe('acct_connected')
238
+ const body = init?.body as URLSearchParams
239
+ expect(body.get('application_fee_amount')).toBe('12')
240
+ expect(body.get('on_behalf_of')).toBe('acct_merchant')
241
+ expect(body.get('transfer_data[amount]')).toBe('88')
242
+ expect(body.get('transfer_data[destination]')).toBe('acct_destination')
243
+ expect(body.get('transfer_group')).toBe('order_123')
244
+ })
245
+
246
+ test('error: surfaces Connect PaymentIntent creation failures', async () => {
247
+ const { client } = createMockStripeClient({ throws: true })
248
+
249
+ const server = Mppx.create({
250
+ methods: [
251
+ stripe.charge({
252
+ client,
253
+ connect: { stripeAccount: 'acct_connected' },
254
+ networkId: 'internal',
255
+ paymentMethodTypes: ['card'],
256
+ }),
257
+ ],
258
+ realm,
259
+ secretKey,
260
+ })
261
+
262
+ httpServer = await Http.createServer(async (req, res) => {
263
+ const result = await Mppx.toNodeListener(
264
+ server.charge({ amount: '1', currency: 'usd', decimals: 2 }),
265
+ )(req, res)
266
+ if (result.status === 402) return
267
+ res.end('OK')
268
+ })
269
+
270
+ const response = await fetch(httpServer.url)
271
+ const challenge = Challenge.fromResponse(response)
272
+ const credential = Credential.from({
273
+ challenge,
274
+ payload: { spt: 'spt_test_token' },
275
+ })
276
+
277
+ const paidResponse = await fetch(httpServer.url, {
278
+ headers: { Authorization: Credential.serialize(credential) },
279
+ })
280
+ expect(paidResponse.status).toBe(402)
281
+ const body = (await paidResponse.json()) as { detail: string }
282
+ expect(body.detail).toContain('Stripe PaymentIntent failed')
283
+ })
284
+
285
+ const invalidConnectCases: readonly {
286
+ name: string
287
+ connect: StripeCharge.ConnectSettlement
288
+ }[] = [
289
+ { name: 'empty stripeAccount', connect: { stripeAccount: '' } },
290
+ { name: 'fee exceeds amount', connect: { applicationFeeAmount: 101 } },
291
+ { name: 'negative fee', connect: { applicationFeeAmount: -1 } },
292
+ {
293
+ name: 'empty transfer destination',
294
+ connect: { transferData: { destination: '' } },
295
+ },
296
+ {
297
+ name: 'missing transfer destination',
298
+ connect: { transferData: {} } as StripeCharge.ConnectSettlement,
299
+ },
300
+ {
301
+ name: 'transfer amount exceeds amount',
302
+ connect: { transferData: { amount: 101, destination: 'acct_destination' } },
303
+ },
304
+ ]
305
+
306
+ for (const { connect, name } of invalidConnectCases) {
307
+ test(`error: rejects invalid Connect settlement parameters (${name})`, async () => {
308
+ const { client, create } = createMockStripeClient()
309
+
310
+ const server = Mppx.create({
311
+ methods: [
312
+ stripe.charge({
313
+ client,
314
+ connect,
315
+ networkId: 'internal',
316
+ paymentMethodTypes: ['card'],
317
+ }),
318
+ ],
319
+ realm,
320
+ secretKey,
321
+ })
322
+
323
+ const handle = server.charge({ amount: '1', currency: 'usd', decimals: 2 })
324
+ const firstResult = await handle(new Request('https://example.com'))
325
+ expect(firstResult.status).toBe(402)
326
+ if (firstResult.status !== 402) throw new Error()
327
+
328
+ const credential = Credential.from({
329
+ challenge: Challenge.fromResponse(firstResult.challenge),
330
+ payload: { spt: 'spt_test_token' },
331
+ })
332
+ const result = await handle(
333
+ new Request('https://example.com', {
334
+ headers: { Authorization: Credential.serialize(credential) },
335
+ }),
336
+ )
337
+
338
+ expect(result.status).toBe(402)
339
+ expect(create).not.toHaveBeenCalled()
340
+ })
341
+ }
342
+
129
343
  test('behavior: rejects when client throws', async () => {
130
344
  const { client } = createMockStripeClient({ throws: true })
131
345