Skip to content

Commit fb147aa

Browse files
author
Alan Bateman
committed
8300228: ModuleReader.find on exploded module throws if resource name maps to invalid file path
Reviewed-by: jpai, chegar, cstein
1 parent 4cd166f commit fb147aa

File tree

2 files changed

+43
-26
lines changed

2 files changed

+43
-26
lines changed

Diff for: src/java.base/share/classes/jdk/internal/module/Resources.java

+15-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2016, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2016, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -28,6 +28,7 @@
2828
import java.io.IOException;
2929
import java.nio.file.FileSystem;
3030
import java.nio.file.Files;
31+
import java.nio.file.InvalidPathException;
3132
import java.nio.file.NoSuchFileException;
3233
import java.nio.file.Path;
3334
import java.nio.file.attribute.BasicFileAttributes;
@@ -132,15 +133,24 @@ private static Path toSafeFilePath(FileSystem fs, String name) {
132133
return null;
133134
}
134135

135-
// convert to file path
136-
Path path;
136+
// map resource name to a file path string
137+
String pathString;
137138
if (File.separatorChar == '/') {
138-
path = fs.getPath(name);
139+
pathString = name;
139140
} else {
140141
// not allowed to embed file separators
141142
if (name.contains(File.separator))
142143
return null;
143-
path = fs.getPath(name.replace('/', File.separatorChar));
144+
pathString = name.replace('/', File.separatorChar);
145+
}
146+
147+
// try to convert to a Path
148+
Path path;
149+
try {
150+
path = fs.getPath(pathString);
151+
} catch (InvalidPathException e) {
152+
// not a valid file path
153+
return null;
144154
}
145155

146156
// file path not allowed to have root component

Diff for: test/jdk/java/lang/module/ModuleReader/ModuleReaderTest.java

+28-21
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2015, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -23,6 +23,7 @@
2323

2424
/**
2525
* @test
26+
* @bug 8142968 8300228
2627
* @library /test/lib
2728
* @modules java.base/jdk.internal.module
2829
* jdk.compiler
@@ -64,9 +65,7 @@
6465
import org.testng.annotations.Test;
6566
import static org.testng.Assert.*;
6667

67-
@Test
6868
public class ModuleReaderTest {
69-
7069
private static final String TEST_SRC = System.getProperty("test.src");
7170

7271
private static final Path USER_DIR = Paths.get(System.getProperty("user.dir"));
@@ -110,6 +109,12 @@ public class ModuleReaderTest {
110109
"../java/lang/Object.class",
111110
"java/../lang/Object.class",
112111
"java/lang/../Object.class",
112+
113+
// junk resource names
114+
"java\u0000",
115+
"C:java",
116+
"C:\\java",
117+
"java\\lang\\Object.class"
113118
};
114119

115120
// resources in test module (can't use module-info.class as a test
@@ -136,26 +141,28 @@ public class ModuleReaderTest {
136141
"./p/Main.class",
137142
"p/./Main.class",
138143
"../p/Main.class",
139-
"p/../p/Main.class"
140-
};
144+
"p/../p/Main.class",
141145

146+
// junk resource names
147+
"p\u0000",
148+
"C:p",
149+
"C:\\p",
150+
"p\\Main.class"
151+
};
142152

143153
@BeforeTest
144154
public void compileTestModule() throws Exception {
145-
146155
// javac -d mods/$TESTMODULE src/$TESTMODULE/**
147-
boolean compiled
148-
= CompilerUtils.compile(SRC_DIR.resolve(TEST_MODULE),
149-
MODS_DIR.resolve(TEST_MODULE));
156+
boolean compiled = CompilerUtils.compile(SRC_DIR.resolve(TEST_MODULE),
157+
MODS_DIR.resolve(TEST_MODULE));
150158
assertTrue(compiled, "test module did not compile");
151159
}
152160

153-
154161
/**
155-
* Test ModuleReader to module in runtime image
162+
* Test ModuleReader with module in runtime image.
156163
*/
164+
@Test
157165
public void testImage() throws IOException {
158-
159166
ModuleFinder finder = ModuleFinder.ofSystem();
160167
ModuleReference mref = finder.find(BASE_MODULE).get();
161168
ModuleReader reader = mref.open();
@@ -227,18 +234,18 @@ public void testImage() throws IOException {
227234
} catch (IOException expected) { }
228235
}
229236

230-
231237
/**
232-
* Test ModuleReader to exploded module
238+
* Test ModuleReader with exploded module.
233239
*/
240+
@Test
234241
public void testExplodedModule() throws IOException {
235242
test(MODS_DIR);
236243
}
237244

238-
239245
/**
240-
* Test ModuleReader to modular JAR
246+
* Test ModuleReader with module in modular JAR.
241247
*/
248+
@Test
242249
public void testModularJar() throws IOException {
243250
Path dir = Files.createTempDirectory(USER_DIR, "mlib");
244251

@@ -249,10 +256,10 @@ public void testModularJar() throws IOException {
249256
test(dir);
250257
}
251258

252-
253259
/**
254-
* Test ModuleReader to JMOD
260+
* Test ModuleReader with module in a JMOD file.
255261
*/
262+
@Test
256263
public void testJMod() throws IOException {
257264
Path dir = Files.createTempDirectory(USER_DIR, "mlib");
258265

@@ -269,13 +276,11 @@ public void testJMod() throws IOException {
269276
test(dir);
270277
}
271278

272-
273279
/**
274280
* The test module is found on the given module path. Open a ModuleReader
275281
* to the test module and test the reader.
276282
*/
277283
void test(Path mp) throws IOException {
278-
279284
ModuleFinder finder = ModulePath.of(Runtime.version(), true, mp);
280285
ModuleReference mref = finder.find(TEST_MODULE).get();
281286
ModuleReader reader = mref.open();
@@ -284,6 +289,7 @@ void test(Path mp) throws IOException {
284289

285290
// test resources in test module
286291
for (String name : TEST_RESOURCES) {
292+
System.out.println("resource: " + name);
287293
byte[] expectedBytes
288294
= Files.readAllBytes(MODS_DIR
289295
.resolve(TEST_MODULE)
@@ -297,7 +303,7 @@ void test(Path mp) throws IOException {
297303

298304
// test resources that may be in the test module
299305
for (String name : MAYBE_TEST_RESOURCES) {
300-
System.out.println(name);
306+
System.out.println("resource: " + name);
301307
Optional<URI> ouri = reader.find(name);
302308
ouri.ifPresent(uri -> {
303309
if (name.endsWith("/"))
@@ -307,6 +313,7 @@ void test(Path mp) throws IOException {
307313

308314
// test "not found" in test module
309315
for (String name : NOT_TEST_RESOURCES) {
316+
System.out.println("resource: " + name);
310317
assertFalse(reader.find(name).isPresent());
311318
assertFalse(reader.open(name).isPresent());
312319
assertFalse(reader.read(name).isPresent());

0 commit comments

Comments
 (0)