telegram: a 200 that is not the bot API envelope is not a send (V-615)
The sink parsed the response, and when the body did not unmarshal it fell through to the status check and returned nil on any 2xx. This box reaches telegram through a relay, and a relay that is up but cannot reach api.telegram.org answers 200 with a page of its own. That read as delivered: the dispatcher wrote a 'sent' outbox row and MarkSent restarted the repeat clock, so a sev4 alarm nobody received went quiet for a full interval. Only ok=true is a send now. The response cap moves from 4096 to 64KiB, because a truncated body no longer parses and would read as a failure, and error lines carry a 200-byte snippet instead of the relay's whole page. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
@@ -172,21 +172,49 @@ func (s *Sink) Send(ctx context.Context, d delivery.Sendable) error {
|
||||
return fmt.Errorf("telegramsink: sendMessage: %w", s.redact(err))
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
rb, _ := io.ReadAll(io.LimitReader(resp.Body, 4096))
|
||||
rb, _ := io.ReadAll(io.LimitReader(resp.Body, maxRespBytes))
|
||||
|
||||
// telegram returns 200 with ok=true on success; non-2xx with ok=false +
|
||||
// error_code + description on failure. parse the body either way so a 200
|
||||
// with ok=false (shouldn't happen, but the API reserves that) still surfaces.
|
||||
var tr telegramResp
|
||||
if jsonErr := json.Unmarshal(rb, &tr); jsonErr == nil && !tr.Ok {
|
||||
jsonErr := json.Unmarshal(rb, &tr)
|
||||
if jsonErr == nil && !tr.Ok {
|
||||
return fmt.Errorf("telegramsink: telegram returned error %d: %s", tr.ErrorCode, strings.TrimSpace(tr.Description))
|
||||
}
|
||||
if resp.StatusCode/100 != 2 {
|
||||
return fmt.Errorf("telegramsink: telegram returned %d: %s", resp.StatusCode, strings.TrimSpace(string(rb)))
|
||||
return fmt.Errorf("telegramsink: telegram returned %d: %s", resp.StatusCode, snippet(rb))
|
||||
}
|
||||
// A 2xx whose body is not the bot API's envelope did not come from the bot
|
||||
// API. The normal path here is the relay: this box reaches telegram through
|
||||
// an HTTP/SOCKS5 proxy, and a proxy that is up but cannot reach
|
||||
// api.telegram.org answers 200 with an HTML page of its own. Reading that as
|
||||
// a delivered message is the worst outcome the sink has — the dispatcher
|
||||
// writes a 'sent' outbox row, MarkSent restarts the repeat clock, and the
|
||||
// sev4 alarm that never arrived goes quiet for a whole interval. Only
|
||||
// ok=true is a send.
|
||||
if jsonErr != nil {
|
||||
return fmt.Errorf("telegramsink: telegram returned %d with a body that is not the bot API envelope (not a confirmed send): %s", resp.StatusCode, snippet(rb))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// maxRespBytes caps the response read — the body is wire-controlled and the
|
||||
// relay in front of it is not telegram. It is far above any sendMessage
|
||||
// envelope (a few hundred bytes; the result echoes one short away message),
|
||||
// because a truncated body no longer parses and now reads as a failed send.
|
||||
const maxRespBytes = 64 << 10
|
||||
|
||||
// snippet trims a response body down to something an error line can carry. A
|
||||
// relay's HTML page is measured in kilobytes and none of it belongs in the log.
|
||||
func snippet(rb []byte) string {
|
||||
s := strings.TrimSpace(string(rb))
|
||||
if len(s) > 200 {
|
||||
return s[:200] + "…"
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// sendMessageURL — the bot API path. the token is in the URL path
|
||||
// (https://api.telegram.org/bot<token>/sendMessage); telegram does not accept
|
||||
// it anywhere else. the URL is built per-send from the resolved base and never
|
||||
|
||||
Reference in New Issue
Block a user