-
Notifications
You must be signed in to change notification settings - Fork 481
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Plugin to detect Telegram bot tokens
- Loading branch information
Showing
3 changed files
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
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 | ||
) |
This file contains 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
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) |