"""Detection/redaction de secrets avant envoi a l'assistant IA.""" import pathlib import sys sys.path.insert(0, str(pathlib.Path(__file__).resolve().parent.parent)) from app.ai.redaction import detect_secrets, redact print("=== 1. Cle privee PEM detectee ===") text = "-----BEGIN RSA PRIVATE KEY-----\nMIIEowIBAAKCAQEA...\n-----END RSA PRIVATE KEY-----" found = detect_secrets(text) print(" detecte:", [f.label for f in found]) assert any("privee" in f.label.lower() for f in found) print(" OK") print() print("=== 2. Jeton Bearer detecte ===") found = detect_secrets("Authorization: Bearer sk-abcdefghijklmnopqrstuvwxyz123456") labels = [f.label for f in found] print(" detecte:", labels) assert any("bearer" in label.lower() for label in labels) assert any("authorization" in label.lower() for label in labels) print(" OK") print() print("=== 3. Cle AWS detectee ===") found = detect_secrets("export AWS_ACCESS_KEY_ID=AKIAIOSFODNN7EXAMPLE") assert any("aws" in f.label.lower() for f in found) print(" OK") print() print("=== 4. password= / api_key= / token= detectes ===") assert any("mot de passe" in f.label.lower() for f in detect_secrets("password=Sup3rSecret!")) assert any("api" in f.label.lower() for f in detect_secrets("api_key=abcd1234")) assert any("jeton" in f.label.lower() for f in detect_secrets("token: eyJhbGciOiJIUzI1NiJ9")) print(" OK") print() print("=== 5. Chaine longue ressemblant a un secret ===") found = detect_secrets("valeur=" + "a1b2c3d4e5f6g7h8i9j0" * 3) assert any("longue" in f.label.lower() for f in found) print(" OK") print() print("=== 6. Texte anodin : rien detecte ===") assert detect_secrets("systemd[1]: nginx.service: Failed with result 'exit-code'") == [] assert detect_secrets("nginx: [emerg] bind() to 0.0.0.0:443 failed (98: Address already in use)") == [] print(" OK") print() print("=== 7. redact() masque sans lever, texte anodin inchange ===") redacted = redact("password=abc123 et Bearer xyzxyzxyzxyzxyzxyz") print(" resultat:", redacted) assert "abc123" not in redacted assert "xyzxyzxyzxyzxyzxyz" not in redacted assert "[MASQUE]" in redacted clean = "systemctl status nginx" assert redact(clean) == clean print(" OK") print() print("AI REDACTION : TOUS LES TESTS OK")