redact.ipAddress
v0.1.0 latestRedact an IP address per RFC 6235 by masking the host portion, consistent with GDPR /24 (IPv4) and /48 (IPv6) truncation practice.
Redact an IP address per RFC 6235 by masking the host portion, consistent with GDPR /24 (IPv4) and /48 (IPv6) truncation practice.
Signature
function ipAddress(v: string): string
Problem
IP addresses in server logs, analytics events, and error reports constitute personal data under GDPR. Full IP storage requires consent; full removal prevents network-level debugging. Standard practice is to truncate to the network prefix, consistent with Google Analytics IP anonymization.
How It Works
Auto-detects the IP version. IPv4: keeps the first 3 octets and replaces the last with '*' (/24 truncation). IPv6: keeps the first 3 groups and masks the remaining 5 groups (/48 truncation). IPv4-mapped IPv6 addresses (::ffff:x.x.x.x) are detected and the embedded IPv4 is masked separately.
Boundaries
- Does not validate IP address ranges or reserved addresses.
- IPv6 addresses with fewer than 4 groups return '***'.
- Unrecognized formats (not IPv4, IPv6, or IPv4-mapped) return '***'.
Replaces
Common boilerplate this function replaces:
ip.split('.').slice(0, 3).join('.') + '.*'
Examples
redact.ipAddress("192.168.1.100"); // "192.168.1.*"
redact.ipAddress("2001:0db8:85a3::1"); // "2001:0db8:85a3:*:*:*:*:*"
redact.ipAddress("::ffff:10.0.0.1"); // "::ffff:10.0.0.*"
redact.ipAddress(""); // "***"
redact.ipAddress("not-an-ip"); // "***"
Standards
- GDPR Art. 5 — Principles of Processing
- RFC 6235 — IP Flow Anonymization Support
- RFC 791 — Internet Protocol (IPv4)
Caveats
- IPv6 truncation always produces exactly 8 colon-separated groups in the output, regardless of the input's use of '::' abbreviation.
- The /24 (IPv4) and /48 (IPv6) truncation levels are fixed and not configurable.
FAQ
How to anonymize IP addresses for GDPR compliance in JavaScript?
redact.ipAddress('192.168.1.100') returns '192.168.1.*' — /24 truncation consistent with Google Analytics IP anonymization and GDPR practice.
How to mask IPv6 addresses in server logs?
redact.ipAddress('2001:0db8:85a3::1') returns '2001:0db8:85a3:::::*' — keeps the first 3 groups (/48 prefix).
How to handle IPv4-mapped IPv6 addresses during redaction?
The function detects '::ffff:' prefixed addresses and applies IPv4 masking to the embedded address portion.
What truncation level is used for IP anonymization?
/24 for IPv4 (last octet masked) and /48 for IPv6 (last 5 groups masked). These levels are consistent with industry practice for network-level anonymization.
How to redact IP addresses without losing network information?
The /24 and /48 truncation preserves the network prefix, allowing subnet-level analysis while removing host-specific information.