Browse Source

Decode import URI before using it as a file name (#1206)

Co-authored-by: Marko Lahma <[email protected]>
Simon Berg 3 years ago
parent
commit
df82672681

+ 10 - 0
Jint.Tests/Runtime/ModuleTests.cs

@@ -332,6 +332,16 @@ export const count = globals.counter;
         Assert.Equal("John Doe", result);
     }
 
+    [Fact]
+    public void CanImportFromFileWithSpacesInPath()
+    {
+        var engine = new Engine(options => options.EnableModules(GetBasePath()));
+        var ns = engine.ImportModule("./dir with spaces/format name.js");
+        var result = engine.Invoke(ns.Get("formatName"), "John", "Doe").AsString();
+
+        Assert.Equal("John Doe", result);
+    }
+
     internal static string GetBasePath()
     {
         var assemblyDirectory = new DirectoryInfo(AppDomain.CurrentDomain.RelativeSearchPath ?? AppDomain.CurrentDomain.BaseDirectory);

+ 4 - 0
Jint.Tests/Runtime/Scripts/dir with spaces/format name.js

@@ -0,0 +1,4 @@
+export function formatName(firstName, lastName) {
+    return `${firstName} ${lastName}`;
+}
+

+ 3 - 3
Jint/Runtime/Modules/DefaultModuleLoader.cs

@@ -120,14 +120,14 @@ public sealed class DefaultModuleLoader : IModuleLoader
         {
             ExceptionHelper.ThrowInvalidOperationException($"Module '{resolved.Specifier}' of type '{resolved.Type}' has no resolved URI.");
         }
-
-        if (!File.Exists(resolved.Uri.AbsolutePath))
+        var fileName = Uri.UnescapeDataString(resolved.Uri.AbsolutePath);
+        if (!File.Exists(fileName))
         {
             ExceptionHelper.ThrowArgumentException("Module Not Found: ", resolved.Specifier);
             return default;
         }
 
-        var code = File.ReadAllText(resolved.Uri.LocalPath);
+        var code = File.ReadAllText(fileName);
 
         Module module;
         try