File size: 435 Bytes
8d60e33
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
import re

SECRET_PATTERN = re.compile(r"(bearer\s+[a-z0-9\-.~+/]+=*|sk-[a-z0-9]{20,})", re.IGNORECASE)
EMAIL_PATTERN = re.compile(r"[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}", re.IGNORECASE)

def redact(text: str) -> str:
    """Redacts sensitive information like API keys and emails from a string."""
    text = SECRET_PATTERN.sub("[REDACTED_TOKEN]", text)
    text = EMAIL_PATTERN.sub("[REDACTED_EMAIL]", text)
    return text