Repository navigation
Conversation
The admin key that protects the operations a hosted Studio keeps away from anonymous callers was checked with `==` in three places: the `require_admin_access` decorator (aliased as `check_forbidden_method_in_hosted_studio`), `cancel_transaction` and `admin_upgrade_contract_code`. Comparing strings with `==` returns at the first differing character, so the time a wrong key takes to be rejected tells a remote caller how much of it was right. This is a hardening, not a demonstrated exploit: a timing attack on a network-served comparison is hard, and I did not try one. Constant-time comparison is the standard answer and costs nothing here. Use `hmac.compare_digest` through one small helper. The values are compared as UTF-8 bytes because `compare_digest` only accepts ASCII `str`, and lone surrogates, which JSON can carry, are passed through with `surrogatepass` so that an odd key is rejected like any other wrong key instead of raising. A key that is not a string never matches, as before. The helper agrees with the old `==` on 60,000 random strings (ASCII, unicode, lone surrogates, near-equal prefixes) and on non-string values. A scan of the backend found no other secret compared with `==`: API keys are stored as SHA-256 hashes and looked up by a unique `key_hash`.
Contributor
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
ADMIN_API_KEYwithhmac.compare_digestinstead of==in the three places that check it: therequire_admin_accessdecorator (also exported ascheck_forbidden_method_in_hosted_studio),cancel_transactionandadmin_upgrade_contract_code._admin_key_matches(presented, configured), inendpoints.pynext to the decorator. Behaviour is unchanged: same results, same errors.tests/unit/test_admin_key_comparison.py.Why
==on strings returns at the first differing character, so the time a wrong key takes to be rejected tells a remote caller how much of it was right. The key guards what a hosted Studio keeps away from anonymous callers (validators, providers, snapshots, DB tables, tiers and API keys).This is a hardening, not a demonstrated exploit. A timing attack against a network-served comparison is hard, and I did not try one or measure anything against a live instance. Constant-time comparison is the standard answer and costs nothing here, in the same direction as #1801 and #1731.
Testing done
backend/for secrets compared with==: exactly these three places, and no use ofcompare_digestanywhere. API keys are not affected: they are stored as SHA-256 hashes and looked up by the uniquekey_hash.==on 60,008 inputs (ASCII, unicode, lone surrogates, near-equal prefixes, longer/shorter keys, and non-string values likeNone, ints, bytes, lists): no difference and no exception.Invalid or missing admin key, hosted without a key is still blocked), and the two inline checks (an oddadmin_keygivesJSONRPCError, notTypeErrororUnicodeEncodeError).compare_digestis really used (one per call site, 3 in total) fail when the old==behaviour is restored (verified with a plugin that swaps the helper for plain equality); the other 36 pass on both, they pin the behaviour that must not change.pytest tests/unit: 1890 passed, 7 skipped.ruff check(0.15.22) andblack --checkare clean.tests/integration/test_upgrade_contract.pyexercises the upgrade endpoint and I could not run it.Decisions made
surrogatepass.compare_digestaccepts only ASCIIstr, and JSON can carry a lone surrogate like"\ud800", which.encode()rejects. Withoutsurrogatepasssuch a key would turn "wrong key" into an internal error; with it the answer is the same as with==.==.endpoints.py, next to the decorator, because all three call sites are in that file. I did not add a module for it.sim_*methods that are not gated (sim_fundAccount,sim_cancelTransaction,sim_upgradeContractCode, ...) were checked while I was here: the last two authorize inside the function (admin key or the deployer's/sender's signature), so I left them alone.Checks
Reviewing tips
The behaviour is in the 17 lines of
_admin_key_matches; the three call-site changes are one line each. The tests are easiest to read fromTestAdminKeyMatchesdown.User facing release notes
None. The admin key is checked the same way as before, only in constant time.