Skip to content

[BUG] SimpleTokenAuth and APIKeyServerAuth accept empty credentials and compare secrets with != #9637

Closed

Description

@SDLSY

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:

  1. SimpleTokenAuth(token="") coerces "" into SecretStr(""), which is truthy (if self.token: succeeds), so _get_expected_token() returns "". Similarly, when AUTH_TOKEN="" is set in the environment, os.environ.get("AUTH_TOKEN") returns "" (not None). In both cases expected is None is False, and await auth.authenticate("") succeeds and returns AuthenticatedUser(token="", scheme="simple_token").
  2. APIKeyServerAuth(api_key="") accepts an empty API key at construction, so await auth.authenticate("") returns AuthenticatedUser(token="", scheme="api_key").
  3. Both schemes compare the incoming credential against the expected secret using != rather than hmac.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 token to SimpleTokenAuth or an empty api_key to APIKeyServerAuth should fail validation (similar to WebhookSignatureConfig in fix(a2a): require a secret for HMAC webhook signatures #9459).
  • An empty AUTH_TOKEN environment 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 raise TypeError).

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions