Reconcile dashboard /api/stats auth parameter#81
Merged
Conversation
The dashboard frontend authenticated to /api/stats with ?token=<t>, but the endpoint is wrapped in requireAdminToken (PR #50) which only accepts an admin token via X-Admin-Token header or admin_token= query. ?token= was ignored by the gate, so operator stats requests silently 401'd, and the inner dashboard-token elevation check had become dead code behind the admin gate. Reconcile on the admin gate (the secure, consistent option — the stored token is the same admin token /api/pulse already uses): the frontend now sends admin_token=, and the handler returns the elevated payload once requireAdminToken has confirmed a valid admin token reached it. Auth strength is unchanged; a missing/wrong token and the legacy ?token= still 401. Factor route registration into buildMux() so the real /api/stats auth path is testable, and add coverage for the accepted and rejected auth cases.
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The dashboard frontend authenticated to
/api/statsby sending?token=<t>, but/api/statsis wrapped inrequireAdminToken(PR #50), which only accepts an admin token via theX-Admin-Tokenheader oradmin_token=query param. The?token=param was ignored by the gate, so the operator's stats request silently returned 401 — and the inner dashboard-token elevation check inside the handler had become dead code (the admin gate rejects anything without the admin token first).Fix (consistent + secure)
The token the frontend stores is the operator admin token — it is already sent as
admin_token=to/api/pulse. Reconcile on that:/api/statsis now fetched withadmin_token=<t>(wastoken=), matching every other operator call.requireAdminTokenhas confirmed a valid admin token reached the handler, the elevated per-network payload is returned. Removed the dead inner dashboard-token branch.Auth strength is unchanged: a missing token, a wrong token, the legacy
?token=, and the unconfigured-token case all still return 401.Route registration was factored into
buildMux()so the real/api/statsauth path is exercised end-to-end in tests rather than via a re-implemented shim.Tests
TestStatsAuth_AdminTokenQueryParam—admin_token=query andX-Admin-Tokenheader both accepted (200).TestStatsAuth_RejectsBadAndMissingToken— missing / wrong / legacy?token=all 401.TestStatsAuth_UnconfiguredLocksShut— no admin token configured => 401.Validation
GOWORK=off go build/vet ./...clean;GOWORK=off go test -race -parallel 4 ./...green (all packages); gofmt clean.