Menu

Prevent Mini App Identity Spoofing with Server-Side HMAC Validation in PHP
📰

Prevent Mini App Identity Spoofing with Server-Side HMAC Validation in PHP

DEV Community: php·Serhii·7 days ago
#rVKEGI5d
#devto#dev#string#telegram#initdata
Reading 0:00
15s threshold

When building a Telegram Mini App, front-end context is provided through the window.Telegram.WebApp object. Developers often inspect window.Telegram.WebApp.initDataUnsafe to retrieve the current user's Telegram ID, first name, and username. Using initDataUnsafe to identify users in your API endpoints introduces a massive security vulnerability. Any client can open browser Developer Tools, alter the JavaScript memory state, replace initDataUnsafe.user.id with another user's ID, and trigger API calls. If your PHP backend trusts client-provided IDs without verification, attackers can hijack accounts, spend other users' balances, or manipulate state arbitrarily.

To secure your application, your backend must process the raw, encrypted query string provided by window.Telegram.WebApp.initData . This string includes a cryptographic hash generated by Telegram's servers.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.