mirror of
https://github.com/tiennm99/tiennm99bot.git
synced 2026-10-19 02:14:57 +00:00
refactor: audit-driven hygiene pass across modules and infra
Concurrency - lolschedule: serialize subscriber Get→mutate→Put via state.subscribersMu; the single-slot list was previously losing writes under concurrent /lolschedule_subscribe. - trading: PriceClient memoises its default *http.Client so /trade_stats reuses TLS connections across held tickers. Observability - server/log_middleware: defer the req log line and recover panics so a panicking cron handler still emits the structured req entry CloudWatch filters on for 5xx alerting. - server/router (cron): inner recover with cron-name context captures the panicking job before the middleware's safety net does. - telegram/webhook: rune-safe truncation in dispatch logs — Vietnamese, Korean, and emoji previews no longer ship as garbled bytes. - lolschedule/api_client: same rune-safe fix for error-body log truncation. - telegram/webhook: gate the post-recover WriteHeader(200) so a panicking handler that already touched w doesn't trigger superfluous-WriteHeader. Correctness - twentyq: clearGame error during solved-relaunch is logged instead of silently swallowed (was a permanent deadlock vector on KV failure). - misc /mstats: KV read failure replies "Could not load stats. Try again later." to the user instead of returning into the dispatcher; matches the pattern other modules use. - migrate_cf_data trading-audit-dump: surface f.Close error so a truncated JSONL never passes silently as a complete audit dump. Operator ergonomics - migrate_cf_data (all 4 subcommands): signal.NotifyContext for SIGINT / SIGTERM. Ctrl-C mid-Scan now propagates cleanly instead of leaving a half-converted DynamoDB table. - ai/ratelimit: doc the Lambda-recycle memory bound to match keylock.Map so a future reviewer doesn't re-flag the unbounded map. I/O-changing (user-approved) - lolschedule daily push auto-prunes subscribers whose Telegram error matches a terminal marker (blocked / deactivated / chat gone). Transient errors keep the chat on the list. Subscribe message updated to mention the auto-cleanup. - twentyq seed pool grown 50 → 178; repeat-collision threshold moves from ~9 plays to ~17 (birthday paradox). - util /info flipped Public → Protected — chat/thread/sender IDs are no longer enumerable by every group member. - cmd/server WriteTimeout 6min → 75s (cron 60s + 15s slack). No-op on Lambda; matters only for local non-Lambda runs. - webhook + cron rejection paths drop response bodies (no fingerprintable text for internet scanners hitting the public Function URL). Status codes preserved for CloudWatch metrics; structured log lines carry the rejection reason for operator triage. Tests added: TestTruncateRunes, TestRunDailyPush_PrunesDeadSubscribers, TestIsTerminalSendError, TestInfo_DeniedToNonOwner, TestInfo_DeniedToChannelMessageNoFrom, plus owner-allowed counterparts.
This commit is contained in:
1 parent
16c9cce91d
commit
31f4e22c4d
19 files changed
+451
-61
No files matched your search
@@ -28,8 +28,9 @@ func installUtil(t *testing.T, ownerID int64) *testutil.RecordingBot {
|
||||
return rb
|
||||
}
|
||||
|
||||
func TestInfo_PrivateChat(t *testing.T) {
|
||||
rb := installUtil(t, 0)
|
||||
func TestInfo_PrivateChat_OwnerAllowed(t *testing.T) {
|
||||
// /info is Protected — sender must be the bot owner to get a reply.
|
||||
rb := installUtil(t, 42)
|
||||
rb.Bot.ProcessUpdate(context.Background(), testutil.NewPrivateMessage(42, "/info"))
|
||||
|
||||
got := rb.LastSent().Text()
|
||||
@@ -40,8 +41,8 @@ func TestInfo_PrivateChat(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestInfo_GroupChat(t *testing.T) {
|
||||
rb := installUtil(t, 0)
|
||||
func TestInfo_GroupChat_OwnerAllowed(t *testing.T) {
|
||||
rb := installUtil(t, 7)
|
||||
rb.Bot.ProcessUpdate(context.Background(), testutil.NewGroupMessage(-100, 7, "/info"))
|
||||
|
||||
got := rb.LastSent().Text()
|
||||
@@ -52,13 +53,22 @@ func TestInfo_GroupChat(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestInfo_ChannelMessageNoFrom(t *testing.T) {
|
||||
func TestInfo_DeniedToNonOwner(t *testing.T) {
|
||||
// Non-admin sender → Protected denies silently (no reply, no leak of
|
||||
// the command's existence).
|
||||
rb := installUtil(t, 999)
|
||||
rb.Bot.ProcessUpdate(context.Background(), testutil.NewGroupMessage(-100, 7, "/info"))
|
||||
if calls := rb.Sent(); len(calls) != 0 {
|
||||
t.Errorf("non-owner /info replied: %+v", calls)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInfo_DeniedToChannelMessageNoFrom(t *testing.T) {
|
||||
// Channel posts have no From. Protected denies (no sender to check).
|
||||
rb := installUtil(t, 0)
|
||||
rb.Bot.ProcessUpdate(context.Background(), testutil.NewChannelMessage(-200, "/info"))
|
||||
|
||||
got := rb.LastSent().Text()
|
||||
if !strings.Contains(got, "sender id: n/a") {
|
||||
t.Errorf("info channel reply missing 'sender id: n/a'; got %q", got)
|
||||
if calls := rb.Sent(); len(calls) != 0 {
|
||||
t.Errorf("channel-post /info replied: %+v", calls)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -15,8 +15,13 @@ import (
|
||||
// IDs, with "n/a" fallbacks. Used to debug bot routing in groups + topics.
|
||||
func infoCommand() modules.Command {
|
||||
return modules.Command{
|
||||
Name: "info",
|
||||
Visibility: modules.VisibilityPublic,
|
||||
Name: "info",
|
||||
// Protected (not Public) because the response exposes internal
|
||||
// routing IDs — chat id, thread id, sender id. Useful for admins
|
||||
// debugging group/topic routing; not something every group member
|
||||
// should be able to enumerate. Non-admins see no response at all
|
||||
// (Visibility denies are silent — see dispatcher.go:31).
|
||||
Visibility: modules.VisibilityProtected,
|
||||
Description: "Show chat id, thread id, and sender id (debug helper)",
|
||||
Handler: func(ctx context.Context, b *bot.Bot, update *models.Update) error {
|
||||
msg := update.Message
|
||||
|
||||
Reference in new issue
Block a user