diff --git a/webgoat-container/src/main/java/org/owasp/webgoat/MvcConfiguration.java b/webgoat-container/src/main/java/org/owasp/webgoat/MvcConfiguration.java index 96940764a..13ff7139d 100644 --- a/webgoat-container/src/main/java/org/owasp/webgoat/MvcConfiguration.java +++ b/webgoat-container/src/main/java/org/owasp/webgoat/MvcConfiguration.java @@ -32,6 +32,7 @@ package org.owasp.webgoat; import com.google.common.collect.Sets; import org.owasp.webgoat.i18n.Messages; +import org.owasp.webgoat.i18n.PluginMessages; import org.owasp.webgoat.session.Course; import org.owasp.webgoat.session.LabelDebugger; import org.springframework.beans.factory.annotation.Autowired; @@ -117,8 +118,13 @@ public class MvcConfiguration extends WebMvcConfigurerAdapter { } @Bean - public Messages messageSource() { - Messages messages = new Messages(localeResolver()); + public PluginMessages pluginMessages(Messages messages) { + return new PluginMessages(messages); + } + + @Bean + public Messages messageSource(LocaleResolver localeResolver) { + Messages messages = new Messages(localeResolver); messages.setBasename("classpath:/i18n/messages"); return messages; } diff --git a/webgoat-container/src/main/java/org/owasp/webgoat/WebGoat.java b/webgoat-container/src/main/java/org/owasp/webgoat/WebGoat.java index f2ac1365c..588cf51a7 100644 --- a/webgoat-container/src/main/java/org/owasp/webgoat/WebGoat.java +++ b/webgoat-container/src/main/java/org/owasp/webgoat/WebGoat.java @@ -34,6 +34,7 @@ import com.fasterxml.jackson.annotation.JsonInclude; import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; import org.apache.catalina.Context; +import org.owasp.webgoat.i18n.PluginMessages; import org.owasp.webgoat.plugins.PluginClassLoader; import org.owasp.webgoat.plugins.PluginEndpointPublisher; import org.owasp.webgoat.plugins.PluginsExtractor; @@ -91,8 +92,8 @@ public class WebGoat extends SpringBootServletInitializer { } @Bean - public PluginsExtractor pluginsLoader(@Qualifier("pluginTargetDirectory") File pluginTargetDirectory, PluginClassLoader classLoader) { - return new PluginsExtractor(pluginTargetDirectory, classLoader); + public PluginsExtractor pluginsLoader(@Qualifier("pluginTargetDirectory") File pluginTargetDirectory, PluginClassLoader classLoader, PluginMessages messages) { + return new PluginsExtractor(pluginTargetDirectory, classLoader, messages); } @Bean diff --git a/webgoat-container/src/main/java/org/owasp/webgoat/assignments/AssignmentEndpoint.java b/webgoat-container/src/main/java/org/owasp/webgoat/assignments/AssignmentEndpoint.java index 260fc3c6f..f44b1cfd0 100644 --- a/webgoat-container/src/main/java/org/owasp/webgoat/assignments/AssignmentEndpoint.java +++ b/webgoat-container/src/main/java/org/owasp/webgoat/assignments/AssignmentEndpoint.java @@ -25,7 +25,7 @@ package org.owasp.webgoat.assignments; import lombok.Getter; -import org.owasp.webgoat.i18n.Messages; +import org.owasp.webgoat.i18n.PluginMessages; import org.owasp.webgoat.session.UserSessionData; import org.owasp.webgoat.session.UserTracker; import org.owasp.webgoat.session.WebSession; @@ -50,7 +50,7 @@ public abstract class AssignmentEndpoint extends Endpoint { private UserSessionData userSessionData; @Getter @Autowired - private Messages messages; + private PluginMessages messages; //// TODO: 11/13/2016 events better fit? protected AttackResult trackProgress(AttackResult attackResult) { diff --git a/webgoat-container/src/main/java/org/owasp/webgoat/assignments/AttackResult.java b/webgoat-container/src/main/java/org/owasp/webgoat/assignments/AttackResult.java index 207575c8c..4cf1dbad8 100644 --- a/webgoat-container/src/main/java/org/owasp/webgoat/assignments/AttackResult.java +++ b/webgoat-container/src/main/java/org/owasp/webgoat/assignments/AttackResult.java @@ -27,7 +27,7 @@ package org.owasp.webgoat.assignments; import lombok.AllArgsConstructor; import lombok.Getter; -import org.owasp.webgoat.i18n.Messages; +import org.owasp.webgoat.i18n.PluginMessages; @AllArgsConstructor public class AttackResult { @@ -35,13 +35,13 @@ public class AttackResult { public static class AttackResultBuilder { private boolean lessonCompleted; - private Messages messages; + private PluginMessages messages; private Object[] feedbackArgs; private String feedbackResourceBundleKey; private String output; private Object[] outputArgs; - public AttackResultBuilder(Messages messages) { + public AttackResultBuilder(PluginMessages messages) { this.messages = messages; } @@ -84,7 +84,7 @@ public class AttackResult { private String output; - public static AttackResultBuilder builder(Messages messages) { + public static AttackResultBuilder builder(PluginMessages messages) { return new AttackResultBuilder(messages); } diff --git a/webgoat-container/src/main/java/org/owasp/webgoat/i18n/Messages.java b/webgoat-container/src/main/java/org/owasp/webgoat/i18n/Messages.java index d44fbedd8..527a69b93 100644 --- a/webgoat-container/src/main/java/org/owasp/webgoat/i18n/Messages.java +++ b/webgoat-container/src/main/java/org/owasp/webgoat/i18n/Messages.java @@ -46,6 +46,7 @@ public class Messages extends ReloadableResourceBundleMessageSource { /** * Gets all messages for presented Locale. + * * @return all messages */ public Properties getMessages() { @@ -64,4 +65,6 @@ public class Messages extends ReloadableResourceBundleMessageSource { return localeResolver.resolveLocale(((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest()); } + + } diff --git a/webgoat-container/src/main/java/org/owasp/webgoat/i18n/PluginMessages.java b/webgoat-container/src/main/java/org/owasp/webgoat/i18n/PluginMessages.java new file mode 100644 index 000000000..6db37dd0e --- /dev/null +++ b/webgoat-container/src/main/java/org/owasp/webgoat/i18n/PluginMessages.java @@ -0,0 +1,80 @@ +/* + * This file is part of WebGoat, an Open Web Application Security Project utility. For details, + * please see http://www.owasp.org/ + *
+ * Copyright (c) 2002 - 2017 Bruce Mayhew + *
+ * This program is free software; you can redistribute it and/or modify it under the terms of the + * GNU General Public License as published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + *
+ * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without + * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + *
+ * You should have received a copy of the GNU General Public License along with this program; if + * not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA + * 02111-1307, USA. + *
+ * Getting Source ============== + *
+ * Source for this application is maintained at https://github.com/WebGoat/WebGoat, a repository for free software + * projects. + *
+ */
+
+package org.owasp.webgoat.i18n;
+
+import lombok.SneakyThrows;
+import org.springframework.context.support.ReloadableResourceBundleMessageSource;
+import org.springframework.core.io.Resource;
+import org.springframework.core.io.ResourceLoader;
+import org.springframework.core.io.UrlResource;
+
+import java.io.File;
+import java.util.Properties;
+
+/**
+ * Message resource bundle for plugins. The files is created after startup during the init of the plugins so we
+ * need to load this file through a ResourceLoader instead of location on the classpath.
+ *
+ * @author nbaars
+ * @date 2/4/17
+ */
+public class PluginMessages extends ReloadableResourceBundleMessageSource {
+
+ private Messages messages;
+
+ public PluginMessages(Messages messages) {
+ this.messages = messages;
+ this.setParentMessageSource(messages);
+ }
+
+ public Properties getMessages() {
+ return getMergedProperties(messages.resolveLocale()).getProperties();
+ }
+
+ public String getMessage(String code, Object... args) {
+ return getMessage(code, args, messages.resolveLocale());
+ }
+
+ public String getMessage(String code, String defaultValue, Object... args) {
+ return super.getMessage(code, args, defaultValue, messages.resolveLocale());
+ }
+
+ public void addPluginMessageBundles(final File i18nPluginDirectory) {
+ this.setBasename("WebGoatLabels");
+ this.setResourceLoader(new ResourceLoader() {
+ @Override
+ @SneakyThrows
+ public Resource getResource(String location) {
+ return new UrlResource(new File(i18nPluginDirectory, location).toURI());
+ }
+
+ @Override
+ public ClassLoader getClassLoader() {
+ return Thread.currentThread().getContextClassLoader();
+ }
+ });
+ }
+}
diff --git a/webgoat-container/src/main/java/org/owasp/webgoat/plugins/MessagePropertiesMerger.java b/webgoat-container/src/main/java/org/owasp/webgoat/plugins/MessagePropertyMerger.java
similarity index 53%
rename from webgoat-container/src/main/java/org/owasp/webgoat/plugins/MessagePropertiesMerger.java
rename to webgoat-container/src/main/java/org/owasp/webgoat/plugins/MessagePropertyMerger.java
index 7983614a8..04760d0bf 100644
--- a/webgoat-container/src/main/java/org/owasp/webgoat/plugins/MessagePropertiesMerger.java
+++ b/webgoat-container/src/main/java/org/owasp/webgoat/plugins/MessagePropertyMerger.java
@@ -24,49 +24,49 @@
*/
package org.owasp.webgoat.plugins;
-import com.google.common.primitives.Bytes;
import lombok.SneakyThrows;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
-import java.nio.file.Files;
-import java.nio.file.Path;
+import java.io.InputStream;
import java.util.Properties;
-import java.util.stream.Stream;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipFile;
+
+import static com.google.common.io.Files.createParentDirs;
/**
* Merges the main message.properties with the plugins WebGoatLabels
*/
-public class MessagePropertiesMerger {
+public class MessagePropertyMerger {
private final File targetDirectory;
- public MessagePropertiesMerger(File targetDirectory) {
+ public MessagePropertyMerger(File targetDirectory) {
this.targetDirectory = targetDirectory;
}
@SneakyThrows
- public void mergeAllLanguage() {
- try(Stream Getter for the field classes
.