fix(security): use hmac.compare_digest for Telegram auth#355
Open
kevinnft wants to merge 1 commit into
Open
Conversation
Replace short-circuiting string inequality with constant-time hmac.compare_digest when verifying Telegram initData hashes so a remote timing oracle cannot recover the expected hash byte-by-byte. Guard unequal-length inputs so verification fails closed. Closes Quantarq#203 Signed-off-by: NossXBT <kevinnft@users.noreply.github.com>
| with patch.object( | ||
| hmac_mod, "compare_digest", wraps=hmac_mod.compare_digest | ||
| ) as mock_cmp: | ||
| assert check_telegram_authorization(self.BOT_TOKEN, data) is True |
| hmac_mod, "compare_digest", wraps=hmac_mod.compare_digest | ||
| ) as mock_cmp: | ||
| assert check_telegram_authorization(self.BOT_TOKEN, data) is True | ||
| assert mock_cmp.called |
| assert mock_cmp.called | ||
| # Both sides must be str hex digests of equal length. | ||
| left, right = mock_cmp.call_args[0] | ||
| assert isinstance(left, str) and isinstance(right, str) |
| # Both sides must be str hex digests of equal length. | ||
| left, right = mock_cmp.call_args[0] | ||
| assert isinstance(left, str) and isinstance(right, str) | ||
| assert len(left) == len(right) == 64 |
2 tasks
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.
Summary
Closes #203.
check_telegram_authorizationcompared Telegram initData hashes with!=, which short-circuits on the first differing byte and is theoretically timing-observable. This PR switches tohmac.compare_digest(constant-time) and fails closed on unequal-length/type inputs.Changes
quantara/web_app/telegram/utils.py: usehmac.compare_digestquantara/web_app/tests/test_telegram.py: assertcompare_digestis usedTest plan
hmac.compare_digestcall path