Skip to content

Commit 28a6658

Browse files
authored
Merge pull request #808 from Chandra158/gh-768
Plugin to detect Telegram bot tokens
2 parents 2e65082 + 1f25533 commit 28a6658

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ SlackDetector
114114
SoftlayerDetector
115115
SquareOAuthDetector
116116
StripeDetector
117+
TelegramBotTokenDetector
117118
TwilioKeyDetector
118119
```
119120

Diff for: detect_secrets/plugins/telegram_token.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"""
2+
This plugin searches for Telegram bot tokens
3+
"""
4+
import re
5+
6+
import requests
7+
8+
from ..constants import VerifiedResult
9+
from detect_secrets.plugins.base import RegexBasedDetector
10+
11+
12+
class TelegramBotTokenDetector(RegexBasedDetector):
13+
"""Scans for Telegram bot tokens."""
14+
secret_type = 'Telegram Bot Token'
15+
16+
denylist = [
17+
# refs https://core.telegram.org/bots/api#authorizing-your-bot
18+
re.compile(r'\d{8,10}:[0-9A-Za-z_-]{35}'),
19+
]
20+
21+
def verify(self, secret: str) -> VerifiedResult: # pragma: no cover
22+
response = requests.get(
23+
'https://api.telegram.org/bot{}/getMe'.format(
24+
secret,
25+
),
26+
)
27+
return (
28+
VerifiedResult.VERIFIED_TRUE
29+
if response.status_code == 200
30+
else VerifiedResult.VERIFIED_FALSE
31+
)

Diff for: tests/plugins/telegram_token_test.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import pytest
2+
3+
from detect_secrets.plugins.telegram_token import TelegramBotTokenDetector
4+
5+
6+
class TestTelegramTokenDetector:
7+
8+
@pytest.mark.parametrize(
9+
'payload, should_flag',
10+
[
11+
('bot110201543:AAHdqTcvCH1vGWJxfSe1ofSAs0K5PALDsaw', True),
12+
('110201543:AAHdqTcvCH1vGWJxfSe1ofSAs0K5PALDsaw', True),
13+
('7213808860:AAH1bjqpKKW3maRSPAxzIU-0v6xNuq2-NjM', True),
14+
('foo:AAH1bjqpKKW3maRSPAxzIU-0v6xNuq2-NjM', False),
15+
('foo', False),
16+
],
17+
)
18+
def test_analyze(self, payload, should_flag):
19+
logic = TelegramBotTokenDetector()
20+
output = logic.analyze_line(filename='mock_filename', line=payload)
21+
22+
assert len(output) == int(should_flag)

0 commit comments

Comments
 (0)