_spoolman_helpers.py 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 logging
  7. import math
  8. import re
  9. from datetime import datetime, timezone
  10. from urllib.parse import urlparse
  11. logger = logging.getLogger(__name__)
  12. def assert_safe_spoolman_url(url: str) -> None:
  13. """Raise ValueError if *url* should be blocked as an SSRF risk.
  14. Checks performed:
  15. - Scheme must be http or https.
  16. - Numeric-encoded IP addresses in decimal (e.g. ``2130706433``) or hex
  17. (e.g. ``0x7f000001``) are rejected — Python's ``ipaddress`` module raises
  18. ``ValueError`` for these forms so they would otherwise bypass the IP-range
  19. guard, but libc resolves them as valid IPv4 addresses.
  20. - Bare numeric IP hosts in loopback (127.x, ::1), link-local (169.254.x,
  21. fe80::), private (RFC-1918), multicast (224.x, ff::/8), or unspecified
  22. (0.0.0.0, ::) ranges are rejected.
  23. - IPv4-mapped IPv6 addresses (::ffff:x.x.x.x) are unwrapped to their IPv4
  24. equivalent and subject to the same checks.
  25. Hostname-based addresses ("localhost", "internal.corp") require DNS resolution
  26. and are outside the scope of this guard — they are mitigated by network-level
  27. controls in the deployment environment. "localhost" is intentionally *not*
  28. blocked here because running Spoolman on the same host is a common and
  29. supported topology.
  30. """
  31. parsed = urlparse(url)
  32. if parsed.scheme.lower() not in ("http", "https"):
  33. raise ValueError("Spoolman URL must use http or https")
  34. hostname = (parsed.hostname or "").lower()
  35. # Reject decimal- and hex-encoded IPs (e.g. http://2130706433/ or
  36. # http://0x7f000001/). Python's ipaddress.ip_address() raises ValueError for
  37. # these forms so they slip past the except-clause below, but the C library
  38. # (and browsers) parse them as valid IPv4 addresses.
  39. if re.match(r"^(0x[0-9a-f]+|[0-9]+)$", hostname, re.I):
  40. raise ValueError("Spoolman URL must not use numeric-encoded IP addresses; use standard dotted-decimal notation")
  41. try:
  42. addr = ipaddress.ip_address(hostname)
  43. except ValueError:
  44. # Not a bare IP — hostname-based addresses are out of scope.
  45. return
  46. # Unwrap IPv4-mapped IPv6 (::ffff:169.254.x.x etc.) so their IPv4
  47. # properties are evaluated correctly.
  48. effective: ipaddress.IPv4Address | ipaddress.IPv6Address = addr
  49. if isinstance(addr, ipaddress.IPv6Address) and addr.ipv4_mapped is not None:
  50. effective = addr.ipv4_mapped
  51. if (
  52. effective.is_loopback
  53. or effective.is_link_local
  54. or effective.is_private
  55. or effective.is_multicast
  56. or effective.is_unspecified
  57. ):
  58. raise ValueError(
  59. "Spoolman URL must not point to a private, loopback, link-local, multicast, or unspecified address"
  60. )
  61. _COLOR_HEX_RE = re.compile(r"^[0-9A-Fa-f]{6}$")
  62. _TAG_HEX_RE = re.compile(r"^[0-9A-F]+$")
  63. def _safe_int(value: object, fallback: int) -> int:
  64. """Convert value to int, returning fallback for None/NaN/Inf/non-numeric."""
  65. try:
  66. f = float(value) # type: ignore[arg-type]
  67. if math.isfinite(f):
  68. return int(f)
  69. except (TypeError, ValueError):
  70. pass
  71. return fallback
  72. def _safe_float(value: object, fallback: float) -> float:
  73. """Convert value to float, returning fallback for None/NaN/Inf/non-numeric."""
  74. try:
  75. f = float(value) # type: ignore[arg-type]
  76. if math.isfinite(f):
  77. return f
  78. except (TypeError, ValueError):
  79. pass
  80. return fallback
  81. def _safe_optional_float(value: object) -> float | None:
  82. """Convert value to finite float, or None if missing/NaN/Infinite/non-numeric.
  83. Used for optional monetary fields (price) to prevent Infinity/NaN from
  84. reaching JSON serialisation, which raises ValueError with allow_nan=False.
  85. """
  86. if value is None:
  87. return None
  88. try:
  89. f = float(value) # type: ignore[arg-type]
  90. if math.isfinite(f):
  91. return f
  92. except (TypeError, ValueError):
  93. pass
  94. return None
  95. def _map_spoolman_spool(spool: dict) -> dict:
  96. """Convert a raw Spoolman spool dict to the InventorySpool-compatible format.
  97. Fields not supported by Spoolman (k_profiles, slicer_filament, …) are
  98. returned as None / empty so the frontend can still render them without
  99. errors. The ``data_origin`` field is set to ``"spoolman"`` so UI code can
  100. distinguish these spools from local ones.
  101. """
  102. raw_id = spool.get("id")
  103. if raw_id is None:
  104. raise ValueError("Spoolman spool is missing required 'id' field")
  105. try:
  106. spool_id: int = int(raw_id)
  107. except (TypeError, ValueError):
  108. raise ValueError(f"Spoolman spool 'id' is not a valid integer: {raw_id!r}")
  109. if spool_id <= 0:
  110. raise ValueError(f"Spoolman spool 'id' must be a positive integer, got {spool_id}")
  111. filament: dict = spool.get("filament") or {}
  112. if not filament:
  113. logger.warning(
  114. "Spoolman spool %s has no filament data — all filament fields will use defaults",
  115. spool_id,
  116. )
  117. vendor: dict = filament.get("vendor") or {}
  118. extra: dict = spool.get("extra") or {}
  119. # RFID tag stored as JSON-encoded string in Spoolman extra.tag.
  120. # 32-char hex → Bambu Lab tray UUID; 8–30-char hex → NFC tag UID.
  121. # Accepting the full realistic UID range (4-byte = 8 chars, 7-byte = 14 chars,
  122. # 10-byte = 20 chars) avoids silently dropping valid SpoolBuddy-written tags.
  123. raw_tag: str = (extra.get("tag") or "").strip('"').upper()
  124. _raw_is_hex = bool(_TAG_HEX_RE.match(raw_tag))
  125. tag_uid = raw_tag if _raw_is_hex and 8 <= len(raw_tag) <= 30 else None
  126. tray_uuid = raw_tag if _raw_is_hex and len(raw_tag) == 32 else None
  127. # Subtype = filament name with material prefix stripped
  128. material: str = (filament.get("material") or "").strip()
  129. filament_name: str = (filament.get("name") or "").strip()
  130. if material and filament_name.upper().startswith(material.upper()):
  131. subtype: str | None = filament_name[len(material) :].strip() or None
  132. else:
  133. subtype = filament_name or None
  134. # Colour: validate as 6-char hex; fall back to neutral grey for invalid values
  135. raw_color = (filament.get("color_hex") or "").upper().removeprefix("#")
  136. color_hex: str = raw_color if _COLOR_HEX_RE.match(raw_color) else "808080"
  137. rgba: str = color_hex + "FF"
  138. label_weight: int = _safe_int(filament.get("weight"), 1000)
  139. used_weight: float = _safe_float(spool.get("used_weight"), 0.0)
  140. # Archived state – Spoolman uses a boolean ``archived`` field
  141. archived: bool = spool.get("archived", False)
  142. archived_at: str | None = None
  143. if archived:
  144. archived_at = spool.get("last_used") or spool.get("registered")
  145. if not archived_at:
  146. archived_at = datetime.now(timezone.utc).isoformat()
  147. created_at: str = spool.get("registered") or datetime.now(timezone.utc).isoformat()
  148. color_name: str | None = filament.get("color_name") or None
  149. nozzle_temp_raw = filament.get("settings_extruder_temp")
  150. nozzle_temp_min: int | None = _safe_int(nozzle_temp_raw, 0) or None
  151. return {
  152. "id": spool_id,
  153. "material": material,
  154. "subtype": subtype,
  155. "color_name": color_name,
  156. "rgba": rgba,
  157. "brand": vendor.get("name") or None,
  158. "label_weight": label_weight,
  159. "core_weight": _safe_int(filament.get("spool_weight"), 250),
  160. "core_weight_catalog_id": None,
  161. "weight_used": used_weight,
  162. "weight_locked": False,
  163. "last_scale_weight": None,
  164. "last_weighed_at": None,
  165. # slicer_filament_name carries the Spoolman filament name for display
  166. "slicer_filament": None,
  167. "slicer_filament_name": filament_name or None,
  168. "nozzle_temp_min": nozzle_temp_min,
  169. "nozzle_temp_max": None,
  170. "note": spool.get("comment") or None,
  171. "added_full": None,
  172. "last_used": spool.get("last_used"),
  173. # encode_time semantics differ: local records NFC write time; Spoolman first_used
  174. # records first print use — different events; using first_used as best available proxy.
  175. "encode_time": spool.get("first_used"),
  176. "tag_uid": tag_uid,
  177. "tray_uuid": tray_uuid,
  178. "data_origin": "spoolman",
  179. "tag_type": "spoolman",
  180. "archived_at": archived_at,
  181. "created_at": created_at,
  182. # Spoolman has no updated_at field; use registered timestamp as best available proxy
  183. "updated_at": created_at,
  184. "cost_per_kg": _safe_optional_float(spool.get("price")),
  185. "storage_location": spool.get("location") or None,
  186. "k_profiles": [],
  187. }