Skip to content

fix(security): use hmac.compare_digest for Telegram auth#355

Open
kevinnft wants to merge 1 commit into
Quantarq:mainfrom
kevinnft:fix/telegram-hmac-compare-digest
Open

fix(security): use hmac.compare_digest for Telegram auth#355
kevinnft wants to merge 1 commit into
Quantarq:mainfrom
kevinnft:fix/telegram-hmac-compare-digest

Conversation

@kevinnft

Copy link
Copy Markdown

Summary

Closes #203.

check_telegram_authorization compared Telegram initData hashes with !=, which short-circuits on the first differing byte and is theoretically timing-observable. This PR switches to hmac.compare_digest (constant-time) and fails closed on unequal-length/type inputs.

Changes

  • quantara/web_app/telegram/utils.py: use hmac.compare_digest
  • quantara/web_app/tests/test_telegram.py: assert compare_digest is used

Test plan

  • Local unit check: valid hash → True; wrong hash → False; short hash → False
  • New test pins hmac.compare_digest call path
  • CI full suite

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Use hmac.compare_digest in check_telegram_authorization

2 participants