Securing Telegram Mini Apps: Server-Side HMAC Validation to Stop Identity Spoofing in PHP
Many Telegram Mini Apps read user IDs from the client-side initDataUnsafe object and trust them on the backend, which lets attackers change the ID in browser memory and act as other users. The article explains that the backend should instead verify the raw initData query string, which Telegram signs with a hash. Verification uses a two-step HMAC-SHA256 process: deriving a secret key from the bot token with the constant "WebAppData", then hashing the alphabetically sorted key-value pairs. The author warns that PHP's parse_str() alters key order and decoding, causing hash mismatches, so manual parsing is recommended. The piece also provides a standalone PHP validation function and advises checking auth_date for expiration.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.
Discussion (0)
Log in to join the discussion and vote.
Log in