hermes - ✅(Solved) Fix WeCom inbound file decryption fails for urlsafe unpadded `aeskey` values [1 pull requests, 1 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
NousResearch/hermes-agent#11890Fetched 2026-04-18 05:58:28
View on GitHub
Comments
0
Participants
1
Timeline
1
Reactions
0
Author
Participants
Timeline (top)
cross-referenced ×1

Root Cause

The WeCom adapter used:

base64.b64decode(aes_key)

This assumes a standard Base64 string.

However, actual WeCom aeskey values may be:

  • URL-safe Base64 (- / _)
  • missing trailing = padding

This can lead to invalid decode results or wrong key length, which then breaks AES decryption for inbound file attachments.

Fix Action

Fix

Normalize the aeskey before decryption:

  • trim whitespace
  • require non-empty key
  • restore missing = padding
  • decode with base64.urlsafe_b64decode(...)

PR fix notes

PR #11899: fix(wecom): accept urlsafe unpadded inbound aeskey

Description (problem / solution / changelog)

Summary

  • normalize WeCom inbound aeskey values before decrypting downloaded attachments
  • accept urlsafe base64 and restore missing padding before decoding
  • add regression coverage for unpadded urlsafe keys and cached inbound media decryption

Fixes #11890

Testing

  • python3 -m pytest -o addopts='' tests/gateway/test_wecom.py -k 'decrypt_file_bytes_accepts_unpadded_urlsafe_aes_key or cache_media_decrypts_url_payload_before_writing or decrypt_file_bytes_round_trip'
  • python3 -m pytest -o addopts='' tests/gateway/test_wecom.py

Changed files

  • gateway/platforms/wecom.py (modified, +14/-1)
  • tests/gateway/test_wecom.py (modified, +18/-2)

Code Example

base64.b64decode(aes_key)

---

key = base64.b64decode(aes_key)

---

key = WeComAdapter._decode_aes_key(aes_key)
RAW_BUFFERClick to expand / collapse

Bug Description

WeCom inbound file attachments can fail to be received/processed because the callback aeskey is treated as standard Base64, while real WeCom payloads may provide it as URL-safe Base64 without = padding.

As a result, Hermes can successfully receive the callback and download the encrypted bytes, but fail during local decryption of the attachment.

Root Cause

The WeCom adapter used:

base64.b64decode(aes_key)

This assumes a standard Base64 string.

However, actual WeCom aeskey values may be:

  • URL-safe Base64 (- / _)
  • missing trailing = padding

This can lead to invalid decode results or wrong key length, which then breaks AES decryption for inbound file attachments.

Impact

Inbound attachments such as:

  • PDF
  • ZIP
  • TAR.GZ

may fail to decrypt and therefore fail to be cached/handed off correctly for downstream processing.

Fix

Normalize the aeskey before decryption:

  • trim whitespace
  • require non-empty key
  • restore missing = padding
  • decode with base64.urlsafe_b64decode(...)

Code Changes

gateway/platforms/wecom.py

  • added _decode_aes_key(aes_key: str) -> bytes
  • switched _decrypt_file_bytes(...) to use the normalized URL-safe decoder instead of base64.b64decode(...)

tests/gateway/test_wecom.py

  • added regression coverage for urlsafe unpadded aeskey
  • updated media test data to use a more realistic WeCom-style key format

Verification

Targeted tests passed:

  • test_decrypt_file_bytes_accepts_unpadded_urlsafe_aes_key
  • load_inbound_media_decrypts_encrypted_file

Result:

  • 2 passed

Minimal Diff Summary

Before:

key = base64.b64decode(aes_key)

After:

key = WeComAdapter._decode_aes_key(aes_key)

where _decode_aes_key(...) handles URL-safe decoding and missing padding.

extent analysis

TL;DR

To fix the issue with WeCom inbound file attachments failing to be received/processed, normalize the aeskey before decryption by trimming whitespace, requiring a non-empty key, restoring missing = padding, and decoding with base64.urlsafe_b64decode(...).

Guidance

  • Verify that the aeskey is being passed correctly from WeCom and that it matches the expected URL-safe Base64 format without = padding.
  • Update the gateway/platforms/wecom.py file to include the _decode_aes_key(aes_key: str) -> bytes function, which handles the normalization of the aeskey.
  • Switch the _decrypt_file_bytes(...) function to use the normalized URL-safe decoder instead of base64.b64decode(...).
  • Add regression tests to tests/gateway/test_wecom.py to cover the scenario with a urlsafe unpadded aeskey.

Example

The updated code should look like this:

def _decode_aes_key(aes_key: str) -> bytes:
    # trim whitespace and require non-empty key
    aes_key = aes_key.strip()
    if not aes_key:
        raise ValueError("AES key cannot be empty")
    
    # restore missing = padding
    aes_key += "=" * (-len(aes_key) % 4)
    
    # decode with base64.urlsafe_b64decode(...)
    return base64.urlsafe_b64decode(aes_key)

# ...

key = WeComAdapter._decode_aes_key(aes_key)

Notes

This fix assumes that the aeskey is always provided in URL-safe Base64 format without = padding. If the format can vary, additional error handling may be necessary.

Recommendation

Apply the workaround by normalizing the aeskey before decryption, as this will ensure that the decryption process works correctly even with URL-safe Base64 keys without = padding.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING