Skip to content

fix: use constant-time Sophia governor admin auth#4658

Merged
Scottcjn merged 1 commit into
Scottcjn:mainfrom
godd-ctrl:codex/fix-sophia-governor-admin-compare
May 14, 2026
Merged

fix: use constant-time Sophia governor admin auth#4658
Scottcjn merged 1 commit into
Scottcjn:mainfrom
godd-ctrl:codex/fix-sophia-governor-admin-compare

Conversation

@godd-ctrl
Copy link
Copy Markdown
Contributor

Fixes #4656. Reporter credit remains with @RJamieKelly. This patch replaces direct Sophia governor admin key equality with hmac.compare_digest, preserves existing X-Admin-Key/X-API-Key behavior, and adds regression coverage for both denied and accepted admin requests. Validation: python -m pytest node\tests\test_sophia_governor.py -q; python tools\bcos_spdx_check.py --base-ref origin/main; git diff --check.

@github-actions github-actions Bot added size/M PR: 51-200 lines BCOS-L1 Beacon Certified Open Source tier BCOS-L1 (required for non-doc PRs) node Node server related tests Test suite changes labels May 11, 2026
Copy link
Copy Markdown

@Finesssee Finesssee left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved after local validation.

I reviewed the Sophia governor admin-auth change in node/sophia_governor.py and the regression coverage in node/tests/test_sophia_governor.py. The patch preserves the existing X-Admin-Key / X-API-Key behavior and switches the secret comparison to hmac.compare_digest, so the admin gate no longer uses a direct equality comparison for configured keys.

Validation run locally on PR head:

  • PYTHONPATH=node uv run --no-project --with pytest --with flask --with requests python -m pytest node/tests/test_sophia_governor.py -q -> 8 passed
  • python3 -m py_compile node/sophia_governor.py node/tests/test_sophia_governor.py -> passed
  • git diff --check origin/main...pr-4658 -- node/sophia_governor.py node/tests/test_sophia_governor.py -> passed
  • python3 tools/bcos_spdx_check.py --base-ref origin/main -> BCOS SPDX check: OK

I did not find a blocking issue in this patch.

Copy link
Copy Markdown
Contributor

@douglance douglance left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved after local validation.

I reviewed the focused Sophia governor admin-auth change in node/sophia_governor.py and the regression coverage in node/tests/test_sophia_governor.py. The patch preserves the existing X-Admin-Key / X-API-Key behavior, keeps missing configured admin keys fail-closed through the existing required guard, and replaces the direct configured-secret equality check with hmac.compare_digest().

Validation run locally on PR head aaa94d4:

  • PYTHONPATH=node uv run --no-project --with pytest --with flask --with requests python -m pytest node/tests/test_sophia_governor.py -q -> 8 passed
  • python3 -m py_compile node/sophia_governor.py node/tests/test_sophia_governor.py -> passed
  • git diff --check origin/main...HEAD -- node/sophia_governor.py node/tests/test_sophia_governor.py -> passed
  • python3 tools/bcos_spdx_check.py --base-ref origin/main -> BCOS SPDX check: OK

No blocking findings from this patch. Scope note: PR #4659 overlaps this change; this PR has the cleaner route-level denied/accepted regression coverage.

Copy link
Copy Markdown
Contributor

@saim256 saim256 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved. I verified this PR independently and do not see a blocker.

What I checked:

  • The Sophia governor admin helper now uses hmac.compare_digest() instead of direct equality for configured admin-key comparisons.
  • Existing X-Admin-Key and X-API-Key behavior is preserved.
  • The regression exercises both denied and accepted /sophia/governor/review paths and proves both comparisons go through the patched compare_digest function.
  • The temp DB fixture cleanup retry is limited to test cleanup and does not alter production behavior.

Local validation:

  • PYTHONPATH=node 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 -> passed
  • git diff --check origin/main...HEAD -- node/sophia_governor.py node/tests/test_sophia_governor.py -> passed
  • python tools\bcos_spdx_check.py --base-ref origin/main -> BCOS SPDX check: OK

Scope note: this overlaps PR #4659 for the same issue, so maintainers will need to choose the canonical implementation. This patch is sound when reviewed on its own.

Copy link
Copy Markdown
Contributor

@jaxint jaxint left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thanks for contributing. Approved.

Copy link
Copy Markdown
Contributor

@jaxint jaxint left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thanks for contributing. Approved.

Copy link
Copy Markdown
Contributor

@jaxint jaxint left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thanks for contributing. Approved.

Copy link
Copy Markdown
Contributor

@jaxint jaxint left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thanks for contributing. Approved.

Copy link
Copy Markdown
Contributor

@jaxint jaxint left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thanks for contributing. Approved.

Copy link
Copy Markdown
Contributor

@jaxint jaxint left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thanks for contributing. Approved.

Copy link
Copy Markdown
Contributor

@jaxint jaxint left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thanks for contributing. Approved.

Copy link
Copy Markdown
Contributor

@jaxint jaxint left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code review: LGTM! Thanks for contributing to RustChain. Approved.

Copy link
Copy Markdown
Contributor

@loganoe loganoe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a correct constant-time admin-key comparison fix. The governor keeps the same fail-closed behavior for missing config, accepts both admin headers, and the regression test verifies hmac.compare_digest is used for denied and accepted requests.

Checked:

  • PYTEST_DISABLE_PLUGIN_AUTOLOAD=1 /tmp/rustchain-flask-venv/bin/python -m pytest -q node/tests/test_sophia_governor.py::test_governor_admin_auth_uses_constant_time_compare
  • PYTEST_DISABLE_PLUGIN_AUTOLOAD=1 /tmp/rustchain-flask-venv/bin/python -m py_compile node/sophia_governor.py node/tests/test_sophia_governor.py
  • git diff --check origin/main...HEAD

Copy link
Copy Markdown

@TJCurnutte TJCurnutte left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review: Approve

I inspected this PR's metadata and diff locally against origin/main.

Scope checked

  • PR: #4658 — fix: use constant-time Sophia governor admin auth
  • Changed files: node/sophia_governor.py, node/tests/test_sophia_governor.py
  • Diff focus: authentication/authorization hardening in node/sophia_governor.py, node/tests/test_sophia_governor.py
  • Diff evidence: import hmac; return bool(provided and hmac.compare_digest(provided, required)); def test_governor_admin_auth_uses_constant_time_compare(client, monkeypatch):

Validation run

  • git diff --check origin/main...HEAD — passed: passed with no output
  • python3 tools/bcos_spdx_check.py --base-ref origin/main — passed: BCOS SPDX check: OK
  • python3 -B -m py_compile node/sophia_governor.py node/tests/test_sophia_governor.py — passed: passed with no output
  • python3 -B -m pytest node/tests/test_sophia_governor.py -q — passed: -- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html; 8 passed, 1 warning in 0.41s
  • Diff secret-pattern scan — passed

Review decision
The change is focused, includes/updates targeted coverage where applicable, and the local validation gates passed. I did not find a blocker in the inspected diff.

Boundary: review only; no live node mutation, wallet action, private key use, or destructive operation was performed.

@guangningsun
Copy link
Copy Markdown
Contributor

PR Review — Standard Quality ✓

PR: #4658 — Fix: use constant-time Sophia governor admin auth

What I reviewed

  • node/sophia_governor.py
  • node/tests/test_sophia_governor.py

Observations

  1. Adding import hmac for constant-time admin authentication — using hmac.compare_digest instead of == for admin key comparison prevents timing attacks.

  2. The _is_admin(req) function now uses HMAC comparison — this ensures admin key checking takes constant time regardless of how many characters match.

  3. Test improvements with gc and time imports suggest additional memory and timing tests were added.

LGTM.

Bounty: #2782
Disclosure: I received RTC compensation for this review.

@Scottcjn Scottcjn merged commit 4f0f8e3 into Scottcjn:main May 14, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

BCOS-L1 Beacon Certified Open Source tier BCOS-L1 (required for non-doc PRs) node Node server related size/M PR: 51-200 lines tests Test suite changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

9 participants