Skip to content

Commit 5b8cc1c

Browse files
committed
chore: Improve redaction logic to support more complex paths
1 parent 8f8f6a0 commit 5b8cc1c

4 files changed

Lines changed: 321 additions & 23 deletions

File tree

roborock/devices/device_manager.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,9 @@ async def close(self) -> None:
138138
def diagnostic_data(self) -> Mapping[str, Any]:
139139
"""Return diagnostics information about the device manager."""
140140
return {
141-
**self._diagnostics.as_dict(),
142141
"home_data": redact_device_data(asdict(self._home_data) if self._home_data else {}),
143142
"devices": [device.diagnostic_data() for device in self._devices.values()],
143+
"diagnostics": self._diagnostics.as_dict(),
144144
}
145145

146146

roborock/diagnostics.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -102,39 +102,49 @@ def reset(self) -> None:
102102
"mapData",
103103
"rawApiResponse",
104104
# Home data
105-
"id",
105+
"id", # We want to redact home_data.id but keep some other ids, see below
106106
"name",
107107
"productId",
108108
"ipAddress",
109109
"mac",
110110
"wifiName",
111-
"schema", # Large
112111
"lat",
113112
"long",
114113
}
114+
KEEP_KEYS = {
115+
# Product information no unique per user
116+
"product.id",
117+
"schema.id",
118+
# Room ids are likely unique per user, but don't seem too sensitive and are
119+
# useful for debugging
120+
"rooms.id",
121+
}
115122
DEVICE_UID = "duid"
116123
REDACTED = "**REDACTED**"
117124

118125

119-
def redact_device_data(data: T) -> T | dict[str, Any]:
126+
def redact_device_data(data: T, path: str = "") -> T | dict[str, Any]:
120127
"""Redact sensitive data in a dict."""
121128
if not isinstance(data, (Mapping, list)):
122129
return data
123130

124131
if isinstance(data, list):
125-
return cast(T, [redact_device_data(item) for item in data])
132+
return cast(T, [redact_device_data(item, path) for item in data])
126133

127134
redacted = {**data}
128135

129136
for key, value in redacted.items():
130-
if key in REDACT_KEYS:
137+
curr_path = f"{path}.{key}" if path else key
138+
if key in KEEP_KEYS or curr_path in KEEP_KEYS:
139+
continue
140+
if key in REDACT_KEYS or curr_path in REDACT_KEYS:
131141
redacted[key] = REDACTED
132142
elif key == DEVICE_UID and isinstance(value, str):
133143
redacted[key] = redact_device_uid(value)
134144
elif isinstance(value, dict):
135-
redacted[key] = redact_device_data(value)
145+
redacted[key] = redact_device_data(value, curr_path)
136146
elif isinstance(value, list):
137-
redacted[key] = [redact_device_data(item) for item in value]
147+
redacted[key] = [redact_device_data(item, curr_path) for item in value]
138148

139149
return redacted
140150

0 commit comments

Comments
 (0)