Skip to content

Commit

Permalink
Merge pull request #808 from Chandra158/gh-768
Browse files Browse the repository at this point in the history
Plugin to detect Telegram bot tokens
  • Loading branch information
lorenzodb1 authored Apr 30, 2024
2 parents 2e65082 + 1f25533 commit 28a6658
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ SlackDetector
SoftlayerDetector
SquareOAuthDetector
StripeDetector
TelegramBotTokenDetector
TwilioKeyDetector
```

Expand Down
31 changes: 31 additions & 0 deletions detect_secrets/plugins/telegram_token.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"""
This plugin searches for Telegram bot tokens
"""
import re

import requests

from ..constants import VerifiedResult
from detect_secrets.plugins.base import RegexBasedDetector


class TelegramBotTokenDetector(RegexBasedDetector):
"""Scans for Telegram bot tokens."""
secret_type = 'Telegram Bot Token'

denylist = [
# refs https://core.telegram.org/bots/api#authorizing-your-bot
re.compile(r'\d{8,10}:[0-9A-Za-z_-]{35}'),
]

def verify(self, secret: str) -> VerifiedResult: # pragma: no cover
response = requests.get(
'https://api.telegram.org/bot{}/getMe'.format(
secret,
),
)
return (
VerifiedResult.VERIFIED_TRUE
if response.status_code == 200
else VerifiedResult.VERIFIED_FALSE
)
22 changes: 22 additions & 0 deletions tests/plugins/telegram_token_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import pytest

from detect_secrets.plugins.telegram_token import TelegramBotTokenDetector


class TestTelegramTokenDetector:

@pytest.mark.parametrize(
'payload, should_flag',
[
('bot110201543:AAHdqTcvCH1vGWJxfSe1ofSAs0K5PALDsaw', True),
('110201543:AAHdqTcvCH1vGWJxfSe1ofSAs0K5PALDsaw', True),
('7213808860:AAH1bjqpKKW3maRSPAxzIU-0v6xNuq2-NjM', True),
('foo:AAH1bjqpKKW3maRSPAxzIU-0v6xNuq2-NjM', False),
('foo', False),
],
)
def test_analyze(self, payload, should_flag):
logic = TelegramBotTokenDetector()
output = logic.analyze_line(filename='mock_filename', line=payload)

assert len(output) == int(should_flag)

0 comments on commit 28a6658

Please sign in to comment.