forked from Yelp/detect-secrets
-
Notifications
You must be signed in to change notification settings - Fork 54
feat(telegram_token): Add TelegramBotTokenDetector plugin #187
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jfagoagas
wants to merge
6
commits into
IBM:master
Choose a base branch
from
jfagoagas:feat/telegram-bot-token-detector
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+71
−0
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
112e06b
feat(telegram_token): Add TelegramBotTokenDetector plugin
jfagoagas 38be562
fix: address Copilot suggestions
jfagoagas c73b54c
fix: address comment
jfagoagas fde1667
fix: enable plugin
jfagoagas 9f89230
Merge branch 'feat/telegram-bot-token-detector' of https://github.com…
jfagoagas 9741762
fix: improve regex and verification
jfagoagas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,37 @@ | ||
| """ | ||
| 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' | ||
|
|
||
| denylist = [ | ||
| # refs https://core.telegram.org/bots/api#authorizing-your-bot | ||
| re.compile(r'(?<![:\w])\d{8,10}:[0-9A-Za-z_-]{35}(?![0-9A-Za-z_-])'), | ||
| ] | ||
|
|
||
| def verify(self, token, *args, **kwargs): # pragma: no cover | ||
| try: | ||
| response = requests.get( | ||
| 'https://api.telegram.org/bot{}/getMe'.format( | ||
| token, | ||
| ), | ||
| timeout=5, | ||
| ) | ||
| except requests.exceptions.RequestException: | ||
| return VerifiedResult.UNVERIFIED | ||
|
|
||
| if response.status_code == 200: | ||
| return VerifiedResult.VERIFIED_TRUE | ||
| if response.status_code == 401: | ||
| return VerifiedResult.VERIFIED_FALSE | ||
|
|
||
| return VerifiedResult.UNVERIFIED | ||
This file contains hidden or 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,28 @@ | ||
| import pytest | ||
|
|
||
| from detect_secrets.plugins.telegram_token import TelegramBotTokenDetector | ||
|
|
||
|
|
||
| class TestTelegramTokenDetector: | ||
|
|
||
| @pytest.mark.parametrize( | ||
| 'payload, should_flag', | ||
| [ | ||
| ('110201543:AAHdqTcvCH1vGWJxfSe1ofSAs0K5PALDsaw', True), | ||
| ('7213808860:AAH1bjqpKKW3maRSPAxzIU-0v6xNuq2-NjM', True), | ||
| # Embedded in assignments, quotes, JSON | ||
| ('TELEGRAM_TOKEN=110201543:AAHdqTcvCH1vGWJxfSe1ofSAs0K5PALDsaw', True), | ||
| ('token = "110201543:AAHdqTcvCH1vGWJxfSe1ofSAs0K5PALDsaw"', True), | ||
| ('{"bot_token": "110201543:AAHdqTcvCH1vGWJxfSe1ofSAs0K5PALDsaw"}', True), | ||
| # Should not match | ||
| ('bot110201543:AAHdqTcvCH1vGWJxfSe1ofSAs0K5PALDsaw', False), | ||
| ('foo:AAH1bjqpKKW3maRSPAxzIU-0v6xNuq2-NjM', False), | ||
| ('foo', False), | ||
| ('arn:aws:sns:aaa:111122223333:aaaaaaaaaaaaaaaaaaassssssddddddddddddd', False), | ||
jfagoagas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ], | ||
| ) | ||
| def test_analyze(self, payload, should_flag): | ||
| logic = TelegramBotTokenDetector() | ||
| output = logic.analyze_line(payload, 1, 'mock_filename') | ||
|
|
||
| assert len(output) == int(should_flag) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.