Security
PII detection and redaction, and encryption for trace data at rest. The deployment-level
security model — tenant isolation, the judge trust boundary — is in
SECURITY.md.
uaef.security.pii
PII detection for UAEF.
This module provides functionality to detect Personally Identifiable Information (PII) in agent traces, evaluation data, and other sensitive content. It supports both regex-based detection (fast, deterministic) and ML-based detection (more accurate, requires additional libraries).
Requirements: 20.1-20.2, 24.1
BasePIIDetector
Bases: ABC
Base class for PII detectors.
Source code in uaef/security/pii.py
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | |
detect(text)
abstractmethod
Detect PII in text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Text to scan for PII |
required |
Returns:
| Type | Description |
|---|---|
List[PIIMatch]
|
List of PIIMatch objects |
Source code in uaef/security/pii.py
89 90 91 92 93 94 95 96 97 98 99 100 | |
MLPIIDetector
Bases: BasePIIDetector
ML-based PII detector (more accurate, requires additional libraries).
This is a placeholder/interface for ML-based detection using libraries like spaCy, Presidio, or custom models. The actual implementation would require additional dependencies and model loading.
Example usage with Presidio
from presidio_analyzer import AnalyzerEngine analyzer = AnalyzerEngine() results = analyzer.analyze(text=text, language='en')
Example usage with spaCy
import spacy nlp = spacy.load("en_core_web_sm") doc = nlp(text) for ent in doc.ents: if ent.label_ in ["PERSON", "GPE", "ORG"]: # Process entity
Source code in uaef/security/pii.py
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 | |
detect(text)
Detect PII in text using ML models.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Text to scan for PII |
required |
Returns:
| Type | Description |
|---|---|
List[PIIMatch]
|
List of PIIMatch objects |
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
ML detection not yet implemented |
Source code in uaef/security/pii.py
271 272 273 274 275 276 277 278 279 280 281 282 283 284 | |
PIIDetectionConfig
Bases: BaseModel
Configuration for PII detection.
Source code in uaef/security/pii.py
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | |
PIIDetector
Main PII detector that orchestrates detection strategies.
This class provides a unified interface for PII detection, supporting both regex-based and ML-based detection methods.
Source code in uaef/security/pii.py
287 288 289 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 | |
detect(text)
Detect PII in text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Text to scan for PII |
required |
Returns:
| Type | Description |
|---|---|
List[PIIMatch]
|
List of PIIMatch objects |
Raises:
| Type | Description |
|---|---|
ValueError
|
If text is invalid |
Source code in uaef/security/pii.py
313 314 315 316 317 318 319 320 321 322 323 324 325 326 | |
has_pii(text)
Check if text contains any PII.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Text to check |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if PII detected, False otherwise |
Source code in uaef/security/pii.py
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 | |
redact(text, *, placeholder_fmt='[REDACTED:{pii_type}]')
Replace detected PII spans in text with a placeholder.
Security review M-02: this is the redaction primitive that
uaef.security.pii.redact_for_storage (and the service's
opt-in persisted-storage redaction) build on. Only covers the
PII types this instance's detection_config.enabled_types is
configured for — by default EMAIL/PHONE/SSN/CREDIT_CARD (regex-
detectable); NAME/ADDRESS require use_ml_detection=True,
which raises NotImplementedError today, so text containing
only names/addresses and no other configured PII type passes
through unredacted. This is a real, documented limitation, not
silently glossed over — see the module docstring and the
PIIType enum's own note.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Text to redact |
required |
placeholder_fmt
|
str
|
Format string for the replacement, with a
|
'[REDACTED:{pii_type}]'
|
Returns:
| Type | Description |
|---|---|
str
|
Text with every detected PII span replaced. Returns the |
str
|
input unchanged if no PII is detected or the input is falsy. |
Source code in uaef/security/pii.py
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 | |
scan_dict(data)
Scan a dictionary for PII in string values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Dict[str, Any]
|
Dictionary to scan |
required |
Returns:
| Type | Description |
|---|---|
Dict[str, List[PIIMatch]]
|
Dictionary mapping keys to lists of PIIMatch objects |
Source code in uaef/security/pii.py
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 | |
PIIMatch
Bases: BaseModel
Represents a detected PII match.
Source code in uaef/security/pii.py
43 44 45 46 47 48 49 50 51 52 53 54 55 | |
PIIType
Bases: str, Enum
Types of PII that can be detected.
Note: NAME and ADDRESS require ML-based detection (not yet implemented). They will not be detected by the regex-based detector.
Source code in uaef/security/pii.py
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | |
RegexPIIDetector
Bases: BasePIIDetector
Regex-based PII detector (fast, deterministic).
Source code in uaef/security/pii.py
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 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 | |
detect(text)
Detect PII in text using regex patterns.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Text to scan for PII |
required |
Returns:
| Type | Description |
|---|---|
List[PIIMatch]
|
List of PIIMatch objects |
Raises:
| Type | Description |
|---|---|
ValueError
|
If text is None or empty |
Source code in uaef/security/pii.py
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 | |
create_default_detector()
Create a PIIDetector with default configuration.
Returns:
| Type | Description |
|---|---|
PIIDetector
|
PIIDetector instance with default settings |
Source code in uaef/security/pii.py
425 426 427 428 429 430 431 432 | |
detect_pii(text, pii_types=None)
Convenience function to detect PII in text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Text to scan |
required |
pii_types
|
Optional[Set[PIIType]]
|
PII types to detect (uses defaults if None) |
None
|
Returns:
| Type | Description |
|---|---|
List[PIIMatch]
|
List of PIIMatch objects |
Source code in uaef/security/pii.py
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 | |
redact_for_storage(text)
Redact EMAIL/PHONE/SSN/CREDIT_CARD spans in text before persisting it.
Security review M-02: prompts, agent responses, and ground-truth answers
were persisted (DynamoDB/S3) and rendered in the UI completely
un-redacted, with PII scanning both off the evaluation path and
NAME/ADDRESS detection unimplemented. This function is the storage-path
redaction primitive: it operates on already-computed evaluation output
(agent response text, ground-truth answers, judge reasoning) — it MUST
NOT be called anywhere upstream of a metric's own Bedrock judge call,
since redacting the text a judge scores would change evaluation results
for anything containing PII-shaped content, not just its handling in
storage. Every call site wiring this in (uaef-service's UI-result storage,
the library's experiment-persistence layer) applies it strictly after
batch_evaluate/evaluate has already returned scores, and each is
opt-in via its own env var — this function does not change behavior by
existing; something has to call it.
Uses the default (regex-based) detector's default enabled_types
(EMAIL/PHONE/SSN/CREDIT_CARD). NAME/ADDRESS are NOT redacted by this
function — PIIType.NAME/PIIType.ADDRESS require ML-based
detection, which is unimplemented (MLPIIDetector raises
NotImplementedError); this is a documented limitation, not a silent
gap introduced here.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Text to redact (e.g. an agent response, ground-truth answer, or judge reasoning string) |
required |
Returns:
| Type | Description |
|---|---|
str
|
Text with detected PII spans replaced by |
str
|
placeholders. Returns the input unchanged if it's falsy or contains |
str
|
no detectable PII. |
Source code in uaef/security/pii.py
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 | |
uaef.security.encryption
Encryption utilities for data at rest.
This module provides AES-256 encryption for sensitive data including agent traces, evaluation results, and experiment configurations. It supports customer-managed encryption keys (CMEK) and key rotation.
Validates: Requirements 24.2, 24.6
DataEncryptor
AES-256-GCM encryption for data at rest.
This class provides transparent encryption and decryption of sensitive data using AES-256 in GCM (Galois/Counter Mode) which provides both confidentiality and authenticity.
Features: - AES-256-GCM encryption (industry standard) - Customer-managed encryption keys (CMEK) - Key rotation support - Transparent encryption/decryption for dictionaries and objects
Example
config = EncryptionConfig(enabled=True, key_source="env") encryptor = DataEncryptor(config) encrypted = encryptor.encrypt("sensitive data") decrypted = encryptor.decrypt(encrypted)
Source code in uaef/security/encryption.py
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 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 288 289 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 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 | |
is_enabled
property
Check if encryption is enabled.
key_id
property
Get current key ID.
decrypt(encrypted_data)
Decrypt data encrypted with AES-256-GCM.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
encrypted_data
|
str
|
Base64-encoded encrypted data |
required |
Returns:
| Type | Description |
|---|---|
str
|
Decrypted data as string |
Raises:
| Type | Description |
|---|---|
EncryptionOperationError
|
If decryption fails |
ValueError
|
If encrypted data is invalid |
Source code in uaef/security/encryption.py
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 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 | |
decrypt_dict(encrypted_data)
Decrypt data and parse as dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
encrypted_data
|
str
|
Base64-encoded encrypted data |
required |
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
Decrypted dictionary |
Raises:
| Type | Description |
|---|---|
EncryptionOperationError
|
If decryption fails |
ValueError
|
If encrypted data is invalid |
Source code in uaef/security/encryption.py
546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 | |
encrypt(data)
Encrypt data using AES-256-GCM.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Union[str, bytes]
|
Data to encrypt (string or bytes) |
required |
Returns:
| Type | Description |
|---|---|
str
|
Base64-encoded encrypted data with format: version:key_id:iv:ciphertext:tag |
Raises:
| Type | Description |
|---|---|
EncryptionOperationError
|
If encryption fails |
ValueError
|
If data is invalid |
Source code in uaef/security/encryption.py
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 | |
encrypt_dict(data)
Encrypt a dictionary by converting to JSON and encrypting.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Dict[str, Any]
|
Dictionary to encrypt |
required |
Returns:
| Type | Description |
|---|---|
str
|
Base64-encoded encrypted data |
Raises:
| Type | Description |
|---|---|
EncryptionOperationError
|
If encryption fails |
ValueError
|
If data is invalid |
Source code in uaef/security/encryption.py
523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 | |
rotate_key(new_key, new_key_id, *, retain_old_key=True)
Rotate to a new encryption key.
New calls to :meth:encrypt use the new key immediately. Existing
ciphertext does NOT need to be re-encrypted for :meth:decrypt to
keep working: with retain_old_key=True (the default), the
outgoing key is kept in memory under its own key_id and
:meth:decrypt transparently selects the correct key based on the
key_id recorded in each ciphertext (security review L-02 — this
previously switched the key with no path back to the old one, so
anything encrypted before a rotation became permanently
undecryptable by this instance; see :meth:decrypt for the
resolution logic).
Retained old keys live only in this process's memory — they are
never persisted, and a new process (e.g. after a Lambda cold start)
will not have them unless re-supplied via whatever mechanism
originally provided them (this method doesn't prescribe one; it's
the caller's responsibility to call rotate_key with the
appropriate historical key material on process start if continuity
across restarts is required for non-KMS key sources). KMS-sourced
ciphertext does not have this limitation, since the wrapped key
travels with the ciphertext itself and any instance with KMS
permission can unwrap it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
new_key
|
bytes
|
New encryption key (32 bytes for AES-256) |
required |
new_key_id
|
str
|
Identifier for the new key |
required |
retain_old_key
|
bool
|
If True (default), keep the outgoing key available for decrypting ciphertext produced before this rotation. Set False only if you are certain no un-migrated ciphertext under the old key remains. |
True
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If new key is invalid |
Source code in uaef/security/encryption.py
568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 | |
EncryptionConfig
Bases: BaseModel
Configuration for encryption at rest.
Source code in uaef/security/encryption.py
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 | |
from_env()
classmethod
Load encryption configuration from environment variables.
Source code in uaef/security/encryption.py
97 98 99 100 101 102 103 104 105 106 107 108 109 | |
validate_algorithm(v)
classmethod
Validate algorithm is supported.
Source code in uaef/security/encryption.py
87 88 89 90 91 92 93 94 95 | |
validate_key_source(v)
classmethod
Validate key source is supported.
Source code in uaef/security/encryption.py
76 77 78 79 80 81 82 83 84 85 | |
EncryptionError
Bases: Exception
Base exception for encryption errors.
Source code in uaef/security/encryption.py
112 113 114 | |
EncryptionOperationError
Bases: EncryptionError
Exception raised when encryption/decryption operation fails.
Source code in uaef/security/encryption.py
122 123 124 | |
KeyLoadError
Bases: EncryptionError
Exception raised when encryption key cannot be loaded.
Source code in uaef/security/encryption.py
117 118 119 | |
get_encryptor()
Get the global data encryptor.
Loads from environment configuration on first call.
Returns:
| Type | Description |
|---|---|
DataEncryptor
|
DataEncryptor instance |
Source code in uaef/security/encryption.py
644 645 646 647 648 649 650 651 652 653 654 655 656 | |
reset_encryptor()
Reset the global encryptor to None.
Source code in uaef/security/encryption.py
670 671 672 673 | |
set_encryptor(encryptor)
Set the global data encryptor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
encryptor
|
DataEncryptor
|
DataEncryptor instance to set as global |
required |
Source code in uaef/security/encryption.py
659 660 661 662 663 664 665 666 667 | |