A lightweight, extensible Go module for sanitizing and masking sensitive data
built to prevent accidental leakage of secrets, credentials, PII, and financial
information into logs, monitoring systems, or third-party tools.
Designed with real-world logging safety in mind.
- 🔐 Mask authentication data
(passwords, tokens, API keys, session IDs, OTPs) - 💳 Mask financial data
(card PAN, CVV, IBAN) - 🧑💼 Mask PII
(email, mobile numbers, identifiers) - 🧠 Rule-based and extensible registry
- 🧬 Recursive sanitization (nested maps & slices)
- 📦 Built-in helpers for:
- JSON payloads (
[]byte) - Query parameters (
url.Values) - HTTP headers (
http.Header)
- JSON payloads (
- 🧪 Fully unit-tested (table-driven tests)
go get github.com/imRezaAlie/sanitizer@latestimport "github.com/imRezaAlie/sanitizer/sanitize"
payload := map[string]any{
"email": "ali@gmail.com",
"password": "123456",
"token": "eyJhbGciOi...",
}
safe := sanitize.SanitizeAny(payload)map[string]any{
"email": "a***@gmail.com",
"password": "***",
"token": "***",
}🧩 Supported Data Types
- Any / map / slice (recursive)
sanitize.SanitizeAny(data)- JSON payloads
safeJSON, err := sanitize.SanitizeJSON(rawJSON)If the JSON is invalid, the original input is returned safely. 4. HTTP Headers
safeHeaders := sanitize.SanitizeHeaders(req.Header)Authorization, cookies, and sensitive headers are masked automatically.
You can define your own registry and rules:
r := sanitize.NewRegistry()
sanitize.RegisterDefaults(r)
r.Register(sanitize.Rule{
Name: "custom-secret",
KeyRegex: regexp.MustCompile(`(?i)secret_value`),
Action: sanitize.ActionMask,
})
safe := r.SanitizeAny(payload)- password / pwd
- token / jwt / access_token / refresh_token
- api_key / secret_key
- session_id
- otp
- Card number (6 first + 4 last digits)
- CVV / CVC (removed)
- IBAN
- Email (partial mask)
- Mobile number (digits-only masking)
- sensitive fields
Contributions are very welcome ❤️
- Fork the repository
- Create a new branch (
feat/...,fix/...) - Add tests for new behavior
- Run
go test ./... - Open a Pull Request