Skip to content

Commit 42dcad2

Browse files
authored
Fix error message when reading a resource/module past root dir (#1234)
1 parent 3a29ea8 commit 42dcad2

File tree

4 files changed

+43
-30
lines changed

4 files changed

+43
-30
lines changed

pkl-core/src/main/java/org/pkl/core/SecurityManagers.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -145,17 +145,17 @@ private static class Standard implements SecurityManager {
145145

146146
@Override
147147
public void checkResolveModule(URI uri) throws SecurityManagerException {
148-
checkRead(uri, allowedModules, "moduleNotInAllowList");
148+
checkRead(uri, allowedModules, false);
149149
}
150150

151151
@Override
152152
public void checkResolveResource(URI resource) throws SecurityManagerException {
153-
checkRead(resource, allowedResources, "resourceNotInAllowList");
153+
checkRead(resource, allowedResources, true);
154154
}
155155

156156
@Override
157157
public void checkReadResource(URI uri) throws SecurityManagerException {
158-
checkRead(uri, allowedResources, "resourceNotInAllowList");
158+
checkRead(uri, allowedResources, true);
159159
}
160160

161161
@Override
@@ -185,21 +185,21 @@ public void checkImportModule(URI importingModule, URI importedModule)
185185
}
186186
}
187187

188-
private void checkRead(URI uri, List<Pattern> allowedPatterns, String errorMessageKey)
188+
private void checkRead(URI uri, List<Pattern> allowedPatterns, boolean isResource)
189189
throws SecurityManagerException {
190190
for (var pattern : allowedPatterns) {
191191
if (pattern.matcher(uri.toString()).lookingAt()) {
192-
checkIsUnderRootDir(uri, errorMessageKey);
192+
checkIsUnderRootDir(uri, isResource);
193193
return;
194194
}
195195
}
196196

197-
var message = ErrorMessages.create(errorMessageKey, uri);
197+
var messageKey = isResource ? "resourceNotInAllowList" : "moduleNotInAllowList";
198+
var message = ErrorMessages.create(messageKey, uri);
198199
throw new SecurityManagerException(message);
199200
}
200201

201-
private void checkIsUnderRootDir(URI uri, String errorMessageKey)
202-
throws SecurityManagerException {
202+
private void checkIsUnderRootDir(URI uri, boolean isResource) throws SecurityManagerException {
203203
if (!uri.isAbsolute()) {
204204
throw new AssertionError("Expected absolute URI but got: " + uri);
205205
}
@@ -220,7 +220,8 @@ private void checkIsUnderRootDir(URI uri, String errorMessageKey)
220220
}
221221

222222
if (!path.startsWith(rootDir)) {
223-
var message = ErrorMessages.create(errorMessageKey, uri);
223+
var errorMessageKey = isResource ? "resourcePastRootDir" : "modulePastRootDir";
224+
var message = ErrorMessages.create(errorMessageKey, uri, rootDir);
224225
throw new SecurityManagerException(message);
225226
}
226227
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,12 @@ Refusing to read resource `{0}` because it does not match any entry in the resou
690690
moduleNotInAllowList=\
691691
Refusing to load module `{0}` because it does not match any entry in the module allowlist (`--allowed-modules`).
692692

693+
resourcePastRootDir=\
694+
Refusing to read resource `{0}` because it is not within the root directory (`--root-dir`).
695+
696+
modulePastRootDir=\
697+
Refusing to load module `{0}` because it is not within the root directory (`--root-dir`).
698+
693699
insufficientModuleTrustLevel=\
694700
Refusing to import module `{0}` because importing module `{1}` has an insufficient trust level.
695701

pkl-core/src/test/kotlin/org/pkl/core/EvaluatorTest.kt

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -269,27 +269,35 @@ class EvaluatorTest {
269269
@Test
270270
fun `cannot import module located outside root dir`(@TempDir tempDir: Path) {
271271
val evaluator =
272-
EvaluatorBuilder.preconfigured()
273-
.setSecurityManager(
274-
SecurityManagers.standard(
275-
SecurityManagers.defaultAllowedModules,
276-
SecurityManagers.defaultAllowedResources,
277-
SecurityManagers.defaultTrustLevels,
278-
tempDir,
279-
)
280-
)
281-
.build()
272+
with(EvaluatorBuilder.preconfigured()) {
273+
rootDir = tempDir
274+
build()
275+
}
282276

283-
val module = tempDir.resolve("test.pkl")
284-
module.writeString(
285-
"""
286-
amends "/non/existing.pkl"
287-
"""
288-
.trimIndent()
289-
)
277+
val module = tempDir.resolve("test.pkl").writeString("amends \"/non/existing.pkl\"")
278+
279+
val e = assertThrows<PklException> { evaluator.evaluate(path(module)) }
280+
assertThat(e.message)
281+
.contains(
282+
"Refusing to load module `file:///non/existing.pkl` because it is not within the root directory (`--root-dir`)."
283+
)
284+
}
285+
286+
@Test
287+
fun `cannot read resource located outside root dir`(@TempDir tempDir: Path) {
288+
val evaluator =
289+
with(EvaluatorBuilder.preconfigured()) {
290+
rootDir = tempDir
291+
build()
292+
}
293+
294+
val module = tempDir.resolve("test.pkl").writeString("res = read(\"/bar.txt\")")
290295

291296
val e = assertThrows<PklException> { evaluator.evaluate(path(module)) }
292-
assertThat(e.message).contains("Refusing to load module `file:///non/existing.pkl`")
297+
assertThat(e)
298+
.hasMessageContaining(
299+
"Refusing to read resource `file:///bar.txt` because it is not within the root directory (`--root-dir`)."
300+
)
293301
}
294302

295303
@Test

pkl-gradle/src/test/kotlin/org/pkl/gradle/EvaluatorsTest.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -306,9 +306,7 @@ class EvaluatorsTest : AbstractTest() {
306306
val result = runTask("evalTest", expectFailure = true)
307307
assertThat(result.output).contains("Refusing to load module")
308308
assertThat(result.output)
309-
.contains(
310-
"because it does not match any entry in the module allowlist (`--allowed-modules`)."
311-
)
309+
.contains("because it is not within the root directory (`--root-dir`).")
312310
}
313311

314312
@Test

0 commit comments

Comments
 (0)