A Telegram Mini App served by the core under /app/, embedded in the binary. - API under /app/api/ with initData validated (HMAC, age) on every call - Pages: Home, Queue, Buy (target, product, invoice), History, Groups, group detail (Overview, Settings, Wallet, Usage) and Me - Every action runs through the same service, billing and scheduler code as the chat, so limits, prices and admin checks are decided on the server - Texts come from the catalog over the API; layout mirrors for RTL - Front end in web/miniapp (Vite, React, TypeScript); the build is committed and guarded by a source hash test; the Docker image rebuilds it - MINIAPP_ENABLED with an https PUBLIC_URL; entry points from /app, group settings and the over-limit offer - Browser test (make miniapp-e2e) against a core in test mode - Fix: each download attempt gets its own scratch directory, so a retry cannot lose its file to the cleanup of the failed attempt Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
68 lines
2.5 KiB
Go
68 lines
2.5 KiB
Go
package api
|
|
|
|
import (
|
|
"errors"
|
|
"net/url"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
const testToken = "123456789:FAKEfakeFAKEfakeFAKEfakeFAKEfake-01"
|
|
|
|
func TestValidateInitData(t *testing.T) {
|
|
now := time.Unix(1_800_000_000, 0)
|
|
user := InitUser{ID: 42, FirstName: "Ana & Co", Username: "ana", LanguageCode: "es-PY"}
|
|
raw := SignInitData(testToken, user, now.Add(-time.Minute), "group_-1001")
|
|
|
|
got, err := ValidateInitData(raw, testToken, time.Hour, now)
|
|
if err != nil {
|
|
t.Fatalf("valid data refused: %v", err)
|
|
}
|
|
if got.User != user || got.StartParam != "group_-1001" || !got.AuthDate.Equal(now.Add(-time.Minute)) {
|
|
t.Fatalf("parsed = %+v", got)
|
|
}
|
|
|
|
mutate := func(f func(url.Values)) string {
|
|
v, _ := url.ParseQuery(raw)
|
|
f(v)
|
|
return v.Encode()
|
|
}
|
|
cases := []struct {
|
|
name string
|
|
raw string
|
|
tok string
|
|
want error
|
|
}{
|
|
{"empty", "", testToken, ErrInitDataMissing},
|
|
{"no hash", mutate(func(v url.Values) { v.Del("hash") }), testToken, ErrInitDataInvalid},
|
|
{"other bot's token", raw, strings.Replace(testToken, "01", "02", 1), ErrInitDataInvalid},
|
|
{"user swapped", mutate(func(v url.Values) { v.Set("user", `{"id":43,"first_name":"Eve"}`) }), testToken, ErrInitDataInvalid},
|
|
{"field added", mutate(func(v url.Values) { v.Set("chat_type", "private") }), testToken, ErrInitDataInvalid},
|
|
{"start param changed", mutate(func(v url.Values) { v.Set("start_param", "group_-1002") }), testToken, ErrInitDataInvalid},
|
|
{"hash of another length", mutate(func(v url.Values) { v.Set("hash", "abcd") }), testToken, ErrInitDataInvalid},
|
|
{"not a query string", "%zz", testToken, ErrInitDataInvalid},
|
|
}
|
|
for _, c := range cases {
|
|
if _, err := ValidateInitData(c.raw, c.tok, time.Hour, now); !errors.Is(err, c.want) {
|
|
t.Errorf("%s: err = %v, want %v", c.name, err, c.want)
|
|
}
|
|
}
|
|
|
|
// Old data is refused even with a good signature, and so is data from
|
|
// the future beyond clock skew.
|
|
if _, err := ValidateInitData(raw, testToken, time.Hour, now.Add(2*time.Hour)); !errors.Is(err, ErrInitDataExpired) {
|
|
t.Errorf("stale data: err = %v", err)
|
|
}
|
|
future := SignInitData(testToken, user, now.Add(time.Hour), "")
|
|
if _, err := ValidateInitData(future, testToken, time.Hour, now); err == nil {
|
|
t.Error("data dated an hour ahead was accepted")
|
|
}
|
|
|
|
// A signature over a user without an ID is not a user.
|
|
anon := SignInitData(testToken, InitUser{}, now, "")
|
|
if _, err := ValidateInitData(anon, testToken, time.Hour, now); err == nil {
|
|
t.Error("data without a user ID was accepted")
|
|
}
|
|
}
|