|
| 1 | +import os |
| 2 | +import warnings |
| 3 | +from typing import Any, Callable, Dict, List, Tuple, Union |
| 4 | + |
| 5 | +from guardrails.validator_base import ( |
| 6 | + FailResult, |
| 7 | + PassResult, |
| 8 | + ValidationResult, |
| 9 | + Validator, |
| 10 | + register_validator, |
| 11 | +) |
| 12 | + |
| 13 | +try: |
| 14 | + import detect_secrets # type: ignore |
| 15 | +except ImportError: |
| 16 | + detect_secrets = None |
| 17 | + |
| 18 | + |
| 19 | +@register_validator(name="detect-secrets", data_type="string") |
| 20 | +class DetectSecrets(Validator): |
| 21 | + """Validates whether the generated code snippet contains any secrets. |
| 22 | +
|
| 23 | + **Key Properties** |
| 24 | + | Property | Description | |
| 25 | + | ----------------------------- | --------------------------------- | |
| 26 | + | Name for `format` attribute | `detect-secrets` | |
| 27 | + | Supported data types | `string` | |
| 28 | + | Programmatic fix | None | |
| 29 | +
|
| 30 | + Parameters: Arguments |
| 31 | + None |
| 32 | +
|
| 33 | + This validator uses the detect-secrets library to check whether the generated code |
| 34 | + snippet contains any secrets. If any secrets are detected, the validator fails and |
| 35 | + returns the generated code snippet with the secrets replaced with asterisks. |
| 36 | + Else the validator returns the generated code snippet. |
| 37 | +
|
| 38 | + Following are some caveats: |
| 39 | + - Multiple secrets on the same line may not be caught. e.g. |
| 40 | + - Minified code |
| 41 | + - One-line lists/dictionaries |
| 42 | + - Multi-variable assignments |
| 43 | + - Multi-line secrets may not be caught. e.g. |
| 44 | + - RSA/SSH keys |
| 45 | +
|
| 46 | + Example: |
| 47 | + ```py |
| 48 | +
|
| 49 | + guard = Guard.from_string(validators=[ |
| 50 | + DetectSecrets(on_fail="fix") |
| 51 | + ]) |
| 52 | + guard.parse( |
| 53 | + llm_output=code_snippet, |
| 54 | + ) |
| 55 | + """ |
| 56 | + |
| 57 | + def __init__(self, on_fail: Union[Callable[..., Any], None] = None, **kwargs): |
| 58 | + super().__init__(on_fail, **kwargs) |
| 59 | + |
| 60 | + # Check if detect-secrets is installed |
| 61 | + if detect_secrets is None: |
| 62 | + raise ValueError( |
| 63 | + "You must install detect-secrets in order to " |
| 64 | + "use the DetectSecrets validator." |
| 65 | + ) |
| 66 | + self.temp_file_name = "temp.txt" |
| 67 | + self.mask = "********" |
| 68 | + |
| 69 | + def get_unique_secrets(self, value: str) -> Tuple[Dict[str, Any], List[str]]: |
| 70 | + """Get unique secrets from the value. |
| 71 | +
|
| 72 | + Args: |
| 73 | + value (str): The generated code snippet. |
| 74 | +
|
| 75 | + Returns: |
| 76 | + unique_secrets (Dict[str, Any]): A dictionary of unique secrets and their |
| 77 | + line numbers. |
| 78 | + lines (List[str]): The lines of the generated code snippet. |
| 79 | + """ |
| 80 | + try: |
| 81 | + # Write each line of value to a new file |
| 82 | + with open(self.temp_file_name, "w") as f: |
| 83 | + f.writelines(value) |
| 84 | + except Exception as e: |
| 85 | + raise OSError( |
| 86 | + "Problems creating or deleting the temporary file. " |
| 87 | + "Please check the permissions of the current directory." |
| 88 | + ) from e |
| 89 | + |
| 90 | + try: |
| 91 | + # Create a new secrets collection |
| 92 | + from detect_secrets import settings |
| 93 | + from detect_secrets.core.secrets_collection import SecretsCollection |
| 94 | + |
| 95 | + secrets = SecretsCollection() |
| 96 | + |
| 97 | + # Scan the file for secrets |
| 98 | + with settings.default_settings(): |
| 99 | + secrets.scan_file(self.temp_file_name) |
| 100 | + except ImportError: |
| 101 | + raise ValueError( |
| 102 | + "You must install detect-secrets in order to " |
| 103 | + "use the DetectSecrets validator." |
| 104 | + ) |
| 105 | + except Exception as e: |
| 106 | + raise RuntimeError( |
| 107 | + "Problems with creating a SecretsCollection or " |
| 108 | + "scanning the file for secrets." |
| 109 | + ) from e |
| 110 | + |
| 111 | + # Get unique secrets from these secrets |
| 112 | + unique_secrets = {} |
| 113 | + for secret in secrets: |
| 114 | + _, potential_secret = secret |
| 115 | + actual_secret = potential_secret.secret_value |
| 116 | + line_number = potential_secret.line_number |
| 117 | + if actual_secret not in unique_secrets: |
| 118 | + unique_secrets[actual_secret] = [line_number] |
| 119 | + else: |
| 120 | + # if secret already exists, avoid duplicate line numbers |
| 121 | + if line_number not in unique_secrets[actual_secret]: |
| 122 | + unique_secrets[actual_secret].append(line_number) |
| 123 | + |
| 124 | + try: |
| 125 | + # File no longer needed, read the lines from the file |
| 126 | + with open(self.temp_file_name, "r") as f: |
| 127 | + lines = f.readlines() |
| 128 | + except Exception as e: |
| 129 | + raise OSError( |
| 130 | + "Problems reading the temporary file. " |
| 131 | + "Please check the permissions of the current directory." |
| 132 | + ) from e |
| 133 | + |
| 134 | + try: |
| 135 | + # Delete the file |
| 136 | + os.remove(self.temp_file_name) |
| 137 | + except Exception as e: |
| 138 | + raise OSError( |
| 139 | + "Problems deleting the temporary file. " |
| 140 | + "Please check the permissions of the current directory." |
| 141 | + ) from e |
| 142 | + return unique_secrets, lines |
| 143 | + |
| 144 | + def get_modified_value( |
| 145 | + self, unique_secrets: Dict[str, Any], lines: List[str] |
| 146 | + ) -> str: |
| 147 | + """Replace the secrets on the lines with asterisks. |
| 148 | +
|
| 149 | + Args: |
| 150 | + unique_secrets (Dict[str, Any]): A dictionary of unique secrets and their |
| 151 | + line numbers. |
| 152 | + lines (List[str]): The lines of the generated code snippet. |
| 153 | +
|
| 154 | + Returns: |
| 155 | + modified_value (str): The generated code snippet with secrets replaced with |
| 156 | + asterisks. |
| 157 | + """ |
| 158 | + # Replace the secrets on the lines with asterisks |
| 159 | + for secret, line_numbers in unique_secrets.items(): |
| 160 | + for line_number in line_numbers: |
| 161 | + lines[line_number - 1] = lines[line_number - 1].replace( |
| 162 | + secret, self.mask |
| 163 | + ) |
| 164 | + |
| 165 | + # Convert lines to a multiline string |
| 166 | + modified_value = "".join(lines) |
| 167 | + return modified_value |
| 168 | + |
| 169 | + def validate(self, value: str, metadata: Dict[str, Any]) -> ValidationResult: |
| 170 | + # Check if value is a multiline string |
| 171 | + if "\n" not in value: |
| 172 | + # Raise warning if value is not a multiline string |
| 173 | + warnings.warn( |
| 174 | + "The DetectSecrets validator works best with " |
| 175 | + "multiline code snippets. " |
| 176 | + "Refer validator docs for more details." |
| 177 | + ) |
| 178 | + |
| 179 | + # Add a newline to value |
| 180 | + value += "\n" |
| 181 | + |
| 182 | + # Get unique secrets from the value |
| 183 | + unique_secrets, lines = self.get_unique_secrets(value) |
| 184 | + |
| 185 | + if unique_secrets: |
| 186 | + # Replace the secrets on the lines with asterisks |
| 187 | + modified_value = self.get_modified_value(unique_secrets, lines) |
| 188 | + |
| 189 | + return FailResult( |
| 190 | + error_message=( |
| 191 | + "The following secrets were detected in your response:\n" |
| 192 | + + "\n".join(unique_secrets.keys()) |
| 193 | + ), |
| 194 | + fix_value=modified_value, |
| 195 | + ) |
| 196 | + return PassResult() |
0 commit comments