Skip to content

Commit eff9a9e

Browse files
Carpe-WangHT154
andauthored
[fix] Add support for handling remote file URIs. (#629)
Co-authored-by: Jen Basch <jbasch@apple.com>
1 parent 8399f11 commit eff9a9e

3 files changed

Lines changed: 14 additions & 1 deletion

File tree

‎pkl-core/src/main/java/org/pkl/core/util/IoUtils.java‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -892,6 +892,9 @@ public static void validateFileUri(URI uri) throws URISyntaxException {
892892
if (!uri.getSchemeSpecificPart().startsWith("/")) {
893893
throw new URISyntaxException(uri.toString(), ErrorMessages.create("invalidOpaqueFileUri"));
894894
}
895+
if (uri.getRawAuthority() != null) {
896+
throw new URISyntaxException(uri.toString(), ErrorMessages.create("invalidRemoteFileUri"));
897+
}
895898
}
896899

897900
public static void validateRewriteRule(URI rewrite) {

‎pkl-core/src/main/resources/org/pkl/core/errorMessages.properties‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1130,6 +1130,9 @@ invalidOpaqueFileUri=\
11301130
File URIs must have a path that starts with `/` (e.g. file:/path/to/my_module.pkl).\n\
11311131
To resolve relative paths, remove the scheme prefix (remove "file:").
11321132

1133+
invalidRemoteFileUri=\
1134+
File URIs may not include an authority.
1135+
11331136
invalidStringBase64=\
11341137
`{0}` is not in base64 encoding.
11351138

‎pkl-core/src/test/kotlin/org/pkl/core/util/IoUtilsTest.kt‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright © 2024-2025 Apple Inc. and the Pkl project authors. All rights reserved.
2+
* Copyright © 2024-2026 Apple Inc. and the Pkl project authors. All rights reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -400,6 +400,13 @@ class IoUtilsTest {
400400
assertThrows<IllegalArgumentException> { IoUtils.readString(URI("http://example.com").toURL()) }
401401
}
402402

403+
@Test
404+
fun `toUrl() throws UnsupportedOperationException for file URIs with remote hostnames`() {
405+
val uri = URI("file://example.com/bar/baz.pkl")
406+
val exception = assertThrows<URISyntaxException> { IoUtils.validateFileUri(uri) }
407+
assertThat(exception.message).contains("File URIs may not include an authority.")
408+
}
409+
403410
@Test
404411
fun `encodePath encodes characters reserved on windows`() {
405412
assertThat(IoUtils.encodePath("foo:bar")).isEqualTo("foo(3a)bar")

0 commit comments

Comments
 (0)