Files

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.initData string 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:

  1. Reject an empty or unreasonably large payload, malformed percent encoding, invalid key names, and duplicate decoded keys.
  2. Parse query pairs without silently overwriting duplicates. URL-decode keys and values according to form/query encoding.
  3. Remove and retain the hash field. Require a 64-character lowercase hexadecimal SHA-256 digest.
  4. Sort the remaining decoded key=value strings by key using ordinal/string ordering.
  5. Join the pairs with a single LF (\n), with no trailing newline, to form data_check_string.
  6. Compute raw bytes secret_key = HMAC_SHA256(key="WebAppData", message=app_token).
  7. Compute hexadecimal expected_hash = HMAC_SHA256(key=secret_key, message=data_check_string).
  8. Compare expected_hash with the received hash using a constant-time comparison.
  9. 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.
  10. Parse user JSON 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-compatible SameSite policy; 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.