WEB-197 Images referenced in the plans are not changed by the plugin loader

This commit is contained in:
nbaars 2015-02-23 12:45:19 +01:00
parent 32b6ef9c53
commit f140669edf
6 changed files with 51 additions and 28 deletions

View File

@ -290,6 +290,12 @@
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>18.0</version>
</dependency>
<!-- JSTL -->
<dependency>
<groupId>javax.servlet</groupId>

View File

@ -10,7 +10,6 @@ import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -117,13 +116,8 @@ public class Plugin {
public void rewritePaths(Path pluginTarget) {
try {
for (Map.Entry<String, File> html : solutionLanguageFiles.entrySet()) {
byte[] htmlFileAsBytes = Files.readAllBytes(Paths.get(html.getValue().toURI()));
String htmlFile = new String(htmlFileAsBytes);
htmlFile = htmlFile.replaceAll("lesson_solutions/" + this.lesson.getSimpleName() + "_files", pluginTarget.getFileName().toString() + "/lessons/plugin/" + this.lesson.getSimpleName() + "/lessonSolutions/en/" + this.lesson.getSimpleName() + "_files");
Files.write(Paths.get(html.getValue().toURI()), htmlFile.getBytes(), CREATE,
TRUNCATE_EXISTING);
}
PluginFileUtils.replaceInFiles(this.lesson.getSimpleName() + "_files", pluginTarget.getFileName().toString() + "/plugin/" + this.lesson.getSimpleName() + "/lessonSolutions/en/" + this.lesson.getSimpleName() + "_files", solutionLanguageFiles.values());
PluginFileUtils.replaceInFiles(this.lesson.getSimpleName() + "_files", pluginTarget.getFileName().toString() + "/plugin/" + this.lesson.getSimpleName() + "/lessonPlans/en/" + this.lesson.getSimpleName() + "_files", lessonPlansLanguageFiles.values());
} catch (IOException e) {
throw new PluginLoadingFailure("Unable to rewrite the paths in the solutions", e);
}

View File

@ -1,11 +1,17 @@
package org.owasp.webgoat.plugins;
import com.google.common.base.Preconditions;
import java.io.File;
import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
public class PluginFileUtils {
@ -25,21 +31,42 @@ public class PluginFileUtils {
}
public static Path createDirsIfNotExists(Path p) throws IOException {
if ( Files.notExists(p)) {
if (Files.notExists(p)) {
Files.createDirectories(p);
}
return p;
}
public static List<Path> getFilesInDirectory( Path directory) throws IOException {
List<Path> files = new ArrayList<>();
DirectoryStream<Path> dirStream;
dirStream = Files.newDirectoryStream(directory);
for (Path entry : dirStream) {
files.add(entry);
}
dirStream.close();
return files;
public static List<Path> getFilesInDirectory(Path directory) throws IOException {
List<Path> files = new ArrayList<>();
DirectoryStream<Path> dirStream;
dirStream = Files.newDirectoryStream(directory);
for (Path entry : dirStream) {
files.add(entry);
}
dirStream.close();
return files;
}
public static void replaceInFiles(String replace, String with, Collection<File> files) throws IOException {
Preconditions.checkNotNull(replace);
Preconditions.checkNotNull(with);
Preconditions.checkNotNull(files);
for (File file : files) {
replaceInFile(replace, with, Paths.get(file.toURI()));
}
}
public static void replaceInFile(String replace, String with, Path file) throws IOException {
Preconditions.checkNotNull(replace);
Preconditions.checkNotNull(with);
Preconditions.checkNotNull(file);
byte[] fileAsBytes = Files.readAllBytes(file);
String fileAsString = new String(fileAsBytes);
fileAsString = fileAsString.replaceAll(replace, with);
Files.write(file, fileAsString.getBytes(), StandardOpenOption.TRUNCATE_EXISTING);
}
}

View File

@ -5,16 +5,13 @@ import org.owasp.webgoat.lessons.AbstractLesson;
import org.owasp.webgoat.lessons.Category;
import org.owasp.webgoat.plugins.GlobalProperties;
import org.owasp.webgoat.plugins.Plugin;
import org.owasp.webgoat.plugins.PluginFileUtils;
import org.owasp.webgoat.plugins.PluginsLoader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.servlet.ServletContext;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
@ -22,7 +19,6 @@ import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* *************************************************************************************************

View File

@ -26,11 +26,11 @@ public class PluginTest {
List<String> allLines = Files.readAllLines(htmlFile, StandardCharsets.UTF_8);
assertThat(allLines,
hasItem(containsString("lessons/plugin/TestPlugin/lessonSolutions/en/TestPlugin_files/image001.png")));
hasItem(containsString("plugin/TestPlugin/lessonSolutions/en/TestPlugin_files/image001.png")));
}
@Test
public void shouldNotRewriteOtherLinksStartingWithLesson_solutions() throws Exception {
public void shouldNotRewriteOtherLinks() throws Exception {
Path tmpDir = PluginTestHelper.createTmpDir();
Path pluginSourcePath = PluginTestHelper.pathForLoading();
Plugin plugin = PluginTestHelper.createPluginFor(TestPlugin.class);
@ -40,6 +40,6 @@ public class PluginTest {
List<String> allLines = Files.readAllLines(htmlFile, StandardCharsets.UTF_8);
assertThat(allLines,
hasItem(containsString("lesson_solutions/Unknown_files/image001.png")));
hasItem(containsString("Unknown_files/image001.png")));
}
}

View File

@ -5,7 +5,7 @@
<title></title>
</head>
<body>
<v:imagedata src="lesson_solutions/TestPlugin_files/image001.png" o:title=""/>
<v:imagedata src="lesson_solutions/Unknown_files/image001.png" o:title=""/>
<v:imagedata src="TestPlugin_files/image001.png" o:title=""/>
<v:imagedata src="Unknown_files/image001.png" o:title=""/>
</body>
</html>