mppx 0.22.0 → 0.22.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/CHANGELOG.md +15 -0
- package/dist/cli/account.d.ts +2 -1
- package/dist/cli/account.js +22 -11
- package/dist/cli/cli.js +18 -18
- package/dist/cli/plugins/index.js +12 -12
- package/dist/extensions/identity/attestation/client/index.js +1 -0
- package/dist/extensions/identity/attestation/server/index.js +1 -0
- package/dist/extensions/identity/email-otp/server/index.js +1 -0
- package/dist/extensions/identity/erc8004/client/index.js +1 -0
- package/dist/extensions/identity/erc8004/server/index.js +3 -1
- package/dist/extensions/identity/link/server/index.js +1 -0
- package/dist/extensions/identity/poppy/server/index.js +1 -0
- package/dist/extensions/identity/tempo/client/index.js +1 -0
- package/dist/extensions/identity/tempo/server/index.js +7 -1
- package/dist/extensions/identity/world/server/index.js +1 -0
- package/dist/identity/Headers.d.ts +3 -2
- package/dist/identity/Headers.js +3 -2
- package/dist/identity/Server.d.ts +8 -0
- package/dist/identity/Server.js +18 -0
- package/dist/identity/Types.d.ts +16 -2
- package/dist/internal/version.d.ts +2 -2
- package/dist/internal/version.js +1 -1
- package/dist/proxy/Proxy.js +59 -46
- package/dist/proxy/internal/Headers.d.ts +9 -2
- package/dist/proxy/internal/Headers.js +16 -3
- package/dist/server/Mppx.d.ts +2 -0
- package/dist/server/Mppx.js +1 -0
- package/dist/tempo/client/Charge.js +19 -0
- package/dist/tempo/server/Charge.d.ts +7 -0
- package/dist/tempo/server/Charge.js +100 -12
- package/dist/tempo/server/internal/html.gen.js +1 -1
- package/package.json +1 -1
|
@@ -73,6 +73,7 @@ export function prover(config) {
|
|
|
73
73
|
}
|
|
74
74
|
return {
|
|
75
75
|
scope: config.scope,
|
|
76
|
+
headers: [Constants.header.toLowerCase()],
|
|
76
77
|
prove(request, context) {
|
|
77
78
|
const origin = new URL(request.url).origin;
|
|
78
79
|
// A bearer attestation never goes over plaintext HTTP, except to localhost.
|
|
@@ -42,6 +42,7 @@ export function verifier(config) {
|
|
|
42
42
|
]),
|
|
43
43
|
});
|
|
44
44
|
return {
|
|
45
|
+
headers: { request: [Constants.header], response: [Constants.requiredHeader] },
|
|
45
46
|
async verify(request) {
|
|
46
47
|
const audience = origin(typeof config.audience === 'function' ? config.audience(request) : config.audience);
|
|
47
48
|
// At most `maxCandidates` distinct tokens are verified, latest expiry first,
|
|
@@ -19,6 +19,7 @@ export function prover(config) {
|
|
|
19
19
|
});
|
|
20
20
|
return {
|
|
21
21
|
scope: config.scope,
|
|
22
|
+
headers: [Constants.idHeader.toLowerCase(), Constants.signatureHeader.toLowerCase()],
|
|
22
23
|
async prove(request, { created, nonce }) {
|
|
23
24
|
const signature = await config.account.signMessage({
|
|
24
25
|
message: Agent.message({ created, id, nonce, request }),
|
|
@@ -153,9 +153,11 @@ export function create(config = {}) {
|
|
|
153
153
|
return result;
|
|
154
154
|
return { status: 'forbidden', reason: 'This agent may not call this API.' };
|
|
155
155
|
}
|
|
156
|
+
const headers = { request: [Constants.idHeader, Constants.signatureHeader] };
|
|
156
157
|
return {
|
|
157
|
-
verifier: { verify: async (request) => allowed(await getSigned(request)) },
|
|
158
|
+
verifier: { headers, verify: async (request) => allowed(await getSigned(request)) },
|
|
158
159
|
paymentCheck: {
|
|
160
|
+
headers,
|
|
159
161
|
requires: [{ type: Constants.type }],
|
|
160
162
|
async verify({ challenge, credential, request }) {
|
|
161
163
|
// Only a tempo charge the source signed binds it to the payer: a transaction or a proof.
|
|
@@ -121,6 +121,7 @@ export function attestations(config) {
|
|
|
121
121
|
}
|
|
122
122
|
return {
|
|
123
123
|
scope: config.scope,
|
|
124
|
+
headers: [AttestationConstants.header.toLowerCase()],
|
|
124
125
|
async prove(request, context) {
|
|
125
126
|
const { prover, user } = await resolve();
|
|
126
127
|
// A request already in flight (such as a payment retry) may still carry the previous
|
|
@@ -198,10 +198,16 @@ export function create(config) {
|
|
|
198
198
|
verified.set(request, (result = check(request)));
|
|
199
199
|
return result;
|
|
200
200
|
}
|
|
201
|
+
const protocolHeaders = { request: [Constants.idTokenHeader, 'DPoP'] };
|
|
201
202
|
return {
|
|
202
|
-
verifier: {
|
|
203
|
+
verifier: {
|
|
204
|
+
authorization: true,
|
|
205
|
+
headers: protocolHeaders,
|
|
206
|
+
verify: async (request) => allowed(await verify(request)),
|
|
207
|
+
},
|
|
203
208
|
paymentCheck: {
|
|
204
209
|
authorization: true,
|
|
210
|
+
headers: protocolHeaders,
|
|
205
211
|
requires: [{ type: Constants.type, issuer }],
|
|
206
212
|
async verify(parameters) {
|
|
207
213
|
const caller = await verify(parameters.request);
|
|
@@ -4,8 +4,9 @@
|
|
|
4
4
|
*/
|
|
5
5
|
/**
|
|
6
6
|
* Bearer identity request headers. Anyone holding one can replay it until it expires, so it is
|
|
7
|
-
* never sent across origins on a redirect or forwarded upstream by a proxy
|
|
8
|
-
*
|
|
7
|
+
* never sent across origins on a redirect or forwarded upstream by a proxy, even when an app sets
|
|
8
|
+
* it by hand. Each protocol also declares its own, in `Prover.headers` and on its verifier or
|
|
9
|
+
* payment check, so a protocol outside mppx is stripped the same way.
|
|
9
10
|
*/
|
|
10
11
|
export declare const request: readonly ["agent-id", "agent-signature", "dpop", "identity-attestation", "identity-presentation", "poppy-session", "tempo-id-token"];
|
|
11
12
|
/**
|
package/dist/identity/Headers.js
CHANGED
|
@@ -4,8 +4,9 @@
|
|
|
4
4
|
*/
|
|
5
5
|
/**
|
|
6
6
|
* Bearer identity request headers. Anyone holding one can replay it until it expires, so it is
|
|
7
|
-
* never sent across origins on a redirect or forwarded upstream by a proxy
|
|
8
|
-
*
|
|
7
|
+
* never sent across origins on a redirect or forwarded upstream by a proxy, even when an app sets
|
|
8
|
+
* it by hand. Each protocol also declares its own, in `Prover.headers` and on its verifier or
|
|
9
|
+
* payment check, so a protocol outside mppx is stripped the same way.
|
|
9
10
|
*/
|
|
10
11
|
export const request = Object.freeze([
|
|
11
12
|
'agent-id',
|
|
@@ -172,6 +172,14 @@ export type RouteConfig = {
|
|
|
172
172
|
readonly request: readonly Types.Verifier[];
|
|
173
173
|
readonly payment: Types.PaymentCheck | undefined;
|
|
174
174
|
};
|
|
175
|
+
/**
|
|
176
|
+
* Returns the protocol headers declared by identity verifiers and payment checks, lowercased and
|
|
177
|
+
* deduplicated, such as for a proxy to strip. Throws a `TypeError` for a name that is not an HTTP
|
|
178
|
+
* field name, checked before lowercasing so a Unicode name cannot fold into an ASCII one.
|
|
179
|
+
*/
|
|
180
|
+
export declare function headersOf(checks: readonly (Types.Verifier | Types.PaymentCheck | undefined)[]): Required<{
|
|
181
|
+
[key in keyof Types.ProtocolHeaders]: readonly string[];
|
|
182
|
+
}>;
|
|
175
183
|
/**
|
|
176
184
|
* Parses a route's `identity` option. Throws a `TypeError` for a payment-stage or unknown-stage
|
|
177
185
|
* verifier, and for payment requirements that cannot be serialized. @internal
|
package/dist/identity/Server.js
CHANGED
|
@@ -26,6 +26,7 @@ export function anyOf(...verifiers) {
|
|
|
26
26
|
const verifier = Object.freeze({
|
|
27
27
|
// A member that reads Authorization makes the whole set need requiresAuth.
|
|
28
28
|
...(verifiers.some((member) => member.authorization) ? { authorization: true } : {}),
|
|
29
|
+
headers: headersOf(verifiers),
|
|
29
30
|
async verify(request) {
|
|
30
31
|
// Every member runs, so each presented proof's replay nonce is consumed even when an
|
|
31
32
|
// earlier member already verified.
|
|
@@ -313,6 +314,23 @@ export function results(state) {
|
|
|
313
314
|
},
|
|
314
315
|
});
|
|
315
316
|
}
|
|
317
|
+
/**
|
|
318
|
+
* Returns the protocol headers declared by identity verifiers and payment checks, lowercased and
|
|
319
|
+
* deduplicated, such as for a proxy to strip. Throws a `TypeError` for a name that is not an HTTP
|
|
320
|
+
* field name, checked before lowercasing so a Unicode name cannot fold into an ASCII one.
|
|
321
|
+
*/
|
|
322
|
+
export function headersOf(checks) {
|
|
323
|
+
const names = (key) => Object.freeze([
|
|
324
|
+
...new Set(checks
|
|
325
|
+
.flatMap((check) => check?.headers?.[key] ?? [])
|
|
326
|
+
.map((name) => {
|
|
327
|
+
if (typeof name !== 'string' || !/^[!#$%&'*+.^_`|~0-9A-Za-z-]+$/.test(name))
|
|
328
|
+
throw new TypeError(`Invalid identity header name: ${JSON.stringify(name)}.`);
|
|
329
|
+
return name.toLowerCase();
|
|
330
|
+
})),
|
|
331
|
+
]);
|
|
332
|
+
return Object.freeze({ request: names('request'), response: names('response') });
|
|
333
|
+
}
|
|
316
334
|
/**
|
|
317
335
|
* Parses a route's `identity` option. Throws a `TypeError` for a payment-stage or unknown-stage
|
|
318
336
|
* verifier, and for payment requirements that cannot be serialized. @internal
|
package/dist/identity/Types.d.ts
CHANGED
|
@@ -62,8 +62,20 @@ export type Verification<value = unknown> = {
|
|
|
62
62
|
/** Human-readable reason. */
|
|
63
63
|
readonly reason: string;
|
|
64
64
|
};
|
|
65
|
+
/**
|
|
66
|
+
* Headers an identity protocol sends. A proxy serving a route that uses the protocol strips them:
|
|
67
|
+
* the proxy, not the upstream service, decides which identity the client presents.
|
|
68
|
+
*/
|
|
69
|
+
export type ProtocolHeaders = {
|
|
70
|
+
/** Request headers the client sends, never forwarded upstream, such as a proof. */
|
|
71
|
+
readonly request?: readonly string[] | undefined;
|
|
72
|
+
/** Response headers the server sends, never passed from upstream to the client. */
|
|
73
|
+
readonly response?: readonly string[] | undefined;
|
|
74
|
+
};
|
|
65
75
|
/** Verifies one identity protocol on the server, before any payment Challenge is issued. */
|
|
66
76
|
export type Verifier<value = unknown> = {
|
|
77
|
+
/** Headers the protocol sends, such as a bearer proof, stripped by a proxy. */
|
|
78
|
+
readonly headers?: ProtocolHeaders | undefined;
|
|
67
79
|
/**
|
|
68
80
|
* When the verifier runs.
|
|
69
81
|
*
|
|
@@ -117,6 +129,8 @@ export type PaymentCheck<value = unknown> = {
|
|
|
117
129
|
* created with `requiresAuth: true`, so Payment Credentials travel in `Payment-Authorization`.
|
|
118
130
|
*/
|
|
119
131
|
readonly authorization?: boolean | undefined;
|
|
132
|
+
/** Headers the check's protocol sends, such as a bearer proof, stripped by a proxy. */
|
|
133
|
+
readonly headers?: ProtocolHeaders | undefined;
|
|
120
134
|
/** Identities the check requires, advertised in every 402 of the route. */
|
|
121
135
|
readonly requires: readonly Requirement[];
|
|
122
136
|
/** Returns the check's outcome for the paid retry (or, without a Credential, for `authorize`). */
|
|
@@ -193,8 +207,8 @@ export type Prover = {
|
|
|
193
207
|
/** Origins the prover may run for, such as `['https://shop.example']`, or `'*'` for any origin. */
|
|
194
208
|
readonly scope: readonly string[] | '*';
|
|
195
209
|
/**
|
|
196
|
-
*
|
|
197
|
-
*
|
|
210
|
+
* Sensitive request headers the prover writes, such as a bearer token. The client strips them
|
|
211
|
+
* on a cross-origin redirect.
|
|
198
212
|
*/
|
|
199
213
|
readonly headers?: readonly string[] | undefined;
|
|
200
214
|
/** Returns a new request carrying the protocol's identity material. */
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/** Current mppx package version. */
|
|
2
|
-
export declare const version = "0.22.
|
|
2
|
+
export declare const version = "0.22.2";
|
|
3
3
|
/** Canonical SDK identifier for this mppx release. */
|
|
4
|
-
export declare const sdkIdentifier = "mppx/0.22.
|
|
4
|
+
export declare const sdkIdentifier = "mppx/0.22.2";
|
|
5
5
|
//# sourceMappingURL=version.d.ts.map
|
package/dist/internal/version.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// Generated by scripts/sync-version.ts.
|
|
2
2
|
/** Current mppx package version. */
|
|
3
|
-
export const version = '0.22.
|
|
3
|
+
export const version = '0.22.2';
|
|
4
4
|
/** Canonical SDK identifier for this mppx release. */
|
|
5
5
|
export const sdkIdentifier = `mppx/${version}`;
|
|
6
6
|
//# sourceMappingURL=version.js.map
|
package/dist/proxy/Proxy.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import * as Credential from '../Credential.js';
|
|
2
|
+
import * as Metadata from '../discovery/internal/Metadata.js';
|
|
2
3
|
import { generateProxy } from '../discovery/OpenApi.js';
|
|
3
4
|
import * as Scope from '../server/internal/scope.js';
|
|
4
5
|
import * as Mppx from '../server/Mppx.js';
|
|
@@ -40,6 +41,16 @@ export function create(config) {
|
|
|
40
41
|
throw new RangeError('maxRequestBodySize must be a non-negative safe integer');
|
|
41
42
|
const fetchImpl = config.fetch ?? globalThis.fetch;
|
|
42
43
|
const replay = config.replay ? Replay.resolve(config.replay) : undefined;
|
|
44
|
+
const strip = stripHeadersOf(config.services);
|
|
45
|
+
/** Forwards a request to a service's upstream, with the proxy's header stripping and retries. */
|
|
46
|
+
const forward = (service, proxy, request, ctx) => proxyUpstream({
|
|
47
|
+
ctx,
|
|
48
|
+
onUpstreamError: service.onUpstreamError ?? config.onUpstreamError,
|
|
49
|
+
proxy,
|
|
50
|
+
request,
|
|
51
|
+
service,
|
|
52
|
+
strip,
|
|
53
|
+
});
|
|
43
54
|
const services = new Map(config.services.map((s) => {
|
|
44
55
|
const proxy = createFetchProxy(s.baseUrl, { fetch: fetchImpl });
|
|
45
56
|
return [s.id, { service: s, proxy }];
|
|
@@ -121,13 +132,7 @@ export function create(config) {
|
|
|
121
132
|
}
|
|
122
133
|
const ctx = { request, service, upstreamPath };
|
|
123
134
|
if (!handler)
|
|
124
|
-
return
|
|
125
|
-
request,
|
|
126
|
-
service,
|
|
127
|
-
ctx: { ...ctx, ...Service.getOptions(endpoint) },
|
|
128
|
-
proxy,
|
|
129
|
-
onUpstreamError: service.onUpstreamError ?? config.onUpstreamError,
|
|
130
|
-
});
|
|
135
|
+
return forward(service, proxy, request, { ...ctx, ...Service.getOptions(endpoint) });
|
|
131
136
|
let selected;
|
|
132
137
|
try {
|
|
133
138
|
selected = paymentCredential ? Credential.deserialize(paymentCredential) : undefined;
|
|
@@ -190,13 +195,7 @@ export function create(config) {
|
|
|
190
195
|
run: (signal) => {
|
|
191
196
|
// Send the headers of the request that was paid for, not the retry's.
|
|
192
197
|
const upstream = Replay.withBody(paidRequest, body, signal, found.requestHeaders);
|
|
193
|
-
return
|
|
194
|
-
request: upstream,
|
|
195
|
-
service,
|
|
196
|
-
ctx: { ...ctx, request: upstream, ...options },
|
|
197
|
-
proxy,
|
|
198
|
-
onUpstreamError: service.onUpstreamError ?? config.onUpstreamError,
|
|
199
|
-
});
|
|
198
|
+
return forward(service, proxy, upstream, { ...ctx, request: upstream, ...options });
|
|
200
199
|
},
|
|
201
200
|
});
|
|
202
201
|
}
|
|
@@ -241,26 +240,14 @@ export function create(config) {
|
|
|
241
240
|
attempts: 1,
|
|
242
241
|
replay,
|
|
243
242
|
replayKey,
|
|
244
|
-
requestHeaders: [...Headers.scrub(paidRequest.headers).entries()],
|
|
243
|
+
requestHeaders: [...Headers.scrub(paidRequest.headers, strip).entries()],
|
|
245
244
|
run: (signal) => {
|
|
246
245
|
const upstream = Replay.withBody(paidRequest, body, signal);
|
|
247
|
-
return
|
|
248
|
-
request: upstream,
|
|
249
|
-
service,
|
|
250
|
-
ctx: { ...ctx, request: upstream, ...options },
|
|
251
|
-
proxy,
|
|
252
|
-
onUpstreamError: service.onUpstreamError ?? config.onUpstreamError,
|
|
253
|
-
});
|
|
246
|
+
return forward(service, proxy, upstream, { ...ctx, request: upstream, ...options });
|
|
254
247
|
},
|
|
255
248
|
});
|
|
256
249
|
}
|
|
257
|
-
const upstreamRes = await
|
|
258
|
-
request,
|
|
259
|
-
service,
|
|
260
|
-
ctx: { ...ctx, ...options },
|
|
261
|
-
proxy,
|
|
262
|
-
onUpstreamError: service.onUpstreamError ?? config.onUpstreamError,
|
|
263
|
-
});
|
|
250
|
+
const upstreamRes = await forward(service, proxy, request, { ...ctx, ...options });
|
|
264
251
|
return result.withReceipt(upstreamRes);
|
|
265
252
|
}
|
|
266
253
|
return {
|
|
@@ -270,9 +257,9 @@ export function create(config) {
|
|
|
270
257
|
}
|
|
271
258
|
/** @internal */
|
|
272
259
|
async function proxyUpstream(options) {
|
|
273
|
-
const { request, service, ctx, proxy, onUpstreamError } = options;
|
|
260
|
+
const { request, service, ctx, proxy, onUpstreamError, strip } = options;
|
|
274
261
|
const url = ctx.upstreamPath + new URL(request.url).search;
|
|
275
|
-
const headers = Headers.scrub(request.headers);
|
|
262
|
+
const headers = Headers.scrub(request.headers, strip);
|
|
276
263
|
const method = request.method.toUpperCase();
|
|
277
264
|
const hasBody = method !== 'GET' && method !== 'HEAD';
|
|
278
265
|
const init = {
|
|
@@ -290,6 +277,7 @@ async function proxyUpstream(options) {
|
|
|
290
277
|
ctx,
|
|
291
278
|
proxy,
|
|
292
279
|
service,
|
|
280
|
+
strip,
|
|
293
281
|
upstreamRequest,
|
|
294
282
|
});
|
|
295
283
|
for (let attempt = 1;; attempt++) {
|
|
@@ -297,6 +285,7 @@ async function proxyUpstream(options) {
|
|
|
297
285
|
ctx,
|
|
298
286
|
proxy,
|
|
299
287
|
service,
|
|
288
|
+
strip,
|
|
300
289
|
upstreamRequest,
|
|
301
290
|
});
|
|
302
291
|
let response = outcome.response;
|
|
@@ -331,28 +320,29 @@ async function proxyUpstream(options) {
|
|
|
331
320
|
await waitForRetry(action.delay, request.signal);
|
|
332
321
|
}
|
|
333
322
|
}
|
|
323
|
+
/** Applies the service's request rewrite, if any. */
|
|
324
|
+
async function rewrite(options) {
|
|
325
|
+
const { ctx, service, upstreamRequest } = options;
|
|
326
|
+
return service.rewriteRequest ? service.rewriteRequest(upstreamRequest, ctx) : upstreamRequest;
|
|
327
|
+
}
|
|
328
|
+
/** Sends a rewritten request upstream and strips the response's headers. */
|
|
329
|
+
async function send(options, request) {
|
|
330
|
+
return Headers.scrubResponse(await options.proxy(request), options.strip);
|
|
331
|
+
}
|
|
334
332
|
/** Proxies one request without retaining a replay copy of its body. */
|
|
335
333
|
async function proxyUpstreamOnce(options) {
|
|
336
|
-
const
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
let response = await proxy(upstreamRequest);
|
|
341
|
-
response = Headers.scrubResponse(response);
|
|
342
|
-
if (service.rewriteResponse)
|
|
343
|
-
response = await service.rewriteResponse(response, ctx);
|
|
344
|
-
return response;
|
|
334
|
+
const response = await send(options, await rewrite(options));
|
|
335
|
+
return options.service.rewriteResponse
|
|
336
|
+
? options.service.rewriteResponse(response, options.ctx)
|
|
337
|
+
: response;
|
|
345
338
|
}
|
|
346
339
|
/** Performs one independently replayable upstream request attempt. */
|
|
347
340
|
async function proxyUpstreamAttempt(options) {
|
|
348
|
-
|
|
341
|
+
// The result carries the request as rewritten, as `onUpstreamError` documents.
|
|
349
342
|
let upstreamRequest = options.upstreamRequest.clone();
|
|
350
343
|
try {
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
let response = await proxy(upstreamRequest);
|
|
354
|
-
response = Headers.scrubResponse(response);
|
|
355
|
-
return { error: undefined, response, upstreamRequest };
|
|
344
|
+
upstreamRequest = await rewrite({ ...options, upstreamRequest });
|
|
345
|
+
return { error: undefined, response: await send(options, upstreamRequest), upstreamRequest };
|
|
356
346
|
}
|
|
357
347
|
catch (error) {
|
|
358
348
|
return { error, response: undefined, upstreamRequest };
|
|
@@ -432,6 +422,29 @@ function requiresManualGeneration(handler, selected) {
|
|
|
432
422
|
return ((selected.intent === 'session' && selectedOffers.length === 0) ||
|
|
433
423
|
selectedOffers.some(unsupported));
|
|
434
424
|
}
|
|
425
|
+
/**
|
|
426
|
+
* Headers the proxy strips beyond the built-in ones: those declared by the identity verifiers and
|
|
427
|
+
* payment checks of its routes (`headers` on each), so a route that uses an identity extension
|
|
428
|
+
* never needs them listed by hand. The names are validated and lowercased when each route is
|
|
429
|
+
* created (`IdentityServer.headersOf`), so a bad declaration fails before the proxy starts.
|
|
430
|
+
*/
|
|
431
|
+
function stripHeadersOf(services) {
|
|
432
|
+
const request = new Set();
|
|
433
|
+
const response = new Set();
|
|
434
|
+
for (const service of services)
|
|
435
|
+
for (const endpoint of Object.values(service.routes)) {
|
|
436
|
+
const handler = endpoint === undefined ? undefined : Service.getHandler(endpoint);
|
|
437
|
+
if (!handler || !('_internal' in handler) || !handler._internal)
|
|
438
|
+
continue;
|
|
439
|
+
for (const offer of Metadata.offers(handler._internal)) {
|
|
440
|
+
for (const name of offer._identityHeaders?.request ?? [])
|
|
441
|
+
request.add(name);
|
|
442
|
+
for (const name of offer._identityHeaders?.response ?? [])
|
|
443
|
+
response.add(name);
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
return { request: [...request], response: [...response] };
|
|
447
|
+
}
|
|
435
448
|
/** Identity and session offers must re-run authorization and delivery-time metering on every request. */
|
|
436
449
|
function requiresFreshVerification(handler, selected) {
|
|
437
450
|
if (selected?.intent === 'session')
|
|
@@ -1,5 +1,12 @@
|
|
|
1
|
+
/** Headers the proxy strips beyond the built-in ones, declared by its routes' identity protocols. */
|
|
2
|
+
export type Strip = {
|
|
3
|
+
/** Request headers never forwarded upstream, such as an identity proof. */
|
|
4
|
+
readonly request?: readonly string[] | undefined;
|
|
5
|
+
/** Upstream response headers never returned to the client, such as an identity request. */
|
|
6
|
+
readonly response?: readonly string[] | undefined;
|
|
7
|
+
};
|
|
1
8
|
/** Strips hop-by-hop, auth, identity, encoding, cookie, and forwarding headers from a request before proxying upstream. */
|
|
2
|
-
export declare function scrub(headers: Headers): Headers;
|
|
9
|
+
export declare function scrub(headers: Headers, strip?: Strip | undefined): Headers;
|
|
3
10
|
/**
|
|
4
11
|
* Strips re-streaming headers (`content-encoding`, `content-length`) and
|
|
5
12
|
* security-sensitive headers (`set-cookie`) from an upstream response.
|
|
@@ -13,5 +20,5 @@ export declare function scrub(headers: Headers): Headers;
|
|
|
13
20
|
* primitive. Proxied services authenticate via bearer tokens / signed
|
|
14
21
|
* payloads, never cookies, so dropping `set-cookie` is purely defensive.
|
|
15
22
|
*/
|
|
16
|
-
export declare function scrubResponse(response: Response): Response;
|
|
23
|
+
export declare function scrubResponse(response: Response, strip?: Strip | undefined): Response;
|
|
17
24
|
//# sourceMappingURL=Headers.d.ts.map
|
|
@@ -9,6 +9,14 @@ const hopByHopHeaders = new Set([
|
|
|
9
9
|
'te',
|
|
10
10
|
'trailer',
|
|
11
11
|
]);
|
|
12
|
+
// Client-supplied forwarding headers: a client could claim any address or path through them.
|
|
13
|
+
const forwardingHeaders = new Set([
|
|
14
|
+
'cf-connecting-ip',
|
|
15
|
+
'forwarded',
|
|
16
|
+
'true-client-ip',
|
|
17
|
+
'via',
|
|
18
|
+
'x-real-ip',
|
|
19
|
+
]);
|
|
12
20
|
// Payment credentials are consumed by the proxy and must never reach upstream services.
|
|
13
21
|
const paymentHeaders = new Set([
|
|
14
22
|
'accept-payment',
|
|
@@ -24,14 +32,17 @@ const paymentHeaders = new Set([
|
|
|
24
32
|
'www-authenticate',
|
|
25
33
|
]);
|
|
26
34
|
/** Strips hop-by-hop, auth, identity, encoding, cookie, and forwarding headers from a request before proxying upstream. */
|
|
27
|
-
export function scrub(headers) {
|
|
35
|
+
export function scrub(headers, strip) {
|
|
28
36
|
const scrubbed = new Headers();
|
|
37
|
+
const extra = new Set(strip?.request?.map((name) => name.toLowerCase()));
|
|
29
38
|
for (const [name, value] of headers) {
|
|
30
39
|
const lower = name.toLowerCase();
|
|
31
40
|
if (paymentHeaders.has(lower))
|
|
32
41
|
continue;
|
|
33
42
|
if (IdentityHeaders.request.includes(lower))
|
|
34
43
|
continue;
|
|
44
|
+
if (extra.has(lower))
|
|
45
|
+
continue;
|
|
35
46
|
if (lower === 'accept-encoding')
|
|
36
47
|
continue;
|
|
37
48
|
if (lower === 'content-length')
|
|
@@ -40,7 +51,7 @@ export function scrub(headers) {
|
|
|
40
51
|
continue;
|
|
41
52
|
if (hopByHopHeaders.has(lower))
|
|
42
53
|
continue;
|
|
43
|
-
if (lower.startsWith('x-forwarded-'))
|
|
54
|
+
if (forwardingHeaders.has(lower) || lower.startsWith('x-forwarded-'))
|
|
44
55
|
continue;
|
|
45
56
|
scrubbed.append(name, value);
|
|
46
57
|
}
|
|
@@ -59,7 +70,7 @@ export function scrub(headers) {
|
|
|
59
70
|
* primitive. Proxied services authenticate via bearer tokens / signed
|
|
60
71
|
* payloads, never cookies, so dropping `set-cookie` is purely defensive.
|
|
61
72
|
*/
|
|
62
|
-
export function scrubResponse(response) {
|
|
73
|
+
export function scrubResponse(response, strip) {
|
|
63
74
|
const headers = new Headers(response.headers);
|
|
64
75
|
headers.delete('content-encoding');
|
|
65
76
|
headers.delete('content-length');
|
|
@@ -72,6 +83,8 @@ export function scrubResponse(response) {
|
|
|
72
83
|
// The proxy decides which identity the client presents, so upstream identity headers never pass.
|
|
73
84
|
for (const name of [...IdentityHeaders.request, ...IdentityHeaders.response])
|
|
74
85
|
headers.delete(name);
|
|
86
|
+
for (const name of strip?.response ?? [])
|
|
87
|
+
headers.delete(name);
|
|
75
88
|
const authorization = headers.get('authorization');
|
|
76
89
|
if (authorization && /^\s*Payment(?:\s|$)/i.test(authorization))
|
|
77
90
|
headers.delete('authorization');
|
package/dist/server/Mppx.d.ts
CHANGED
|
@@ -433,6 +433,8 @@ type ConfiguredHandler<transport extends Transport.AnyTransport = Transport.Http
|
|
|
433
433
|
_stableBinding?: Method.StableBindingFn<Method.Method> | undefined;
|
|
434
434
|
/** Whether the route requires identity (request-stage verifiers or a payment check). */
|
|
435
435
|
_identity?: boolean | undefined;
|
|
436
|
+
/** Headers the route's identity protocols send, which a proxy strips. */
|
|
437
|
+
_identityHeaders?: Identity.ProtocolHeaders | undefined;
|
|
436
438
|
/** The route's payment check, compared across composed offers. */
|
|
437
439
|
_paymentCheck?: Identity.PaymentCheck | undefined;
|
|
438
440
|
_transport: Transport.AnyTransport;
|
package/dist/server/Mppx.js
CHANGED
|
@@ -493,6 +493,7 @@ function createMethodFn(parameters) {
|
|
|
493
493
|
],
|
|
494
494
|
_canonicalRequest: PaymentRequest.fromMethod(method, { ...defaults, ...rest }),
|
|
495
495
|
_identity: hasIdentity,
|
|
496
|
+
_identityHeaders: IdentityServer.headersOf([...identityRoute.request, paymentCheck]),
|
|
496
497
|
_paymentCheck: paymentCheck,
|
|
497
498
|
_stableBinding: stableBinding,
|
|
498
499
|
_transport: transport,
|
|
@@ -83,6 +83,15 @@ export function charge(parameters = {}) {
|
|
|
83
83
|
canHandleChallenge({ challenge }) {
|
|
84
84
|
try {
|
|
85
85
|
assertChallengePolicy(parameters, caps, challenge.request);
|
|
86
|
+
// A challenge with a server memo can't be paid by push, so one that takes only push
|
|
87
|
+
// can't be paid at all: leave it for another offer.
|
|
88
|
+
const { amount, methodDetails } = challenge.request;
|
|
89
|
+
const modes = methodDetails?.supportedModes;
|
|
90
|
+
if (methodDetails?.memo !== undefined &&
|
|
91
|
+
modes &&
|
|
92
|
+
!modes.includes('pull') &&
|
|
93
|
+
BigInt(amount) > 0n)
|
|
94
|
+
return false;
|
|
86
95
|
return true;
|
|
87
96
|
}
|
|
88
97
|
catch {
|
|
@@ -299,11 +308,21 @@ export function charge(parameters = {}) {
|
|
|
299
308
|
: undefined;
|
|
300
309
|
const mode = (() => {
|
|
301
310
|
const explicitMode = context?.mode ?? parameters.mode;
|
|
311
|
+
// A server memo is shared by every payer of a route, so the server refuses a pushed payment
|
|
312
|
+
// that carries it: pay such a challenge with a transaction.
|
|
313
|
+
const serverMemo = methodDetails?.memo !== undefined;
|
|
302
314
|
if (explicitMode) {
|
|
303
315
|
if (!supportedModes.includes(explicitMode))
|
|
304
316
|
throw new Error(`Challenge does not support ${explicitMode} mode.`);
|
|
317
|
+
if (serverMemo && explicitMode === 'push')
|
|
318
|
+
throw new Error('Push mode is not supported for a challenge with a memo.');
|
|
305
319
|
return explicitMode;
|
|
306
320
|
}
|
|
321
|
+
if (serverMemo) {
|
|
322
|
+
if (!supportedModes.includes('pull'))
|
|
323
|
+
throw new Error('Challenge has a memo but does not support pull mode.');
|
|
324
|
+
return 'pull';
|
|
325
|
+
}
|
|
307
326
|
const preferredMode = account.type === 'json-rpc' ? 'push' : 'pull';
|
|
308
327
|
if (supportedModes.includes(preferredMode))
|
|
309
328
|
return preferredMode;
|
|
@@ -206,6 +206,13 @@ export declare namespace charge {
|
|
|
206
206
|
type ExpectedTransfer = {
|
|
207
207
|
amount: string;
|
|
208
208
|
allowAnyMemo?: boolean | undefined;
|
|
209
|
+
/**
|
|
210
|
+
* Push payments: whether a memo is this challenge's attribution memo. Matching prefers such a
|
|
211
|
+
* transfer and skips one carrying another challenge's attribution memo.
|
|
212
|
+
*/
|
|
213
|
+
isBound?: ((memo: `0x${string}`) => boolean) | undefined;
|
|
214
|
+
/** The transfer must carry this challenge's attribution memo (the primary of a push payment). */
|
|
215
|
+
requireBound?: boolean | undefined;
|
|
209
216
|
memo?: `0x${string}` | undefined;
|
|
210
217
|
recipient: `0x${string}`;
|
|
211
218
|
};
|