Skip to content

Commit

Permalink
Merge pull request #292 from cognoa/fix/json-redaction
Browse files Browse the repository at this point in the history
Implement deep JSON redaction
  • Loading branch information
kean authored Sep 18, 2024
2 parents 855411e + 00e532f commit 29c1ad5
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions Sources/Pulse/NetworkLogger/NetworkLogger+Redacting.swift
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,20 @@ private extension Data {
private func _redactingSensitiveFields(_ value: Any, _ fields: Set<String>) -> Any {
switch value {
case var object as [String: Any]:
for key in object.keys.filter(fields.contains) {
object[key] = "<private>"
for key in object.keys {
if fields.contains(key) {
// Redact any value (including objects and arrays) matching the key
object[key] = "<private>"
} else if let value = object[key] as? [String: Any] {
// Recessively process values that are objects
object[key] = _redactingSensitiveFields(value, fields)
} else if var value = object[key] as? [Any] {
// Recessively process values that are arrays
value = value.map({ _redactingSensitiveFields($0, fields) })
object[key] = value
} else {
// All other key/values are left untouched.
}
}
return object
case let array as [Any]:
Expand Down

0 comments on commit 29c1ad5

Please sign in to comment.