Keep invoices, LNURLs, ecash tokens and node credentials out of the log

The message interceptor logged every private text at Info, which put
bolt11 invoices, LNURLs, Cashu tokens and '/node add lnd <host>
<macaroon>' into journald. The API middleware logged the submitted key
of banned users. Both are redacted now.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
Michilis
2026-11-13 20:41:39 +00:00
co-authored by Claude Fable 5.1
parent bb1037bcb7
commit afd1050e57
3 changed files with 39 additions and 2 deletions
+1 -1
View File
@@ -65,7 +65,7 @@ func AuthorizationMiddleware(database *gorm.DB, authType AuthType, accessType Ac
// first we make sure that the password is not already "banned_"
if strings.Contains(password, "_") || strings.HasPrefix(password, "banned_") {
w.WriteHeader(401)
log.Warnf("[api] Banned user %s. Not forwarding request", password)
log.Warnf("[api] Request with a banned key. Not forwarding request")
return
}
// then we check whether the "normal" password provided is in the database (it should be not if the user is banned)
+1 -1
View File
@@ -228,7 +228,7 @@ func (bot TipBot) logMessageInterceptor(ctx intercept.Context) (intercept.Contex
if ctx.Message() != nil {
if ctx.Message().Text != "" {
log_string := fmt.Sprintf("[%s:%d %s:%d] %s", ctx.Message().Chat.Title, ctx.Message().Chat.ID, GetUserStr(ctx.Message().Sender), ctx.Message().Sender.ID, ctx.Message().Text)
log_string := fmt.Sprintf("[%s:%d %s:%d] %s", ctx.Message().Chat.Title, ctx.Message().Chat.ID, GetUserStr(ctx.Message().Sender), ctx.Message().Sender.ID, redactForLog(ctx.Message().Text))
if ctx.Message().IsReply() {
log_string = fmt.Sprintf("%s -> %s", log_string, GetUserStr(ctx.Message().ReplyTo.Sender))
}
+37
View File
@@ -0,0 +1,37 @@
package telegram
import (
"fmt"
"regexp"
"strings"
"github.com/LightningTipBot/LightningTipBot/pkg/lightning"
)
// cashuTokenPattern matches a Cashu ecash token (a bearer instrument).
var cashuTokenPattern = regexp.MustCompile(`(?i)^cashu[a-z][a-z0-9_-]+$`)
// redactForLog returns text with everything that is money or a credential
// replaced by a placeholder, so the message log never holds bolt11 invoices,
// LNURLs, ecash tokens or node credentials (`/node add lnd <host> <macaroon>`).
func redactForLog(text string) string {
fields := strings.Fields(text)
if len(fields) == 0 {
return text
}
if strings.HasPrefix(strings.ToLower(fields[0]), "/node") {
return fields[0] + " <redacted>"
}
for i, f := range fields {
lower := strings.ToLower(f)
switch {
case lightning.IsInvoice(lower):
fields[i] = fmt.Sprintf("<invoice len=%d>", len(f))
case lightning.IsLnurl(lower) || strings.HasPrefix(lower, "lnurl"):
fields[i] = fmt.Sprintf("<lnurl len=%d>", len(f))
case cashuTokenPattern.MatchString(f):
fields[i] = fmt.Sprintf("<cashu-token len=%d>", len(f))
}
}
return strings.Join(fields, " ")
}