Created
September 10, 2025 12:08
-
-
Save gm3dmo/d7a6e848df1dba35ca4221bb8abd7324 to your computer and use it in GitHub Desktop.
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
| #!/usr/bin/env python | |
| import re | |
| import pyperclip | |
| # pip install pyperclip | |
| def redact_github_tokens(text): | |
| # Patterns for GitHub tokens (Personal Access Tokens, fine-tuned tokens, etc.) | |
| # Example formats: | |
| # - ghp_xxx... (Personal Access Token) | |
| # - github_pat_xxx... (Fine-grained) | |
| # - gho_xxx..., ghs_xxx..., ghr_xxx..., etc. | |
| token_patterns = [ | |
| r'(gh[pousr]_[A-Za-z0-9]{30,})', | |
| r'(github_pat_[A-Za-z0-9_]{22,255})' | |
| ] | |
| # Combine all patterns | |
| combined_pattern = '|'.join(token_patterns) | |
| regex = re.compile(combined_pattern) | |
| def mask_token(match): | |
| token = match.group(0) | |
| underscore_indices = [i for i, c in enumerate(token) if c == '_'] | |
| if len(underscore_indices) >= 3: | |
| prefix_end = underscore_indices[1] + 1 # preserve up to second underscore | |
| elif len(underscore_indices) == 1: | |
| prefix_end = 4 # preserve first 4 characters | |
| else: | |
| # default behavior: preserve up to last underscore | |
| prefix_end = token.rfind('_') + 1 if '_' in token else 4 | |
| suffix_length = 8 | |
| if len(token) <= prefix_end + suffix_length: | |
| return token # too short to mask | |
| prefix = token[:prefix_end] | |
| suffix = token[-suffix_length:] | |
| masked_length = len(token) - len(prefix) - len(suffix) | |
| return prefix + '*' * masked_length + suffix | |
| # Substitute tokens with redacted version | |
| return regex.sub(mask_token, text) | |
| def main(): | |
| clipboard_content = pyperclip.paste() | |
| redacted_content = redact_github_tokens(clipboard_content) | |
| pyperclip.copy(redacted_content) | |
| print("Clipboard content scanned and redacted (if any GitHub tokens were found).") | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment