diff --git a/backend/app/services/gltf.py b/backend/app/services/gltf.py index d84c386..fab388d 100644 --- a/backend/app/services/gltf.py +++ b/backend/app/services/gltf.py @@ -19,6 +19,10 @@ _GLB_MAGIC_INT = 0x46546C67 # b"glTF" interpreted as little-endian uint32 _GLB_JSON_CHUNK_TYPE = 0x4E4F534A # "JSON" _GLB_BIN_CHUNK_TYPE = 0x004E4942 # "BIN\0" +# Whitespace allowed around a GLB JSON chunk. The spec pads with spaces (0x20), +# but be lenient with null bytes and other whitespace that real exporters emit. +_JSON_CHUNK_TRIM = b" \t\r\n\x00" + def looks_like_gltf(content: bytes, filename: Optional[str] = None) -> bool: """Return True when the bytes or filename indicate a glTF/GLB asset.""" @@ -65,7 +69,7 @@ def _parse_glb(content: bytes) -> dict[str, Any]: chunk_data = content[offset:chunk_end] if chunk_type == _GLB_JSON_CHUNK_TYPE: try: - json_chunk = json.loads(chunk_data.decode("utf-8")) + json_chunk = json.loads(chunk_data.strip(_JSON_CHUNK_TRIM).decode("utf-8")) except (ValueError, UnicodeDecodeError) as exc: raise ValueError(f"invalid GLB JSON chunk: {exc}") from exc