_spoolman_helpers.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. """Pure helper functions for Spoolman spool mapping.
  2. No heavy dependencies — importable in unit tests without the full backend stack.
  3. """
  4. from __future__ import annotations
  5. import ipaddress
  6. import json
  7. import logging
  8. import math
  9. import re
  10. from typing import Any
  11. from urllib.parse import urlparse
  12. from typing_extensions import TypedDict
  13. logger = logging.getLogger(__name__)
  14. class MappedSpoolFields(TypedDict):
  15. """Full shape of the dict returned by _map_spoolman_spool (InventorySpool-compatible)."""
  16. id: int
  17. material: str | None
  18. subtype: str | None
  19. brand: str | None
  20. color_name: str | None
  21. rgba: str | None
  22. label_weight: int | None
  23. core_weight: int | None
  24. core_weight_catalog_id: None
  25. weight_used: float | None
  26. weight_locked: bool
  27. last_scale_weight: None
  28. last_weighed_at: None
  29. slicer_filament: None
  30. slicer_filament_name: str | None
  31. nozzle_temp_min: int | None
  32. nozzle_temp_max: None
  33. note: str | None
  34. added_full: None
  35. last_used: str | None
  36. encode_time: str | None
  37. tag_uid: str | None
  38. tray_uuid: str | None
  39. data_origin: str | None
  40. tag_type: str | None
  41. archived_at: str | None
  42. created_at: str | None # None when Spoolman spool has no registered timestamp
  43. updated_at: str | None
  44. cost_per_kg: float | None
  45. storage_location: str | None
  46. k_profiles: list[Any]
  47. class NormalizedVendorRef(TypedDict):
  48. """Vendor reference embedded in a NormalizedFilament."""
  49. id: int
  50. name: str
  51. class NormalizedFilament(TypedDict):
  52. """Normalised Spoolman filament dict returned by the /filaments catalog endpoint."""
  53. id: int
  54. name: str
  55. material: str | None
  56. color_hex: str | None
  57. color_name: str | None
  58. weight: int | None
  59. spool_weight: float | None
  60. vendor: NormalizedVendorRef | None
  61. _CLOUD_METADATA_IPS = frozenset(
  62. {
  63. # AWS / GCP / Azure / Oracle / DigitalOcean IMDS
  64. ipaddress.ip_address("169.254.169.254"),
  65. # Alibaba Cloud metadata
  66. ipaddress.ip_address("100.100.100.200"),
  67. # AWS IMDS IPv6
  68. ipaddress.ip_address("fd00:ec2::254"),
  69. }
  70. )
  71. def assert_safe_spoolman_url(url: str) -> None:
  72. """Raise ValueError if *url* should be blocked as an SSRF risk.
  73. Bambuddy is typically deployed on a home LAN alongside Spoolman, so
  74. loopback (127.0.0.1) and RFC-1918 private ranges (192.168.x.x, 10.x.x.x,
  75. 172.16-31.x) must be permitted — they are THE normal Spoolman topology.
  76. This guard therefore targets the genuinely dangerous cases only.
  77. Checks performed:
  78. - Scheme must be http or https (no file://, gopher://, dict://, etc.).
  79. - Numeric-encoded IP addresses in decimal (e.g. ``2130706433``) or hex
  80. (e.g. ``0x7f000001``) are rejected. Python's ``ipaddress`` module raises
  81. ``ValueError`` for these forms so they would otherwise bypass the
  82. explicit-IP block below, but libc (and browsers) resolve them as valid
  83. IPv4 addresses.
  84. - Cloud provider metadata endpoints (169.254.169.254, 100.100.100.200,
  85. fd00:ec2::254) are blocked — the classic SSRF credential-exfil target.
  86. - Multicast (224.0.0.0/4, ff00::/8) and unspecified (0.0.0.0, ::) addresses
  87. are blocked — pointless as a destination and suggests misuse.
  88. - IPv4-mapped IPv6 addresses (::ffff:x.x.x.x) are unwrapped so they cannot
  89. bypass the checks above.
  90. Hostname-based addresses ("localhost", "spoolman.lan", "internal.corp")
  91. are out of scope — DNS resolution is deliberately not performed here.
  92. """
  93. parsed = urlparse(url)
  94. if parsed.scheme.lower() not in ("http", "https"):
  95. raise ValueError("Spoolman URL must use http or https")
  96. hostname = (parsed.hostname or "").lower()
  97. # Reject decimal- and hex-encoded IPs (e.g. http://2130706433/ or
  98. # http://0x7f000001/). These slip past ipaddress.ip_address() but libc
  99. # (and browsers) parse them as IPv4 — an obvious bypass if not caught.
  100. if re.match(r"^(0x[0-9a-f]+|[0-9]+)$", hostname, re.I):
  101. raise ValueError("Spoolman URL must not use numeric-encoded IP addresses; use standard dotted-decimal notation")
  102. try:
  103. addr = ipaddress.ip_address(hostname)
  104. except ValueError:
  105. # Not a bare IP address — includes intentional cases such as "localhost" and
  106. # RFC-1918 hostnames ("spoolman.lan", "192.168.1.10" would be caught above as
  107. # a dotted-decimal IP; symbolic names resolve via DNS which is out of scope).
  108. # Running Spoolman on the same host or home LAN is the standard Bambuddy
  109. # topology, so loopback and private ranges are deliberately NOT blocked here.
  110. return
  111. # Unwrap IPv4-mapped IPv6 (::ffff:169.254.169.254 etc.) so attackers can't
  112. # encode a blocked IPv4 into an IPv6 literal to bypass the check.
  113. effective: ipaddress.IPv4Address | ipaddress.IPv6Address = addr
  114. if isinstance(addr, ipaddress.IPv6Address) and addr.ipv4_mapped is not None:
  115. effective = addr.ipv4_mapped
  116. if effective in _CLOUD_METADATA_IPS:
  117. raise ValueError("Spoolman URL must not point to a cloud metadata endpoint")
  118. if effective.is_multicast or effective.is_unspecified:
  119. raise ValueError("Spoolman URL must not point to a multicast or unspecified address")
  120. _COLOR_HEX_RE = re.compile(r"^[0-9A-Fa-f]{6}$")
  121. _TAG_HEX_RE = re.compile(r"^[0-9A-F]+$")
  122. def _safe_int(value: object, fallback: int) -> int:
  123. """Convert value to int, returning fallback for None/NaN/Inf/non-numeric."""
  124. try:
  125. f = float(value) # type: ignore[arg-type]
  126. if math.isfinite(f):
  127. return int(f)
  128. except (TypeError, ValueError):
  129. pass
  130. return fallback
  131. def _safe_float(value: object, fallback: float) -> float:
  132. """Convert value to float, returning fallback for None/NaN/Inf/non-numeric."""
  133. try:
  134. f = float(value) # type: ignore[arg-type]
  135. if math.isfinite(f):
  136. return f
  137. except (TypeError, ValueError):
  138. pass
  139. return fallback
  140. def _safe_optional_float(value: object) -> float | None:
  141. """Convert value to finite float, or None if missing/NaN/Infinite/non-numeric.
  142. Used for optional monetary fields (price) to prevent Infinity/NaN from
  143. reaching JSON serialisation, which raises ValueError with allow_nan=False.
  144. """
  145. if value is None:
  146. return None
  147. try:
  148. f = float(value) # type: ignore[arg-type]
  149. if math.isfinite(f):
  150. return f
  151. except (TypeError, ValueError):
  152. pass
  153. return None
  154. def _extract_extra_str(extra: dict, key: str) -> str:
  155. """Extract a JSON-encoded string from a Spoolman extra dict.
  156. Spoolman stores extra values as JSON-stringified text — a stored string
  157. "GFL05" appears as `'"GFL05"'` (six chars including the quotes). This
  158. unwraps that, returning the bare string. Returns "" for missing keys,
  159. non-strings, or invalid JSON.
  160. """
  161. raw = extra.get(key)
  162. if not isinstance(raw, str):
  163. return ""
  164. try:
  165. decoded = json.loads(raw)
  166. except (json.JSONDecodeError, ValueError):
  167. # Tolerate bare-string values written without JSON encoding.
  168. return raw
  169. return decoded if isinstance(decoded, str) else ""
  170. def _map_spoolman_spool(spool: dict) -> MappedSpoolFields:
  171. """Convert a raw Spoolman spool dict to the InventorySpool-compatible format.
  172. Fields not supported by Spoolman (k_profiles, slicer_filament, …) are
  173. returned as None / empty so the frontend can still render them without
  174. errors. The ``data_origin`` field is set to ``"spoolman"`` so UI code can
  175. distinguish these spools from local ones.
  176. """
  177. raw_id = spool.get("id")
  178. if raw_id is None:
  179. raise ValueError("Spoolman spool is missing required 'id' field")
  180. try:
  181. spool_id: int = int(raw_id)
  182. except (TypeError, ValueError):
  183. raise ValueError(f"Spoolman spool 'id' is not a valid integer: {raw_id!r}")
  184. if spool_id <= 0:
  185. raise ValueError(f"Spoolman spool 'id' must be a positive integer, got {spool_id}")
  186. filament: dict = spool.get("filament") or {}
  187. if not filament:
  188. logger.warning(
  189. "Spoolman spool %s has no filament data — all filament fields will use defaults",
  190. spool_id,
  191. )
  192. vendor: dict = filament.get("vendor") or {}
  193. extra: dict = spool.get("extra") or {}
  194. # RFID tag stored as JSON-encoded string in Spoolman extra.tag.
  195. # 32-char hex → Bambu Lab tray UUID; 8–30-char hex → NFC tag UID.
  196. # Accepting the full realistic UID range (4-byte = 8 chars, 7-byte = 14 chars,
  197. # 10-byte = 20 chars) avoids silently dropping valid SpoolBuddy-written tags.
  198. raw_tag: str = (extra.get("tag") or "").strip('"').upper()
  199. _raw_is_hex = bool(_TAG_HEX_RE.match(raw_tag))
  200. tag_uid = raw_tag if _raw_is_hex and 8 <= len(raw_tag) <= 30 else None
  201. tray_uuid = raw_tag if _raw_is_hex and len(raw_tag) == 32 else None
  202. # Subtype = filament name with material prefix stripped
  203. material: str = (filament.get("material") or "").strip()
  204. filament_name: str = (filament.get("name") or "").strip()
  205. if material and filament_name.upper().startswith(material.upper()):
  206. subtype: str | None = filament_name[len(material) :].strip() or None
  207. else:
  208. subtype = filament_name or None
  209. # Colour: validate as 6-char hex; fall back to neutral grey for invalid values
  210. raw_color = (filament.get("color_hex") or "").upper().removeprefix("#")
  211. color_hex: str = raw_color if _COLOR_HEX_RE.match(raw_color) else "808080"
  212. rgba: str = color_hex + "FF"
  213. label_weight: int = _safe_int(filament.get("weight"), 1000)
  214. used_weight: float = _safe_float(spool.get("used_weight"), 0.0)
  215. # Archived state – Spoolman uses a boolean ``archived`` field
  216. archived: bool = spool.get("archived", False)
  217. archived_at: str | None = None
  218. if archived:
  219. archived_at = spool.get("last_used") or spool.get("registered") or None
  220. created_at: str | None = spool.get("registered") or None
  221. color_name: str | None = filament.get("color_name") or None
  222. nozzle_temp_raw = filament.get("settings_extruder_temp")
  223. nozzle_temp_min: int | None = _safe_int(nozzle_temp_raw, 0) or None
  224. return {
  225. "id": spool_id,
  226. "material": material,
  227. "subtype": subtype,
  228. "color_name": color_name,
  229. "rgba": rgba,
  230. "brand": vendor.get("name") or None,
  231. "label_weight": label_weight,
  232. "core_weight": _safe_int(
  233. spool.get("spool_weight") if spool.get("spool_weight") is not None else filament.get("spool_weight"), 250
  234. ),
  235. "core_weight_catalog_id": None,
  236. "weight_used": used_weight,
  237. "weight_locked": False,
  238. "last_scale_weight": None,
  239. "last_weighed_at": None,
  240. # BambuStudio slicer preset — Spoolman has no native field, so the
  241. # update endpoint persists these under bambu_slicer_filament[_name]
  242. # in the spool's extra dict. Values are JSON-encoded strings; an
  243. # empty string ("") means cleared. Falls back to Spoolman's
  244. # filament_name for slicer_filament_name when nothing is stored.
  245. "slicer_filament": (_extract_extra_str(extra, "bambu_slicer_filament") or None),
  246. "slicer_filament_name": (_extract_extra_str(extra, "bambu_slicer_filament_name") or (filament_name or None)),
  247. "nozzle_temp_min": nozzle_temp_min,
  248. "nozzle_temp_max": None,
  249. "note": spool.get("comment") or None,
  250. "added_full": None,
  251. "last_used": spool.get("last_used"),
  252. # encode_time semantics differ: local records NFC write time; Spoolman first_used
  253. # records first print use — different events; using first_used as best available proxy.
  254. "encode_time": spool.get("first_used"),
  255. "tag_uid": tag_uid,
  256. "tray_uuid": tray_uuid,
  257. "data_origin": "spoolman",
  258. "tag_type": "spoolman",
  259. "archived_at": archived_at,
  260. "created_at": created_at,
  261. # Spoolman has no updated_at field; use registered timestamp as best available proxy
  262. "updated_at": created_at,
  263. "cost_per_kg": _safe_optional_float(spool.get("price")),
  264. "storage_location": spool.get("location") or None,
  265. "k_profiles": [],
  266. }