Skip to content

cap_im_tg: bound accumulated Telegram HTTP response size#62

Open
orbisai0security wants to merge 1 commit into
espressif:masterfrom
orbisai0security:fix-heap-buffer-overflow-tg-http-resp
Open

cap_im_tg: bound accumulated Telegram HTTP response size#62
orbisai0security wants to merge 1 commit into
espressif:masterfrom
orbisai0security:fix-heap-buffer-overflow-tg-http-resp

Conversation

@orbisai0security
Copy link
Copy Markdown

@orbisai0security orbisai0security commented May 9, 2026

Summary

Adds an upper bound for accumulated Telegram HTTP response data in cap_im_tg_http_event_handler().

Motivation

The current handler grows the response buffer as data arrives. Although the buffer is reallocated before memcpy, an unexpectedly large response can still cause excessive memory growth or OOM on memory-constrained ESP devices.

This change adds a maximum response size check before appending new data, causing oversized responses to fail explicitly instead of continuing to grow the buffer.

Notes

This is defensive hardening / resource-exhaustion protection, not a direct heap buffer overflow fix. The previous description overstated the issue.

Changes

  • components/claw_capabilities/cap_im_platform/src/cap_im_tg.c

Verification

  • Build passes
  • Scanner re-scan confirms fix
  • LLM code review passed

Automated security fix by OrbisAI Security

Automated security fix generated by Orbis Security AI
@CLAassistant
Copy link
Copy Markdown

CLAassistant commented May 9, 2026

CLA assistant check
All committers have signed the CLA.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR addresses a critical heap buffer overflow risk in the Telegram IM capability by adding an upper bound to the accumulated HTTP response size during HTTP_EVENT_ON_DATA handling.

Changes:

  • Introduced a maximum allowed Telegram HTTP response size (CAP_IM_TG_MAX_RESP_SIZE).
  • Added a pre-append guard in cap_im_tg_http_event_handler() to reject responses that would exceed the configured maximum.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +141 to +143
if (resp->len + (size_t)event->data_len + 1 > CAP_IM_TG_MAX_RESP_SIZE) {
return ESP_ERR_NO_MEM;
}
Comment on lines +131 to +132
#define CAP_IM_TG_MAX_RESP_SIZE (64U * 1024U)

@laride
Copy link
Copy Markdown
Collaborator

laride commented May 26, 2026

Thanks for the report. I agree that bounding the accumulated HTTP response size is useful to avoid excessive memory growth / OOM. However, the current code reallocates the response buffer before memcpy, so this does not appear to be a direct heap buffer overflow in normal execution. Also, TLS certificate verification is configured via esp_crt_bundle_attach. From the current implementation, I’m not sure this should be characterized as a direct CWE-120 heap buffer overflow, but the proposed limit still looks like a useful defensive hardening improvement.

@orbisai0security
Copy link
Copy Markdown
Author

Thanks for taking a look. That makes sense. I agree with your assessment that the current implementation reallocates before memcpy, so this should not be described as a direct CWE-120 heap buffer overflow in normal execution.

I’m happy to reframe this as defensive hardening / resource-exhaustion protection instead: bounding the accumulated Telegram HTTP response size prevents unbounded response growth on a memory-constrained ESP device and makes the failure mode explicit rather than allowing repeated reallocations until OOM.

I’ll update the PR title/description to remove the “critical heap buffer overflow” and TLS/MITM wording, and describe this as a bounded-allocation hardening change. If 64 KiB is not the preferred limit, I’m also happy to adjust it to a project-appropriate constant or make it configurable.

@orbisai0security orbisai0security changed the title fix: at cap_im_tg in cap_im_tg.c cap_im_tg: bound accumulated Telegram HTTP response size May 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants