DEV Community

Max K.
Max K.

Posted on Edited on

Hardening Telegram Mini App webhooks and non-custodial TON Connect flows

Engineering teams building Telegram Mini Apps (TMAs), TON Connect payment flows, or automated DeFi telemetry services often discover perimeter gaps during a processor review or enterprise security check. These issues rarely come from exotic exploits. They usually come from loose coupling between Mini App authentication, public webhook routes, and settlement verification.

Below is the reference architecture we use across four technical layers to keep TMA and TON Connect backends auditable from day one.

1. Fail-closed initData and webhook isolation

Every authenticated Mini App endpoint should verify the Telegram initData HMAC-SHA256 signature on the server and fail closed with HTTP 401 when headers are missing or tampered with.

if not verify_telegram_init_data(init_data_header, bot_token):
    raise HTTPException(status_code=401, detail="Telegram authentication required")
Enter fullscreen mode Exit fullscreen mode

On the bot side, restrict allowed_updates strictly to the handlers you actually process, such as ["message", "pre_checkout_query"], and serve the webhook behind a permanent TLS domain rather than leaving temporary tunnel URLs exposed. Keep stateless routes like /health, /tonconnect-manifest.json, /terms, and /privacy decoupled from privileged execution paths.

2. Non-custodial TON Connect and DEX telemetry separation

When aggregating live DEX pool feeds (such as STON.fi and DeDust pools on /api/signals), keep read-only market-data ingestion isolated from wallet configuration (/api/config) and payment state.

Never unlock paid features from a client-side callback alone. Verify on-chain TON transfers server-side against destination wallet address, exact nanoton amount, unique invoice memo, and transaction logical time (lt), and keep Telegram Stars pre_checkout_query updates idempotent in the same ledger.

3. Reproducible pre-settlement evidence logs

When a payment processor or counterparty asks how transfers were verified, digging through fragmented container logs wastes days. Running counterparty and on-chain KYT screening before settlement, and tying deployment manifests and webhook configs to a single reconciliation log, turns audit prep into a routine query.

4. Open reference repos and voluntary self-check

You can inspect the reference code and run a 5-step perimeter self-diagnosis with zero shared secrets or external access:

For teams ready to formalize their webhook perimeter or data evidence pipeline:

  • T1 Express API & Webhook Perimeter Audit ($2,500) (prod_cIpL4YWI2gxJJ)
  • T2 Turnkey Implementation ($25,000) and T3 Enterprise Architecture ($100,000) for automated perimeter checks and settlement reconciliation.
  • Teams with 25+ employees and 3+ years of operational records structuring internal documentation alongside these workflows can also explore KnowledgeFuel AI.

Top comments (0)