_spoolman_helpers.py 13 KB

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