@middy/http-x402 8.0.0-alpha.1 → 8.0.0-alpha.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/README.md +2 -2
- package/index.d.ts +30 -5
- package/index.js +168 -34
- package/package.json +17 -6
package/README.md
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
<p>
|
|
6
6
|
<a href="https://github.com/middyjs/middy/actions/workflows/test-unit.yml"><img src="https://github.com/middyjs/middy/actions/workflows/test-unit.yml/badge.svg" alt="GitHub Actions unit test status"></a>
|
|
7
7
|
<a href="https://github.com/middyjs/middy/actions/workflows/test-dast.yml"><img src="https://github.com/middyjs/middy/actions/workflows/test-dast.yml/badge.svg" alt="GitHub Actions dast test status"></a>
|
|
8
|
-
<a href="https://github.com/middyjs/middy/actions/workflows/test-
|
|
8
|
+
<a href="https://github.com/middyjs/middy/actions/workflows/test-bench.yml"><img src="https://github.com/middyjs/middy/actions/workflows/test-bench.yml/badge.svg" alt="GitHub Actions bench test status"></a>
|
|
9
9
|
<a href="https://github.com/middyjs/middy/actions/workflows/test-sast.yml"><img src="https://github.com/middyjs/middy/actions/workflows/test-sast.yml/badge.svg" alt="GitHub Actions SAST test status"></a>
|
|
10
10
|
<a href="https://github.com/middyjs/middy/actions/workflows/test-lint.yml"><img src="https://github.com/middyjs/middy/actions/workflows/test-lint.yml/badge.svg" alt="GitHub Actions lint test status"></a>
|
|
11
11
|
<br/>
|
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
<a href="https://github.com/middyjs/middy/blob/main/docs/CODE_OF_CONDUCT.md"><img src="https://img.shields.io/badge/Contributor%20Covenant-2.1-4baaaa.svg"></a>
|
|
22
22
|
<a href="https://biomejs.dev"><img alt="Checked with Biome" src="https://img.shields.io/badge/Checked_with-Biome-60a5fa?style=flat&logo=biome"></a>
|
|
23
23
|
<a href="https://conventionalcommits.org"><img alt="Conventional Commits" src="https://img.shields.io/badge/Conventional%20Commits-1.0.0-%23FE5196?logo=conventionalcommits&logoColor=white"></a>
|
|
24
|
-
<a href="https://github.com/middyjs/middy/blob/main/package.json
|
|
24
|
+
<a href="https://github.com/middyjs/middy/blob/main/package.json">
|
|
25
25
|
<img alt="code coverage" src="https://img.shields.io/badge/code%20coverage-100%25-brightgreen"></a>
|
|
26
26
|
<br/>
|
|
27
27
|
</p>
|
package/index.d.ts
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
// Copyright 2017 - 2026 will Farrell, Luciano Mammino, and Middy contributors.
|
|
2
2
|
// SPDX-License-Identifier: MIT
|
|
3
3
|
import type middy from "@middy/core";
|
|
4
|
-
import type {
|
|
4
|
+
import type {
|
|
5
|
+
APIGatewayEvent,
|
|
6
|
+
APIGatewayProxyEventV2,
|
|
7
|
+
Context as LambdaContext,
|
|
8
|
+
} from "aws-lambda";
|
|
5
9
|
|
|
6
10
|
export type RequestEvent = APIGatewayEvent | APIGatewayProxyEventV2;
|
|
7
11
|
|
|
@@ -23,18 +27,39 @@ interface BaseOptions {
|
|
|
23
27
|
description?: string;
|
|
24
28
|
mimeType?: string;
|
|
25
29
|
extra?: Record<string, unknown>;
|
|
30
|
+
/**
|
|
31
|
+
* Return `true` to skip payment for this request. Only a literal `true`
|
|
32
|
+
* bypasses; returning a Promise throws a TypeError.
|
|
33
|
+
*/
|
|
26
34
|
human?: (request: middy.Request<RequestEvent>) => boolean;
|
|
27
35
|
}
|
|
28
36
|
|
|
29
37
|
export type Options = BaseOptions &
|
|
30
38
|
({ price: number | string } | { amount: string });
|
|
31
39
|
|
|
40
|
+
/**
|
|
41
|
+
* The verified payment the middleware writes to `request.internal.x402`. The
|
|
42
|
+
* settlement fields are added in `after`, once the facilitator settles.
|
|
43
|
+
*/
|
|
44
|
+
export interface X402Internal {
|
|
45
|
+
payload: unknown;
|
|
46
|
+
requirements: unknown;
|
|
47
|
+
payer?: string;
|
|
48
|
+
transaction?: string;
|
|
49
|
+
network?: string;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* `x402` is absent when `human` bypassed payment for the request.
|
|
54
|
+
*/
|
|
55
|
+
export type Internal = { x402?: X402Internal };
|
|
56
|
+
|
|
32
57
|
declare function httpX402<EventType extends RequestEvent = RequestEvent>(
|
|
33
58
|
options: Options,
|
|
34
|
-
): middy.MiddlewareObj<EventType, unknown, Error>;
|
|
59
|
+
): middy.MiddlewareObj<EventType, unknown, Error, LambdaContext, Internal>;
|
|
35
60
|
|
|
36
|
-
export declare function httpX402ValidateOptions(
|
|
37
|
-
options?:
|
|
38
|
-
):
|
|
61
|
+
export declare function httpX402ValidateOptions<TOptions extends Options>(
|
|
62
|
+
options?: TOptions,
|
|
63
|
+
): TOptions;
|
|
39
64
|
|
|
40
65
|
export default httpX402;
|
package/index.js
CHANGED
|
@@ -150,7 +150,7 @@ const httpX402Middleware = (opts = {}) => {
|
|
|
150
150
|
request.response.headers["PAYMENT-REQUIRED"] =
|
|
151
151
|
encodeHeader(paymentRequired);
|
|
152
152
|
request.response.body = JSON.stringify(paymentRequired);
|
|
153
|
-
return request
|
|
153
|
+
return albMultiValueResponse(request);
|
|
154
154
|
};
|
|
155
155
|
|
|
156
156
|
// x402 v1 PaymentRequirements: every field is required by the v1 schema,
|
|
@@ -184,7 +184,7 @@ const httpX402Middleware = (opts = {}) => {
|
|
|
184
184
|
request.response.body = JSON.stringify(
|
|
185
185
|
buildPaymentRequiredV1(request.event, error),
|
|
186
186
|
);
|
|
187
|
-
return request
|
|
187
|
+
return albMultiValueResponse(request);
|
|
188
188
|
};
|
|
189
189
|
|
|
190
190
|
const httpX402V2Before = async (request, paymentHeader) => {
|
|
@@ -254,22 +254,32 @@ const httpX402Middleware = (opts = {}) => {
|
|
|
254
254
|
};
|
|
255
255
|
|
|
256
256
|
const httpX402MiddlewareBefore = (request) => {
|
|
257
|
-
|
|
257
|
+
// Only a literal `true` bypasses payment. An async callback returns a
|
|
258
|
+
// Promise, which is always truthy, so it would skip payment for every
|
|
259
|
+
// request; reject it loudly instead of failing open.
|
|
260
|
+
const isHuman = human?.(request);
|
|
261
|
+
if (typeof isHuman?.then === "function") {
|
|
262
|
+
throw new TypeError(`${pkg} human must return a boolean, not a Promise`, {
|
|
263
|
+
cause: { package: pkg },
|
|
264
|
+
});
|
|
265
|
+
}
|
|
266
|
+
if (isHuman === true) return;
|
|
258
267
|
|
|
259
268
|
// A disabled version's payment header is not a payment header for this
|
|
260
269
|
// server; the client falls through and is re-challenged with the
|
|
261
270
|
// enabled formats only.
|
|
262
|
-
|
|
271
|
+
// ALB with multi-value headers enabled sends only `multiValueHeaders`.
|
|
272
|
+
// https://docs.aws.amazon.com/elasticloadbalancing/latest/application/lambda-functions.html#multi-value-headers
|
|
273
|
+
const headers =
|
|
274
|
+
request.event.headers ?? request.event.multiValueHeaders ?? {};
|
|
263
275
|
const v2PaymentHeader = v2Enabled
|
|
264
|
-
? (headers
|
|
265
|
-
headers["Payment-Signature"] ??
|
|
266
|
-
headers["PAYMENT-SIGNATURE"])
|
|
276
|
+
? readHeader(headers, "payment-signature")
|
|
267
277
|
: undefined;
|
|
268
278
|
if (v2PaymentHeader) {
|
|
269
279
|
return httpX402V2Before(request, v2PaymentHeader);
|
|
270
280
|
}
|
|
271
281
|
const v1PaymentHeader = v1Enabled
|
|
272
|
-
? (headers
|
|
282
|
+
? readHeader(headers, "x-payment")
|
|
273
283
|
: undefined;
|
|
274
284
|
if (v1PaymentHeader) {
|
|
275
285
|
return httpX402V1Before(request, v1PaymentHeader);
|
|
@@ -296,7 +306,7 @@ const httpX402Middleware = (opts = {}) => {
|
|
|
296
306
|
)
|
|
297
307
|
: paymentRequired,
|
|
298
308
|
);
|
|
299
|
-
return request
|
|
309
|
+
return albMultiValueResponse(request);
|
|
300
310
|
}
|
|
301
311
|
return respondPaymentRequiredV1(request, "X-PAYMENT header is required");
|
|
302
312
|
};
|
|
@@ -309,6 +319,9 @@ const httpX402Middleware = (opts = {}) => {
|
|
|
309
319
|
if (request.response.statusCode >= 400) return;
|
|
310
320
|
|
|
311
321
|
const { payload, requirements: storedRequirements } = stored;
|
|
322
|
+
// Decided by the event, not the handler's response: API Gateway REST
|
|
323
|
+
// accepts `headers` and `multiValueHeaders` side by side, ALB does not.
|
|
324
|
+
const multiValue = isAlbMultiValue(request.event);
|
|
312
325
|
// v1 settlements are reported in X-PAYMENT-RESPONSE, v2 in
|
|
313
326
|
// PAYMENT-RESPONSE.
|
|
314
327
|
const responseHeader =
|
|
@@ -326,29 +339,24 @@ const httpX402Middleware = (opts = {}) => {
|
|
|
326
339
|
typeof error?.transaction === "string" ? error.transaction : "",
|
|
327
340
|
network: storedRequirements.network,
|
|
328
341
|
};
|
|
329
|
-
request.response
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
// The handler's response is replaced in place; a stale
|
|
337
|
-
// isBase64Encoded from a binary body would make API Gateway
|
|
338
|
-
// base64-decode this JSON.
|
|
339
|
-
request.response.isBase64Encoded = false;
|
|
342
|
+
request.response = settlementRefused(
|
|
343
|
+
payload,
|
|
344
|
+
responseHeader,
|
|
345
|
+
settleResponse,
|
|
346
|
+
request.response,
|
|
347
|
+
multiValue,
|
|
348
|
+
);
|
|
340
349
|
return;
|
|
341
350
|
}
|
|
342
351
|
|
|
343
352
|
if (!settleResult.success) {
|
|
344
|
-
request.response
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
request.response.isBase64Encoded = false;
|
|
353
|
+
request.response = settlementRefused(
|
|
354
|
+
payload,
|
|
355
|
+
responseHeader,
|
|
356
|
+
settleResult,
|
|
357
|
+
request.response,
|
|
358
|
+
multiValue,
|
|
359
|
+
);
|
|
352
360
|
return;
|
|
353
361
|
}
|
|
354
362
|
|
|
@@ -358,7 +366,14 @@ const httpX402Middleware = (opts = {}) => {
|
|
|
358
366
|
transaction: settleResult.transaction,
|
|
359
367
|
network: settleResult.network,
|
|
360
368
|
};
|
|
361
|
-
|
|
369
|
+
if (multiValue) {
|
|
370
|
+
request.response.multiValueHeaders ??= {};
|
|
371
|
+
request.response.multiValueHeaders[responseHeader] = [
|
|
372
|
+
encodeHeader(settleResult),
|
|
373
|
+
];
|
|
374
|
+
} else {
|
|
375
|
+
request.response.headers[responseHeader] = encodeHeader(settleResult);
|
|
376
|
+
}
|
|
362
377
|
};
|
|
363
378
|
|
|
364
379
|
return {
|
|
@@ -367,6 +382,111 @@ const httpX402Middleware = (opts = {}) => {
|
|
|
367
382
|
};
|
|
368
383
|
};
|
|
369
384
|
|
|
385
|
+
// Field names are case-insensitive (RFC 9110 §5.1) and API Gateway REST passes
|
|
386
|
+
// the client's casing through. ALB multi-value and VPC Lattice V2 deliver
|
|
387
|
+
// arrays; the first value is the one used.
|
|
388
|
+
const readHeader = (headers, lowerName) => {
|
|
389
|
+
for (const key of Object.keys(headers)) {
|
|
390
|
+
if (key.toLowerCase() === lowerName) {
|
|
391
|
+
const value = headers[key];
|
|
392
|
+
return Array.isArray(value) ? value[0] : value;
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
return undefined;
|
|
396
|
+
};
|
|
397
|
+
|
|
398
|
+
// Response-policy headers, not content: CORS (so a browser can read the 402 at
|
|
399
|
+
// all), `Vary`, and what http-security-headers sets. When those middlewares are
|
|
400
|
+
// .use()d after this one, their after hooks have already run on the handler's
|
|
401
|
+
// response, so they are carried over to the 402.
|
|
402
|
+
const policyHeaderNames = new Set([
|
|
403
|
+
"vary",
|
|
404
|
+
"strict-transport-security",
|
|
405
|
+
"x-content-type-options",
|
|
406
|
+
"x-frame-options",
|
|
407
|
+
"x-dns-prefetch-control",
|
|
408
|
+
"x-download-options",
|
|
409
|
+
"x-permitted-cross-domain-policies",
|
|
410
|
+
"x-xss-protection",
|
|
411
|
+
"referrer-policy",
|
|
412
|
+
"permissions-policy",
|
|
413
|
+
"origin-agent-cluster",
|
|
414
|
+
"reporting-endpoints",
|
|
415
|
+
"report-to",
|
|
416
|
+
"content-security-policy",
|
|
417
|
+
"content-security-policy-report-only",
|
|
418
|
+
]);
|
|
419
|
+
const isPolicyHeader = (name) => {
|
|
420
|
+
const lower = name.toLowerCase();
|
|
421
|
+
return (
|
|
422
|
+
policyHeaderNames.has(lower) ||
|
|
423
|
+
lower.startsWith("access-control-") ||
|
|
424
|
+
lower.startsWith("cross-origin-")
|
|
425
|
+
);
|
|
426
|
+
};
|
|
427
|
+
|
|
428
|
+
// Payment was not taken, so nothing the handler produced for the paid resource
|
|
429
|
+
// may reach the client: not its body, and not its content headers, multi-value
|
|
430
|
+
// headers or v2 cookies (Location, Set-Cookie). The handler's response is
|
|
431
|
+
// replaced whole rather than patched; only policy headers are kept.
|
|
432
|
+
const settlementRefused = (
|
|
433
|
+
payload,
|
|
434
|
+
responseHeader,
|
|
435
|
+
settlement,
|
|
436
|
+
replaced,
|
|
437
|
+
multiValue,
|
|
438
|
+
) => {
|
|
439
|
+
const headers = {};
|
|
440
|
+
for (const map of [replaced.headers, replaced.multiValueHeaders]) {
|
|
441
|
+
if (!map) continue;
|
|
442
|
+
for (const name of Object.keys(map)) {
|
|
443
|
+
if (!isPolicyHeader(name)) continue;
|
|
444
|
+
// Single-value `headers` cannot carry a `multiValueHeaders` array.
|
|
445
|
+
const value = map[name];
|
|
446
|
+
headers[name] =
|
|
447
|
+
!multiValue && Array.isArray(value) ? value.join(", ") : value;
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
headers["Content-Type"] = "application/json";
|
|
451
|
+
headers[responseHeader] = encodeHeader(settlement);
|
|
452
|
+
const response = {
|
|
453
|
+
statusCode: 402,
|
|
454
|
+
headers,
|
|
455
|
+
body: JSON.stringify({
|
|
456
|
+
x402Version: payload.x402Version,
|
|
457
|
+
error: settlement.errorReason,
|
|
458
|
+
}),
|
|
459
|
+
isBase64Encoded: false,
|
|
460
|
+
};
|
|
461
|
+
return multiValue ? toMultiValueHeaders(response) : response;
|
|
462
|
+
};
|
|
463
|
+
|
|
464
|
+
// An ALB target group with multi-value headers enabled sends
|
|
465
|
+
// `multiValueHeaders` and only reads them back: "You must use
|
|
466
|
+
// multiValueHeaders if you have enabled multi-value headers and headers
|
|
467
|
+
// otherwise." Its events are the only ones that carry `requestContext.elb`
|
|
468
|
+
// together with `multiValueHeaders`.
|
|
469
|
+
// https://docs.aws.amazon.com/elasticloadbalancing/latest/application/lambda-functions.html#multi-value-headers
|
|
470
|
+
const isAlbMultiValue = (event) =>
|
|
471
|
+
event.requestContext?.elb !== undefined &&
|
|
472
|
+
event.multiValueHeaders !== undefined;
|
|
473
|
+
|
|
474
|
+
const toMultiValueHeaders = ({ headers, ...response }) => {
|
|
475
|
+
const multiValueHeaders = {};
|
|
476
|
+
for (const name of Object.keys(headers)) {
|
|
477
|
+
const value = headers[name];
|
|
478
|
+
multiValueHeaders[name] = Array.isArray(value) ? value : [value];
|
|
479
|
+
}
|
|
480
|
+
return { ...response, multiValueHeaders };
|
|
481
|
+
};
|
|
482
|
+
|
|
483
|
+
const albMultiValueResponse = (request) => {
|
|
484
|
+
if (isAlbMultiValue(request.event)) {
|
|
485
|
+
request.response = toMultiValueHeaders(request.response);
|
|
486
|
+
}
|
|
487
|
+
return request.response;
|
|
488
|
+
};
|
|
489
|
+
|
|
370
490
|
// Facilitator clients throw errors carrying `invalidReason` / `errorReason`
|
|
371
491
|
// when the facilitator rejected the request; forward that reason without
|
|
372
492
|
// leaking the error message.
|
|
@@ -439,13 +559,27 @@ const toAtomicAmount = (price, decimals, amountOverride) => {
|
|
|
439
559
|
return String(BigInt(`${whole}${padded}`));
|
|
440
560
|
};
|
|
441
561
|
|
|
562
|
+
// The host comes only from API Gateway's `requestContext.domainName`; the Host
|
|
563
|
+
// header is client-controlled and never used. ALB and VPC Lattice carry no
|
|
564
|
+
// trusted domain, so they get `localhost`, with the request path from the
|
|
565
|
+
// event: `path` (ALB, Lattice V2) or `raw_path` (Lattice V1). Lattice includes
|
|
566
|
+
// the query string in both, which is not part of the resource. Lattice V2 also
|
|
567
|
+
// sends `version: "2.0"`, so the payload is told apart by
|
|
568
|
+
// `requestContext.http`, which only API Gateway HTTP API v2 and function URL
|
|
569
|
+
// events carry.
|
|
570
|
+
// https://docs.aws.amazon.com/elasticloadbalancing/latest/application/lambda-functions.html
|
|
571
|
+
// https://docs.aws.amazon.com/vpc-lattice/latest/ug/lambda-functions.html
|
|
442
572
|
const buildResource = (event) => {
|
|
443
|
-
|
|
444
|
-
|
|
573
|
+
const requestContext = event.requestContext;
|
|
574
|
+
if (requestContext?.http) {
|
|
575
|
+
return `https://${requestContext.domainName}${requestContext.http.path}`;
|
|
576
|
+
}
|
|
577
|
+
if (requestContext?.domainName) {
|
|
578
|
+
return `https://${requestContext.domainName}${requestContext.path ?? "/"}`;
|
|
445
579
|
}
|
|
446
|
-
const
|
|
447
|
-
const path =
|
|
448
|
-
return `https
|
|
580
|
+
const rawPath = requestContext?.path ?? event.path ?? event.raw_path;
|
|
581
|
+
const path = typeof rawPath === "string" ? rawPath.split("?", 1)[0] : "";
|
|
582
|
+
return `https://localhost${path || "/"}`;
|
|
449
583
|
};
|
|
450
584
|
|
|
451
585
|
const encodeHeader = (obj) =>
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@middy/http-x402",
|
|
3
|
-
"version": "8.0.0-alpha.
|
|
3
|
+
"version": "8.0.0-alpha.2",
|
|
4
4
|
"description": "x402 payment middleware for the middy framework",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -29,7 +29,7 @@
|
|
|
29
29
|
"test": "npm run test:unit && npm run test:fuzz",
|
|
30
30
|
"test:unit": "node --test",
|
|
31
31
|
"test:fuzz": "node --test index.fuzz.js",
|
|
32
|
-
"test:
|
|
32
|
+
"test:bench": "node --experimental-bench --bench 'index.bench.js'",
|
|
33
33
|
"test:mutation": "cd ../.. && MUTATE_PACKAGE=\"$(basename \"$OLDPWD\")\" stryker run"
|
|
34
34
|
},
|
|
35
35
|
"license": "MIT",
|
|
@@ -64,14 +64,25 @@
|
|
|
64
64
|
"url": "https://github.com/sponsors/willfarrell"
|
|
65
65
|
},
|
|
66
66
|
"peerDependencies": {
|
|
67
|
-
"@x402/core": "^2.
|
|
67
|
+
"@x402/core": "^2.28.0",
|
|
68
|
+
"@middy/core": "8.0.0-alpha.2",
|
|
69
|
+
"@types/aws-lambda": "^8.0.0"
|
|
70
|
+
},
|
|
71
|
+
"peerDependenciesMeta": {
|
|
72
|
+
"@middy/core": {
|
|
73
|
+
"optional": true
|
|
74
|
+
},
|
|
75
|
+
"@types/aws-lambda": {
|
|
76
|
+
"optional": true
|
|
77
|
+
}
|
|
68
78
|
},
|
|
69
79
|
"dependencies": {
|
|
70
|
-
"@middy/util": "8.0.0-alpha.
|
|
80
|
+
"@middy/util": "8.0.0-alpha.2"
|
|
71
81
|
},
|
|
72
82
|
"devDependencies": {
|
|
73
|
-
"@middy/core": "8.0.0-alpha.
|
|
83
|
+
"@middy/core": "8.0.0-alpha.2",
|
|
84
|
+
"@types/aws-lambda": "^8.0.0",
|
|
74
85
|
"@types/node": "^24.0.0",
|
|
75
|
-
"@x402/core": "^2.
|
|
86
|
+
"@x402/core": "^2.28.0"
|
|
76
87
|
}
|
|
77
88
|
}
|