Skip to content

feat(components): override repr_args to truncate long base64 fields for safe logging#8591

Open
KBVsent wants to merge 1 commit into
AstrBotDevs:masterfrom
KBVsent:feat/safe-base64-logging
Open

feat(components): override repr_args to truncate long base64 fields for safe logging#8591
KBVsent wants to merge 1 commit into
AstrBotDevs:masterfrom
KBVsent:feat/safe-base64-logging

Conversation

@KBVsent
Copy link
Copy Markdown
Contributor

@KBVsent KBVsent commented Jun 5, 2026

When a message component carrying base64-encoded media (e.g. an Image built via fromBase64) is logged or appears in a traceback, pydantic's default repr() dumps the entire base64 string into the log. A single image can flood the logs with thousands of characters, making them unreadable and bloating log files. This was observed on the Discord adapter (logger.debug(f"... {image_component}")) but affects any place that stringifies a component.

Modifications / 改动点

  • astrbot/core/message/components.py: Override __repr_args__ on the BaseMessageComponent base class to truncate long / base64 string field values in the repr only. Fields starting with base64:// render as base64://<N chars>, and any other string over 64 chars is truncated with a length suffix. This is purely a display-layer change — it wraps pydantic's default __repr_args__ and never mutates the stored data, so serialization (toDict), sending, and file conversion still see the full payload. Because it lives on the base class, all components (Image/Record/Video/File, etc.) are covered automatically with no per-call-site handling.
  • This is NOT a breaking change. / 这不是一个破坏性变更。

Screenshots or Test Results / 运行截图或测试结果

Before:
HapiGo_2026-06-05_01 18 42
After:
HapiGo_2026-06-05_01 17 57


Checklist / 检查清单

  • 😊 If there are new features added in the PR, I have discussed it with the authors through issues/emails, etc.
    / 如果 PR 中有新加入的功能,已经通过 Issue / 邮件等方式和作者讨论过。

  • 👀 My changes have been well-tested, and "Verification Steps" and "Screenshots" have been provided above.
    / 我的更改经过了良好的测试,并已在上方提供了“验证步骤”和“运行截图”

  • 🤓 I have ensured that no new dependencies are introduced, OR if new dependencies are introduced, they have been added to the appropriate locations in requirements.txt and pyproject.toml.
    / 我确保没有引入新依赖库,或者引入了新依赖库的同时将其添加到 requirements.txtpyproject.toml 文件相应位置。

  • 😮 My changes do not introduce malicious code.
    / 我的更改没有引入恶意代码。

Summary by Sourcery

Enhancements:

  • Override BaseMessageComponent repr to truncate base64 and overly long string fields while preserving full underlying data for normal usage.

@dosubot dosubot Bot added size:S This PR changes 10-29 lines, ignoring generated files. area:core The bug / feature is about astrbot's core, backend labels Jun 5, 2026
Copy link
Copy Markdown
Contributor

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

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

Hey - I've left some high level feedback:

  • Consider replacing the hardcoded 9 in len(value) - 9 with len("base64://") or a named constant to avoid magic numbers and keep the logic robust if the scheme prefix ever changes.
  • You may want to lift max_len = 64 to a class-level constant or configuration option so that truncation behaviour can be adjusted or reused consistently across components.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Consider replacing the hardcoded `9` in `len(value) - 9` with `len("base64://")` or a named constant to avoid magic numbers and keep the logic robust if the scheme prefix ever changes.
- You may want to lift `max_len = 64` to a class-level constant or configuration option so that truncation behaviour can be adjusted or reused consistently across components.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request adds a __repr_args__ method to BaseMessageComponent to truncate long strings and base64-encoded values, preventing log pollution during debugging. The reviewer pointed out that the 64-character limit is too aggressive for general strings like URLs and plain text, which could hinder debugging. They suggested increasing this limit to 1024 characters and adding support for truncating data: URIs containing base64 data.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread astrbot/core/message/components.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:core The bug / feature is about astrbot's core, backend size:S This PR changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant