A bidirectional reversible desensitization middleware for coding agents.
Context Masker intercepts sensitive data before LLM API calls and restores it after, preventing sensitive information leakage when using external LLM APIs.
Agent Tool Output → [Mask] → LLM API → [Restore] → Agent
- Regex-based detection - Fast, deterministic detection of sensitive data
- Informative placeholders - LLM sees
<<EMAIL:0***>>instead of actual emails - Reversible masking - Original values restored in LLM responses
- CLI & Library - Use as command-line tool or import as TypeScript library
- Configurable categories - Enable/disable PII, credentials, infrastructure patterns
- Session-scoped storage - TTL-based mapping expiration
- Tool integration - Works with Claude Code, OpenCode, Cursor, Hermes, and more
- Logging & metrics - Track masking statistics and performance
- Custom patterns - Add your own detection rules
| Category | Patterns |
|---|---|
| PII | Email, phone, SSN, IPv4, credit card |
| Credentials | API keys, AWS secrets, passwords, OAuth tokens, JWTs, GitHub/Stripe/Slack tokens, private keys (RSA, EC, DSA, OpenSSH) |
| Infrastructure | Database URLs |
npm install context-maskerimport { createContextMasker } from 'context-masker';
const masker = createContextMasker();
// Mask before sending to LLM
const { masked } = masker.mask('Email: user@example.com, DB: postgres://admin:pass@host/db');
// masked: "Email: <<EMAIL:0***>>, DB: <<DATABASE_URL:0***>>"
// Restore after receiving LLM response
const restored = masker.restore('Contact <<EMAIL:0***>> at <<DATABASE_URL:0***>>');
// restored: "Contact user@example.com at postgres://admin:pass@host/db"# Mask sensitive data
context-masker mask 'Email: user@example.com, DB: postgres://admin:pass@host/db'
# Output: Email: <<EMAIL:0***>>, DB: <<DATABASE_URL:0***>>
# Restore placeholders
context-masker restore 'Contact <<EMAIL:0***>> at <<DATABASE_URL:0***>>'
# Output: Contact user@example.com at postgres://admin:pass@host/db
# Wrap a command to auto-mask output
context-masker wrap env | grep -i password
# Clear session
context-masker clear# Add to ~/.bashrc or ~/.zshrc
alias claude='context-masker wrap claude'Add to .cursorrules:
When handling tool outputs containing sensitive data (API keys, passwords,
database URLs, emails), use context-masker to mask them before sending to
LLM APIs.
# agent.config.yml
tools:
- name: shell
preUse: "context-masker mask"
postUse: "context-masker restore"# Pipe tool output through masker
tool_output=$(your-command)
masked=$(context-masker mask "$tool_output")
# Send $masked to LLMCreate config/default.toml:
enabled = ["pii", "credentials", "infrastructure"]
sessionTTL = 300000 # 5 minutes
[patterns]
pii = { email = true, phone = true, ssn = true, ipv4 = true, credit_card = true }
credentials = { api_key = true, aws_secret = true, password = true, jwt = true }
infrastructure = { database_url = true }Note: The
[patterns.*]booleans are currently documentation only. Per-pattern enable/disable is controlled at runtime viapatternFlagsin the config object.
Creates a new masker instance.
const masker = createContextMasker({
enabled: ['pii', 'credentials'], // Categories to detect
sessionTTL: 300000, // Session TTL in ms
logging: true, // Enable debug logging
patternFlags: { // Per-pattern enable/disable
pii: { credit_card: false },
},
customPatterns: [ // Add custom patterns
{
name: 'custom_id',
regex: '\\bID-[0-9]{6}\\b',
category: 'pii',
}
]
});Masks sensitive data in text.
const { masked, mappings } = masker.mask(text);
// masked: string - Text with numbered placeholders
// mappings: Map<string, string> - Placeholder to original value mapping
// e.g., "<<EMAIL:0***>>" → "user@example.com"Restores placeholders to original values.
const restored = masker.restore(text);Clears the session store.
masker.clear();Loads pre-existing placeholder-to-original mappings into the session store.
masker.loadMappings({ '<<EMAIL:0***>>': 'user@example.com' });Returns masking statistics.
const metrics = masker.getMetrics();
// {
// totalCalls: 10,
// totalMasked: 25,
// totalRestored: 20,
// detectionsByType: { email: 10, api_key: 8, ... },
// averageProcessingTime: 0.5
// }Adds a custom detection pattern.
masker.addCustomPattern({
name: 'internal_id',
regex: '\\bINT-[0-9]{8}\\b',
category: 'pii',
});Removes a custom pattern by name.
masker.removeCustomPattern('internal_id');Enable or disable debug logging.
masker.setLogging(true);Reset all metrics to zero.
masker.resetMetrics();MIT