diff --git a/.gitignore b/.gitignore index 078aa39cf..830cfa198 100644 --- a/.gitignore +++ b/.gitignore @@ -23,4 +23,6 @@ src/main/main.iml *.BASE.*.jsp *.LOCAL.*.jsp *.REMOTE.*.jsp +src/main/webapp/plugin_extracted/* +/*.iml diff --git a/pom.xml b/pom.xml index bf017f52a..e4d5b7625 100644 --- a/pom.xml +++ b/pom.xml @@ -292,6 +292,12 @@ 1.3.2 + + com.google.guava + guava + 18.0 + + javax.servlet diff --git a/src/main/java/org/owasp/webgoat/lessons/LessonAdapter.java b/src/main/java/org/owasp/webgoat/lessons/LessonAdapter.java index 56c2bcba1..c905cc0ee 100644 --- a/src/main/java/org/owasp/webgoat/lessons/LessonAdapter.java +++ b/src/main/java/org/owasp/webgoat/lessons/LessonAdapter.java @@ -1,9 +1,5 @@ package org.owasp.webgoat.lessons; -import java.io.BufferedReader; -import java.io.FileReader; -import java.util.ArrayList; -import java.util.List; import org.apache.ecs.Element; import org.apache.ecs.ElementContainer; import org.apache.ecs.StringElement; @@ -16,6 +12,11 @@ import org.apache.ecs.html.TR; import org.apache.ecs.html.Table; import org.owasp.webgoat.session.WebSession; +import java.io.BufferedReader; +import java.io.FileReader; +import java.util.ArrayList; +import java.util.List; + /** * ************************************************************************************************* * @@ -157,7 +158,7 @@ public abstract class LessonAdapter extends AbstractLesson { StringBuffer buff = new StringBuffer(); String lang = s.getCurrrentLanguage(); try { - String fileName = s.getWebResource(getLessonPlanFileName(lang)); + String fileName = getLessonPlanFileName(lang); if (fileName != null) { BufferedReader in = new BufferedReader(new FileReader(fileName)); String line = null; diff --git a/src/main/java/org/owasp/webgoat/plugins/Plugin.java b/src/main/java/org/owasp/webgoat/plugins/Plugin.java index e96b56108..7018d697e 100644 --- a/src/main/java/org/owasp/webgoat/plugins/Plugin.java +++ b/src/main/java/org/owasp/webgoat/plugins/Plugin.java @@ -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 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); } diff --git a/src/main/java/org/owasp/webgoat/plugins/PluginFileUtils.java b/src/main/java/org/owasp/webgoat/plugins/PluginFileUtils.java index a3b26a34b..bf8bd6e36 100644 --- a/src/main/java/org/owasp/webgoat/plugins/PluginFileUtils.java +++ b/src/main/java/org/owasp/webgoat/plugins/PluginFileUtils.java @@ -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 getFilesInDirectory( Path directory) throws IOException { - List files = new ArrayList<>(); - DirectoryStream dirStream; - dirStream = Files.newDirectoryStream(directory); - for (Path entry : dirStream) { - files.add(entry); - } - dirStream.close(); - return files; + + public static List getFilesInDirectory(Path directory) throws IOException { + List files = new ArrayList<>(); + DirectoryStream 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 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); } } diff --git a/src/main/java/org/owasp/webgoat/session/Course.java b/src/main/java/org/owasp/webgoat/session/Course.java index ca83d7e9b..0b792b866 100644 --- a/src/main/java/org/owasp/webgoat/session/Course.java +++ b/src/main/java/org/owasp/webgoat/session/Course.java @@ -1,5 +1,15 @@ package org.owasp.webgoat.session; +import org.owasp.webgoat.HammerHead; +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.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.Paths; @@ -8,8 +18,7 @@ import java.util.Collections; import java.util.Iterator; import java.util.LinkedList; import java.util.List; -import java.util.Map; - +import java.util.Map import javax.servlet.ServletContext; import org.owasp.webgoat.HammerHead; @@ -65,6 +74,8 @@ public class Course { private WebgoatProperties properties = null; + private final List files = new LinkedList(); + private WebgoatContext webgoatContext; public Course() { diff --git a/src/main/webapp/plugin_lessons/SqlStringInjection-1.0.jar b/src/main/webapp/plugin_lessons/SqlStringInjection-1.0.jar deleted file mode 100644 index 707d626f9..000000000 Binary files a/src/main/webapp/plugin_lessons/SqlStringInjection-1.0.jar and /dev/null differ diff --git a/src/test/java/org/owasp/webgoat/plugins/PluginTest.java b/src/test/java/org/owasp/webgoat/plugins/PluginTest.java index aac20ba77..4225a3bda 100644 --- a/src/test/java/org/owasp/webgoat/plugins/PluginTest.java +++ b/src/test/java/org/owasp/webgoat/plugins/PluginTest.java @@ -26,11 +26,11 @@ public class PluginTest { List 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 allLines = Files.readAllLines(htmlFile, StandardCharsets.UTF_8); assertThat(allLines, - hasItem(containsString("lesson_solutions/Unknown_files/image001.png"))); + hasItem(containsString("Unknown_files/image001.png"))); } } \ No newline at end of file diff --git a/src/test/resources/org/owasp/webgoat/plugins/lessonSolutions/rewrite_test.html b/src/test/resources/org/owasp/webgoat/plugins/lessonSolutions/rewrite_test.html index aaeb3600b..dde467046 100644 --- a/src/test/resources/org/owasp/webgoat/plugins/lessonSolutions/rewrite_test.html +++ b/src/test/resources/org/owasp/webgoat/plugins/lessonSolutions/rewrite_test.html @@ -5,7 +5,7 @@ - - + + \ No newline at end of file