Repository navigation
Description
Description
In lib/crewai/src/crewai/a2a/auth/server_schemes.py, SimpleTokenAuth and APIKeyServerAuth both allow empty credentials to pass authentication and compare secrets with != instead of a constant-time comparison:
SimpleTokenAuth(token="")coerces""intoSecretStr(""), which is truthy (if self.token:succeeds), so_get_expected_token()returns"". Similarly, whenAUTH_TOKEN=""is set in the environment,os.environ.get("AUTH_TOKEN")returns""(notNone). In both casesexpected is NoneisFalse, andawait auth.authenticate("")succeeds and returnsAuthenticatedUser(token="", scheme="simple_token").APIKeyServerAuth(api_key="")accepts an empty API key at construction, soawait auth.authenticate("")returnsAuthenticatedUser(token="", scheme="api_key").- Both schemes compare the incoming credential against the expected secret using
!=rather thanhmac.compare_digest.
Steps to Reproduce
import asyncio
import os
from crewai.a2a.auth.server_schemes import APIKeyServerAuth, SimpleTokenAuth
async def main() -> None:
os.environ["AUTH_TOKEN"] = ""
print(await SimpleTokenAuth().authenticate(""))
print(await SimpleTokenAuth(token="").authenticate(""))
print(await APIKeyServerAuth(api_key="").authenticate(""))
asyncio.run(main())Expected behavior
- Explicitly passing an empty
tokentoSimpleTokenAuthor an emptyapi_keytoAPIKeyServerAuthshould fail validation (similar toWebhookSignatureConfigin fix(a2a): require a secret for HMAC webhook signatures #9459). - An empty
AUTH_TOKENenvironment variable should be treated as unconfigured and reject authentication with HTTP 401 (Authentication not configured). - Secret comparisons in both schemes should use
hmac.compare_digest(on UTF-8 bytes so non-ASCII inputs do not raiseTypeError).
Screenshots/Code snippets
See reproduction snippet above.
Operating System
Windows 11
Python Version
3.12
crewAI Version
1.15.44 (main)
crewAI Tools Version
1.15.44 (main)
Virtual Environment
Venv
Evidence
Running the reproduction snippet prints three AuthenticatedUser instances with token="" instead of raising ValidationError / HTTPException(401).
Possible Solution
Reject empty secrets via @model_validator(mode="after") on SimpleTokenAuth (when token is not None) and APIKeyServerAuth, treat empty AUTH_TOKEN as unconfigured in _get_expected_token() / authenticate(), and compare secrets with hmac.compare_digest(provided.encode("utf-8"), expected.encode("utf-8")).
Additional context
This issue was authored with AI assistance (unable to apply the llm-generated label as an external contributor).
Activity