Skip to content

armgit / Filee

Public Imported from GitHub
0

Free, open-source file converter. Convert 263 formats offline by drag & drop — images, PDF, Word, Excel, PowerPoint, HWP/HWPX, video, audio, e-books and archives. No upload, no Office needed.

fix(gltf): decode percent-encoded external resource URIs

Stamina9cedeb4485546
diff --git a/src/Filee.Engines/Models3D/GltfFormat.cs b/src/Filee.Engines/Models3D/GltfFormat.cs
index 42cd040a..c90d83fd 100644
--- a/src/Filee.Engines/Models3D/GltfFormat.cs
+++ b/src/Filee.Engines/Models3D/GltfFormat.cs
@@ -26,8 +26,10 @@ internal static class GltfFormat
// Separate buffers and pictures are read only from the model's folder (ObjFormat.Resolve), never from a
// path the file names elsewhere on the disk.
var directory = Path.GetDirectoryName(Path.GetFullPath(path))!;
+ // glTF external resources are URIs: percent-encoded names must be decoded before checking their path.
var context = ReadContext.Create(name => File.ReadAllBytes(
- ObjFormat.Resolve(directory, name) ?? throw new FileNotFoundException($"{name} is not in the model's folder.", name)));
+ ObjFormat.Resolve(directory, Uri.UnescapeDataString(name))
+ ?? throw new FileNotFoundException($"{name} is not in the model's folder.", name)));
context.Validation = ValidationMode.Skip;
using var stream = File.OpenRead(path);
root = context.ReadSchema2(stream);
diff --git a/tests/Filee.Engines.Tests/ModelTests.cs b/tests/Filee.Engines.Tests/ModelTests.cs
index 9b5ce4b6..c5a5b09d 100644
--- a/tests/Filee.Engines.Tests/ModelTests.cs
+++ b/tests/Filee.Engines.Tests/ModelTests.cs
@@ -283,6 +283,34 @@ public class ModelTests(EngineFixture fx) : IClassFixture<EngineFixture>
Assert.Throws<InvalidDataException>(() => Load(buffer, Path.Combine(outside, buffer).Replace('\\', '/')));
}
+ [Fact]
+ public void Gltf_reads_percent_encoded_buffer_and_picture_names()
+ {
+ var folder = fx.NewFolder();
+ var source = Directory.CreateDirectory(Path.Combine(folder, "source")).FullName;
+ var gltf = Path.Combine(source, "quad.gltf");
+ TexturedQuad(Matrix4x4.Identity).SaveGLTF(gltf, new WriteSettings { ImageWriting = ResourceWriteMode.SatelliteFile });
+ var json = JsonNode.Parse(File.ReadAllText(gltf))!;
+ var picture = (string)json["images"]![0]!["uri"]!;
+ var buffer = (string)json["buffers"]![0]!["uri"]!;
+ File.Move(Path.Combine(source, picture), Path.Combine(source, "paint image.png"));
+ File.Move(Path.Combine(source, buffer), Path.Combine(source, "mesh data.bin"));
+ json["images"]![0]!["uri"] = "paint%20image.png";
+ json["buffers"]![0]!["uri"] = "mesh%20data.bin";
+ File.WriteAllText(gltf, json.ToJsonString());
+
+ var model = ModelBuilders.Load(gltf, "gltf");
+ Assert.Equal(2, model.TriangleCount);
+ Assert.Equal(ModelBuilders.Checker(), model.MaterialOf(model.Parts.Single())!.BaseColorTexture!.Data);
+
+ // Decoding must still leave the existing folder boundary in force.
+ var outside = Directory.CreateDirectory(Path.Combine(folder, "outside")).FullName;
+ File.Move(Path.Combine(source, "paint image.png"), Path.Combine(outside, "paint image.png"));
+ json["images"]![0]!["uri"] = "..%2Foutside%2Fpaint%20image.png";
+ File.WriteAllText(gltf, json.ToJsonString());
+ Assert.Throws<InvalidDataException>(() => ModelBuilders.Load(gltf, "gltf"));
+ }
+
private static ModelRoot TexturedQuad(Matrix4x4 transform)
{
var root = ModelRoot.CreateModel();