mirror of
https://github.com/apple/pkl.git
synced 2026-05-25 08:09:17 +02:00
Fix import/read verification when encountering glob wildcards (#1559)
Fixes an issue where the import verifier can possibly throw when packaging on Windows due to `*` being an invalid filename.
This commit is contained in:
@@ -763,6 +763,43 @@ class CliProjectPackagerTest {
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `import path verification with glob imports`(@TempDir tempDir: Path) {
|
||||
tempDir.writeFile(
|
||||
"main.pkl",
|
||||
"""
|
||||
import* "**.pkl" as foo
|
||||
|
||||
res = foo
|
||||
"""
|
||||
.trimIndent(),
|
||||
)
|
||||
tempDir.writeFile(
|
||||
"PklProject",
|
||||
"""
|
||||
amends "pkl:Project"
|
||||
|
||||
package {
|
||||
name = "mypackage"
|
||||
version = "1.0.0"
|
||||
baseUri = "package://example.com/mypackage"
|
||||
packageZipUrl = "https://foo.com"
|
||||
}
|
||||
"""
|
||||
.trimIndent(),
|
||||
)
|
||||
|
||||
CliProjectPackager(
|
||||
CliBaseOptions(workingDir = tempDir),
|
||||
listOf(tempDir),
|
||||
CliTestOptions(),
|
||||
".out/%{name}@%{version}",
|
||||
skipPublishCheck = true,
|
||||
consoleWriter = StringWriter(),
|
||||
)
|
||||
.run()
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisabledOnOs(OS.WINDOWS)
|
||||
fun `import path verification -- absolute read from root dir`(@TempDir tempDir: Path) {
|
||||
|
||||
@@ -393,9 +393,6 @@ public final class ProjectPackager {
|
||||
*/
|
||||
public void validateImportsAndReads(Project project, Path pklModulePath) {
|
||||
var imports = getImportsAndReads(pklModulePath);
|
||||
if (imports == null) {
|
||||
return;
|
||||
}
|
||||
for (var importContext : imports) {
|
||||
var importStr = importContext.stringValue();
|
||||
var sourceSection = importContext.sourceSection();
|
||||
@@ -420,14 +417,14 @@ public final class ProjectPackager {
|
||||
.toPklException(stackFrameTransformer, color);
|
||||
}
|
||||
var currentPath = pklModulePath.getParent();
|
||||
var importPath = Path.of(importUri.getPath());
|
||||
var importPath = importUri.getPath().split("/");
|
||||
// It's not good enough to just check the normalized path to see whether it exists within the
|
||||
// root dir.
|
||||
// It's possible that the import path resolves to a path outside the project dir,
|
||||
// and then back inside the project dir.
|
||||
for (var i = 0; i < importPath.getNameCount(); i++) {
|
||||
var segment = importPath.getName(i);
|
||||
currentPath = currentPath.resolve(segment);
|
||||
for (var segment : importPath) {
|
||||
// replace any possibly reserved filename characters with underscore.
|
||||
currentPath = currentPath.resolve(sanitizePathSegment(segment));
|
||||
var normalized = currentPath.normalize();
|
||||
if (!normalized.startsWith(project.getProjectDir())) {
|
||||
throw new VmExceptionBuilder()
|
||||
@@ -440,6 +437,10 @@ public final class ProjectPackager {
|
||||
}
|
||||
}
|
||||
|
||||
private String sanitizePathSegment(String segment) {
|
||||
return segment.replaceAll("[\\\\<>:\"|?*]", "_");
|
||||
}
|
||||
|
||||
private List<ImportsAndReadsParser.Entry> getImportsAndReads(Path pklModulePath) {
|
||||
try {
|
||||
var moduleKey = ModuleKeys.file(pklModulePath);
|
||||
|
||||
Reference in New Issue
Block a user