Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 detect_secrets.core.constants import VerifiedResult
from .base import RegexBasedDetector


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

Comment thread
jfagoagas marked this conversation as resolved.
denylist = [
# refs https://core.telegram.org/bots/api#authorizing-your-bot
re.compile(r'^\d{8,10}:[0-9A-Za-z_-]{35}$'),
Comment thread
jfagoagas marked this conversation as resolved.
Outdated
Comment thread
jfagoagas marked this conversation as resolved.
Outdated
Comment thread
jfagoagas marked this conversation as resolved.
Outdated
]

def verify(self, token, *args, **kwargs): # pragma: no cover
response = requests.get(
'https://api.telegram.org/bot{}/getMe'.format(
token,
),
)
return (
VerifiedResult.VERIFIED_TRUE
if response.status_code == 200
else VerifiedResult.VERIFIED_FALSE
)
Comment thread
jfagoagas marked this conversation as resolved.
Outdated
23 changes: 23 additions & 0 deletions tests/plugins/telegram_token_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import pytest

from detect_secrets.plugins.telegram_token import TelegramBotTokenDetector


class TestTelegramTokenDetector:

@pytest.mark.parametrize(
'payload, should_flag',
[
('bot110201543:AAHdqTcvCH1vGWJxfSe1ofSAs0K5PALDsaw', False),
('110201543:AAHdqTcvCH1vGWJxfSe1ofSAs0K5PALDsaw', True),
('7213808860:AAH1bjqpKKW3maRSPAxzIU-0v6xNuq2-NjM', True),
('foo:AAH1bjqpKKW3maRSPAxzIU-0v6xNuq2-NjM', False),
('foo', False),
('arn:aws:sns:aaa:111122223333:aaaaaaaaaaaaaaaaaaassssssddddddddddddd', False),
Comment thread
jfagoagas marked this conversation as resolved.
Comment thread
jfagoagas marked this conversation as resolved.
],
)
def test_analyze(self, payload, should_flag):
logic = TelegramBotTokenDetector()
output = logic.analyze_line(payload, 1, 'mock_filename')

assert len(output) == int(should_flag)
Loading