Skip to content

fix: handle data URI audio refs in audio preprocessing and truncate warning log#8715

Open
Foolllll-J wants to merge 2 commits into
AstrBotDevs:masterfrom
Foolllll-J:fix/audio-data-uri-handling
Open

fix: handle data URI audio refs in audio preprocessing and truncate warning log#8715
Foolllll-J wants to merge 2 commits into
AstrBotDevs:masterfrom
Foolllll-J:fix/audio-data-uri-handling

Conversation

@Foolllll-J

@Foolllll-J Foolllll-J commented Jun 10, 2026

Copy link
Copy Markdown
Contributor

Problem: When a user quotes (replies to) a voice message in group chat and @ the bot, the system includes the referenced audio in the LLM request. _resolve_audio_part receives a data:audio/wav;base64,... data URI for the audio, but _audio_ref_to_local_path does not handle the data: scheme — it only recognizes http:// and file:// prefixes. It falls through to return audio_ref, cleanup_paths, and subsequent Path(data_uri).read_bytes() crashes because a data URI is not a valid file path. The error handler then logs the full base64 string (potentially megabytes) as a WARNING, flooding the log.

Fix:

  1. Add a data: branch in _audio_ref_to_local_path that decodes the base64 payload, writes it to a temp file, and returns the file path for normal processing.
  2. Truncate audio_ref to 256 characters in the warning log to prevent log pollution.

Modifications / 改动点

  • astrbot/core/provider/sources/openai_source.py:
    • _audio_ref_to_local_path: added if audio_ref.startswith("data:") branch — regex-match data:audio/\w+;base64,..., decode base64, write to temp file, return path
    • _resolve_audio_part: truncated audio_ref to 256 chars in the logger.warning call
  • This is NOT a breaking change. / 这不是一个破坏性变更。

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

Before — full base64 dumped as WARNING (line truncated at 2000 chars, actual data is much longer):

[2026-06-10 21:20:06.319] [Core] [DBUG] [runners.tool_loop_agent_runner:614]: [BefCompact] messages -> [16] system,user,assistant,user,...,assistant,user,assistant,user
[2026-06-10 21:20:06.319] [Core] [DBUG] [runners.tool_loop_agent_runner:614]: [AftCompact] messages -> [6] system,user,assistant,user,assistant,user
[2026-06-10 21:20:06.320] [Core][WARN][v4.25.5] [sources.openai_source:387]: 音频 data:audio/wav;base64,IyFBTVIKDFLKPP71zfHMWG……
[2026-06-10 21:20:06.321] [Core][WARN][v4.25.5] [sources.openai_source:387]: 音频 data:audio/wav;base64,IyFBTVIKDGx4RvvXzbvbOW……

Afterdata: URI is correctly decoded, written to a temp WAV file, and passed to the LLM.

[2026-06-10 21:32:35.739] [Core] [DBUG] [pipeline.context_utils:95]: hook(OnLLMRequestEvent) -> astrbot - decorate_llm_req
[2026-06-10 21:32:35.741] [Core] [DBUG] [runners.base:64]: Agent state transition: AgentState.IDLE -> AgentState.RUNNING
[2026-06-10 21:32:35.741] [Core] [DBUG] [runners.tool_loop_agent_runner:614]: [BefCompact] messages -> [10] system,user,assistant,user,assistant,user,assistant,user,assistant,user
[2026-06-10 21:32:35.741] [Core] [DBUG] [runners.tool_loop_agent_runner:614]: [AftCompact] messages -> [6] system,user,assistant,user,assistant,user
[2026-06-10 21:32:42.444] [Core] [DBUG] [sources.openai_source:662]: completion: ChatCompletion(id='chatcmpl-202606101332373016003568268d9d6dRGHgjCB', choices=[Choice(finish_reason='stop', index=0, logprobs=None……

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

Handle data URI audio references during audio preprocessing and reduce verbosity of failure logs.

Bug Fixes:

  • Decode data:audio/...;base64,... URIs to temporary audio files so referenced voice messages can be processed without crashing.
  • Prevent exceptions caused by treating data URI audio references as filesystem paths during audio preprocessing.

Enhancements:

  • Truncate long audio reference strings in warning logs to avoid flooding logs with large base64 payloads.

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hey - I've found 1 issue, and left some high level feedback:

  • The data: URI handling in _audio_ref_to_local_path only matches audio/\w+ and will miss common MIME types like audio/x-wav or audio/webm;codecs=opus; consider relaxing the regex and/or defaulting to a generic suffix when the subtype is not a simple word.
  • When decoding and writing the data-URI audio in _audio_ref_to_local_path, it may be safer to wrap the base64 decode and file write in a try/except and fall back to the existing error-handling path so that malformed data URIs don't raise uncaught exceptions before _resolve_audio_part's try/except.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The `data:` URI handling in `_audio_ref_to_local_path` only matches `audio/\w+` and will miss common MIME types like `audio/x-wav` or `audio/webm;codecs=opus`; consider relaxing the regex and/or defaulting to a generic suffix when the subtype is not a simple word.
- When decoding and writing the data-URI audio in `_audio_ref_to_local_path`, it may be safer to wrap the base64 decode and file write in a `try/except` and fall back to the existing error-handling path so that malformed data URIs don't raise uncaught exceptions before `_resolve_audio_part`'s try/except.

## Individual Comments

### Comment 1
<location path="astrbot/core/provider/sources/openai_source.py" line_range="360-369" />
<code_context>

     async def _audio_ref_to_local_path(self, audio_ref: str) -> tuple[str, list[Path]]:
         cleanup_paths: list[Path] = []
+        if audio_ref.startswith("data:"):
+            m = re.match(r"^data:audio/(\w+);base64,(.+)$", audio_ref)
+            if m:
+                suffix = f".{m.group(1)}"
+                audio_bytes = base64.b64decode(m.group(2))
+                temp_dir = Path(get_astrbot_temp_path())
+                temp_dir.mkdir(parents=True, exist_ok=True)
+                target_path = temp_dir / f"provider_audio_{uuid.uuid4().hex}{suffix}"
+                target_path.write_bytes(audio_bytes)
+                cleanup_paths.append(target_path)
+                return str(target_path), cleanup_paths
         if audio_ref.startswith("http"):
             suffix = Path(urlparse(audio_ref).path).suffix or ".wav"
</code_context>
<issue_to_address>
**🚨 suggestion (security):** Consider guarding against very large data: URIs before base64 decoding

Decoding an unbounded `data:` URI directly into memory can be abused for DoS via excessive memory/CPU. Consider enforcing a maximum allowed payload size (e.g., check `len(m.group(2))` before `base64.b64decode` and reject oversized inputs) to make this path safer for untrusted input.

Suggested implementation:

```python
    async def _audio_ref_to_local_path(self, audio_ref: str) -> tuple[str, list[Path]]:
        cleanup_paths: list[Path] = []
        if audio_ref.startswith("data:"):
            m = re.match(r"^data:audio/(\w+);base64,(.+)$", audio_ref)
            if m:
                suffix = f".{m.group(1)}"
                base64_payload = m.group(2)

                # Guard against excessively large data: URIs before decoding
                # The base64 length is ~4/3 of the decoded size; this keeps decoded audio under a safe cap.
                max_base64_length = 8 * 1024 * 1024  # ~8MB base64, ~6MB decoded
                if len(base64_payload) > max_base64_length:
                    truncated = audio_ref[:256] if len(audio_ref) > 256 else audio_ref
                    logger.warning(
                        "音频 data: URI 过大,已拒绝。长度: %d,最大允许: %d,前缀: %s",
                        len(base64_payload),
                        max_base64_length,
                        truncated,
                    )
                    raise ValueError("data: URI payload too large")

                audio_bytes = base64.b64decode(base64_payload, validate=True)
                temp_dir = Path(get_astrbot_temp_path())
                temp_dir.mkdir(parents=True, exist_ok=True)
                target_path = temp_dir / f"provider_audio_{uuid.uuid4().hex}{suffix}"
                target_path.write_bytes(audio_bytes)
                cleanup_paths.append(target_path)
                return str(target_path), cleanup_paths
        if audio_ref.startswith("http"):

```

1. Ensure that `logger` is available in this module (it appears to be used later in this function; if not already defined/imported, a module-level logger should be configured).
2. If you have a shared configuration or constants module for size limits, consider replacing the hard-coded `max_base64_length` with a named constant imported from there to keep limits consistent across the codebase.
</issue_to_address>

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.

Comment on lines +360 to +369
if audio_ref.startswith("data:"):
m = re.match(r"^data:audio/(\w+);base64,(.+)$", audio_ref)
if m:
suffix = f".{m.group(1)}"
audio_bytes = base64.b64decode(m.group(2))
temp_dir = Path(get_astrbot_temp_path())
temp_dir.mkdir(parents=True, exist_ok=True)
target_path = temp_dir / f"provider_audio_{uuid.uuid4().hex}{suffix}"
target_path.write_bytes(audio_bytes)
cleanup_paths.append(target_path)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🚨 suggestion (security): Consider guarding against very large data: URIs before base64 decoding

Decoding an unbounded data: URI directly into memory can be abused for DoS via excessive memory/CPU. Consider enforcing a maximum allowed payload size (e.g., check len(m.group(2)) before base64.b64decode and reject oversized inputs) to make this path safer for untrusted input.

Suggested implementation:

    async def _audio_ref_to_local_path(self, audio_ref: str) -> tuple[str, list[Path]]:
        cleanup_paths: list[Path] = []
        if audio_ref.startswith("data:"):
            m = re.match(r"^data:audio/(\w+);base64,(.+)$", audio_ref)
            if m:
                suffix = f".{m.group(1)}"
                base64_payload = m.group(2)

                # Guard against excessively large data: URIs before decoding
                # The base64 length is ~4/3 of the decoded size; this keeps decoded audio under a safe cap.
                max_base64_length = 8 * 1024 * 1024  # ~8MB base64, ~6MB decoded
                if len(base64_payload) > max_base64_length:
                    truncated = audio_ref[:256] if len(audio_ref) > 256 else audio_ref
                    logger.warning(
                        "音频 data: URI 过大,已拒绝。长度: %d,最大允许: %d,前缀: %s",
                        len(base64_payload),
                        max_base64_length,
                        truncated,
                    )
                    raise ValueError("data: URI payload too large")

                audio_bytes = base64.b64decode(base64_payload, validate=True)
                temp_dir = Path(get_astrbot_temp_path())
                temp_dir.mkdir(parents=True, exist_ok=True)
                target_path = temp_dir / f"provider_audio_{uuid.uuid4().hex}{suffix}"
                target_path.write_bytes(audio_bytes)
                cleanup_paths.append(target_path)
                return str(target_path), cleanup_paths
        if audio_ref.startswith("http"):
  1. Ensure that logger is available in this module (it appears to be used later in this function; if not already defined/imported, a module-level logger should be configured).
  2. If you have a shared configuration or constants module for size limits, consider replacing the hard-coded max_base64_length with a named constant imported from there to keep limits consistent across the codebase.

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

Copy link
Copy Markdown
Contributor

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 support for processing base64-encoded data: URI audio references by decoding them and saving them to temporary files. It also truncates the logged audio reference in case of preprocessing failures to prevent bloated logs. The review feedback highlights a potential performance issue when running regex on large base64 payloads and suggests splitting the string first. Additionally, it requests adding unit tests to verify the new functionality.

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/provider/sources/openai_source.py

async def _audio_ref_to_local_path(self, audio_ref: str) -> tuple[str, list[Path]]:
cleanup_paths: list[Path] = []
if audio_ref.startswith("data:"):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

Please add corresponding unit tests to verify the handling of data: URI audio references, ensuring that decoding, temp file creation, and error handling work as expected.

References
  1. New functionality, such as handling attachments, should be accompanied by corresponding unit tests.

…nAI provider

Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
@dosubot dosubot Bot added the size:S This PR changes 10-29 lines, ignoring generated files. label Jun 11, 2026
@Foolllll-J Foolllll-J requested a review from Dt8333 June 11, 2026 06:02

@Dt8333 Dt8333 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks good.

@dosubot dosubot Bot added the lgtm This PR has been approved by a maintainer label Jun 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

lgtm This PR has been approved by a maintainer size:S This PR changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants