Sanitize Redfish string fields to strip non-ASCII/non-printable characters
IOM1's HostName field contains binary artifacts that decode to garbage unicode characters (e.g. prefixing a UUID). Strip any character outside printable ASCII (0x20–0x7F) from all string fields returned by _parse_network_data. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -27,6 +27,12 @@ from ui import (
|
||||
|
||||
# ── Shared helpers ─────────────────────────────────────────────────────────────
|
||||
|
||||
def _sanitize(value: str) -> str:
|
||||
"""Strip non-printable and non-ASCII characters from a string field.
|
||||
IOM Redfish responses occasionally contain binary artifacts in text fields."""
|
||||
return "".join(c for c in value if 32 <= ord(c) < 128)
|
||||
|
||||
|
||||
def _parse_network_data(data: dict) -> tuple:
|
||||
"""
|
||||
Extract network details from a Redfish EthernetInterfaces/1 response dict.
|
||||
@@ -41,15 +47,15 @@ def _parse_network_data(data: dict) -> tuple:
|
||||
addrs = data.get("IPv4StaticAddresses") or data.get("IPv4Addresses", [])
|
||||
if addrs:
|
||||
addr = addrs[0]
|
||||
ip = addr.get("Address", "--")
|
||||
gateway = addr.get("Gateway", "--")
|
||||
netmask = addr.get("SubnetMask", "--")
|
||||
ip = _sanitize(addr.get("Address", "--"))
|
||||
gateway = _sanitize(addr.get("Gateway", "--"))
|
||||
netmask = _sanitize(addr.get("SubnetMask", "--"))
|
||||
else:
|
||||
ip = gateway = netmask = "--"
|
||||
|
||||
mac = data.get("MACAddress", "--")
|
||||
hostname = data.get("HostName", "--")
|
||||
link_status = data.get("LinkStatus", "--")
|
||||
mac = _sanitize(data.get("MACAddress", "--"))
|
||||
hostname = _sanitize(data.get("HostName", "--"))
|
||||
link_status = _sanitize(data.get("LinkStatus", "--"))
|
||||
|
||||
return dhcp_enabled, ip, gateway, netmask, mac, hostname, link_status
|
||||
|
||||
|
||||
Reference in New Issue
Block a user