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

@ -596,7 +596,10 @@ def _rpc_server_loop(
continue
if not rpc_token or not secrets.compare_digest(
str(request.get("token") or ""), rpc_token
# Compare as bytes: compare_digest raises TypeError on a
# str with non-ASCII characters, and the token comes from
# sandbox-script-supplied JSON.
str(request.get("token") or "").encode(), rpc_token.encode()
):
resp = json.dumps({"error": "Unauthorized RPC request"})
conn.sendall((resp + "\n").encode())
@ -881,7 +884,10 @@ def _rpc_poll_loop(
continue
if not rpc_token or not secrets.compare_digest(
str(request.get("token") or ""), rpc_token
# Compare as bytes: compare_digest raises TypeError on a
# str with non-ASCII characters, and the token comes from
# sandbox-script-supplied JSON.
str(request.get("token") or "").encode(), rpc_token.encode()
):
logger.debug("Unauthorized RPC request in %s", req_file)
env.execute(f"rm -f {quoted_req_file}", cwd="/", timeout=5)