fix(security): widen non-ASCII compare_digest crash fix to all sibling sites

Same bug class as the salvaged #65305/#65307: hmac.compare_digest (and
secrets.compare_digest) raise TypeError when given a str containing
non-ASCII characters, and these call sites feed it raw request input.
Compare as UTF-8 bytes everywhere:

- gateway/platforms/msgraph_webhook.py: clientState from request body
- gateway/platforms/whatsapp_cloud.py: hub.verify_token query param +
  X-Hub-Signature-256 header (comment claimed 'works on str' — it
  doesn't for non-ASCII)
- plugins/platforms/feishu: verification token + x-lark-signature
- plugins/platforms/raft: bridge token header
- plugins/platforms/line: X-Line-Signature
- plugins/platforms/sms: X-Twilio-Signature
- tools/code_execution_tool.py: sandbox RPC token (both loops)

Regression tests for the two gateway-core sites (msgraph, whatsapp).
This commit is contained in:
Teknium 2026-07-16 06:47:36 -07:00
parent 4ccb232af9
commit a6d9d1d2cf
9 changed files with 76 additions and 13 deletions

View file

@ -3512,7 +3512,11 @@ class FeishuAdapter(BasePlatformAdapter):
if self._verification_token:
header = payload.get("header") or {}
incoming_token = str(header.get("token") or payload.get("token") or "")
if not incoming_token or not hmac.compare_digest(incoming_token, self._verification_token):
# Compare as bytes: compare_digest raises TypeError on a str with
# non-ASCII characters, and the token comes from the request body.
if not incoming_token or not hmac.compare_digest(
incoming_token.encode(), self._verification_token.encode()
):
logger.warning("[Feishu] Webhook rejected: invalid verification token from %s", remote_ip)
self._record_webhook_anomaly(remote_ip, "401-token")
return web.Response(status=401, text="Invalid verification token")
@ -3575,7 +3579,9 @@ class FeishuAdapter(BasePlatformAdapter):
body_str = body_bytes.decode("utf-8", errors="replace")
content = f"{timestamp}{nonce}{self._encrypt_key}{body_str}"
computed = hashlib.sha256(content.encode("utf-8")).hexdigest()
return hmac.compare_digest(computed, signature)
# Compare as bytes: compare_digest raises TypeError on a str with
# non-ASCII characters, and the signature is a raw request header.
return hmac.compare_digest(computed.encode(), signature.encode())
except Exception:
logger.debug("[Feishu] Signature verification raised an exception", exc_info=True)
return False