makerworld.py 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  1. """MakerWorld API service.
  2. Thin async client for MakerWorld's ``/api/v1/design-service/*`` endpoints.
  3. Lets Bambuddy resolve a MakerWorld URL, enumerate plate/profile metadata, and
  4. download the 3MF bundle so users can import and print MakerWorld models
  5. without leaving the app.
  6. The endpoints and header set were reverse-engineered from the
  7. `kloshi-io/makerworld-api-reverse` TypeScript project (Apache-2.0) and
  8. cross-validated against live MakerWorld traffic. Authenticated calls reuse
  9. Bambuddy's existing Bambu Cloud bearer token (same SSO backend — no separate
  10. OAuth flow needed).
  11. Only interoperability — not affiliated with or endorsed by MakerWorld or
  12. Bambu Lab, and not intended to circumvent any access control.
  13. """
  14. from __future__ import annotations
  15. import asyncio
  16. import logging
  17. import re
  18. import ssl
  19. from collections.abc import Awaitable, Callable
  20. from typing import Any
  21. from urllib.parse import urlparse
  22. import certifi
  23. import httpx
  24. from backend.app.services.bambu_cloud import is_captcha_challenge, is_expiry_401
  25. logger = logging.getLogger(__name__)
  26. # API base: ``api.bambulab.com/v1/design-service`` — the same Bambu Cloud
  27. # backend that the MakerWorld web UI talks to, but not behind Cloudflare
  28. # (the website ``makerworld.com`` is, and plain httpx requests there get
  29. # fingerprinted as bot traffic and served "Please log in"). Confirmed by
  30. # Pr0zak/YASTL#51 and verified with direct curl.
  31. MAKERWORLD_API_BASE = "https://api.bambulab.com/v1/design-service"
  32. MAKERWORLD_HOST = "makerworld.com" # Used only for URL parsing (input validation)
  33. MAKERWORLD_CDN_HOSTS = ("makerworld.bblmw.com", "public-cdn.bblmw.com")
  34. # Hosts that the iot-service download endpoint may return presigned URLs
  35. # for. Besides MakerWorld's own CDN, Bambu Cloud also issues AWS S3
  36. # presigned URLs (e.g. ``s3.us-west-2.amazonaws.com``) — confirmed by
  37. # Pr0zak/YASTL#52. The suffix check matches any regional S3 endpoint.
  38. _ALLOWED_DOWNLOAD_SUFFIXES = (".amazonaws.com",)
  39. # Client identity sent to MakerWorld / api.bambulab.com. We identify honestly
  40. # as Bambuddy with a source URL so Bambu can distinguish our traffic from
  41. # impersonators — the opposite of what the OrcaSlicer fork was called out for
  42. # in the May 2026 Bambu Lab blog post on cloud access. Verified 2026-05-12 via
  43. # curl that MakerWorld treats this UA identically to a Firefox UA at the
  44. # Cloudflare edge (same response shape on /api/v1/design-service/* paths).
  45. # The Referer is kept because MakerWorld's CSRF / origin-check middleware uses
  46. # it on some endpoints — that's distinct from client impersonation.
  47. _CLIENT_HEADERS = {
  48. "User-Agent": "Bambuddy/1.0 (+https://github.com/maziggy/bambuddy)",
  49. "Accept": "text/html,application/json,*/*",
  50. "Accept-Language": "en-US,en;q=0.9",
  51. "Referer": "https://makerworld.com/",
  52. }
  53. # Shown whenever Bambu rejects the stored bearer. Bambu's own 401 body is
  54. # ``{"code":4,"error":"Please login.","message":""}`` and we used to forward that
  55. # string verbatim, which surfaced as a "Please login." toast on a UI that was
  56. # simultaneously reporting the user as connected — maximally confusing, and it
  57. # named no page to go to. Say what happened and where to fix it. Bambu Cloud
  58. # sign-in lives on the Profiles page (ProfilesPage.tsx, "Cloud Profiles" tab);
  59. # there is no Settings → Bambu Cloud page, which is what the old fallback text
  60. # told people to look for.
  61. _SIGN_IN_EXPIRED_MESSAGE = (
  62. "Your Bambu Cloud sign-in has expired. Open the Profiles page and sign in to Bambu Cloud again."
  63. )
  64. _MODEL_ID_RE = re.compile(r"/models/(\d+)")
  65. _PROFILE_ID_RE = re.compile(r"#profileId[-=](\d+)")
  66. _MAX_3MF_BYTES = 200 * 1024 * 1024 # 200 MB hard cap
  67. _MAX_THUMBNAIL_BYTES = 10 * 1024 * 1024 # 10 MB hard cap — MakerWorld's "thumbnails" can be 2–3 MB source images
  68. _IMAGE_EXT_TO_MIME = {
  69. ".png": "image/png",
  70. ".jpg": "image/jpeg",
  71. ".jpeg": "image/jpeg",
  72. ".gif": "image/gif",
  73. ".webp": "image/webp",
  74. ".bmp": "image/bmp",
  75. }
  76. # Content types we refuse even if the URL extension looks image-y — prevents
  77. # forwarding an upstream error page or JSON blob with image framing.
  78. _REFUSED_THUMBNAIL_MIMES = ("text/html", "text/plain", "application/json")
  79. _shared_http_client: httpx.AsyncClient | None = None
  80. def _s3_ssl_context() -> ssl.SSLContext:
  81. """Build the TLS context used for the S3 presigned download (#2562).
  82. ``urllib.request`` verifies against the *OS* trust store, while httpx —
  83. every other network call in Bambuddy — verifies against the bundled
  84. ``certifi`` CA bundle. On Windows those two disagree: Python's
  85. ``ssl.load_default_certs()`` only enumerates the roots already cached in
  86. the Windows ROOT store, and Windows populates that store lazily via
  87. CryptoAPI's auto-update, which Python never triggers. If the Amazon root
  88. signing the S3 chain isn't cached on that machine yet, verification fails
  89. with ``unable to get local issuer certificate`` — even though the
  90. api.bambulab.com calls that preceded it (httpx) succeeded.
  91. Pinning urllib to certifi makes the S3 hop trust exactly what the rest of
  92. the app already trusts. Built per call rather than at import so a certifi
  93. refresh doesn't require a restart; construction is cheap relative to the
  94. download that follows.
  95. """
  96. return ssl.create_default_context(cafile=certifi.where())
  97. def set_shared_http_client(client: httpx.AsyncClient | None) -> None:
  98. """Register an app-scoped ``httpx.AsyncClient`` for service reuse.
  99. Same pattern as ``bambu_cloud.set_shared_http_client`` — lets the FastAPI
  100. lifespan share one connection pool across per-request service instances.
  101. """
  102. global _shared_http_client
  103. _shared_http_client = client
  104. class MakerWorldError(Exception):
  105. """Base exception for MakerWorld API errors."""
  106. class MakerWorldAuthError(MakerWorldError):
  107. """Raised when the endpoint requires a Bambu Cloud token and we don't have
  108. one (or the one we sent was rejected). True auth failure."""
  109. class MakerWorldForbiddenError(MakerWorldError):
  110. """Raised when MakerWorld refuses access despite valid authentication —
  111. content-gated (points required, purchase required, region restricted,
  112. early-access, etc.). The message includes MakerWorld's own reason text
  113. when provided."""
  114. class MakerWorldNotFoundError(MakerWorldError):
  115. """Raised when a design / profile / instance doesn't exist."""
  116. class MakerWorldUnavailableError(MakerWorldError):
  117. """Raised on 5xx, network errors, or malformed payloads."""
  118. class MakerWorldUrlError(MakerWorldError):
  119. """Raised when a URL isn't a makerworld.com model page."""
  120. async def _download_s3_urllib(url: str, filename_fallback: str) -> tuple[bytes, str]:
  121. """Fetch an AWS S3 presigned URL without touching the query string.
  122. ``urllib.request`` passes the URL to the transport verbatim — which is
  123. essential for S3 presigned URLs where the signature is computed over
  124. the exact query-string bytes. httpx's ``URL`` class and curl_cffi's
  125. libcurl layer both normalise encodings and produce
  126. ``SignatureDoesNotMatch`` 400s from S3.
  127. Runs the blocking urllib call in a thread executor so we don't stall
  128. the event loop.
  129. """
  130. from urllib.request import HTTPRedirectHandler, HTTPSHandler, Request, build_opener
  131. # Don't follow redirects: the host allowlist above is only enforced on
  132. # the initial URL. A 302 from S3 to any other host would otherwise
  133. # transparently bypass the allowlist — so insist S3 resolve directly.
  134. class _NoRedirect(HTTPRedirectHandler):
  135. def redirect_request(self, *args, **kwargs): # type: ignore[override]
  136. return None
  137. # HTTPSHandler swaps only the TLS context — the URL still reaches the
  138. # transport verbatim, which is what the S3 signature depends on.
  139. opener = build_opener(_NoRedirect, HTTPSHandler(context=_s3_ssl_context()))
  140. def _blocking_fetch() -> bytes:
  141. req = Request(url, headers={"User-Agent": _CLIENT_HEADERS["User-Agent"]})
  142. with opener.open(req, timeout=60.0) as resp:
  143. if resp.status != 200:
  144. raise MakerWorldUnavailableError(f"3MF download returned HTTP {resp.status}")
  145. data = b""
  146. while True:
  147. chunk = resp.read(65536)
  148. if not chunk:
  149. break
  150. data += chunk
  151. if len(data) > _MAX_3MF_BYTES:
  152. raise MakerWorldUnavailableError(f"3MF exceeds {_MAX_3MF_BYTES // (1024 * 1024)} MB cap")
  153. return data
  154. try:
  155. data = await asyncio.to_thread(_blocking_fetch)
  156. except MakerWorldUnavailableError:
  157. raise
  158. except Exception as exc: # noqa: BLE001 — urllib throws a zoo of exceptions
  159. raise MakerWorldUnavailableError(f"S3 download failed: {exc}") from exc
  160. return data, filename_fallback
  161. def _extract_upstream_error(response: httpx.Response) -> str | None:
  162. """Pull MakerWorld's own error text out of a 4xx/5xx response body.
  163. MakerWorld returns ``{"code": N, "error": "text"}`` on auth/perm failures
  164. and sometimes ``{"message": "..."}`` on other errors. Returns ``None`` if
  165. the body isn't JSON or doesn't have a recognised error field — callers
  166. should fall back to a generic message in that case.
  167. """
  168. try:
  169. data = response.json()
  170. except ValueError:
  171. return None
  172. if not isinstance(data, dict):
  173. return None
  174. for key in ("error", "message", "detail"):
  175. value = data.get(key)
  176. if isinstance(value, str) and value.strip():
  177. return value.strip()
  178. return None
  179. class MakerWorldService:
  180. """Per-request MakerWorld API client.
  181. Mirrors ``BambuCloudService``'s construction pattern so callers can
  182. instantiate per request, reuse the shared connection pool in production,
  183. inject a client in tests, and close the client only if they own it.
  184. """
  185. def __init__(
  186. self,
  187. client: httpx.AsyncClient | None = None,
  188. auth_token: str | None = None,
  189. on_auth_failure: Callable[[], Awaitable[None]] | None = None,
  190. ):
  191. # Fired when Bambu rejects the stored token (401). MakerWorld runs on the
  192. # same Bambu Cloud bearer as everything else, so a rejection here means
  193. # the credential is dead app-wide — see ``build_authenticated_cloud``.
  194. self._on_auth_failure = on_auth_failure
  195. self._auth_failure_reported = False
  196. if client is not None:
  197. self._client = client
  198. self._owns_client = False
  199. elif _shared_http_client is not None:
  200. self._client = _shared_http_client
  201. self._owns_client = False
  202. else:
  203. self._client = httpx.AsyncClient(timeout=30.0)
  204. self._owns_client = True
  205. self._auth_token = auth_token
  206. async def close(self) -> None:
  207. if self._owns_client:
  208. await self._client.aclose()
  209. async def _note_auth_failure(self, response: httpx.Response) -> None:
  210. """Durably record a dead credential — only for Bambu's genuine expiry 401.
  211. A MakerWorld 401 without the ``{"code":4,"error":"Please login."}``
  212. signature is endpoint- or edge-specific noise, not an expired token;
  213. invalidating on it would sign the user out of the whole cloud
  214. integration on a single stray rejection (the #2562 follow-up
  215. regression). Best-effort, once per service instance.
  216. """
  217. if not is_expiry_401(response):
  218. logger.info("MakerWorld returned 401 without the expiry signature — not signing the stored token out")
  219. return
  220. if self._on_auth_failure is None or self._auth_failure_reported:
  221. return
  222. self._auth_failure_reported = True
  223. try:
  224. await self._on_auth_failure()
  225. except Exception:
  226. logger.exception("Failed to record Bambu Cloud auth failure from MakerWorld")
  227. def _headers(self) -> dict[str, str]:
  228. headers = dict(_CLIENT_HEADERS)
  229. if self._auth_token:
  230. headers["Authorization"] = f"Bearer {self._auth_token}"
  231. return headers
  232. async def _get_json(self, path: str) -> dict[str, Any]:
  233. """GET ``{MAKERWORLD_API_BASE}{path}`` returning the decoded JSON body.
  234. Raises ``MakerWorld{Auth,Forbidden,NotFound,Unavailable}Error`` based
  235. on status. Retries once on 418 (Cloudflare bot-detection) with a
  236. short backoff — that flagging is often request-scoped and clears on
  237. a subsequent call; hammering beyond one retry provokes a stronger
  238. block, so we stop there and surface a useful error.
  239. """
  240. url = f"{MAKERWORLD_API_BASE}{path}"
  241. for attempt in range(2):
  242. try:
  243. response = await self._client.get(url, headers=self._headers(), timeout=30.0)
  244. except httpx.TimeoutException as exc:
  245. raise MakerWorldUnavailableError(f"MakerWorld request timed out: {exc}") from exc
  246. except httpx.HTTPError as exc:
  247. raise MakerWorldUnavailableError(f"MakerWorld request failed: {exc}") from exc
  248. if response.status_code == 418 and attempt == 0:
  249. logger.info("MakerWorld returned 418 for %s; retrying once after backoff", path)
  250. await asyncio.sleep(1.5)
  251. continue
  252. break
  253. # 401: genuine auth failure — token expired, malformed, not accepted.
  254. # 403: MakerWorld accepted the token but refuses the specific resource
  255. # — usually content gating (points-redeemable, purchase-required,
  256. # region-restricted, early-access). These must surface differently
  257. # because the UI remedy is completely different: 401 → re-login,
  258. # 403 → user has to go to MakerWorld and meet the access requirement.
  259. if response.status_code == 401:
  260. if self._auth_token:
  261. # We sent a token and Bambu refused it — the credential is dead,
  262. # not merely absent. Record that before raising so the rest of the
  263. # app stops claiming the user is connected.
  264. await self._note_auth_failure(response)
  265. raise MakerWorldAuthError(_SIGN_IN_EXPIRED_MESSAGE)
  266. raise MakerWorldAuthError(f"Signing in to Bambu Cloud is required for {path}")
  267. if response.status_code == 403:
  268. upstream = _extract_upstream_error(response)
  269. raise MakerWorldForbiddenError(
  270. upstream
  271. or f"MakerWorld refused access to {path} — the model may require purchase, points redemption, or be region-restricted"
  272. )
  273. if response.status_code == 404:
  274. raise MakerWorldNotFoundError(f"MakerWorld resource not found: {path}")
  275. if response.status_code == 418:
  276. # Bambu's anti-abuse layer challenges the source IP with a CAPTCHA
  277. # (``{"captchaId":"...","error":"We need to confirm..."}``). This is
  278. # application-level, not Cloudflare-edge, and clears on its own
  279. # within 1–4 hours of quiet traffic. There's no server-side solve —
  280. # CAPTCHAs are intentionally unsolvable without a real browser.
  281. # Surface the upstream message so the user can recognise it and
  282. # reach for the "Open on MakerWorld" fallback instead of thinking
  283. # the feature is broken.
  284. #
  285. # The same challenge also lands on the Bambu Cloud sign-in endpoint,
  286. # so the shape test lives in ``bambu_cloud`` and is shared (#2790).
  287. # It used to be a bare "robot" substring check on the error text,
  288. # which missed a challenge worded any other way.
  289. if is_captcha_challenge(response):
  290. upstream = _extract_upstream_error(response)
  291. detail = f" ({upstream})" if upstream else ""
  292. raise MakerWorldUnavailableError(
  293. f"MakerWorld is challenging this IP with a CAPTCHA{detail}. "
  294. "This usually clears within a few hours. In the meantime, use "
  295. "'Open on MakerWorld' below to download the 3MF manually."
  296. )
  297. raise MakerWorldUnavailableError(
  298. f"MakerWorld blocked the request (HTTP 418) for {path}. "
  299. "Try again in a few minutes, or use 'Open on MakerWorld' to import manually."
  300. )
  301. if response.status_code == 429:
  302. raise MakerWorldUnavailableError(
  303. f"MakerWorld rate-limited the request (HTTP 429) for {path}. Try again shortly."
  304. )
  305. if response.status_code >= 500:
  306. raise MakerWorldUnavailableError(f"MakerWorld server error (HTTP {response.status_code}) for {path}")
  307. if response.status_code != 200:
  308. raise MakerWorldUnavailableError(f"MakerWorld unexpected status {response.status_code} for {path}")
  309. try:
  310. data = response.json()
  311. except ValueError as exc:
  312. raise MakerWorldUnavailableError(f"MakerWorld returned non-JSON for {path}") from exc
  313. if not isinstance(data, dict):
  314. raise MakerWorldUnavailableError(
  315. f"MakerWorld returned unexpected JSON shape for {path}: {type(data).__name__}"
  316. )
  317. return data
  318. # ------------------------------------------------------------------ URL parse
  319. @staticmethod
  320. def parse_url(url: str) -> tuple[int, int | None]:
  321. """Extract ``(model_id, profile_id_or_None)`` from a MakerWorld URL.
  322. Accepts any of:
  323. - ``https://makerworld.com/en/models/1400373``
  324. - ``https://makerworld.com/en/models/1400373-slug-with-dashes``
  325. - ``https://makerworld.com/en/models/1400373#profileId-1452154``
  326. - ``makerworld.com/models/1400373`` (scheme optional)
  327. Rejects non-makerworld hosts.
  328. """
  329. if not url or not isinstance(url, str):
  330. raise MakerWorldUrlError("URL is empty or not a string")
  331. candidate = url.strip()
  332. if "://" not in candidate:
  333. candidate = "https://" + candidate
  334. try:
  335. parsed = urlparse(candidate)
  336. except ValueError as exc:
  337. raise MakerWorldUrlError(f"Could not parse URL: {exc}") from exc
  338. host = (parsed.hostname or "").lower()
  339. if host != MAKERWORLD_HOST and not host.endswith("." + MAKERWORLD_HOST):
  340. raise MakerWorldUrlError(f"Not a MakerWorld URL (host={host!r}); expected makerworld.com")
  341. model_match = _MODEL_ID_RE.search(parsed.path)
  342. if not model_match:
  343. raise MakerWorldUrlError("URL does not contain a /models/{id} segment")
  344. model_id = int(model_match.group(1))
  345. profile_id: int | None = None
  346. if parsed.fragment:
  347. profile_match = _PROFILE_ID_RE.search("#" + parsed.fragment)
  348. if profile_match:
  349. profile_id = int(profile_match.group(1))
  350. return model_id, profile_id
  351. # ---------------------------------------------------------------- endpoints
  352. async def get_design(self, model_id: int) -> dict[str, Any]:
  353. """Fetch full model metadata. Works anonymously.
  354. Returns the MakerWorld ``design`` object — title, summary, creator,
  355. license, tags, coverUrl, instances[] with profileId+cover per plate,
  356. categories, etc.
  357. """
  358. return await self._get_json(f"/design/{int(model_id)}")
  359. async def get_design_instances(self, model_id: int) -> dict[str, Any]:
  360. """Fetch list of profiles/instances for a model. Works anonymously.
  361. Returns ``{"total": N, "hits": [{id, profileId, title, cover,
  362. instanceCreator, instanceFilaments, needAms, ...}, ...]}``.
  363. """
  364. return await self._get_json(f"/design/{int(model_id)}/instances")
  365. async def get_profile(self, profile_id: int) -> dict[str, Any]:
  366. """Fetch a single profile's summary (designId/modelId/title/cover/
  367. instanceId). Works anonymously.
  368. """
  369. return await self._get_json(f"/profile/{int(profile_id)}")
  370. async def get_profile_download(self, profile_id: int, model_id: str) -> dict[str, Any]:
  371. """Fetch the signed 3MF download URL for a specific MakerWorld profile.
  372. Note on ``model_id`` — this is MakerWorld's internal alphanumeric
  373. identifier (e.g. ``"US2bb73b106683e5"``), **not** the integer
  374. ``designId`` that appears in the ``/models/{N}`` URL. Callers must
  375. fetch the design first (``get_design(design_id)``) and pass the
  376. ``modelId`` field from the response.
  377. Returns ``{"url": "https://makerworld.bblmw.com/...?at=<unix>
  378. &exp=<unix>&key=<hmac>&uid=<int>", ...}``. URL is short-lived (~5
  379. min); download immediately.
  380. Hits ``api.bambulab.com/v1/iot-service/api/user/profile/{profileId}
  381. ?model_id={modelId}`` with the stored Bambu Cloud bearer. This is the
  382. endpoint Pr0zak/YASTL#51 reverse-engineered — it lives on the
  383. ``api.bambulab.com`` backend (not Cloudflare-protected
  384. ``makerworld.com``), accepts the same long-lived bearer users already
  385. sign in with, and mints the signed CDN URL that the browser would
  386. otherwise fetch via session cookies. This is the only known non-
  387. cookie path to a download URL, after ruling out ``/design-service/``
  388. endpoints on ``makerworld.com`` (cookie-gated) and the now-dead
  389. ``/instance/{id}/f3mf?type=download`` shape.
  390. """
  391. if not self._auth_token:
  392. raise MakerWorldAuthError("Downloading files from MakerWorld requires a Bambu Cloud login")
  393. url = f"https://api.bambulab.com/v1/iot-service/api/user/profile/{int(profile_id)}"
  394. headers = dict(_CLIENT_HEADERS)
  395. headers["Authorization"] = f"Bearer {self._auth_token}"
  396. try:
  397. response = await self._client.get(
  398. url,
  399. headers=headers,
  400. params={"model_id": str(model_id)},
  401. timeout=30.0,
  402. )
  403. except httpx.TimeoutException as exc:
  404. raise MakerWorldUnavailableError(f"Bambu Lab API request timed out: {exc}") from exc
  405. except httpx.HTTPError as exc:
  406. raise MakerWorldUnavailableError(f"Bambu Lab API request failed: {exc}") from exc
  407. if response.status_code == 401:
  408. await self._note_auth_failure(response)
  409. raise MakerWorldAuthError(_SIGN_IN_EXPIRED_MESSAGE)
  410. if response.status_code == 403:
  411. upstream = _extract_upstream_error(response)
  412. raise MakerWorldForbiddenError(upstream or f"Bambu Lab refused access to profile {profile_id}")
  413. if response.status_code == 404:
  414. raise MakerWorldNotFoundError(f"MakerWorld profile not found: {profile_id}")
  415. if response.status_code != 200:
  416. raise MakerWorldUnavailableError(
  417. f"Bambu Lab API unexpected status {response.status_code} for profile {profile_id}"
  418. )
  419. try:
  420. data = response.json()
  421. except ValueError as exc:
  422. raise MakerWorldUnavailableError(f"Bambu Lab API returned non-JSON for profile {profile_id}") from exc
  423. if not isinstance(data, dict):
  424. raise MakerWorldUnavailableError(f"Bambu Lab API returned unexpected JSON shape for profile {profile_id}")
  425. return data
  426. async def download_3mf(self, signed_url: str) -> tuple[bytes, str]:
  427. """Fetch the 3MF bytes from a signed MakerWorld CDN URL.
  428. Validates that the URL's host is one of the known MakerWorld CDN hosts
  429. (SSRF guard — pattern matches ``_spoolman_helpers.assert_safe_spoolman_url``).
  430. Enforces a 200 MB cap so a single bad response can't exhaust disk.
  431. Returns ``(file_bytes, suggested_filename)``.
  432. """
  433. try:
  434. parsed = urlparse(signed_url)
  435. except ValueError as exc:
  436. raise MakerWorldUrlError(f"Invalid download URL: {exc}") from exc
  437. host = (parsed.hostname or "").lower()
  438. is_allowed = host in MAKERWORLD_CDN_HOSTS or any(host.endswith(suffix) for suffix in _ALLOWED_DOWNLOAD_SUFFIXES)
  439. if not is_allowed:
  440. raise MakerWorldUrlError(f"Refusing to download from non-MakerWorld host: {host!r}")
  441. # Filename fallback from the signed path (before query string)
  442. path_tail = parsed.path.rsplit("/", 1)[-1] or "model.3mf"
  443. # Presigned S3 URLs (``s3.<region>.amazonaws.com``) compute the
  444. # signature over exact query-string bytes. Both httpx and curl_cffi
  445. # re-serialize the URL through ``urllib.parse.urlencode`` which
  446. # normalises encodings — breaks the signature and yields HTTP 400
  447. # ``SignatureDoesNotMatch`` (confirmed, and matches Pr0zak/YASTL#52's
  448. # analysis). ``urllib.request`` transmits the URL verbatim, so we
  449. # use it for S3 hosts and keep httpx for MakerWorld's own CDN.
  450. if host.endswith(".amazonaws.com"):
  451. return await _download_s3_urllib(signed_url, path_tail)
  452. # The signed URL's query-string IS the credential — don't send the
  453. # Bambu Cloud bearer to the CDN too. Strips Authorization/x-bbl-* and
  454. # keeps only User-Agent, matching what ``_download_s3_urllib`` does.
  455. cdn_headers = {"User-Agent": _CLIENT_HEADERS["User-Agent"]}
  456. try:
  457. async with self._client.stream(
  458. "GET", signed_url, headers=cdn_headers, timeout=60.0, follow_redirects=False
  459. ) as response:
  460. if response.status_code != 200:
  461. raise MakerWorldUnavailableError(f"3MF download returned HTTP {response.status_code}")
  462. chunks: list[bytes] = []
  463. total = 0
  464. async for chunk in response.aiter_bytes():
  465. total += len(chunk)
  466. if total > _MAX_3MF_BYTES:
  467. raise MakerWorldUnavailableError(f"3MF exceeds {_MAX_3MF_BYTES // (1024 * 1024)} MB cap")
  468. chunks.append(chunk)
  469. return b"".join(chunks), path_tail
  470. except httpx.TimeoutException as exc:
  471. raise MakerWorldUnavailableError(f"3MF download timed out: {exc}") from exc
  472. except httpx.HTTPError as exc:
  473. raise MakerWorldUnavailableError(f"3MF download failed: {exc}") from exc
  474. async def fetch_thumbnail(self, url: str) -> tuple[bytes, str]:
  475. """Fetch a MakerWorld CDN image (thumbnail / cover / plate preview).
  476. Used by the ``/makerworld/thumbnail`` proxy so the frontend doesn't
  477. have to hotlink MakerWorld's CDN directly — avoids loosening the
  478. SPA's ``img-src`` CSP and keeps users' IP addresses out of
  479. MakerWorld's access logs.
  480. Validates that the URL's host is one of the known MakerWorld CDN
  481. hosts (SSRF guard — same allowlist as :meth:`download_3mf`). Caps
  482. payload at 5 MB. Returns ``(bytes, content_type)``; content type
  483. defaults to ``image/jpeg`` if the upstream didn't set one.
  484. """
  485. try:
  486. parsed = urlparse(url)
  487. except ValueError as exc:
  488. raise MakerWorldUrlError(f"Invalid thumbnail URL: {exc}") from exc
  489. host = (parsed.hostname or "").lower()
  490. if host not in MAKERWORLD_CDN_HOSTS:
  491. raise MakerWorldUrlError(f"Refusing to fetch thumbnail from non-MakerWorld host: {host!r}")
  492. # ``follow_redirects=False``: the host allowlist above is only
  493. # meaningful on the initial URL. A 302 from the CDN to any other host
  494. # would otherwise be followed transparently (including RFC1918 /
  495. # metadata endpoints), so we insist upstream resolve the asset
  496. # directly. A redirect response surfaces as ``MakerWorldUnavailable``
  497. # below.
  498. try:
  499. response = await self._client.get(url, headers=self._headers(), timeout=20.0, follow_redirects=False)
  500. except httpx.TimeoutException as exc:
  501. raise MakerWorldUnavailableError(f"Thumbnail request timed out: {exc}") from exc
  502. except httpx.HTTPError as exc:
  503. raise MakerWorldUnavailableError(f"Thumbnail request failed: {exc}") from exc
  504. if response.status_code != 200:
  505. raise MakerWorldUnavailableError(f"Thumbnail fetch returned HTTP {response.status_code}")
  506. # MakerWorld's CDN serves real PNG/JPG files with
  507. # ``Content-Type: application/octet-stream`` (they use
  508. # ``Content-Disposition: attachment; filename="...png"`` instead). So
  509. # we can't just trust the header — derive the MIME from the URL's
  510. # file extension and only fall back to the header if the URL doesn't
  511. # carry one. Reject text/* / json outright regardless of extension
  512. # so an upstream error page can't slip through as "image/png".
  513. upstream_type = response.headers.get("content-type", "").split(";")[0].strip().lower()
  514. if upstream_type in _REFUSED_THUMBNAIL_MIMES:
  515. raise MakerWorldUnavailableError(f"Thumbnail upstream returned non-image content-type: {upstream_type!r}")
  516. path_lower = parsed.path.lower()
  517. ext_mime: str | None = None
  518. for ext, mime in _IMAGE_EXT_TO_MIME.items():
  519. if path_lower.endswith(ext):
  520. ext_mime = mime
  521. break
  522. if upstream_type.startswith("image/"):
  523. content_type = upstream_type
  524. elif ext_mime is not None:
  525. content_type = ext_mime
  526. else:
  527. # No image extension and no image/* content-type — can't confidently
  528. # serve this as an image, so refuse.
  529. raise MakerWorldUnavailableError(
  530. f"Thumbnail upstream returned {upstream_type!r} and URL has no image extension"
  531. )
  532. payload = response.content
  533. if len(payload) > _MAX_THUMBNAIL_BYTES:
  534. raise MakerWorldUnavailableError(f"Thumbnail exceeds {_MAX_THUMBNAIL_BYTES // (1024 * 1024)} MB cap")
  535. return payload, content_type