Skip to content

Add full Mini App init data validation - #325

Merged
negasus merged 2 commits into
go-telegram:mainfrom
INKCR0W:feat-webapp-init-data
Nov 24, 2026
Merged

negasus merged 2 commits into
go-telegram:mainfrom
INKCR0W:feat-webapp-init-data

Conversation

@INKCR0W

@INKCR0W INKCR0W commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Related: #322

Add ValidateWebAppInitData for bot-token HMAC validation and ValidateWebAppInitDataSignature for third-party Ed25519 validation. Both take the raw Telegram.WebApp.initData string and return the parsed data. Call CheckAuthDate separately to enforce a maximum age.

The new names use WebApp like AnswerWebAppQuery. ValidateWebappRequest remains supported for callers that only need the user. Both HMAC validators reject an empty token. All three validators reject separators that make signed field boundaries ambiguous.

Like InlineQuery.ChatType, WebAppInitData.ChatType is a string. WidgetQuery.ChatType has the same five documented values and uses WidgetQueryChatType. A shared named type would require callers that assign either string field to a string variable to convert it. Would you prefer that in a follow-up PR?

When migrating from r.URL.Query(), note that the new APIs report query parsing errors. For example, r.URL.Query() silently drops a pair with an unescaped semicolon.

Tests cover both validation methods, malformed input, expiry, and the legacy API. Ran the suite with Go 1.27.4 and Go 1.18.10.

Telegram HMAC validation
Telegram Ed25519 validation

@codecov-commenter

codecov-commenter commented Nov 22, 2026 •

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 64.89%. Comparing base (d23f36c) to head (59e3883).
⚠️ Report is 7 commits behind head on main.
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #325      +/-   ##
==========================================
+ Coverage   63.16%   64.89%   +1.73%     
==========================================
  Files          40       41       +1     
  Lines        3404     3561     +157     
==========================================
+ Hits         2150     2311     +161     
+ Misses       1195     1193       -2     
+ Partials       59       57       -2     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@negasus negasus left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please update webAppDataCheckString in webapp.go to sort field names first and only then format the key=value lines, as your webAppTestSignature helper already does. Sorting formatted lines gives a different order for keys such as field and field2: field2=… sorts before field=…. This does not affect the current Telegram field set, but both new validators include unknown fields in verification and could reject valid data if such fields are added.

Please add a regression case with those two keys using an independently specified expected order (field first, then field2). Telegram’s construction is documented at https://core.telegram.org/bots/webapps#validating-data-for-third-party-use. The rest of the PR looks good; go test -race ./... and go vet ./... passed on 2d7122f.

@INKCR0W

INKCR0W commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Please update webAppDataCheckString in webapp.go to sort field names first and only then format the key=value lines, as your webAppTestSignature helper already does. Sorting formatted lines gives a different order for keys such as field and field2: field2=… sorts before field=…. This does not affect the current Telegram field set, but both new validators include unknown fields in verification and could reject valid data if such fields are added.

Please add a regression case with those two keys using an independently specified expected order (field first, then field2). Telegram’s construction is documented at https://core.telegram.org/bots/webapps#validating-data-for-third-party-use. The rest of the PR looks good; go test -race ./... and go vet ./... passed on 2d7122f.

Thanks for catching this. The shared helper inherited the old validator’s whole-line sort. All three validators now sort field names before formatting the key=value lines. The order is unchanged for the fields Telegram currently documents.

I added a regression fixture that explicitly puts field before field2 and verified it through all three validators.

@INKCR0W
INKCR0W requested a review from negasus November 23, 2026 12:11

@negasus negasus left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified the field-order fix across all three validators. The new regression test fails on the previous commit and passes on this one; go vet ./... and go test -race ./... pass.

@negasus
negasus merged commit 9e546ff into go-telegram:main Nov 24, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants