Skip to content

Commit

Permalink
add error when old format is used
Browse files Browse the repository at this point in the history
  • Loading branch information
rjernst committed Feb 9, 2018
1 parent 2acb871 commit 8b8bd7c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,10 @@ private Path unzip(Path zip, Path pluginsDir) throws IOException, UserException
ZipEntry entry;
byte[] buffer = new byte[8192];
while ((entry = zipInput.getNextEntry()) != null) {
if (entry.getName().startsWith("elasticsearch/")) {
throw new UserException(PLUGIN_MALFORMED, "This plugin was built with an older plugin structure." +
" Contact the plugin author to remove the intermediate \"elasticsearch\" directory within the plugin zip.");
}
Path targetFile = target.resolve(entry.getName());

// Using the entry name as a path can result in an entry outside of the plugin dir,
Expand Down Expand Up @@ -493,6 +497,9 @@ private Path unzip(Path zip, Path pluginsDir) throws IOException, UserException
}
zipInput.closeEntry();
}
} catch (UserException e) {
IOUtils.rm(target);
throw e;
}
Files.delete(zip);
return target;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,13 +199,14 @@ static void writeJar(Path jar, String... classes) throws IOException {
}
}

static Path writeZip(Path structure) throws IOException {
static Path writeZip(Path structure, String prefix) throws IOException {
Path zip = createTempDir().resolve(structure.getFileName() + ".zip");
try (ZipOutputStream stream = new ZipOutputStream(Files.newOutputStream(zip))) {
Files.walkFileTree(structure, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
stream.putNextEntry(new ZipEntry(structure.relativize(file).toString()));
String target = (prefix == null ? "" : prefix + "/") + structure.relativize(file).toString();
stream.putNextEntry(new ZipEntry(target));
Files.copy(file, stream);
return FileVisitResult.CONTINUE;
}
Expand Down Expand Up @@ -258,12 +259,12 @@ static void writePluginSecurityPolicy(Path pluginDir, String... permissions) thr

static Path createPlugin(String name, Path structure, String... additionalProps) throws IOException {
writePlugin(name, structure, additionalProps);
return writeZip(structure);
return writeZip(structure, null);
}

static Path createMetaPlugin(String name, Path structure) throws IOException {
writeMetaPlugin(name, structure);
return writeZip(structure);
return writeZip(structure, null);
}

void installPlugin(String pluginUrl, Path home) throws Exception {
Expand Down Expand Up @@ -810,7 +811,7 @@ public void testMissingDescriptor() throws Exception {
Path pluginDir = metaDir.resolve("fake");
Files.createDirectory(pluginDir);
Files.createFile(pluginDir.resolve("fake.yml"));
String pluginZip = writeZip(pluginDir).toUri().toURL().toString();
String pluginZip = writeZip(pluginDir, null).toUri().toURL().toString();
NoSuchFileException e = expectThrows(NoSuchFileException.class, () -> installPlugin(pluginZip, env.v1()));
assertTrue(e.getMessage(), e.getMessage().contains("plugin-descriptor.properties"));
assertInstallCleaned(env.v2());
Expand All @@ -821,15 +822,36 @@ public void testMissingDescriptor() throws Exception {
assertInstallCleaned(env.v2());
}

public void testContainsIntermediateDirectory() throws Exception {
Tuple<Path, Environment> env = createEnv(fs, temp);
Path pluginDir = createPluginDir(temp);
Files.createFile(pluginDir.resolve(PluginInfo.ES_PLUGIN_PROPERTIES));
String pluginZip = writeZip(pluginDir, "elasticsearch").toUri().toURL().toString();
UserException e = expectThrows(UserException.class, () -> installPlugin(pluginZip, env.v1()));
assertThat(e.getMessage(), containsString("This plugin was built with an older plugin structure"));
assertInstallCleaned(env.v2());
}

public void testContainsIntermediateDirectoryMeta() throws Exception {
Tuple<Path, Environment> env = createEnv(fs, temp);
Path pluginDir = createPluginDir(temp);
Files.createFile(pluginDir.resolve(MetaPluginInfo.ES_META_PLUGIN_PROPERTIES));
String pluginZip = writeZip(pluginDir, "elasticsearch").toUri().toURL().toString();
UserException e = expectThrows(UserException.class, () -> installPlugin(pluginZip, env.v1()));
assertThat(e.getMessage(), containsString("This plugin was built with an older plugin structure"));
assertInstallCleaned(env.v2());
}

public void testZipRelativeOutsideEntryName() throws Exception {
Tuple<Path, Environment> env = createEnv(fs, temp);
Path zip = createTempDir().resolve("broken.zip");
try (ZipOutputStream stream = new ZipOutputStream(Files.newOutputStream(zip))) {
stream.putNextEntry(new ZipEntry("elasticsearch/../blah"));
stream.putNextEntry(new ZipEntry("../blah"));
}
String pluginZip = zip.toUri().toURL().toString();
UserException e = expectThrows(UserException.class, () -> installPlugin(pluginZip, env.v1()));
assertTrue(e.getMessage(), e.getMessage().contains("resolving outside of plugin directory"));
assertInstallCleaned(env.v2());
}

public void testOfficialPluginsHelpSorted() throws Exception {
Expand Down

0 comments on commit 8b8bd7c

Please sign in to comment.