_spoolman_helpers.py 13 KB

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