Refuse a banned user's invoice key and keep nsec and k1 out of logs
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 5.5
parent
8b8b0fa369
commit
15b95d962b
@@ -87,6 +87,14 @@ func AuthorizationMiddleware(database *gorm.DB, authType AuthType, accessType Ac
|
||||
return
|
||||
}
|
||||
|
||||
// The ban rewrites only the admin key, so a banned user's invoice key
|
||||
// still matches above. Refuse every key of a banned user.
|
||||
if user.Banned || user.Wallet == nil || strings.HasPrefix(user.Wallet.Adminkey, "banned") || strings.Contains(user.Wallet.Adminkey, "_") {
|
||||
w.WriteHeader(401)
|
||||
log.Warnf("[api] Request with a key of a banned user. Not forwarding request")
|
||||
return
|
||||
}
|
||||
|
||||
log.Debugf("[api] User: %s Endpoint: %s %s %s", telegram.GetUserStr(user.Telegram), r.Method, r.URL.Path, r.URL.RawQuery)
|
||||
r = r.WithContext(context.WithValue(r.Context(), "user", user))
|
||||
next.ServeHTTP(w, r)
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/LightningTipBot/LightningTipBot/internal/lnbits"
|
||||
"gorm.io/driver/sqlite"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/logger"
|
||||
)
|
||||
|
||||
// The ban rewrites only the admin key. Every key of a banned user must be
|
||||
// refused, the invoice key included, and a normal user must still pass.
|
||||
func TestAuthorizationMiddlewareRefusesBannedUsersKeys(t *testing.T) {
|
||||
db, err := gorm.Open(sqlite.Open("file::memory:?cache=shared&_banmw=1"), &gorm.Config{Logger: logger.Default.LogMode(logger.Silent)})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := db.AutoMigrate(&lnbits.User{}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
users := []*lnbits.User{
|
||||
{ID: "u1", Name: "111", Banned: true, Wallet: &lnbits.Wallet{ID: "w1", Adminkey: "banned_aaaaaaaaaaaaaaaa", Inkey: "bbbbbbbbbbbbbbbb"}},
|
||||
{ID: "u2", Name: "222", Wallet: &lnbits.Wallet{ID: "w2", Adminkey: "cccccccccccccccc", Inkey: "dddddddddddddddd"}},
|
||||
}
|
||||
for _, u := range users {
|
||||
if err := db.Create(u).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
call := func(access AccessKeyType, key string) int {
|
||||
h := AuthorizationMiddleware(db, AuthTypeBasic, access, func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(200) })
|
||||
req := httptest.NewRequest("GET", "/api/v1/balance", nil)
|
||||
req.Header.Set("Authorization", "Basic "+key)
|
||||
rr := httptest.NewRecorder()
|
||||
h(rr, req)
|
||||
return rr.Code
|
||||
}
|
||||
for _, c := range []struct {
|
||||
access AccessKeyType
|
||||
key string
|
||||
want int
|
||||
}{
|
||||
{AccessKeyTypeInvoice, "bbbbbbbbbbbbbbbb", 401}, // banned user's invoice key
|
||||
{AccessKeyTypeInvoice, "aaaaaaaaaaaaaaaa", 401}, // banned user's old admin key
|
||||
{AccessKeyTypeAdmin, "aaaaaaaaaaaaaaaa", 401},
|
||||
{AccessKeyTypeInvoice, "dddddddddddddddd", 200}, // normal user still works
|
||||
{AccessKeyTypeAdmin, "cccccccccccccccc", 200},
|
||||
} {
|
||||
if got := call(c.access, c.key); got != c.want {
|
||||
t.Errorf("%s key %s: got %d, want %d", c.access.Type, c.key, got, c.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -86,7 +86,7 @@ func (bot *TipBot) lnurlHandler(ctx intercept.Context) (intercept.Context, error
|
||||
if err != nil {
|
||||
bot.tryEditMessage(statusMsg, fmt.Sprintf(Translate(ctx, "errorReasonMessage"), "LNURL error."))
|
||||
// bot.tryEditMessage(statusMsg, fmt.Sprintf(Translate(ctx, "errorReasonMessage"), err.Error()))
|
||||
log.Warnf("[HandleLNURL] Error: %s", err.Error())
|
||||
log.Warnf("[HandleLNURL] Error: %s", errorForLog(err))
|
||||
return ctx, err
|
||||
}
|
||||
switch params.(type) {
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
package telegram
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
@@ -19,16 +21,19 @@ func redactForLog(text string) string {
|
||||
if len(fields) == 0 {
|
||||
return text
|
||||
}
|
||||
if strings.HasPrefix(strings.ToLower(fields[0]), "/node") {
|
||||
// /nostr takes a public key, but users paste their private key by mistake
|
||||
if cmd := strings.ToLower(fields[0]); strings.HasPrefix(cmd, "/node") || strings.HasPrefix(cmd, "/nostr") {
|
||||
return fields[0] + " <redacted>"
|
||||
}
|
||||
for i, f := range fields {
|
||||
lower := strings.ToLower(f)
|
||||
switch {
|
||||
case lightning.IsInvoice(lower):
|
||||
case lightning.IsInvoice(lower) || strings.HasPrefix(lower, "lntb") || strings.HasPrefix(lower, "lnbcrt"):
|
||||
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 strings.HasPrefix(lower, "nsec1"):
|
||||
fields[i] = "<nsec redacted>"
|
||||
case cashuTokenPattern.MatchString(f):
|
||||
fields[i] = fmt.Sprintf("<cashu-token len=%d>", len(f))
|
||||
case strings.Contains(lower, "k1="):
|
||||
@@ -38,3 +43,14 @@ func redactForLog(text string) string {
|
||||
}
|
||||
return strings.Join(fields, " ")
|
||||
}
|
||||
|
||||
// errorForLog returns err without the URL of a failed HTTP request. The
|
||||
// *url.Error text embeds the full URL, and an LNURL-withdraw link carries its
|
||||
// claim token (k1) in the query.
|
||||
func errorForLog(err error) string {
|
||||
var ue *url.Error
|
||||
if errors.As(err, &ue) {
|
||||
return fmt.Sprintf("%s failed: %v", ue.Op, ue.Err)
|
||||
}
|
||||
return err.Error()
|
||||
}
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
package telegram
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestRedactForLogCredentials(t *testing.T) {
|
||||
nsec := "nsec1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq"
|
||||
for _, in := range []string{"/nostr add " + nsec, "/nostr " + nsec, "please use " + nsec} {
|
||||
if out := redactForLog(in); strings.Contains(out, "nsec1qqq") {
|
||||
t.Errorf("%q leaked: %q", in, out)
|
||||
}
|
||||
}
|
||||
if out := redactForLog("/pay lntb10u1pabcdefghijklmnopqrstuvwxyz0123456789"); strings.Contains(out, "lntb10u1p") {
|
||||
t.Errorf("testnet invoice leaked: %q", out)
|
||||
}
|
||||
}
|
||||
|
||||
func TestErrorForLogDropsURLQuery(t *testing.T) {
|
||||
_, err := http.Get("http://127.0.0.1:1/w?tag=withdrawRequest&k1=SECRETK1")
|
||||
if err == nil {
|
||||
t.Skip("unexpectedly reachable")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "SECRETK1") {
|
||||
t.Fatal("test premise: the url.Error text should contain the URL")
|
||||
}
|
||||
if got := errorForLog(err); strings.Contains(got, "SECRETK1") {
|
||||
t.Errorf("k1 leaked: %q", got)
|
||||
}
|
||||
if got := errorForLog(errors.New("plain")); got != "plain" {
|
||||
t.Errorf("plain error changed: %q", got)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user