|
| 1 | +""" |
| 2 | +File filtering logic for GitHub repository indexing. |
| 3 | +""" |
| 4 | + |
| 5 | +import re |
| 6 | +from pathlib import Path |
| 7 | +from typing import Optional |
| 8 | + |
| 9 | +# Keyish pattern regex - matches files that likely contain secrets/keys |
| 10 | +KEYISH_PATTERN = re.compile( |
| 11 | + r'^(\.git|.*\.pem|.*\.key|.*\.pfx|.*\.p12|.*\.jks|.*\.keystore|.*\.pkcs12|.*\.crt|.*\.cer|id_rsa|id_ed25519|id_ecdsa|id_dsa)$' |
| 12 | +) |
| 13 | + |
| 14 | +# Default max file size in bytes (1 MB) |
| 15 | +DEFAULT_MAX_FILE_SIZE = 1024 * 1024 # 1 MB |
| 16 | + |
| 17 | + |
| 18 | +def always_ignore_path(path: str) -> bool: |
| 19 | + """ |
| 20 | + Check if a path should always be ignored (security measure). |
| 21 | +
|
| 22 | + Args: |
| 23 | + path: The file path to check. |
| 24 | +
|
| 25 | + Returns: |
| 26 | + True if the path contains ".." and should be ignored. |
| 27 | + """ |
| 28 | + return ".." in path |
| 29 | + |
| 30 | + |
| 31 | +def is_keyish_path(path: str) -> bool: |
| 32 | + """ |
| 33 | + Check if a path matches the keyish pattern (secrets/keys). |
| 34 | +
|
| 35 | + Args: |
| 36 | + path: The file path to check. |
| 37 | +
|
| 38 | + Returns: |
| 39 | + True if the filename matches patterns for secret/key files. |
| 40 | + """ |
| 41 | + # Extract filename from path |
| 42 | + filename = Path(path).name |
| 43 | + return bool(KEYISH_PATTERN.match(filename)) |
| 44 | + |
| 45 | + |
| 46 | +def is_valid_file_size(size_bytes: int, max_file_size: int = DEFAULT_MAX_FILE_SIZE) -> bool: |
| 47 | + """ |
| 48 | + Check if file size is valid for upload. |
| 49 | +
|
| 50 | + Args: |
| 51 | + size_bytes: The size of the file in bytes. |
| 52 | + max_file_size: Maximum allowed file size in bytes. Defaults to 1 MB. |
| 53 | +
|
| 54 | + Returns: |
| 55 | + True if the file size is within the allowed limit. |
| 56 | + """ |
| 57 | + return size_bytes <= max_file_size |
| 58 | + |
| 59 | + |
| 60 | +def is_valid_utf8(content: bytes) -> bool: |
| 61 | + """ |
| 62 | + Check if file content is valid UTF-8 (not binary). |
| 63 | +
|
| 64 | + Args: |
| 65 | + content: The file content as bytes. |
| 66 | +
|
| 67 | + Returns: |
| 68 | + True if the content is valid UTF-8, False if it's binary or invalid. |
| 69 | + """ |
| 70 | + try: |
| 71 | + content.decode("utf-8") |
| 72 | + return True |
| 73 | + except UnicodeDecodeError: |
| 74 | + return False |
| 75 | + |
| 76 | + |
| 77 | +def should_filter_file( |
| 78 | + path: str, |
| 79 | + content: bytes, |
| 80 | + max_file_size: Optional[int] = None, |
| 81 | +) -> dict: |
| 82 | + """ |
| 83 | + Check if a file should be filtered out. |
| 84 | +
|
| 85 | + Returns {"filtered": True, "reason": "..."} if file should be skipped. |
| 86 | + Returns {"filtered": False} if file should be included. |
| 87 | +
|
| 88 | + Priority order (from file-filtering.md): |
| 89 | + 1. Path validation (contains "..") |
| 90 | + 2. File size check |
| 91 | + 3. .augmentignore rules (checked by caller) |
| 92 | + 4. Keyish patterns |
| 93 | + 5. .gitignore rules (checked by caller) |
| 94 | + 6. UTF-8 validation |
| 95 | +
|
| 96 | + Args: |
| 97 | + path: The file path to check. |
| 98 | + content: The file content as bytes. |
| 99 | + max_file_size: Maximum allowed file size in bytes. Defaults to DEFAULT_MAX_FILE_SIZE. |
| 100 | +
|
| 101 | + Returns: |
| 102 | + A dict with "filtered" (bool) and optionally "reason" (str) keys. |
| 103 | + """ |
| 104 | + effective_max_size = max_file_size if max_file_size is not None else DEFAULT_MAX_FILE_SIZE |
| 105 | + |
| 106 | + # 1. Check for ".." in path (security) |
| 107 | + if always_ignore_path(path): |
| 108 | + return {"filtered": True, "reason": "path_contains_dotdot"} |
| 109 | + |
| 110 | + # 2. Check file size |
| 111 | + if not is_valid_file_size(len(content), effective_max_size): |
| 112 | + return {"filtered": True, "reason": f"file_too_large ({len(content)} bytes)"} |
| 113 | + |
| 114 | + # 3. Check keyish patterns (secrets/keys) |
| 115 | + if is_keyish_path(path): |
| 116 | + return {"filtered": True, "reason": "keyish_pattern"} |
| 117 | + |
| 118 | + # 4. Check UTF-8 validity (binary detection) |
| 119 | + if not is_valid_utf8(content): |
| 120 | + return {"filtered": True, "reason": "binary_file"} |
| 121 | + |
| 122 | + return {"filtered": False} |
| 123 | + |
0 commit comments