Repository navigation
fix: redact bot token from Telegram HTTP errors (#65) - #67
Merged
Merged
Conversation
net/http reports transport failures as *url.Error containing the full request URL, which embeds the bot token. The agent logs these errors, so any network failure wrote the token to the journal in plain text. Sanitize errors in Client.call: rewrite url.Error.URL in place (keeping the error type, Timeout() and errors.Is/As chains intact) and fall back to a redacting wrapper if the token still appears in the message.
|
Important
This repository does not receive automatic reviews because it has fewer than 10 stars. ⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Advanced Run ID: Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What & why
Fixes #65.
telegram.Newputs the bot token inBaseURL(https://api.telegram.org/bot<TOKEN>). When a request fails (DNS, dial, TLS, timeout, cancelled context),net/httpreturns a*url.Errorwhose message contains the full request URL.Client.callwrapped that error and the agent logged it (getUpdates,push failed,edit, …), so any network hiccup wrote the token to journald in plain text — repeatedly, sincebotLoopretries every 5s.Every Bot API request goes through
Client.call, so the fix lives there and covers all current and future callers without touchinginternal/agent:redact()replacesbot<digits>:<token>withbot<redacted>via a regex (not tied toBaseURL).sanitizeErr()rewritesurl.Error.URLin place, so the concrete type,Timeout()anderrors.Is(err, context.Canceled)keep working. If the token still shows up anywhere in the message, it returns a wrapper with the redacted text andUnwrap()pointing to the original error.NewRequestWithContextandHTTP.Doerror paths. The success path doesn't change, and the regex only runs when a request fails.Example log line after the fix:
How it was tested
go vet ./...clean,gofmt -l .empty,go test -race ./...all green (Go 1.26).internal/telegram/telegram_test.go:SendMessage,EditMessageKB,AnswerCallback,DeleteMessages,SetMyCommands,GetUpdates): no token in%v/%+v,bot<redacted>and method name present.errors.As(*url.Error)still works and itsURLis clean.errors.Is(err, context.Canceled)still holds.url.Error.Timeout()still true.log.SetOutputhas no token.redact(multiple occurrences, no-op on token-free strings) and for thesanitizeErrfallback wrapper (errors.Ispreserved).sanitizeErrcalls temporarily removed, 5 of the new tests fail; with the fix restored, they pass.Checklist
go test ./...passesgo vet ./...is cleangofmt-formattedNote for operators: if your agent has logged Telegram network errors before this fix, the token may already be in your journal — rotate it with @Botfather.