Skip to content

fix: handle missing base64 padding in WeCom aes_key decryption - #18654

Closed
highland0971 wants to merge 4 commits into
NousResearch:mainfrom
highland0971:fix-wecom-base64-padding
Closed

fix: handle missing base64 padding in WeCom aes_key decryption#18654
highland0971 wants to merge 4 commits into
NousResearch:mainfrom
highland0971:fix-wecom-base64-padding

Conversation

@highland0971

Copy link
Copy Markdown

Problem

WeCom AI Bot sends image messages with an aes_key that may lack standard base64 padding (missing trailing '=' characters). This causes base64.b64decode() to fail with Incorrect padding error, preventing image decryption.

Solution

Add padding validation before decoding the aes_key. Base64 strings must have length that is a multiple of 4 characters. The fix automatically appends the required '=' padding characters.

Changes

File: gateway/platforms/wecom.py
Method: _decrypt_file_bytes() (around line 1013)

# WeCom aes_key may lack standard base64 padding (missing trailing '=')
# Add padding if needed: base64 strings must be multiple of 4 chars
missing_padding = len(aes_key) % 4
if missing_padding:
    aes_key = aes_key + '=' * (4 - missing_padding)
key = base64.b64decode(aes_key)

Testing

Verified on production WeCom integration:

  • Sent multiple test images via WeCom AI Bot
  • Images successfully decrypted and cached
  • Visual analysis of images confirmed correct decryption

Before fix: Incorrect padding error in logs
After fix: Images processed successfully (JPEG format verified)

Impact

  • Low risk: Only affects WeCom image decryption flow
  • No breaking changes to existing functionality
  • Handles edge case that WeCom API exhibits in practice

highland0971 and others added 4 commits April 28, 2026 11:16
- feat(feishu): add markdown table rendering support
- fix(feishu): silent message drop handling
- fix(feishu): ensure replies stay inside topic threads in group chats
- Set NO_COLOR=1 env var to disable colors in agent-browser CLI
- Add fallback regex to strip any remaining ANSI sequences
- Fixes JSON parse failures from escape codes like \x1b[A (cursor up)
Previous regex \x1b/[a-z]* was greedy and consumed trailing letters.
Fixed to \x1b/ which only matches the ESC+/ sequence.

Example: \x1b/tabs now correctly becomes 'tabs' instead of ''.

Also expanded pattern to handle:
- CSI sequences with private mode params (\x1b[?25h)
- OSC sequences (\x1b]...BEL)
- Single ESC+letter sequences (\x1bM)
- Non-standard ESC+/ sequences
@alt-glitch alt-glitch added type/bug Something isn't working P2 Medium — degraded but workaround exists platform/wecom WeCom / WeChat Work adapter comp/gateway Gateway runner, session dispatch, delivery labels May 2, 2026
@alt-glitch

Copy link
Copy Markdown
Collaborator

Likely duplicate of #14888 and #14580 — same root cause (unpadded base64 aes_key in WeCom media decrypt) and same fix (pad to multiple of 4). Also overlaps with #17375 which handles multiple AES key formats. Note: this PR touches files beyond wecom.py (feishu.py, run.py, stream_consumer.py, browser_tool.py) which seems unrelated to the stated fix.

@alt-glitch

Copy link
Copy Markdown
Collaborator

duplicate

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp/gateway Gateway runner, session dispatch, delivery P2 Medium — degraded but workaround exists platform/wecom WeCom / WeChat Work adapter type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants