Microsoft Security Utilities Secret Masker
Microsoft Security Utilities - Secret Masker (version 1.0.0b4) is a Python library designed for the detection and masking of sensitive data. It provides built-in JSON-formatted detection rules, enabling users to identify and redact secrets using simple symbols or SHA256 hashes. This tool is part of Microsoft's internal security utilities and focuses on preventing secret exposure. It was last released on March 10, 2025, and is actively maintained in a beta state.
Warnings
- gotcha The library is currently in a beta release (1.0.0b4). This means that API interfaces, behavior, and underlying implementations may change without strict adherence to semantic versioning until a stable 1.0.0 release. Users should be prepared for potential adjustments when upgrading to newer beta or release candidate versions.
- gotcha In earlier iterations, the `SecretMasker.mask_secrets` method might have only returned the masked string without providing details about the detected secrets that triggered the masking. While a 'detection callback' was noted as added in later development, users on version 1.0.0b4 (or older) should be aware of this.
- gotcha The effectiveness of secret detection relies heavily on the completeness and accuracy of the loaded JSON regex patterns. The built-in patterns provide a good starting point, but they may not cover all custom or evolving secret formats. Outdated or insufficient patterns can lead to undetected secrets (false negatives).
- gotcha While this library helps mask secrets that appear in strings, the fundamental best practice for security is to avoid hardcoding secrets directly into code, configuration files, or committing them to version control systems. Relying solely on masking tools as a primary defense is a common cybersecurity mistake.
Install
-
pip install microsoft-security-utilities-secret-masker
Imports
- SecretMasker
from microsoft_security_utilities_secret_masker import SecretMasker
- load_regex_patterns_from_json_file
from microsoft_security_utilities_secret_masker import load_regex_patterns_from_json_file
Quickstart
from microsoft_security_utilities_secret_masker import SecretMasker, load_regex_patterns_from_json_file
# Load built-in detection rules
precisely_classified_regex_patterns = load_regex_patterns_from_json_file('PreciselyClassifiedSecurityKeys.json')
unclassified_regex_patterns = load_regex_patterns_from_json_file('UnclassifiedPotentialSecurityKeys.json')
# Combine patterns
regex_patterns = precisely_classified_regex_patterns.union(unclassified_regex_patterns)
# Construct secret masker with chosen patterns
secret_masker = SecretMasker(regex_patterns)
# Example usage
input_text = "My API key is sk-1234567890abcdef1234567890abcdef and my email is test@example.com"
detected_secrets = secret_masker.detect_secrets(input_text)
print(f"Detected secrets: {detected_secrets}")
processed_input = secret_masker.mask_secrets(input_text)
print(f"Masked input: {processed_input}")
# Example with custom masking character (e.g., SHA256)
processed_input_sha256 = secret_masker.mask_secrets(input_text, mask_with_sha256=True)
print(f"Masked with SHA256: {processed_input_sha256}")