ENTANGLEIT
Notes

Building the x402 Gateway — SSRF, replays, and credential injection

The moment you proxy someone else's API for money, you inherit three problems. Two of them can lose the seller money.

x402 Gateway turns any HTTPS upstream into a pay-per-call endpoint. Sellers register a base URL, an auth header, and per-route prices; buyers pay in sats and the gateway proxies the call with credentials injected.

That description hides the interesting parts. A paid proxy has three attack surfaces that a plain reverse proxy doesn't.

1. SSRF: the upstream URL is user input

Sellers register an arbitrary baseUrl. If the gateway will fetch anything, it becomes a post-exploitation pivot into private networks, metadata endpoints, and internal services.

What the gateway enforces:

None of this is exotic, but every paid-proxy builder forgets at least one of these. The metadata endpoint is the one that ends you.

2. Replay: one payment must buy exactly one call

On-chain payments are bearer instruments. If the gateway verifies a txid and serves, a buyer can replay the same PAYMENT-SIGNATURE and get N calls for one payment. Worse, two concurrent retries can race between verify and dedupe.

The fix is a claim-before-serve write: the txid is inserted into a unique table before upstream fetch, and a conflict means the payment is already spent. It's the same invariant as agentpay's ledger refs — claim the reference atomically, then do the work.

Also checked at verification: amount ≥ price, correct payTo, correct network, and a well-formed exact-scheme envelope. Verification is not settlement; the broadcast happens after the claim wins, so a losing replay never touches the chain.

3. Credentials: injected, never returned

Sellers don't want to hand buyers their upstream key, and buyers shouldn't see it. The gateway injects authHeader/authValue server-side after verification. Consequences:

The operational bits that saved us

What we'd tell a first-time builder

  1. Model the threat, not the feature. The feature is "proxy with payment." The threats are SSRF, replay, and credential leak. Design those first.
  2. Claim before you serve. Anywhere money and work meet, make the dedupe write the gate, not a check after the fact.
  3. Treat upstreams as hostile. Timeouts, size caps, and redirect discipline are table stakes when someone else controls the other side.
  4. Put analytics behind the paid tier. Per-call logs are real infrastructure cost and a genuinely useful upgrade — but never let billing depend on them.

The gateway is live at entangleit.com/x402gateway; the registration UI doubles as the directory. If you just want one API monetized without running any of this, that's the Agent-ready API engagement.