Secrets Detection
DetectSecretsModel
Bases: Model
Model for detecting secrets using the detect-secrets library.
Source code in safeguards/guardrails/secrets_detection/secrets_detection.py
invoke(text)
Invokes the scan method to detect secrets in the given text.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
text
|
str
|
The text to scan for secrets. |
required |
Returns:
Type | Description |
---|---|
dict[str, list[SecretsInfo]]
|
dict[str, list[SecretsInfo]]: A dictionary where the keys are secret types and the values are lists of SecretsInfo objects. |
Source code in safeguards/guardrails/secrets_detection/secrets_detection.py
scan(text)
staticmethod
Scans the given text for secrets using the detect-secrets library.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
text
|
str
|
The text to scan for secrets. |
required |
Returns:
Type | Description |
---|---|
dict[str, list[SecretsInfo]]
|
dict[str, list[SecretsInfo]]: A dictionary where the keys are secret types and the values are lists of SecretsInfo objects. |
Source code in safeguards/guardrails/secrets_detection/secrets_detection.py
HyperScanModel
Bases: Model
Model for detecting secrets using the Hyperscan library. We use the Hyperscan library to scan for secrets using regex patterns. The patterns are mined from https://github.com/mazen160/secrets-patterns-db This model is used in conjunction with the DetectSecretsModel to improve the detection of secrets.
Source code in safeguards/guardrails/secrets_detection/secrets_detection.py
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 |
|
__init__(**kwargs)
invoke(text)
Invokes the scan method to detect secrets in the given text.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
text
|
str
|
The text to scan for secrets. |
required |
Returns:
Type | Description |
---|---|
dict[str, list[SecretsInfo]]
|
dict[str, list[SecretsInfo]]: A dictionary where the keys are secret types and the values are lists of SecretsInfo objects. |
Source code in safeguards/guardrails/secrets_detection/secrets_detection.py
model_post_init(__context)
Post-initialization method to load patterns and compile the Hyperscan database.
Source code in safeguards/guardrails/secrets_detection/secrets_detection.py
scan(text)
Scans the given text for secrets using the Hyperscan library.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
text
|
str
|
The text to scan for secrets. |
required |
Returns:
Type | Description |
---|---|
dict[str, list[SecretsInfo]]
|
dict[str, list[SecretsInfo]]: A dictionary where the keys are secret types and the values are lists of SecretsInfo objects. |
Source code in safeguards/guardrails/secrets_detection/secrets_detection.py
REDACTION
Bases: str
, Enum
Enum for different types of redaction modes.
Source code in safeguards/guardrails/secrets_detection/secrets_detection.py
ScanResult
Bases: BaseModel
Model representing the result of a secrets scan.
Attributes:
Name | Type | Description |
---|---|---|
detected_secrets |
dict[str, Any] | None
|
Dictionary of detected secrets, or None if no secrets were found. |
modified_prompt |
str
|
The modified prompt with secrets redacted. |
has_secret |
bool
|
Indicates if any secrets were detected. |
risk_score |
float
|
The risk score of the detection result. |
Source code in safeguards/guardrails/secrets_detection/secrets_detection.py
SecretsDetectionGuardrail
Bases: Guardrail
Guardrail class for secrets detection using both detect-secrets and Hyperscan models.
Attributes:
Name | Type | Description |
---|---|---|
redaction |
REDACTION
|
The redaction mode to be applied. |
_detect_secrets_model |
Any
|
Instance of the DetectSecretsModel. |
_hyperscan_model |
Any
|
Instance of the HyperScanModel. |
Source code in safeguards/guardrails/secrets_detection/secrets_detection.py
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 |
|
__init__(redaction=REDACTION.REDACT_ALL, **kwargs)
Initializes the SecretsDetectionGuardrail instance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
redaction
|
REDACTION
|
The redaction mode to be applied. Defaults to REDACTION.REDACT_ALL. |
REDACT_ALL
|
**kwargs
|
Additional keyword arguments. |
{}
|
Source code in safeguards/guardrails/secrets_detection/secrets_detection.py
get_modified_value(unique_secrets, lines)
Redacts the detected secrets in the given lines of text.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
unique_secrets
|
dict[str, Any]
|
Dictionary of detected secrets. |
required |
lines
|
list[str]
|
List of lines of text. |
required |
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
The modified text with secrets redacted. |
Source code in safeguards/guardrails/secrets_detection/secrets_detection.py
get_scan_result(unique_secrets, lines)
Generates a ScanResult based on the detected secrets.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
unique_secrets
|
dict[str, list[SecretsInfo]]
|
Dictionary of detected secrets. |
required |
lines
|
list[str]
|
List of lines of text. |
required |
Returns:
Type | Description |
---|---|
ScanResult | None
|
ScanResult | None: The scan result if secrets are detected, otherwise None. |
Source code in safeguards/guardrails/secrets_detection/secrets_detection.py
guard(prompt, return_detected_secrets=True, **kwargs)
Guards the given prompt by scanning for secrets and optionally returning detected secrets.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prompt
|
str
|
The text to scan for secrets. |
required |
return_detected_secrets
|
bool
|
Whether to return detected secrets in the response. Defaults to True. |
True
|
**kwargs
|
Additional keyword arguments. |
{}
|
Returns:
Type | Description |
---|---|
SecretsDetectionResponse | SecretsDetectionResponse
|
SecretsDetectionResponse | SecretsDetectionSimpleResponse: The response with scan results and redacted text. |
Source code in safeguards/guardrails/secrets_detection/secrets_detection.py
model_post_init(__context)
Post-initialization method to initialize the detect-secrets and Hyperscan models.
Source code in safeguards/guardrails/secrets_detection/secrets_detection.py
scan(prompt)
Scans the given prompt for secrets using both detect-secrets and Hyperscan models.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prompt
|
str
|
The text to scan for secrets. |
required |
Returns:
Name | Type | Description |
---|---|---|
ScanResult |
ScanResult
|
The scan result with detected secrets and redacted text. |
Source code in safeguards/guardrails/secrets_detection/secrets_detection.py
SecretsDetectionResponse
Bases: SecretsDetectionSimpleResponse
A detailed response model for secrets detection.
Attributes:
Name | Type | Description |
---|---|---|
detected_secrets |
dict[str, list[str]]
|
Dictionary of detected secrets. |
Source code in safeguards/guardrails/secrets_detection/secrets_detection.py
SecretsDetectionSimpleResponse
Bases: BaseModel
A simple response model for secrets detection.
Attributes:
Name | Type | Description |
---|---|---|
contains_secrets |
bool
|
Indicates if secrets were detected. |
explanation |
str
|
Explanation of the detection result. |
redacted_text |
Optional[str]
|
The redacted text if secrets were found. |
risk_score |
float
|
The risk score of the detection result. (0.0, 0.5, 1.0) |
Source code in safeguards/guardrails/secrets_detection/secrets_detection.py
safe
property
Property to check if the text is safe (no secrets detected).
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if no secrets were detected, False otherwise. |
SecretsInfo
Bases: BaseModel
Model representing information about a detected secret.
Attributes:
Name | Type | Description |
---|---|---|
secret |
str
|
The detected secret value. |
line_number |
int
|
The line number where the secret was found. |
Source code in safeguards/guardrails/secrets_detection/secrets_detection.py
redact_value(value, mode)
Redacts the given value based on the specified redaction mode.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value
|
str
|
The string value to be redacted. |
required |
mode
|
str
|
The redaction mode to be applied. It can be one of the following: - REDACTION.REDACT_PARTIAL: Partially redacts the value. - REDACTION.REDACT_ALL: Fully redacts the value. - REDACTION.REDACT_HASH: Redacts the value by hashing it. - REDACTION.REDACT_NONE: No redaction is applied. |
required |
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
The redacted value based on the specified mode. |