402-mcp 4.2.2 → 4.3.1
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 +45 -16
- package/build/config.d.ts +6 -0
- package/build/config.js +41 -10
- package/build/config.js.map +1 -1
- package/build/fetch/resilient-fetch.js +15 -4
- package/build/fetch/resilient-fetch.js.map +1 -1
- package/build/fetch/transport.d.ts +1 -0
- package/build/fetch/transport.js +37 -14
- package/build/fetch/transport.js.map +1 -1
- package/build/http-security.d.ts +13 -0
- package/build/http-security.js +38 -0
- package/build/http-security.js.map +1 -0
- package/build/index.js +184 -119
- package/build/index.js.map +1 -1
- package/build/spend-tracker.d.ts +31 -4
- package/build/spend-tracker.js +102 -17
- package/build/spend-tracker.js.map +1 -1
- package/build/store/cashu-tokens.d.ts +19 -0
- package/build/store/cashu-tokens.js +32 -0
- package/build/store/cashu-tokens.js.map +1 -1
- package/build/store/credentials.d.ts +5 -0
- package/build/store/credentials.js +1 -0
- package/build/store/credentials.js.map +1 -1
- package/build/store/pending-payments.d.ts +36 -0
- package/build/store/pending-payments.js +88 -0
- package/build/store/pending-payments.js.map +1 -0
- package/build/tools/buy-credits.d.ts +9 -0
- package/build/tools/buy-credits.js +40 -3
- package/build/tools/buy-credits.js.map +1 -1
- package/build/tools/config.d.ts +5 -0
- package/build/tools/config.js.map +1 -1
- package/build/tools/confirm.d.ts +12 -0
- package/build/tools/confirm.js +27 -0
- package/build/tools/confirm.js.map +1 -0
- package/build/tools/fetch.d.ts +7 -1
- package/build/tools/fetch.js +255 -140
- package/build/tools/fetch.js.map +1 -1
- package/build/tools/nostr-subscribe.d.ts +5 -2
- package/build/tools/nostr-subscribe.js +0 -2
- package/build/tools/nostr-subscribe.js.map +1 -1
- package/build/tools/pay.d.ts +9 -0
- package/build/tools/pay.js +65 -8
- package/build/tools/pay.js.map +1 -1
- package/build/tools/reconcile.d.ts +33 -0
- package/build/tools/reconcile.js +111 -0
- package/build/tools/reconcile.js.map +1 -0
- package/build/tools/redeem-cashu.js +1 -1
- package/build/tools/redeem-cashu.js.map +1 -1
- package/build/tools/register-widgets.js +5 -5
- package/build/tools/register-widgets.js.map +1 -1
- package/build/tools/search.js +25 -7
- package/build/tools/search.js.map +1 -1
- package/build/tools/untrusted.d.ts +3 -0
- package/build/tools/untrusted.js +18 -0
- package/build/tools/untrusted.js.map +1 -0
- package/build/wallet/cashu.js +101 -21
- package/build/wallet/cashu.js.map +1 -1
- package/build/wallet/lnurlcash.js +25 -7
- package/build/wallet/lnurlcash.js.map +1 -1
- package/build/wallet/nwc.js +51 -3
- package/build/wallet/nwc.js.map +1 -1
- package/build/wallet/types.d.ts +17 -0
- package/build/widgets/index.js +2 -1
- package/build/widgets/index.js.map +1 -1
- package/build/widgets/paths.d.ts +7 -0
- package/build/widgets/paths.js +11 -0
- package/build/widgets/paths.js.map +1 -0
- package/build/x402/parse.d.ts +15 -3
- package/build/x402/parse.js +23 -9
- package/build/x402/parse.js.map +1 -1
- package/build/x402/payment.js +11 -6
- package/build/x402/payment.js.map +1 -1
- package/llms-full.txt +72 -34
- package/llms.txt +49 -22
- package/package.json +3 -3
- package/src/widgets/dist/service-directory.html +11 -2
package/README.md
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
[](https://primal.net/p/npub1mgvlrnf5hm9yf0n5mf9nqmvarhvxkc6remu5ec3vf8r0txqkuk7su0e7q2)
|
|
10
10
|
[](https://github.com/sponsors/TheCryptoDonkey)
|
|
11
11
|
|
|
12
|
-
L402
|
|
12
|
+
L402 client MCP that gives AI agents economic agency. Discover, pay for, and consume Lightning and ecash payment-gated APIs within limits you set: no human registration, no API keys, no middlemen.
|
|
13
13
|
|
|
14
14
|
- **Discover** paid APIs on Nostr — no URLs needed upfront
|
|
15
15
|
- **Auto-pay** with Lightning (NWC), Cashu ecash, LNURLcash bearer notes, or human QR fallback
|
|
@@ -79,20 +79,25 @@ For detailed architecture and payment flow diagrams, see [docs/architecture.md](
|
|
|
79
79
|
| `NWC_URI_FILE` | - | Path to a private `0600` file containing the NWC bearer URI |
|
|
80
80
|
| `CASHU_TOKENS` | - | Path to Cashu token store file |
|
|
81
81
|
| `LNURLCASH_NOTES` | - | Path to LNURLcash bearer note store file (LUD-25) |
|
|
82
|
-
| `MAX_AUTO_PAY_SATS` | 1000 |
|
|
82
|
+
| `MAX_AUTO_PAY_SATS` | 1000 | Most a single automatic payment may cost. Anything dearer is not paid; the challenge goes back to the agent |
|
|
83
|
+
| `MAX_SPEND_PER_MINUTE_SATS` | 10000 | Automatic spend allowed in any rolling 60 seconds. `0` blocks all auto-pay |
|
|
84
|
+
| `MAX_SPEND_PER_DAY_SATS` | 5000 | Automatic spend allowed in any rolling 24 hours, kept in `~/.402-mcp/spend-ledger.json` so a restart does not reset it. `0` blocks all auto-pay |
|
|
83
85
|
| `CREDENTIAL_STORE` | `~/.402-mcp/credentials.json` | Persistent macaroon/credential storage |
|
|
84
86
|
| `TRANSPORT` | `stdio` | Transport mode: `stdio` or `http` |
|
|
85
87
|
| `PORT` | 3402 | HTTP server port (when `TRANSPORT=http`) |
|
|
88
|
+
| `BIND_ADDRESS` | `127.0.0.1` | HTTP bind address |
|
|
89
|
+
| `HTTP_AUTH_TOKEN_FILE` | - | Private `0600` file holding the bearer token HTTP clients must send. Required when `TRANSPORT=http` |
|
|
90
|
+
| `HTTP_ALLOWED_HOSTS` | - | Extra `Host` header values the HTTP transport accepts (comma-separated), such as a reverse proxy's name |
|
|
86
91
|
| `TRANSPORT_PREFERENCE` | `onion,hns,https,http` | Preferred transport order for multi-URL services (comma-separated) |
|
|
87
92
|
| `TOR_PROXY` | - | SOCKS5 proxy for `.onion` addresses only (e.g. `socks5h://127.0.0.1:9050`) |
|
|
88
93
|
| `SOCKS_PROXY` | - | SOCKS5 proxy for every paid-API request (e.g. Tor at `socks5h://127.0.0.1:9050`). Set this or `TOR_PROXY`, not both |
|
|
89
|
-
| `HNS_GATEWAY_URL` |
|
|
94
|
+
| `HNS_GATEWAY_URL` | `https://query.hdns.io/` | DNS-over-HTTPS resolver used for Handshake names. Any host name that ordinary DNS cannot find is looked up here |
|
|
90
95
|
|
|
91
96
|
### Transport selection and fallback
|
|
92
97
|
|
|
93
98
|
When a kind 31402 event advertises multiple URLs (one per transport), 402-mcp selects the best one based on your configuration:
|
|
94
99
|
|
|
95
|
-
1. **Preference first**: URLs are tried in `TRANSPORT_PREFERENCE` order, `onion,hns,https,http` by default. Use `onion`, `hns`, `https` and `http` as the values.
|
|
100
|
+
1. **Preference first**: URLs are tried in `TRANSPORT_PREFERENCE` order, `onion,hns,https,http` by default. Use `onion`, `hns`, `https` and `http` as the values. A URL counts as `hns` when its TLD is `.hns`. One whose TLD is merely unfamiliar (`.pub`, `.fyi`) is more likely an ICANN name, so it is tried just after `https`.
|
|
96
101
|
2. **Capability filter**: `.onion` URLs are skipped unless `TOR_PROXY` or `SOCKS_PROXY` is set, so without a proxy the default order starts at HNS and clearnet.
|
|
97
102
|
3. **Availability fallback**: if a transport is unreachable (connection refused, timeout), the next URL is tried.
|
|
98
103
|
|
|
@@ -115,13 +120,24 @@ SOCKS5 support comes from undici's `Socks5ProxyAgent`, which Node marks experime
|
|
|
115
120
|
|------|-------------|
|
|
116
121
|
| `l402-config` | Introspect payment capabilities (wallets, limits, credential count) |
|
|
117
122
|
| `l402-discover` | Probe an endpoint to discover pricing without paying |
|
|
118
|
-
| `l402-fetch` |
|
|
119
|
-
| `l402-
|
|
123
|
+
| `l402-fetch-preview` | Show what an endpoint costs without paying; drives the payment confirmation widget |
|
|
124
|
+
| `l402-fetch` | HTTP request that pays a 402 challenge when `autoPay` is set and the price is within the limits |
|
|
125
|
+
| `l402-pay` | Pay a challenge returned by `l402-fetch` or `l402-discover`, by its payment hash. Any other invoice needs the human's approval |
|
|
126
|
+
| `l402-reconcile` | List or resolve payments whose outcome is unknown; auto-pay to that service is paused until they are resolved |
|
|
120
127
|
| `l402-credentials` | List stored credentials and cached balances |
|
|
121
128
|
| `l402-balance` | Check cached credit balance for a server |
|
|
122
129
|
| `l402-search` | Discover L402 services on Nostr relays (kind 31402 announcements) |
|
|
123
130
|
| `l402-store-token` | Store an L402 token obtained from a payment page |
|
|
124
131
|
|
|
132
|
+
### Widgets (MCP Apps hosts)
|
|
133
|
+
|
|
134
|
+
| Tool | Description |
|
|
135
|
+
|------|-------------|
|
|
136
|
+
| `l402-service-directory` | Interactive, searchable directory of services found by `l402-search` |
|
|
137
|
+
| `l402-wallet-dashboard` | Interactive view of wallet status, limits and stored credentials |
|
|
138
|
+
|
|
139
|
+
`l402-fetch-preview` also has a payment confirmation widget.
|
|
140
|
+
|
|
125
141
|
### toll-booth extensions
|
|
126
142
|
|
|
127
143
|
| Tool | Description |
|
|
@@ -140,7 +156,7 @@ Four payer methods, tried in priority order:
|
|
|
140
156
|
|
|
141
157
|
The agent can override the method per-call, or you can configure only the methods you want.
|
|
142
158
|
|
|
143
|
-
`l402-fetch` handles
|
|
159
|
+
`l402-fetch` handles four HTTP 402 challenge variants, plus an experimental x402 format:
|
|
144
160
|
|
|
145
161
|
| Protocol | Challenge header | Payment |
|
|
146
162
|
|----------|-----------------|---------|
|
|
@@ -148,7 +164,7 @@ The agent can override the method per-call, or you can configure only the method
|
|
|
148
164
|
| **IETF Payment** (`draft-ryan-httpauth-payment-01`) | `WWW-Authenticate: Payment` | Lightning invoice via wallet stack |
|
|
149
165
|
| **LNURLcash** (LUD-25) | `X-LNURLcash: lnurlcashreq1…` | Bearer note handed over directly (requires a note store) |
|
|
150
166
|
| **xCashu** (NUT-18) | `X-Cashu: creqA…` | Ecash token sent directly (requires Cashu wallet) |
|
|
151
|
-
| **x402** | `X-Payment-Required: x402` |
|
|
167
|
+
| **x402** (experimental, custom format) | `X-Payment-Required: x402` + JSON body | A custom format, not the x402 specification (whose servers send a base64 `PAYMENT-REQUIRED` header), so real x402 services are not supported. Payment details are shown to the human, who pays from their own wallet |
|
|
152
168
|
|
|
153
169
|
An LNURLcash challenge is tried first. A bearer note is already money in hand,
|
|
154
170
|
so paying one costs no Lightning hop and no swap at the mint: the note goes
|
|
@@ -156,13 +172,26 @@ straight into the retry header and the server settles it. When the price does
|
|
|
156
172
|
not match a note exactly, one is split at the mint and the change stays in the
|
|
157
173
|
store. If no note covers it, the other rails are tried as usual.
|
|
158
174
|
|
|
159
|
-
##
|
|
175
|
+
## Spending limits
|
|
160
176
|
|
|
161
|
-
|
|
177
|
+
402-mcp checks every automatic payment against these, and the agent can read them with `l402-config`:
|
|
178
|
+
|
|
179
|
+
- `MAX_AUTO_PAY_SATS` caps each payment. A dearer challenge is returned to the agent unpaid.
|
|
180
|
+
- `maxCostSats` on `l402-fetch` lowers that cap for one call, so the price shown by `l402-fetch-preview` is binding. It can never raise it.
|
|
181
|
+
- `MAX_SPEND_PER_MINUTE_SATS` and `MAX_SPEND_PER_DAY_SATS` cap total automatic spend over rolling windows. The daily window is persisted, so restarting the server does not reset it. `0` in either blocks auto-pay entirely.
|
|
182
|
+
- A payment whose outcome is unknown pauses auto-pay to that service until `l402-reconcile` resolves it, so the same thing is not bought twice.
|
|
183
|
+
|
|
184
|
+
**The real hard limit is the budget on your NWC connection.** Everything above is enforced in software by this process, on the machine it runs on. Most NWC wallets let you set a spending budget when you create the connection; set one, because that is the limit a bug or a misbehaving agent cannot raise. For Cashu and LNURLcash, the hard limit is what you put in the token or note store.
|
|
162
185
|
|
|
163
186
|
## Privacy
|
|
164
187
|
|
|
165
|
-
402-mcp stores credentials locally on your machine only (`~/.402-mcp/credentials.json`, encrypted at rest).
|
|
188
|
+
402-mcp stores credentials locally on your machine only (`~/.402-mcp/credentials.json`, encrypted at rest). There are no accounts, no tracking and no analytics, and 402-mcp has no server of its own. It does talk to parties other than the APIs you call:
|
|
189
|
+
|
|
190
|
+
- **Nostr relays.** `l402-search` subscribes to public relays (by default relay.damus.io, relay.primal.net and nos.lol) for service announcements, sending any topic or payment-method filter you give it. The query text itself is matched locally.
|
|
191
|
+
- **A Handshake resolver.** When ordinary DNS cannot find a host name, it is looked up at `HNS_GATEWAY_URL` (`https://query.hdns.io/` by default), which therefore sees that name. This is off under `SOCKS_PROXY`.
|
|
192
|
+
- **Your wallet's services.** NWC relays, Cashu mints and LNURLcash mints see the payments you make through them.
|
|
193
|
+
|
|
194
|
+
Payments use Lightning or ecash, which are pseudonymous rather than anonymous.
|
|
166
195
|
|
|
167
196
|
## Ecosystem
|
|
168
197
|
|
|
@@ -171,21 +200,21 @@ Browse live L402 services at [402.pub](https://402.pub) — the decentralised ma
|
|
|
171
200
|
| Project | Role |
|
|
172
201
|
|---------|------|
|
|
173
202
|
| [toll-booth](https://github.com/forgesworn/toll-booth) | Payment-backend agnostic HTTP 402 middleware |
|
|
174
|
-
| [satgate](https://github.com/
|
|
175
|
-
| **[402-mcp](https://github.com/forgesworn/402-mcp)** | **MCP client
|
|
203
|
+
| [satgate](https://github.com/TheCryptoDonkey/satgate) | Pay-per-token AI inference proxy (built on toll-booth) |
|
|
204
|
+
| **[402-mcp](https://github.com/forgesworn/402-mcp)** | **MCP client: AI agents discover, pay for and consume L402 APIs** |
|
|
176
205
|
| [402-announce](https://github.com/forgesworn/402-announce) | Publish L402 services on Nostr for decentralised discovery |
|
|
177
206
|
|
|
178
|
-
402-mcp is the **wallet-provider agnostic** alternative to Lightning Labs' [lightning-agent-tools](https://github.com/lightninglabs/lightning-agent-tools)
|
|
207
|
+
402-mcp is the **wallet-provider agnostic** alternative to Lightning Labs' [lightning-agent-tools](https://github.com/lightninglabs/lightning-agent-tools): no Lightning node required, multiple wallets, encrypted credentials.
|
|
179
208
|
|
|
180
209
|
<details>
|
|
181
210
|
<summary>Full comparison</summary>
|
|
182
211
|
|
|
183
212
|
| | 402-mcp | Lightning Labs agent tools |
|
|
184
213
|
|---|---|---|
|
|
185
|
-
| **Payer methods** | NWC + Cashu + human fallback | Lightning only |
|
|
214
|
+
| **Payer methods** | NWC + Cashu + LNURLcash + human fallback | Lightning only |
|
|
186
215
|
| **Node required?** | No — connects to any NWC wallet | Yes — runs LND |
|
|
187
216
|
| **Server compatibility** | Any L402 server | Aperture-focused |
|
|
188
|
-
| **Spend safety** | Per-payment cap
|
|
217
|
+
| **Spend safety** | Per-payment cap, per-call max cost, rolling 60s and persisted 24h windows | Per-call max-cost |
|
|
189
218
|
| **Credential storage** | Encrypted at rest (AES-256-GCM) | File permissions |
|
|
190
219
|
| **Privacy** | No PII, SSRF protection, error sanitisation | Standard |
|
|
191
220
|
|
package/build/config.d.ts
CHANGED
|
@@ -5,6 +5,8 @@ export interface L402Config {
|
|
|
5
5
|
lnurlcashNotesPath: string | undefined;
|
|
6
6
|
maxAutoPaySats: number;
|
|
7
7
|
maxSpendPerMinuteSats: number;
|
|
8
|
+
/** Auto-pay cap over any rolling 24 hours, persisted across restarts. 0 blocks auto-pay. */
|
|
9
|
+
maxSpendPerDaySats: number;
|
|
8
10
|
credentialStorePath: string;
|
|
9
11
|
transport: 'stdio' | 'http';
|
|
10
12
|
port: number;
|
|
@@ -23,6 +25,10 @@ export interface L402Config {
|
|
|
23
25
|
scope: ProxyScope;
|
|
24
26
|
} | undefined;
|
|
25
27
|
hnsGatewayUrl: string;
|
|
28
|
+
/** Bearer token the HTTP transport requires. Always set when transport is http. */
|
|
29
|
+
httpAuthToken: string | undefined;
|
|
30
|
+
/** Extra Host header values the HTTP transport accepts (HTTP_ALLOWED_HOSTS). */
|
|
31
|
+
httpAllowedHosts: string[];
|
|
26
32
|
}
|
|
27
33
|
/** Loads and validates configuration from environment variables, applying defaults. */
|
|
28
34
|
export declare function loadConfig(): L402Config;
|
package/build/config.js
CHANGED
|
@@ -18,44 +18,58 @@ function assertRange(name, value, min, max) {
|
|
|
18
18
|
throw new Error(`${name} must be an integer between ${min} and ${max}; got ${value}`);
|
|
19
19
|
}
|
|
20
20
|
}
|
|
21
|
-
|
|
21
|
+
/**
|
|
22
|
+
* Reads a bearer secret from a private file: a regular file, owner-only on
|
|
23
|
+
* POSIX, bounded in size, read in full exactly once.
|
|
24
|
+
*/
|
|
25
|
+
function readSecretFile(name, filePath) {
|
|
22
26
|
const resolvedPath = resolve(filePath);
|
|
23
27
|
const descriptor = openSync(resolvedPath, 'r');
|
|
24
28
|
let bytes;
|
|
25
29
|
try {
|
|
26
30
|
const stats = fstatSync(descriptor);
|
|
27
31
|
if (!stats.isFile() || stats.size === 0 || stats.size > 8192) {
|
|
28
|
-
throw new Error(
|
|
32
|
+
throw new Error(`${name} must be a non-empty regular file no larger than 8192 bytes`);
|
|
29
33
|
}
|
|
30
34
|
if (process.platform !== 'win32' && (stats.mode & 0o077) !== 0) {
|
|
31
|
-
throw new Error(
|
|
35
|
+
throw new Error(`${name} permissions are too broad; run chmod 600 on the file`);
|
|
32
36
|
}
|
|
33
37
|
bytes = Buffer.allocUnsafe(stats.size);
|
|
34
38
|
let offset = 0;
|
|
35
39
|
while (offset < bytes.length) {
|
|
36
40
|
const count = readSync(descriptor, bytes, offset, bytes.length - offset, null);
|
|
37
41
|
if (count === 0)
|
|
38
|
-
throw new Error(
|
|
42
|
+
throw new Error(`${name} changed while it was being read`);
|
|
39
43
|
offset += count;
|
|
40
44
|
}
|
|
41
45
|
const extra = Buffer.allocUnsafe(1);
|
|
42
46
|
try {
|
|
43
47
|
if (readSync(descriptor, extra, 0, 1, null) !== 0) {
|
|
44
|
-
throw new Error(
|
|
48
|
+
throw new Error(`${name} changed while it was being read`);
|
|
45
49
|
}
|
|
46
50
|
}
|
|
47
51
|
finally {
|
|
48
52
|
extra.fill(0);
|
|
49
53
|
}
|
|
50
|
-
|
|
51
|
-
inspectNwcConnection(uri);
|
|
52
|
-
return uri;
|
|
54
|
+
return bytes.toString('utf-8').trim();
|
|
53
55
|
}
|
|
54
56
|
finally {
|
|
55
57
|
bytes?.fill(0);
|
|
56
58
|
closeSync(descriptor);
|
|
57
59
|
}
|
|
58
60
|
}
|
|
61
|
+
function readNwcUriFile(filePath) {
|
|
62
|
+
const uri = readSecretFile('NWC_URI_FILE', filePath);
|
|
63
|
+
inspectNwcConnection(uri);
|
|
64
|
+
return uri;
|
|
65
|
+
}
|
|
66
|
+
function readHttpAuthTokenFile(filePath) {
|
|
67
|
+
const token = readSecretFile('HTTP_AUTH_TOKEN_FILE', filePath);
|
|
68
|
+
if (token.length < 32 || /\s/.test(token)) {
|
|
69
|
+
throw new Error('HTTP_AUTH_TOKEN_FILE must hold a single token of at least 32 characters, e.g. from: openssl rand -hex 32');
|
|
70
|
+
}
|
|
71
|
+
return token;
|
|
72
|
+
}
|
|
59
73
|
/** Loads and validates configuration from environment variables, applying defaults. */
|
|
60
74
|
export function loadConfig() {
|
|
61
75
|
const defaultCredentialStore = resolve(homedir(), '.402-mcp', 'credentials.json');
|
|
@@ -71,6 +85,19 @@ export function loadConfig() {
|
|
|
71
85
|
if (transport !== 'stdio' && transport !== 'http') {
|
|
72
86
|
throw new Error(`TRANSPORT must be 'stdio' or 'http'; got '${transport}'`);
|
|
73
87
|
}
|
|
88
|
+
const rawHttpToken = process.env.HTTP_AUTH_TOKEN;
|
|
89
|
+
const httpTokenFile = process.env.HTTP_AUTH_TOKEN_FILE;
|
|
90
|
+
delete process.env.HTTP_AUTH_TOKEN;
|
|
91
|
+
delete process.env.HTTP_AUTH_TOKEN_FILE;
|
|
92
|
+
if (rawHttpToken !== undefined) {
|
|
93
|
+
throw new Error('HTTP_AUTH_TOKEN is disabled because bearer secrets must not be stored in environment variables; use HTTP_AUTH_TOKEN_FILE');
|
|
94
|
+
}
|
|
95
|
+
// The HTTP transport can spend from the wallet, so it never runs open.
|
|
96
|
+
if (transport === 'http' && !httpTokenFile) {
|
|
97
|
+
throw new Error('TRANSPORT=http requires HTTP_AUTH_TOKEN_FILE: a private 0600 file holding the bearer token clients must send');
|
|
98
|
+
}
|
|
99
|
+
const httpAuthToken = transport === 'http' && httpTokenFile ? readHttpAuthTokenFile(httpTokenFile) : undefined;
|
|
100
|
+
const httpAllowedHosts = (process.env.HTTP_ALLOWED_HOSTS ?? '').split(',').map(h => h.trim()).filter(Boolean);
|
|
74
101
|
const transportPref = process.env.TRANSPORT_PREFERENCE;
|
|
75
102
|
const transportPreference = transportPref
|
|
76
103
|
? transportPref.split(',').map(s => s.trim()).filter(Boolean)
|
|
@@ -92,6 +119,7 @@ export function loadConfig() {
|
|
|
92
119
|
lnurlcashNotesPath: process.env.LNURLCASH_NOTES,
|
|
93
120
|
maxAutoPaySats: parseInt(process.env.MAX_AUTO_PAY_SATS ?? '1000', 10),
|
|
94
121
|
maxSpendPerMinuteSats: parseInt(process.env.MAX_SPEND_PER_MINUTE_SATS ?? '10000', 10),
|
|
122
|
+
maxSpendPerDaySats: parseInt(process.env.MAX_SPEND_PER_DAY_SATS ?? '5000', 10),
|
|
95
123
|
credentialStorePath: process.env.CREDENTIAL_STORE ?? defaultCredentialStore,
|
|
96
124
|
transport,
|
|
97
125
|
port: parseInt(process.env.PORT ?? '3402', 10),
|
|
@@ -106,9 +134,12 @@ export function loadConfig() {
|
|
|
106
134
|
transportPreference,
|
|
107
135
|
proxy,
|
|
108
136
|
hnsGatewayUrl,
|
|
137
|
+
httpAuthToken,
|
|
138
|
+
httpAllowedHosts,
|
|
109
139
|
};
|
|
110
140
|
assertNonNegativeInt('MAX_AUTO_PAY_SATS', config.maxAutoPaySats);
|
|
111
141
|
assertNonNegativeInt('MAX_SPEND_PER_MINUTE_SATS', config.maxSpendPerMinuteSats);
|
|
142
|
+
assertNonNegativeInt('MAX_SPEND_PER_DAY_SATS', config.maxSpendPerDaySats);
|
|
112
143
|
assertRange('PORT', config.port, 1, 65535);
|
|
113
144
|
assertPositiveInt('FETCH_TIMEOUT_MS', config.fetchTimeoutMs);
|
|
114
145
|
assertNonNegativeInt('FETCH_MAX_RETRIES', config.fetchMaxRetries);
|
|
@@ -137,9 +168,9 @@ export function loadConfig() {
|
|
|
137
168
|
throw new Error(`LNURLCASH_NOTES must be within the home directory (got: ${config.lnurlcashNotesPath})`);
|
|
138
169
|
}
|
|
139
170
|
}
|
|
140
|
-
// Warn if BIND_ADDRESS is non-loopback
|
|
171
|
+
// Warn if BIND_ADDRESS is non-loopback: the bearer token then crosses the network
|
|
141
172
|
if (config.transport === 'http' && config.bindAddress !== '127.0.0.1' && config.bindAddress !== '::1') {
|
|
142
|
-
console.error(`Warning: BIND_ADDRESS is ${config.bindAddress}
|
|
173
|
+
console.error(`Warning: BIND_ADDRESS is ${config.bindAddress}, so the server is reachable from the network and its bearer token travels in cleartext. Put it behind a TLS reverse proxy and list the proxy's host in HTTP_ALLOWED_HOSTS.`);
|
|
143
174
|
}
|
|
144
175
|
// Warn if CORS allows all origins (potential CSRF risk on HTTP transport)
|
|
145
176
|
if (config.corsOrigin === '*') {
|
package/build/config.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"config.js","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAA;AACnC,OAAO,EAAE,OAAO,EAAE,MAAM,SAAS,CAAA;AACjC,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,QAAQ,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAA;AAClE,OAAO,EAAE,oBAAoB,EAAE,MAAM,qBAAqB,CAAA;AAC1D,OAAO,EAAE,kBAAkB,EAAmB,MAAM,wBAAwB,CAAA;
|
|
1
|
+
{"version":3,"file":"config.js","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAA;AACnC,OAAO,EAAE,OAAO,EAAE,MAAM,SAAS,CAAA;AACjC,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,QAAQ,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAA;AAClE,OAAO,EAAE,oBAAoB,EAAE,MAAM,qBAAqB,CAAA;AAC1D,OAAO,EAAE,kBAAkB,EAAmB,MAAM,wBAAwB,CAAA;AA+B5E,SAAS,oBAAoB,CAAC,IAAY,EAAE,KAAa;IACvD,IAAI,CAAC,MAAM,CAAC,QAAQ,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W,KAAK,WAAW,IAAI,MAAM,CAAC,WAAW,KAAK,KAAK,EAAE,CAAC;QACtG,OAAO,CAAC,KAAK,CAAC,4BAA4B,MAAM,CAAC,WAAW,6KAA6K,CAAC,CAAA;IAC5O,CAAC;IAED,0EAA0E;IAC1E,IAAI,MAAM,CAAC,UAAU,KAAK,GAAG,EAAE,CAAC;QAC9B,OAAO,CAAC,KAAK,CAAC,4IAA4I,CAAC,CAAA;IAC7J,CAAC;IAED,2EAA2E;IAC3E,gFAAgF;IAChF,gFAAgF;IAChF,kFAAkF;IAClF,IAAI,OAAO,CAAC,GAAG,CAAC,4BAA4B,KAAK,GAAG,EAAE,CAAC;QACrD,MAAM,eAAe,GAAG,MAAM,CAAC,gBAAgB,IAAI,OAAO,CAAC,GAAG,CAAC,kBAAkB,KAAK,MAAM,CAAA;QAC5F,IAAI,CAAC,eAAe,EAAE,CAAC;YACrB,MAAM,IAAI,KAAK,CAAC,wMAAwM,CAAC,CAAA;QAC3N,CAAC;QACD,OAAO,CAAC,KAAK,CAAC,0JAA0J,CAAC,CAAA;IAC3K,CAAC;IAED,OAAO,MAAM,CAAA;AACf,CAAC"}
|
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
import { validateUrl } from './ssrf-guard.js';
|
|
2
2
|
import { usesProxy } from './socks-proxy.js';
|
|
3
3
|
import { SsrfError, TimeoutError, RetryExhaustedError, DowngradeError, ResponseTooLargeError, TransportUnavailableError } from './errors.js';
|
|
4
|
+
/** Request headers that may follow a redirect to another origin. */
|
|
5
|
+
const CROSS_ORIGIN_SAFE_HEADERS = new Set([
|
|
6
|
+
'accept', 'accept-language', 'content-language', 'content-type',
|
|
7
|
+
'user-agent', 'cache-control', 'pragma',
|
|
8
|
+
]);
|
|
4
9
|
const MAX_REDIRECTS = 5;
|
|
5
10
|
const DEFAULT_TIMEOUT_MS = 30_000;
|
|
6
11
|
const DEFAULT_RETRIES = 2;
|
|
@@ -215,13 +220,19 @@ async function fetchWithTimeoutAndRedirects(fetchFn, url, init, timeoutMs, allow
|
|
|
215
220
|
}
|
|
216
221
|
// SSRF check on redirect target; capture the resolved address for pinning
|
|
217
222
|
currentResolved = await validateUrl(currentUrl, allowPrivate, ssrfOptions);
|
|
218
|
-
//
|
|
219
|
-
//
|
|
223
|
+
// On a cross-origin redirect keep only headers that carry no secret.
|
|
224
|
+
// Authorization is not the only one: X-Cashu and X-LNURLcash carry
|
|
225
|
+
// bearer money, and a caller may have supplied its own API key or cookie
|
|
226
|
+
// under any name. Per Fetch spec, credentials do not follow cross-origin
|
|
227
|
+
// redirects; neither does anything else we cannot vouch for.
|
|
220
228
|
const redirectOrigin = new URL(currentUrl).origin;
|
|
221
229
|
const originalOrigin = new URL(originalUrl).origin;
|
|
222
230
|
if (redirectOrigin !== originalOrigin && currentInit.headers) {
|
|
223
|
-
const headers = new Headers(
|
|
224
|
-
headers
|
|
231
|
+
const headers = new Headers();
|
|
232
|
+
for (const [name, value] of new Headers(currentInit.headers)) {
|
|
233
|
+
if (CROSS_ORIGIN_SAFE_HEADERS.has(name))
|
|
234
|
+
headers.set(name, value);
|
|
235
|
+
}
|
|
225
236
|
currentInit = { ...currentInit, headers };
|
|
226
237
|
}
|
|
227
238
|
// 301/302/303 change POST to GET and drop body
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"resilient-fetch.js","sourceRoot":"","sources":["../../src/fetch/resilient-fetch.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAiD,MAAM,iBAAiB,CAAA;AAC5F,OAAO,EAAE,SAAS,EAAmB,MAAM,kBAAkB,CAAA;AAC7D,OAAO,EAAE,SAAS,EAAE,YAAY,EAAE,mBAAmB,EAAE,cAAc,EAAE,qBAAqB,EAAE,yBAAyB,EAAE,MAAM,aAAa,CAAA;
|
|
1
|
+
{"version":3,"file":"resilient-fetch.js","sourceRoot":"","sources":["../../src/fetch/resilient-fetch.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAiD,MAAM,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QACD,sDAAsD;IACxD,CAAC;AACH,CAAC;AAED,2FAA2F;AAC3F,MAAM,qBAAqB,GAAG,IAAI,GAAG,CAAC,CAAC,cAAc,EAAE,WAAW,EAAE,WAAW,EAAE,yBAAyB,CAAC,CAAC,CAAA;AAE5G;;;;;;;;GAQG;AACH,MAAM,UAAU,gBAAgB,CAAC,GAAY;IAC3C,IAAI,GAAG,YAAY,yBAAyB;QAAE,OAAO,IAAI,CAAA;IACzD,IAAI,GAAG,YAAY,KAAK,EAAE,CAAC;QACzB,MAAM,IAAI,GAAI,GAA6B,CAAC,IAAI,CAAA;QAChD,IAAI,IAAI,IAAI,qBAAqB,CAAC,GAAG,CAAC,IAAI,CAAC;YAAE,OAAO,IAAI,CAAA;IAC1D,CAAC;IACD,OAAO,KAAK,CAAA;AACd,CAAC;AAED;;;;;;;;GAQG;AACH,MAAM,CAAC,KAAK,UAAU,qBAAqB,CACzC,IAAc,EACd,IAAiB,EACjB,OAAsG,EACtG,OAA+B;IAE/B,IAAI,SAA4B,CAAA;IAChC,KAAK,MAAM,GAAG,IAAI,IAAI,EAAE,CAAC;QACvB,IAAI,CAAC;YACH,OAAO,MAAM,OAAO,CAAC,GAAG,EAAE,IAAI,EAAE,OAAO,CAAC,CAAA;QAC1C,CAAC;QAAC,OAAO,GAAG,EAAE,CAAC;YACb,IAAI,gBAAgB,CAAC,GAAG,CAAC,EAAE,CAAC;gBAC1B,SAAS,GAAG,GAAY,CAAA;gBACxB,SAAQ;YACV,CAAC;YACD,MAAM,GAAG,CAAA;QACX,CAAC;IACH,CAAC;IACD,MAAM,SAAS,IAAI,IAAI,KAAK,CAAC,0BAA0B,CAAC,CAAA;AAC1D,CAAC"}
|
|
@@ -6,6 +6,7 @@ export interface TransportCapabilities {
|
|
|
6
6
|
*
|
|
7
7
|
* - .onion URLs are filtered out when `hasTorProxy` is false
|
|
8
8
|
* - URLs are sorted by `preference` order (lower index = higher priority)
|
|
9
|
+
* - A TLD that is neither familiar nor known to be Handshake sorts after https
|
|
9
10
|
* - URLs whose transport type is not in the preference list are placed last
|
|
10
11
|
* - Relative order is preserved within the same tier (stable sort)
|
|
11
12
|
*/
|
package/build/fetch/transport.js
CHANGED
|
@@ -15,49 +15,72 @@ const COMMON_TLDS = new Set([
|
|
|
15
15
|
// Country codes that see common non-country usage
|
|
16
16
|
'ly', 'gg', 'gg', 'sh', 'io',
|
|
17
17
|
]);
|
|
18
|
+
/**
|
|
19
|
+
* TLDs that are Handshake names and not ICANN ones. A TLD merely missing
|
|
20
|
+
* from COMMON_TLDS is far more often an ICANN TLD this list does not know
|
|
21
|
+
* (.pub, .fyi, and hundreds more) than a Handshake one.
|
|
22
|
+
*/
|
|
23
|
+
const KNOWN_HNS_TLDS = new Set(['hns']);
|
|
24
|
+
/** `guessed` marks an HNS classification made only because the TLD is unfamiliar. */
|
|
18
25
|
function classifyUrl(url) {
|
|
19
26
|
let parsed;
|
|
20
27
|
try {
|
|
21
28
|
parsed = new URL(url);
|
|
22
29
|
}
|
|
23
30
|
catch {
|
|
24
|
-
return 'http';
|
|
31
|
+
return { type: 'http', guessed: false };
|
|
25
32
|
}
|
|
26
33
|
const hostname = parsed.hostname.toLowerCase();
|
|
27
34
|
if (hostname.endsWith('.onion'))
|
|
28
|
-
return 'onion';
|
|
35
|
+
return { type: 'onion', guessed: false };
|
|
29
36
|
const tld = hostname.split('.').pop() ?? '';
|
|
37
|
+
if (KNOWN_HNS_TLDS.has(tld))
|
|
38
|
+
return { type: 'hns', guessed: false };
|
|
30
39
|
if (!COMMON_TLDS.has(tld))
|
|
31
|
-
return 'hns';
|
|
40
|
+
return { type: 'hns', guessed: true };
|
|
32
41
|
if (parsed.protocol === 'https:')
|
|
33
|
-
return 'https';
|
|
34
|
-
return 'http';
|
|
42
|
+
return { type: 'https', guessed: false };
|
|
43
|
+
return { type: 'http', guessed: false };
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Where a URL sorts under `preference`. A guessed HNS name sorts just after
|
|
47
|
+
* both the hns and https tiers, so an unfamiliar ICANN TLD never jumps ahead
|
|
48
|
+
* of a service's ordinary HTTPS endpoint.
|
|
49
|
+
*/
|
|
50
|
+
function rank(url, preference) {
|
|
51
|
+
const { type, guessed } = classifyUrl(url);
|
|
52
|
+
const idx = preference.indexOf(type);
|
|
53
|
+
if (idx === -1)
|
|
54
|
+
return Infinity;
|
|
55
|
+
if (!guessed)
|
|
56
|
+
return idx;
|
|
57
|
+
const httpsIdx = preference.indexOf('https');
|
|
58
|
+
return Math.max(idx, httpsIdx) + 0.5;
|
|
35
59
|
}
|
|
36
60
|
/**
|
|
37
61
|
* Filter and sort URLs by client capabilities and preference order.
|
|
38
62
|
*
|
|
39
63
|
* - .onion URLs are filtered out when `hasTorProxy` is false
|
|
40
64
|
* - URLs are sorted by `preference` order (lower index = higher priority)
|
|
65
|
+
* - A TLD that is neither familiar nor known to be Handshake sorts after https
|
|
41
66
|
* - URLs whose transport type is not in the preference list are placed last
|
|
42
67
|
* - Relative order is preserved within the same tier (stable sort)
|
|
43
68
|
*/
|
|
44
69
|
export function selectTransports(urls, preference, capabilities) {
|
|
45
70
|
// Filter: remove .onion URLs when no Tor proxy is available
|
|
46
71
|
const filtered = urls.filter(url => {
|
|
47
|
-
const type = classifyUrl(url);
|
|
72
|
+
const { type } = classifyUrl(url);
|
|
48
73
|
if (type === 'onion' && !capabilities.hasTorProxy)
|
|
49
74
|
return false;
|
|
50
75
|
return true;
|
|
51
76
|
});
|
|
52
|
-
// Sort by preference
|
|
77
|
+
// Sort by preference rank (stable; Array.prototype.sort is stable in Node 18+).
|
|
78
|
+
// Not in the preference list → Infinity, placed at the end.
|
|
53
79
|
return filtered.slice().sort((a, b) => {
|
|
54
|
-
const
|
|
55
|
-
const
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
// Not in preference list → place at end (index = Infinity)
|
|
59
|
-
const posA = idxA === -1 ? Infinity : idxA;
|
|
60
|
-
const posB = idxB === -1 ? Infinity : idxB;
|
|
80
|
+
const posA = rank(a, preference);
|
|
81
|
+
const posB = rank(b, preference);
|
|
82
|
+
if (posA === posB)
|
|
83
|
+
return 0;
|
|
61
84
|
return posA - posB;
|
|
62
85
|
});
|
|
63
86
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"transport.js","sourceRoot":"","sources":["../../src/fetch/transport.ts"],"names":[],"mappings":"AAIA;;;GAGG;AACH,MAAM,WAAW,GAAG,IAAI,GAAG,CAAC;IAC1B,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,IAAI,EAAE,KAAK,EAAE,KAAK,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI;IACrE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI;IACtE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,KAAK,EAAE,QAAQ,EAAE,QAAQ,EAAE,MAAM;IACvE,MAAM,EAAE,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM;IACjD,KAAK,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,SAAS;IAC5E,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,EAAE,OAAO,EAAE,SAAS,EAAE,MAAM,EAAE,OAAO,EAAE,KAAK;IAC1E,MAAM,EAAE,KAAK,EAAE,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,SAAS,EAAE,UAAU;IAC3E,QAAQ,EAAE,WAAW,EAAE,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO;IACzE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,I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CAAC"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { Request, Response, NextFunction } from 'express';
|
|
2
|
+
/**
|
|
3
|
+
* Host header values the HTTP transport accepts. The MCP SDK compares the
|
|
4
|
+
* whole header, port included, so a DNS-rebinding page served from another
|
|
5
|
+
* name cannot reach the server even from the user's own browser.
|
|
6
|
+
*
|
|
7
|
+
* A loopback bind accepts the usual loopback names. Any other bind accepts
|
|
8
|
+
* its own address. `extra` (HTTP_ALLOWED_HOSTS) adds names such as the one a
|
|
9
|
+
* reverse proxy forwards, written exactly as clients send them.
|
|
10
|
+
*/
|
|
11
|
+
export declare function allowedHostsFor(bindAddress: string, port: number, extra?: string[]): string[];
|
|
12
|
+
/** Express middleware requiring `Authorization: Bearer <token>`, compared in constant time. */
|
|
13
|
+
export declare function bearerAuth(token: string): (req: Request, res: Response, next: NextFunction) => void;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { createHash, timingSafeEqual } from 'node:crypto';
|
|
2
|
+
const LOOPBACK = new Set(['127.0.0.1', '::1', 'localhost']);
|
|
3
|
+
function hostWithPort(host, port) {
|
|
4
|
+
return host.includes(':') ? `[${host}]:${port}` : `${host}:${port}`;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* Host header values the HTTP transport accepts. The MCP SDK compares the
|
|
8
|
+
* whole header, port included, so a DNS-rebinding page served from another
|
|
9
|
+
* name cannot reach the server even from the user's own browser.
|
|
10
|
+
*
|
|
11
|
+
* A loopback bind accepts the usual loopback names. Any other bind accepts
|
|
12
|
+
* its own address. `extra` (HTTP_ALLOWED_HOSTS) adds names such as the one a
|
|
13
|
+
* reverse proxy forwards, written exactly as clients send them.
|
|
14
|
+
*/
|
|
15
|
+
export function allowedHostsFor(bindAddress, port, extra = []) {
|
|
16
|
+
const hosts = LOOPBACK.has(bindAddress)
|
|
17
|
+
? ['localhost', '127.0.0.1', '::1'].map(h => hostWithPort(h, port))
|
|
18
|
+
: bindAddress === '0.0.0.0' || bindAddress === '::'
|
|
19
|
+
? []
|
|
20
|
+
: [hostWithPort(bindAddress, port)];
|
|
21
|
+
return [...new Set([...hosts, ...extra])];
|
|
22
|
+
}
|
|
23
|
+
/** Express middleware requiring `Authorization: Bearer <token>`, compared in constant time. */
|
|
24
|
+
export function bearerAuth(token) {
|
|
25
|
+
const expected = createHash('sha256').update(token).digest();
|
|
26
|
+
return (req, res, next) => {
|
|
27
|
+
const header = req.headers.authorization ?? '';
|
|
28
|
+
const match = /^Bearer (.+)$/.exec(header);
|
|
29
|
+
const presented = createHash('sha256').update(match ? match[1] : '').digest();
|
|
30
|
+
if (!match || !timingSafeEqual(presented, expected)) {
|
|
31
|
+
res.setHeader('WWW-Authenticate', 'Bearer');
|
|
32
|
+
res.status(401).json({ error: 'Unauthorised' });
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
next();
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
//# sourceMappingURL=http-security.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"http-security.js","sourceRoot":"","sources":["../src/http-security.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,eAAe,EAAE,MAAM,aAAa,CAAA;AAGzD,MAAM,QAAQ,GAAG,IAAI,GAAG,CAAC,CAAC,WAAW,EAAE,KAAK,EAAE,WAAW,CAAC,CAAC,CAAA;AAE3D,SAAS,YAAY,CAAC,IAAY,EAAE,IAAY;IAC9C,OAAO,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,IAAI,IAAI,KAAK,IAAI,EAAE,CAAC,CAAC,CAAC,GAAG,IAAI,IAAI,IAAI,EAAE,CAAA;AACrE,CAAC;AAED;;;;;;;;GAQG;AACH,MAAM,UAAU,eAAe,CAAC,WAAmB,EAAE,IAAY,EAAE,QAAkB,EAAE;IACrF,MAAM,KAAK,GAAG,QAAQ,CAAC,GAAG,CAAC,WAAW,CAAC;QACrC,CAAC,CA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