tgbot_callback_auth_test.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package tgbot
  2. import (
  3. "testing"
  4. "github.com/mymmrac/telego"
  5. )
  6. // A non-admin callback must never reach a privileged handler. The second
  7. // callback switch runs outside the isAdmin guard, so without the default-deny
  8. // check a non-admin who can tap an admin's inline button (e.g. in a group) could
  9. // export the database backup or reset all traffic. Here the privileged handler
  10. // would panic on the nil bot/services of a bare Tgbot; the guard must return
  11. // first, so no panic occurs.
  12. func TestAnswerCallbackDeniesPrivilegedActionToNonAdmin(t *testing.T) {
  13. defer func() {
  14. if r := recover(); r != nil {
  15. t.Fatalf("a non-admin callback reached a privileged handler: %v", r)
  16. }
  17. }()
  18. tg := &Tgbot{}
  19. for _, data := range []string{"get_backup", "reset_all_traffics_c", "add_client", "onlines", "inbounds"} {
  20. q := &telego.CallbackQuery{
  21. Data: data,
  22. From: telego.User{ID: 999999},
  23. Message: &telego.Message{Chat: telego.Chat{ID: 1}},
  24. }
  25. tg.answerCallback(q, false)
  26. }
  27. }
  28. func TestIsClientSelfCallback(t *testing.T) {
  29. allowed := []string{"client_traffic", "client_sub_links", "client_qr_links", "client_sub_links alice@x"}
  30. for _, d := range allowed {
  31. if !isClientSelfCallback(d) {
  32. t.Errorf("%q should be a per-user client callback", d)
  33. }
  34. }
  35. denied := []string{"get_backup", "reset_all_traffics_c", "add_client", "onlines", "get_banlogs", "get_usage"}
  36. for _, d := range denied {
  37. if isClientSelfCallback(d) {
  38. t.Errorf("%q is an admin-only callback and must not be treated as per-user", d)
  39. }
  40. }
  41. }