scrubPii function

Map<String, dynamic> scrubPii(
  1. Map<String, dynamic> data, {
  2. String? homeDir,
})

Scrub PII from a map of telemetry properties.

Replaces email addresses, API keys / tokens, and absolute file paths that contain the user's home directory with redacted placeholders.

Implementation

Map<String, dynamic> scrubPii(Map<String, dynamic> data, {String? homeDir}) {
  final home = homeDir ?? Platform.environment['HOME'] ?? '/home/user';
  final result = <String, dynamic>{};

  for (final entry in data.entries) {
    final value = entry.value;
    if (value is String) {
      result[entry.key] = _scrubString(value, home);
    } else if (value is Map<String, dynamic>) {
      result[entry.key] = scrubPii(value, homeDir: home);
    } else if (value is List) {
      result[entry.key] = value.map((v) {
        if (v is String) return _scrubString(v, home);
        if (v is Map<String, dynamic>) return scrubPii(v, homeDir: home);
        return v;
      }).toList();
    } else {
      result[entry.key] = value;
    }
  }
  return result;
}