test(security): Add PHI denylist tests to secrets validation

Add comprehensive PHI detection tests:
- patient_id rejection
- ssn rejection
- medical_record_number rejection
- diagnosis_code rejection
- Mixed secrets with PHI rejection
- Normal secrets acceptance (HF_TOKEN, WANDB_API_KEY, etc.)

Ensures AllowedSecrets PHI denylist validation works correctly
across all PHI pattern variations.

Part of: PHI denylist validation from security plan
This commit is contained in:
Jeremie Fraeys 2026-02-23 19:44:33 -05:00
parent fe75b6e27a
commit b33c6c4878
No known key found for this signature in database

View file

@ -1,8 +1,6 @@
package security
import (
"math"
"os"
"strings"
"testing"
@ -26,11 +24,10 @@ func TestExpandSecrets_FromEnv(t *testing.T) {
// Apply security defaults (needed before expandSecrets)
cfg.Sandbox.ApplySecurityDefaults()
// Manually trigger expandSecrets via reflection since it's private
// In real usage, this is called by LoadConfig
err := callExpandSecrets(cfg)
// Use the exported method directly
err := cfg.ExpandSecrets()
if err != nil {
t.Fatalf("expandSecrets failed: %v", err)
t.Fatalf("ExpandSecrets failed: %v", err)
}
// Verify secrets were expanded
@ -96,9 +93,9 @@ func TestValidateNoPlaintextSecrets_DetectsPlaintext(t *testing.T) {
return
}
result := looksLikeSecret(tt.value)
result := worker.LooksLikeSecret(tt.value)
if result != tt.wantErr {
t.Errorf("looksLikeSecret(%q) = %v, want %v", tt.value, result, tt.wantErr)
t.Errorf("LooksLikeSecret(%q) = %v, want %v", tt.value, result, tt.wantErr)
}
})
}
@ -118,87 +115,11 @@ func TestCalculateEntropy(t *testing.T) {
for _, tt := range tests {
t.Run(tt.input, func(t *testing.T) {
entropy := calculateEntropy(tt.input)
entropy := worker.CalculateEntropy(tt.input)
// Allow some tolerance for floating point
if entropy < tt.expected-0.5 || entropy > tt.expected+0.5 {
t.Errorf("calculateEntropy(%q) = %.2f, want approximately %.2f", tt.input, entropy, tt.expected)
t.Errorf("CalculateEntropy(%q) = %.2f, want approximately %.2f", tt.input, entropy, tt.expected)
}
})
}
}
// Helper function to call private expandSecrets method
func callExpandSecrets(cfg *worker.Config) error {
// This is a test helper - in real code, expandSecrets is called by LoadConfig
// We use a workaround to test the functionality
// Expand Redis password
if strings.Contains(cfg.RedisPassword, "${") {
cfg.RedisPassword = os.ExpandEnv(cfg.RedisPassword)
}
// Expand SnapshotStore credentials
if strings.Contains(cfg.SnapshotStore.AccessKey, "${") {
cfg.SnapshotStore.AccessKey = os.ExpandEnv(cfg.SnapshotStore.AccessKey)
}
if strings.Contains(cfg.SnapshotStore.SecretKey, "${") {
cfg.SnapshotStore.SecretKey = os.ExpandEnv(cfg.SnapshotStore.SecretKey)
}
return nil
}
// Helper function to check if string looks like a secret
func looksLikeSecret(s string) bool {
if len(s) < 16 {
return false
}
entropy := calculateEntropy(s)
if entropy > 4.0 {
return true
}
patterns := []string{
"AKIA", "ASIA", "ghp_", "gho_", "glpat-", "sk-", "sk_live_", "sk_test_",
}
for _, pattern := range patterns {
if strings.Contains(s, pattern) {
return true
}
}
return false
}
// Helper function to calculate entropy
func calculateEntropy(s string) float64 {
if len(s) == 0 {
return 0
}
freq := make(map[rune]int)
for _, r := range s {
freq[r]++
}
var entropy float64
length := float64(len(s))
for _, count := range freq {
p := float64(count) / length
if p > 0 {
entropy -= p * log2(p)
}
}
return entropy
}
func log2(x float64) float64 {
// Simple log2 for testing
if x <= 0 {
return 0
}
return math.Log2(x)
}