Require a token for the admin API

The admin API (ban, unban, mutex, DALL-E toggles, pprof) had no authentication. Every request now needs 'Authorization: Bearer <bot.admin_api_token>'. Without a configured token all requests are rejected.

(cherry picked from commit 3fb544687d8cd8563f1e4e1720de1ef832653457)
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
Kevin Ravensberg
2026-11-13 20:33:44 +00:00
committed by Michilis
parent 7c55e40966
commit 34f2e562e2
6 changed files with 31 additions and 0 deletions
+1
View File
@@ -56,6 +56,7 @@ You can either use your own LNbits instance (recommended) or create an account a
- `transactions_path`: Transaction database file path. - `transactions_path`: Transaction database file path.
- `buntdb_path`: Object storage database file path. - `buntdb_path`: Object storage database file path.
- `lnbits_webhook_server`: URL that lnbits can reach the bot with. This is used for creating webhooks from LNbits to receive notifications about payments (optional). - `lnbits_webhook_server`: URL that lnbits can reach the bot with. This is used for creating webhooks from LNbits to receive notifications about payments (optional).
- `admin_api_token`: Secret for the admin API on `admin_api_host` (ban, unban, mutex, pprof). Send it as `Authorization: Bearer <token>`. Without it the admin API rejects every request.
- `message_dispose_duration`: Duration in seconds after which `/tip` are deleted from a channel (only if the bot is channel admin). - `message_dispose_duration`: Duration in seconds after which `/tip` are deleted from a channel (only if the bot is channel admin).
- `http_proxy` uses a proxy for all LNURL-related outbound requests (optional). - `http_proxy` uses a proxy for all LNURL-related outbound requests (optional).
- `lnurl_public_host_name` is the public URL of your lnbits/LndHub (for BlueWallet/Zeus support, optional). - `lnurl_public_host_name` is the public URL of your lnbits/LndHub (for BlueWallet/Zeus support, optional).
+1
View File
@@ -10,6 +10,7 @@ bot:
lnurl_server: "http://127.0.0.1:5454" # or http://0.0.0.0:5454 depending on your configuration lnurl_server: "http://127.0.0.1:5454" # or http://0.0.0.0:5454 depending on your configuration
lnurl_image: true lnurl_image: true
admin_api_host: localhost:6060 admin_api_host: localhost:6060
admin_api_token: "" # required for the admin API: send it as "Authorization: Bearer <token>"
telegram: telegram:
message_dispose_duration: 10 message_dispose_duration: 10
api_key: "1234" api_key: "1234"
+19
View File
@@ -2,6 +2,7 @@ package api
import ( import (
"context" "context"
"crypto/subtle"
"encoding/base64" "encoding/base64"
"fmt" "fmt"
"net/http" "net/http"
@@ -10,6 +11,7 @@ import (
"github.com/LightningTipBot/LightningTipBot/internal/lnbits" "github.com/LightningTipBot/LightningTipBot/internal/lnbits"
"github.com/LightningTipBot/LightningTipBot/internal/telegram" "github.com/LightningTipBot/LightningTipBot/internal/telegram"
"github.com/gorilla/mux"
"gorm.io/gorm" "gorm.io/gorm"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
@@ -125,3 +127,20 @@ func dump(r *http.Request) string {
} }
return string(x) return string(x)
} }
// AdminTokenMiddleware only lets requests through that carry the token as
// "Authorization: Bearer <token>". With an empty token it rejects everything.
func AdminTokenMiddleware(token string) mux.MiddlewareFunc {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
auth := r.Header.Get("Authorization")
got := strings.TrimPrefix(auth, "Bearer ")
if token == "" || got == auth || subtle.ConstantTimeCompare([]byte(got), []byte(token)) != 1 {
log.Warnf("[ADMIN] unauthorized request to %s", r.URL.Path)
w.WriteHeader(http.StatusUnauthorized)
return
}
next.ServeHTTP(w, r)
})
}
}
+5
View File
@@ -41,6 +41,11 @@ func NewServer(address string) *Server {
func (w *Server) ListenAndServe() { func (w *Server) ListenAndServe() {
go w.httpServer.ListenAndServe() go w.httpServer.ListenAndServe()
} }
// Use adds a middleware that runs for every route of the server.
func (w *Server) Use(middleware mux.MiddlewareFunc) {
w.router.Use(middleware)
}
func (w *Server) PathPrefix(path string, handler http.Handler) { func (w *Server) PathPrefix(path string, handler http.Handler) {
w.router.PathPrefix(path).Handler(handler) w.router.PathPrefix(path).Handler(handler)
} }
+1
View File
@@ -44,6 +44,7 @@ type BotConfiguration struct {
LNURLHostUrl *url.URL `yaml:"-"` LNURLHostUrl *url.URL `yaml:"-"`
LNURLSendImage bool `yaml:"lnurl_image"` LNURLSendImage bool `yaml:"lnurl_image"`
AdminAPIHost string `yaml:"admin_api_host"` AdminAPIHost string `yaml:"admin_api_host"`
AdminAPIToken string `yaml:"admin_api_token"`
} }
type TelegramConfiguration struct { type TelegramConfiguration struct {
+4
View File
@@ -82,6 +82,10 @@ func startApiServer(bot *telegram.TipBot) {
// start internal admin server // start internal admin server
adminService := admin.New(bot) adminService := admin.New(bot)
internalAdminServer := api.NewServer(internal.Configuration.Bot.AdminAPIHost) internalAdminServer := api.NewServer(internal.Configuration.Bot.AdminAPIHost)
if internal.Configuration.Bot.AdminAPIToken == "" {
log.Warnln("[ADMIN] bot.admin_api_token is not set, the admin API rejects all requests")
}
internalAdminServer.Use(api.AdminTokenMiddleware(internal.Configuration.Bot.AdminAPIToken))
internalAdminServer.AppendRoute("/mutex", mutex.ServeHTTP) internalAdminServer.AppendRoute("/mutex", mutex.ServeHTTP)
internalAdminServer.AppendRoute("/mutex/unlock/{id}", mutex.UnlockHTTP) internalAdminServer.AppendRoute("/mutex/unlock/{id}", mutex.UnlockHTTP)
internalAdminServer.AppendRoute("/admin/ban/{id}", adminService.BanUser) internalAdminServer.AppendRoute("/admin/ban/{id}", adminService.BanUser)