Skip to content

[Security] sophia_governor.py uses timing-sensitive admin key comparison #4656

Closed

Description

@RJamieKelly

Summary

The admin guard in node/sophia_governor.py compares attacker-controlled header input to RC_ADMIN_KEY with raw string equality instead of hmac.compare_digest.

Affected code

node/sophia_governor.py:936-941

def _is_admin(req) -> bool:
    required = os.getenv(\"RC_ADMIN_KEY\", \"\").strip()
    if not required:
        return False
    provided = (req.headers.get(\"X-Admin-Key\") or req.headers.get(\"X-API-Key\") or \"\").strip()
    return bool(provided and provided == required)

This helper protects the admin routes:

  • POST /sophia/governor/review
  • POST /sophia/governor/retry/<event_id>

Impact

Python string equality is not a constant-time secret comparison. An attacker who can make repeated requests to these endpoints can use response-time differences to distinguish partially correct admin key guesses from fully incorrect guesses, which weakens the secrecy of RC_ADMIN_KEY.

RustChain already tracks this as a real bug class elsewhere in the repo (for example #3229 on sophia_governor_review_service.py), but node/sophia_governor.py still uses the timing-sensitive pattern.

Steps to reproduce

  1. Configure RC_ADMIN_KEY and start the Flask app with register_sophia_governor_endpoints(...) enabled.
  2. Send many POST requests to /sophia/governor/review or /sophia/governor/retry/<event_id> with candidate X-Admin-Key values of the same length.
  3. Compare average response times for guesses with progressively longer correct prefixes versus guesses with fully incorrect prefixes.
  4. Observe that the current implementation uses raw provided == required, so it does not provide constant-time comparison semantics for the secret.

Expected behavior

The admin guard should compare secrets with hmac.compare_digest(...) so prefix-dependent timing information is not exposed.

Actual behavior

The route guard uses raw string equality on a secret admin key.

Activity

  1. github-actions commented on May 11, 2026

    @github-actions
    Contributor

    Welcome to RustChain! Thanks for opening your first issue.

    New here? Check out these resources:

    Earn RTC tokens by contributing code, docs, or security fixes. Every merged PR gets paid!

    1 RTC = $0.10 USD | pip install clawrtc to start mining

  2. minyanyi commented on May 11, 2026

    @minyanyi
    Contributor

    Submitted PR #4659 for this: #4659

    It switches the Sophia governor admin guard to hmac.compare_digest, adds a focused regression test for /sophia/governor/review, and keeps the existing governor tests passing.

    Validation:

    • python -m pytest node/tests/test_sophia_governor.py -q -> 8 passed
    • python -m py_compile node/sophia_governor.py node/tests/test_sophia_governor.py
    • git diff --check
    • python tools/bcos_spdx_check.py --base-ref origin/main -> OK

    Wallet: 0x2E4380d2e1668Ca9fA3Ef91fF776FDc140Cf3fE8

  3. IgorGanapolsky commented on May 11, 2026

    @IgorGanapolsky

    /attempt I can fix this timing-sensitive admin key comparison by implementing hmac.compare_digest as suggested. Payout address: RTC9d7caca3039130d3b26d41f7343d8f4ef4592360

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