5.5 KiB
Security and server integration
Read this before trusting identity, processing contact data, calling EitaaYar, or sending messages.
Trust boundary
- Browser
initDataUnsafe, user IDs,allows_write_to_pm, device IDs, chat fields, contact previews, and message recipients are untrusted. - Send the exact raw
WebApp.initDatastring to the backend over HTTPS. - Keep the Eitaa app token only in server-side secret storage or a secret environment variable. Never embed it in HTML, JavaScript, URLs, logs, source control, or client-visible error output.
- After validation, create the application's own short-lived session and derive identity from that server session on subsequent requests.
Local initData validation
Given raw query-string launch data and the app token:
- Reject an empty or unreasonably large payload, malformed percent encoding, invalid key names, and duplicate decoded keys.
- Parse query pairs without silently overwriting duplicates. URL-decode keys and values according to form/query encoding.
- Remove and retain the
hashfield. Require a 64-character lowercase hexadecimal SHA-256 digest. - Sort the remaining decoded
key=valuestrings by key using ordinal/string ordering. - Join the pairs with a single LF (
\n), with no trailing newline, to formdata_check_string. - Compute raw bytes
secret_key = HMAC_SHA256(key="WebAppData", message=app_token). - Compute hexadecimal
expected_hash = HMAC_SHA256(key=secret_key, message=data_check_string). - Compare
expected_hashwith the received hash using a constant-time comparison. - Require a numeric
auth_date, reject future dates beyond a small clock-skew allowance, and enforce a short maximum age appropriate to the application. Five minutes plus about 30 seconds of future skew is a defensible default, not a platform mandate. - Parse
userJSON only after signature validation. Validate its shape, preserve the ID without 32-bit truncation, and allowlist fields the application actually needs.
Use the documentation's published intermediate vector in tests if available in the source project, but never copy a real-looking documentation token into application configuration.
Production hardening should also include bounded input sizes, generic client errors, rate limiting, HTTPS enforcement, secure cookies, CSRF protection for session-authenticated writes, and replay-aware session design.
Optional EitaaYar authenticity verification
Endpoint:
POST https://eitaayar.ir/api/app/verify
Server-side payload fields are token, hash, and optional user request ip. A successful response is {"ok": true}.
The documentation says verification must occur within one minute of launch and can succeed only once. It also warns of a maximum 20 requests per second before temporary IP blocking. Therefore:
- use it only from the backend;
- prefer a POST body so the token does not enter query URLs;
- do not retry aggressively;
- preserve the received hash for this call;
- use the actual trusted peer IP unless proxy handling is explicitly configured;
- decide deliberately whether a network failure blocks login; and
- cache successful verification only within the resulting session/launch context.
This remote check is optional according to the supplied documentation. Local HMAC proves the launch data was signed; freshness limits replay duration. The one-time remote check adds stronger replay resistance when its timing and availability constraints fit the product.
Contact permission
requestContact requires a user decision. Documentation describes a success boolean plus a signed contact-data string that can be validated like initData; some clients may wrap that string in an object's response field. Accept only a bounded signed string, send it to the backend, validate its signature and freshness, and require contact.user_id to match the already authenticated session user. Do not trust or persist a client-side responseUnsafe preview.
Do not log phone numbers or signed contact material. Request the permission only at a user-understandable point in the flow.
Sending a message
Endpoint:
POST https://eitaayar.ir/api/app/sendMessage
Server-side fields are token, chat_id, and text. Success is documented as:
{"ok": true, "result": "success"}
The user must have granted write access, for example through Start, opening a direct link from their message, or requestWriteAccess. Permission can later fail or be unavailable, so handle the upstream result rather than relying only on allows_write_to_pm.
Derive chat_id from the verified session identity, never a browser-selected recipient. Bound and normalize message text, rate-limit attempts (including failures), use fixed HTTPS endpoints, verify TLS, reject redirects, set timeouts and response-size limits, and return generic errors to the client. The documentation snapshot mentions an initial 50,000-message credit and purchasable additional credit; treat credit policy as mutable and check EitaaYar.
Server session baseline
- Regenerate the session ID after login and use strict session mode.
- Set
HttpOnly,Secure, and an embedding-compatibleSameSitepolicy; test how the target Eitaa clients handle cookies. - Give sessions an absolute expiry independent of cookie lifetime.
- Bind CSRF tokens to the session for state-changing endpoints.
- Store only allowlisted verified identity/context fields.
- Never expose raw upstream responses, stack traces, token values, filesystem paths, or signing intermediates.