Skip to content

Commit 9c87723

Browse files
committed
Check for root directory exposure also for folder retention.
1 parent 770f6ec commit 9c87723

2 files changed

Lines changed: 16 additions & 4 deletions

File tree

byte-buddy-dep/src/main/java/net/bytebuddy/build/Plugin.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3860,11 +3860,15 @@ public void store(ClassFileVersion classFileVersion, Map<TypeDescription, byte[]
38603860
public void retain(Source.Element element) throws IOException {
38613861
String name = element.getName();
38623862
File target = new File(folder, name);
3863-
if (!name.endsWith("/")) {
3863+
String basePath = folder.getCanonicalPath(), targetPath = target.getCanonicalPath(), prefix = basePath;
3864+
if (!prefix.endsWith(File.separator)) {
3865+
prefix += File.separatorChar;
3866+
}
3867+
if (!targetPath.equals(basePath) && !targetPath.startsWith(prefix)) {
3868+
throw new IllegalArgumentException(target + " is not a subdirectory of " + folder);
3869+
} else if (!name.endsWith("/")) {
38643870
File resolved = element.resolveAs(File.class);
3865-
if (!target.getCanonicalPath().startsWith(folder.getCanonicalPath() + File.separatorChar)) {
3866-
throw new IllegalArgumentException(target + " is not a subdirectory of " + folder);
3867-
} else if (!target.getParentFile().isDirectory() && !target.getParentFile().mkdirs()) {
3871+
if (!target.getParentFile().isDirectory() && !target.getParentFile().mkdirs()) {
38683872
throw new IOException("Could not create directory: " + target.getParent());
38693873
} else if (resolved != null && !resolved.equals(target)) {
38703874
if (link) {

byte-buddy-dep/src/test/java/net/bytebuddy/build/PluginEngineTargetForFolderTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,4 +209,12 @@ public void testCannotWriteRelativeLocation() throws Exception {
209209
when(element.getName()).thenReturn("../illegal");
210210
target.write(Plugin.Engine.Source.Origin.NO_MANIFEST).retain(element);
211211
}
212+
213+
@Test(expected = IllegalArgumentException.class)
214+
public void testCannotWriteRelativeDirectory() throws Exception {
215+
Plugin.Engine.Target target = new Plugin.Engine.Target.ForFolder(folder);
216+
Plugin.Engine.Source.Element element = mock(Plugin.Engine.Source.Element.class);
217+
when(element.getName()).thenReturn("../illegal/");
218+
target.write(Plugin.Engine.Source.Origin.NO_MANIFEST).retain(element);
219+
}
212220
}

0 commit comments

Comments
 (0)