-
Notifications
You must be signed in to change notification settings - Fork 52.8k
fix(wecom): support pic_url and fix AES key base64 padding for image … #12390
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
zogwei
wants to merge
1
commit into
NousResearch:main
Choose a base branch
from
zogwei:fix/wecom-image-decryption
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -64,6 +64,7 @@ | |
| MessageEvent, | ||
| MessageType, | ||
| SendResult, | ||
| _looks_like_image, | ||
| cache_document_from_bytes, | ||
| cache_image_from_bytes, | ||
| ) | ||
|
|
@@ -72,6 +73,21 @@ | |
|
|
||
| DEFAULT_WS_URL = "wss://openws.work.weixin.qq.com" | ||
|
|
||
|
|
||
| def _decode_wecom_aes_key(aes_key: str) -> bytes: | ||
| """Decode WeCom AES key (43-char base64 without padding -> 32 bytes).""" | ||
| payload = str(aes_key or "").strip() | ||
| if not payload: | ||
| raise ValueError("aes_key is empty") | ||
| # WeCom encoding_aeskey is 43 chars, missing the trailing '=' padding | ||
| # that standard base64 requires. Add it back. | ||
| padded = payload + "=" * ((4 - len(payload) % 4) % 4) | ||
| key = base64.b64decode(padded) | ||
| if len(key) != 32: | ||
| raise ValueError(f"Invalid WeCom AES key length: expected 32 bytes, got {len(key)}") | ||
| return key | ||
|
|
||
|
|
||
| APP_CMD_SUBSCRIBE = "aibot_subscribe" | ||
| APP_CMD_CALLBACK = "aibot_msg_callback" | ||
| APP_CMD_LEGACY_CALLBACK = "aibot_callback" | ||
|
|
@@ -729,8 +745,9 @@ async def _cache_media(self, kind: str, media: Dict[str, Any]) -> Optional[Tuple | |
| filename = str(media.get("filename") or media.get("name") or "wecom_file") | ||
| return cache_document_from_bytes(raw, filename), mimetypes.guess_type(filename)[0] or "application/octet-stream" | ||
|
|
||
| url = str(media.get("url") or "").strip() | ||
| url = str(media.get("url") or media.get("pic_url") or "").strip() | ||
| if not url: | ||
| logger.debug("[%s] No url/pic_url found in inbound %s media", self.name, kind) | ||
| return None | ||
|
|
||
| try: | ||
|
|
@@ -741,10 +758,19 @@ async def _cache_media(self, kind: str, media: Dict[str, Any]) -> Optional[Tuple | |
|
|
||
| aes_key = str(media.get("aeskey") or "").strip() | ||
| if aes_key: | ||
| decrypted = None | ||
| try: | ||
| raw = self._decrypt_file_bytes(raw, aes_key) | ||
| decrypted = self._decrypt_file_bytes(raw, aes_key) | ||
| except Exception as exc: | ||
| logger.debug("[%s] Failed to decrypt %s from %s: %s", self.name, kind, url, exc) | ||
| logger.debug("[%s] Primary decrypt failed for %s, trying variants: %s", self.name, kind, exc) | ||
|
|
||
| if decrypted is None: | ||
| decrypted = self._try_decrypt_variants(raw, aes_key, kind) | ||
|
|
||
| if decrypted is not None: | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| raw = decrypted | ||
| else: | ||
| logger.debug("[%s] All decryption variants failed for %s", self.name, kind) | ||
| return None | ||
|
|
||
| content_type = str(headers.get("content-type") or "").split(";", 1)[0].strip() or "application/octet-stream" | ||
|
|
@@ -997,12 +1023,8 @@ def _raise_for_wecom_error(cls, response: Dict[str, Any], operation: str) -> Non | |
| def _decrypt_file_bytes(encrypted_data: bytes, aes_key: str) -> bytes: | ||
| if not encrypted_data: | ||
| raise ValueError("encrypted_data is empty") | ||
| if not aes_key: | ||
| raise ValueError("aes_key is required") | ||
|
|
||
| key = base64.b64decode(aes_key) | ||
| if len(key) != 32: | ||
| raise ValueError(f"Invalid WeCom AES key length: expected 32 bytes, got {len(key)}") | ||
| key = _decode_wecom_aes_key(aes_key) | ||
|
|
||
| try: | ||
| from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes | ||
|
|
@@ -1021,6 +1043,91 @@ def _decrypt_file_bytes(encrypted_data: bytes, aes_key: str) -> bytes: | |
|
|
||
| return decrypted[:-pad_len] | ||
|
|
||
| @staticmethod | ||
| def _try_decrypt_variants(encrypted_data: bytes, aes_key: str, kind: str = "media") -> Optional[bytes]: | ||
| """Try multiple AES decryption strategies and return the best result.""" | ||
| if not encrypted_data or not aes_key: | ||
| return None | ||
|
|
||
| try: | ||
| key = _decode_wecom_aes_key(aes_key) | ||
| except Exception: | ||
| return None | ||
|
|
||
| try: | ||
| from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes | ||
| except ImportError: | ||
| return None | ||
|
|
||
| def _decrypt_cbc(data: bytes, key_bytes: bytes, iv: bytes) -> bytes: | ||
| cipher = Cipher(algorithms.AES(key_bytes), modes.CBC(iv)) | ||
| decryptor = cipher.decryptor() | ||
| decrypted = decryptor.update(data) + decryptor.finalize() | ||
| pad_len = decrypted[-1] | ||
| if 1 <= pad_len <= 32 and decrypted.endswith(bytes([pad_len]) * pad_len): | ||
| return decrypted[:-pad_len] | ||
| return decrypted | ||
|
|
||
| def _decrypt_ecb(data: bytes, key_bytes: bytes) -> bytes: | ||
| cipher = Cipher(algorithms.AES(key_bytes), modes.ECB()) | ||
| decryptor = cipher.decryptor() | ||
| decrypted = decryptor.update(data) + decryptor.finalize() | ||
| pad_len = decrypted[-1] | ||
| if 1 <= pad_len <= 32 and decrypted.endswith(bytes([pad_len]) * pad_len): | ||
| return decrypted[:-pad_len] | ||
| return decrypted | ||
|
|
||
| variants = [] | ||
|
|
||
| if len(key) == 32: | ||
| variants.append(("AES-256-CBC(iv=key[:16])", lambda d: _decrypt_cbc(d, key, key[:16]))) | ||
| variants.append(("AES-256-ECB", lambda d: _decrypt_ecb(d, key))) | ||
| variants.append(("AES-128-CBC(iv=key[:16])", lambda d: _decrypt_cbc(d, key[:16], key[:16]))) | ||
| variants.append(("AES-128-ECB", lambda d: _decrypt_ecb(d, key[:16]))) | ||
| elif len(key) == 16: | ||
| variants.append(("AES-128-CBC(iv=key)", lambda d: _decrypt_cbc(d, key, key))) | ||
| variants.append(("AES-128-ECB", lambda d: _decrypt_ecb(d, key))) | ||
|
|
||
| # Weixin-style: key may be hex-encoded inside base64 | ||
| if len(key) == 32: | ||
| try: | ||
| hex_text = key.decode("ascii", errors="ignore") | ||
| if hex_text and all(ch in "0123456789abcdefABCDEF" for ch in hex_text): | ||
| hex_bytes = bytes.fromhex(hex_text) | ||
| if len(hex_bytes) == 16: | ||
| variants.append(("AES-128-CBC(hex, iv=hex)", lambda d: _decrypt_cbc(d, hex_bytes, hex_bytes))) | ||
| variants.append(("AES-128-ECB(hex)", lambda d: _decrypt_ecb(d, hex_bytes))) | ||
| except Exception: | ||
| pass | ||
|
|
||
| best_result = None | ||
| best_score = -1 | ||
|
|
||
| for name, decrypt_fn in variants: | ||
| try: | ||
| decrypted = decrypt_fn(encrypted_data) | ||
| score = 0 | ||
| if _looks_like_image(decrypted): | ||
| score = 100 | ||
| elif len(decrypted) > 100: | ||
| # Heuristic: avoid error pages / XML | ||
| head = decrypted[:200].lower() | ||
| if b"<html" not in head and b"<?xml" not in head and b"error" not in head: | ||
| score = 1 | ||
|
|
||
| if score > best_score: | ||
| best_score = score | ||
| best_result = decrypted | ||
| logger.debug( | ||
| "[%s] Decryption success with %s for %s (score=%d, header=%s)", | ||
| "WeCom", name, kind, score, decrypted[:8].hex(), | ||
| ) | ||
| except Exception as exc: | ||
| logger.debug("Decryption failed with %s for %s: %s", name, kind, exc) | ||
| continue | ||
|
|
||
| return best_result | ||
|
|
||
| async def _download_remote_bytes( | ||
| self, | ||
| url: str, | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This fallback only helps if
_extract_media()has already passed a media dictionary to_cache_media(). The PR does not add a reference for a top-level AI Botpic_url, so that reported payload shape still never reaches this line. Construct the image reference in_extract_media()and cover it with a regression test.