Skip to content
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

Add a plugin for GitHub token detection #465

Merged
merged 1 commit into from
Aug 1, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
16 changes: 16 additions & 0 deletions detect_secrets/plugins/github_token.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""
This plugin searches for GitHub tokens
"""
import re

from detect_secrets.plugins.base import RegexBasedDetector


class GitHubTokenDetector(RegexBasedDetector):
"""Scans for GitHub tokens."""
secret_type = 'GitHub token'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just for consistency, can you do GitHub token -> GitHub Token?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll change ;)


denylist = [
# ref. https://github.blog/2021-04-05-behind-githubs-new-authentication-token-formats/
re.compile(r'(ghp|gho|ghu|ghs|ghr)_[A-Za-z0-9_]{36}'),
]
19 changes: 19 additions & 0 deletions tests/plugins/github_token_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import pytest

from detect_secrets.plugins.github_token import GitHubTokenDetector


class TestGitHubTokenDetector:

@pytest.mark.parametrize(
'payload, should_flag',
[
('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', True),
('foo_wWPw5k4aXcaT4fNP0UcnZwJUVFk6LO0pINUx', False),
('foo', False),
],
)
def test_analyze(self, payload, should_flag):
logic = GitHubTokenDetector()
output = logic.analyze_line(filename='mock_filename', line=payload)
assert len(output) == int(should_flag)