From 34f2e562e2c6e30e35ae584bd6b1b985c5edd229 Mon Sep 17 00:00:00 2001 From: Kevin Ravensberg Date: Fri, 13 Nov 2026 00:21:58 +0100 Subject: [PATCH] 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 '. Without a configured token all requests are rejected. (cherry picked from commit 3fb544687d8cd8563f1e4e1720de1ef832653457) Co-Authored-By: Claude Fable 5.1 --- README.md | 1 + config.yaml.example | 1 + internal/api/middleware.go | 19 +++++++++++++++++++ internal/api/server.go | 5 +++++ internal/config.go | 1 + main.go | 4 ++++ 6 files changed, 31 insertions(+) diff --git a/README.md b/README.md index 1a3e5b06..3ff70176 100644 --- a/README.md +++ b/README.md @@ -56,6 +56,7 @@ You can either use your own LNbits instance (recommended) or create an account a - `transactions_path`: Transaction 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). +- `admin_api_token`: Secret for the admin API on `admin_api_host` (ban, unban, mutex, pprof). Send it as `Authorization: Bearer `. 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). - `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). diff --git a/config.yaml.example b/config.yaml.example index c6a32266..4bcdcb81 100644 --- a/config.yaml.example +++ b/config.yaml.example @@ -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_image: true admin_api_host: localhost:6060 + admin_api_token: "" # required for the admin API: send it as "Authorization: Bearer " telegram: message_dispose_duration: 10 api_key: "1234" diff --git a/internal/api/middleware.go b/internal/api/middleware.go index 290e90c2..e53b5fb2 100644 --- a/internal/api/middleware.go +++ b/internal/api/middleware.go @@ -2,6 +2,7 @@ package api import ( "context" + "crypto/subtle" "encoding/base64" "fmt" "net/http" @@ -10,6 +11,7 @@ import ( "github.com/LightningTipBot/LightningTipBot/internal/lnbits" "github.com/LightningTipBot/LightningTipBot/internal/telegram" + "github.com/gorilla/mux" "gorm.io/gorm" log "github.com/sirupsen/logrus" @@ -125,3 +127,20 @@ func dump(r *http.Request) string { } return string(x) } + +// AdminTokenMiddleware only lets requests through that carry the token as +// "Authorization: Bearer ". 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) + }) + } +} diff --git a/internal/api/server.go b/internal/api/server.go index 1b01778b..95825d42 100644 --- a/internal/api/server.go +++ b/internal/api/server.go @@ -41,6 +41,11 @@ func NewServer(address string) *Server { func (w *Server) 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) { w.router.PathPrefix(path).Handler(handler) } diff --git a/internal/config.go b/internal/config.go index ad8eb9a3..493123d1 100644 --- a/internal/config.go +++ b/internal/config.go @@ -44,6 +44,7 @@ type BotConfiguration struct { LNURLHostUrl *url.URL `yaml:"-"` LNURLSendImage bool `yaml:"lnurl_image"` AdminAPIHost string `yaml:"admin_api_host"` + AdminAPIToken string `yaml:"admin_api_token"` } type TelegramConfiguration struct { diff --git a/main.go b/main.go index c472aedf..52ac22c3 100644 --- a/main.go +++ b/main.go @@ -82,6 +82,10 @@ func startApiServer(bot *telegram.TipBot) { // start internal admin server adminService := admin.New(bot) 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/unlock/{id}", mutex.UnlockHTTP) internalAdminServer.AppendRoute("/admin/ban/{id}", adminService.BanUser)