#3368·CowAgent

[Security] a non-ASCII credential returns HTTP 500 instead of 401 on five console auth paths (hmac.compare_digest raises TypeError)

Author: aniruddhaadak80Created Sep 29, 2026Updated Sep 29, 2026

English is recommended so global developers can help. 推荐使用英文提交,谢谢 ❤️

  • I'm on the latest version and searched existing issues (incl. closed) — no duplicate.

Environment

Version: current master (commit 08f64f7) OS: any — platform-independent Python: 3.11 / 3.13 (CI matrix) Install: any Config: web_password set (i.e. the console is password-protected — the normal state for a non-loopback bind)

What happened?

A wrong credential produces a 500 instead of a 401, on endpoints that need no authentication to reach.

hmac.compare_digest() raises TypeError when a str operand contains non-ASCII characters:

python
>>> import hmac
>>> hmac.compare_digest("é" * 64, "a" * 64)
TypeError: comparing strings with non-ASCII characters is not supported

Every signature check in the console passes it a value taken straight off the request — a cookie, an Authorization header, a JSON field, a URL path segment. So a client that sends a single non-ASCII character does not fail authentication, it crashes the request.

There are five such call sites, four of them reachable without authenticating:

Site Value comes from Reachable unauthenticated?
channel/web/api/auth.py:113 — POST /auth/login password JSON field yes
channel/web/api/files.py:49 — /preview/<token>/… URL path segment yes
channel/web/api/openai_compat.py:46 — OpenAI-compatible API Authorization header yes (with the API enabled)
channel/web/core/_common.py:73 — _desktop_token_matches X-Cow-Desktop-Token header yes
channel/web/core/_common.py:272 — _verify_auth_token cookie / bearer / query token yes, on every protected endpoint

channel/feishu/feishu_channel.py:2327 has the same shape on the Feishu callback, guarded only by isinstance(callback_token, str) — which a non-ASCII string passes.

Steps to reproduce

python
>>> from channel.web.core import _common
>>> ts_hex, _ = "6d1a2b3c4e5f", "ignored"
>>> _common._verify_auth_token(f"{ts_hex}.{'é' * 64}")
TypeError: comparing strings with non-ASCII characters is not supported

Expected: False — the token simply does not verify, and the request is answered 401 Unauthorized (which is what _require_auth() is written to do, and what its own log line at _common.py:331 says happened).

Actual: an unhandled TypeError, so every protected endpoint returns HTTP 500 for a malformed token instead of 401. Concretely, against a running instance with web_password set:

GET /api/sessions?token=<current-ts-hex>.%C3%A9%C3%A9%C3%A9...   ->  500
POST /auth/login  {"password": "é"}                            ->  500   (instead of "Wrong password")
GET /preview/<body>.<non-ascii-sig>/file.html                  ->  500

The POST /auth/login case is the sharpest one: the login form is the endpoint a user hits because their credential was rejected, so the failure mode is a 500 with a traceback rather than "Wrong password".

Why the obvious fix is not encode()

left.encode() alone is not airtight. json.loads happily produces lone surrogates from input like "\ud800", and encoding those raises UnicodeEncodeError — reintroducing exactly the same 500. The comparison needs errors="surrogatepass", or a shape check first.

Also note the fix must not restrict the comparison to ASCII: web_password is user-configured and a non-ASCII password is legitimate, so auth.py genuinely needs to compare non-ASCII strings successfully.

Suggested fix

One shared helper in common/utils.py that compares the UTF-8 encodings with surrogatepass, used at all five sites. It keeps the comparison constant-time and makes any malformed value simply not match.

channel/feishu/feishu_channel.py is a separate subsystem and should not import from the web layer, so that one can be a byte comparison inline — a good candidate for a follow-up PR to keep this diff reviewable.

Logs

Not applicable — the failure is a synchronous TypeError raised before any logging, reproducible directly as shown above.