Skip to content

Commit

Permalink
Fix handling of file: module URIs with non-ASCII characters
Browse files Browse the repository at this point in the history
Closes #653
  • Loading branch information
HT154 committed Oct 16, 2024
1 parent d00c466 commit 2d75e6d
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
18 changes: 14 additions & 4 deletions pkl-core/src/main/java/org/pkl/core/module/ModuleKeys.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import org.pkl.core.PklBugException;
import org.pkl.core.SecurityManager;
import org.pkl.core.SecurityManagerException;
import org.pkl.core.packages.Dependency;
Expand Down Expand Up @@ -325,10 +326,19 @@ public ResolvedModuleKey resolve(SecurityManager securityManager)
if (java.io.File.separatorChar == '\\' && uriPath != null && uriPath.contains("\\")) {
throw new FileNotFoundException();
}
var realPath = Path.of(uri).toRealPath();
var resolvedUri = realPath.toUri();
securityManager.checkResolveModule(resolvedUri);
return ResolvedModuleKeys.file(this, resolvedUri, realPath);

// Path.of(URI) throws on non-ASCII characters so the module URI here must be normalized to
// ASCII
// Unfortunately there's no way to go from URI -> ASCII URI directly
// so this must transform URI -> ASCII String -> ASCII URI
try {
var realPath = Path.of(new URI(uri.toASCIIString())).toRealPath();
var resolvedUri = realPath.toUri();
securityManager.checkResolveModule(resolvedUri);
return ResolvedModuleKeys.file(this, resolvedUri, realPath);
} catch (URISyntaxException e) {
throw new PklBugException("File module URI could not be normalized to ASCII", e);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// covers https://github.com/apple/pkl/issues/653
日本語 = "Japanese language"
readOne = read("日本語.pkl").text
readGlob = read*("./日*.pkl").keys
importOne = import("日本語.pkl").readOne
importGlob = import*("./日*.pkl").keys
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
日本語 = "Japanese language"
readOne = """
// covers https://github.com/apple/pkl/issues/653
日本語 = "Japanese language"
readOne = read("日本語.pkl").text
readGlob = read*("./日*.pkl").keys
importOne = import("日本語.pkl").readOne
importGlob = import*("./日*.pkl").keys
"""
readGlob = Set("./日本語.pkl")
importOne = """
// covers https://github.com/apple/pkl/issues/653
日本語 = "Japanese language"
readOne = read("日本語.pkl").text
readGlob = read*("./日*.pkl").keys
importOne = import("日本語.pkl").readOne
importGlob = import*("./日*.pkl").keys
"""
importGlob = Set("./日本語.pkl")

0 comments on commit 2d75e6d

Please sign in to comment.