File tree 3 files changed +54
-0
lines changed
3 files changed +54
-0
lines changed Original file line number Diff line number Diff line change @@ -114,6 +114,7 @@ SlackDetector
114
114
SoftlayerDetector
115
115
SquareOAuthDetector
116
116
StripeDetector
117
+ TelegramBotTokenDetector
117
118
TwilioKeyDetector
118
119
```
119
120
Original file line number Diff line number Diff line change
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
+ )
Original file line number Diff line number Diff line change
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 )
You can’t perform that action at this time.
0 commit comments