diff --git a/webgoat-container/pom.xml b/webgoat-container/pom.xml index 9446836d5..6546faf64 100644 --- a/webgoat-container/pom.xml +++ b/webgoat-container/pom.xml @@ -1,5 +1,6 @@ - + webgoat-container 4.0.0 @@ -112,6 +113,41 @@ + + org.apache.maven.plugins + maven-jar-plugin + ${maven-jar-plugin.version} + + + create-jar + compile + + jar + + + + + + org.codehaus.mojo + build-helper-maven-plugin + ${build-helper-maven-plugin.version} + + + attach-artifacts + package + + attach-artifact + + + + + ${project.build.directory}/webgoat-container-${project.version}.jar + + + + + + org.apache.maven.plugins maven-resources-plugin @@ -133,20 +169,6 @@ ISO-8859-1 - - org.apache.maven.plugins - maven-jar-plugin - ${maven-jar-plugin.version} - - - create-jar - compile - - jar - - - - org.apache.maven.plugins maven-surefire-plugin @@ -160,6 +182,21 @@ org.springframework.boot spring-boot-maven-plugin + + + + repackage + + + + + org.thymeleaf.extra + thymeleaf-extras-springsecurity4 + + + + + @@ -184,10 +221,14 @@ true - - - + + + + + org.springframework.boot + spring-boot-loader + javax.servlet jstl diff --git a/webgoat-container/src/main/java/org/owasp/webgoat/HammerHead.java b/webgoat-container/src/main/java/org/owasp/webgoat/HammerHead.java index 144899c78..e8f2cb828 100644 --- a/webgoat-container/src/main/java/org/owasp/webgoat/HammerHead.java +++ b/webgoat-container/src/main/java/org/owasp/webgoat/HammerHead.java @@ -8,10 +8,8 @@ import org.owasp.webgoat.session.ErrorScreen; import org.owasp.webgoat.session.Screen; import org.owasp.webgoat.session.UserTracker; import org.owasp.webgoat.session.WebSession; -import org.owasp.webgoat.session.WebgoatContext; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.core.io.ClassPathResource; import javax.servlet.ServletContext; import javax.servlet.ServletException; @@ -62,42 +60,18 @@ import java.util.TimeZone; */ public class HammerHead extends HttpServlet { - final Logger logger = LoggerFactory.getLogger(HammerHead.class); - - - /** - * - */ private static final long serialVersionUID = 645640331343188020L; + private static SimpleDateFormat httpDateFormat; + private final Logger logger = LoggerFactory.getLogger(HammerHead.class); + private WebSession webSession; - /** - * Description of the Field - */ - protected static SimpleDateFormat httpDateFormat; - - /** - * Set the session timeout to be 2 days - */ - private final static int sessionTimeoutSeconds = 60 * 60 * 24 * 2; - - // private final static int sessionTimeoutSeconds = 1; - /** - * Properties file path - */ - public static String propertiesPath = null; - - /** - * provides convenience methods for getting setup information from the - * ServletContext - */ - private WebgoatContext webgoatContext = null; - - public HammerHead(WebgoatContext context) { - this.webgoatContext = context; + public HammerHead() { + //for catcher subclass } - //TODO_NB - public HammerHead() {} + public HammerHead(WebSession webSession) { + this.webSession = webSession; + } /** * {@inheritDoc} @@ -250,7 +224,6 @@ public class HammerHead extends HttpServlet { logger.info("Initializing main webgoat servlet"); httpDateFormat = new SimpleDateFormat("EEE, dd MMM yyyyy HH:mm:ss z", Locale.US); httpDateFormat.setTimeZone(TimeZone.getTimeZone("GMT")); - propertiesPath = new ClassPathResource("/WEB-INF/webgoat.properties").getPath(); } /** @@ -348,25 +321,6 @@ public class HammerHead extends HttpServlet { return (screen); } - /** - * This method sets the required expiration headers in the response for a - * given RunData object. This method attempts to set all relevant headers, - * both for HTTP 1.0 and HTTP 1.1. - * - * @param response The new cacheHeaders value - * @param expiry The new cacheHeaders value - */ - protected static void setCacheHeaders(HttpServletResponse response, int expiry) { - if (expiry == 0) { - response.setHeader("Pragma", "no-cache"); - response.setHeader("Cache-Control", "no-cache"); - response.setHeader("Expires", formatHttpDate(new Date())); - } else { - Date expiryDate = new Date(System.currentTimeMillis() + expiry); - response.setHeader("Expires", formatHttpDate(expiryDate)); - } - } - /** * Description of the Method * @@ -382,10 +336,11 @@ public class HammerHead extends HttpServlet { // session should already be created by spring security hs = request.getSession(false); + //TODO rewrite this logic logger.debug("HH Entering Session_id: " + hs.getId()); // dumpSession( hs ); // Get our session object out of the HTTP session - WebSession session = null; + WebSession session = this.webSession; Object o = hs.getAttribute(WebSession.SESSION); if ((o != null) && o instanceof WebSession) { @@ -394,13 +349,11 @@ public class HammerHead extends HttpServlet { } else { // Create new custom session and save it in the HTTP session logger.warn("HH Creating new WebSession"); - session = new WebSession(webgoatContext, context); // Ensure splash screen shows on any restart // rlawson - removed this since we show splash screen at login now //hs.removeAttribute(WELCOMED); + //@TODO NO NEED TO PUT IN THE HTTP SESSION, FOCUS WILL FIX LATER hs.setAttribute(WebSession.SESSION, session); - // reset timeout - hs.setMaxInactiveInterval(sessionTimeoutSeconds); } session.update(request, response, this.getServletName()); 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 1075e6c3d..208e900ba 100644 --- a/webgoat-container/src/main/java/org/owasp/webgoat/MvcConfiguration.java +++ b/webgoat-container/src/main/java/org/owasp/webgoat/MvcConfiguration.java @@ -1,18 +1,17 @@ package org.owasp.webgoat; import org.owasp.webgoat.session.LabelDebugger; -import org.owasp.webgoat.session.WebgoatContext; +import org.owasp.webgoat.session.WebSession; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.context.embedded.ServletRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import org.springframework.core.io.ClassPathResource; -import org.springframework.core.io.Resource; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import java.io.File; -import java.io.IOException; /** * @@ -20,10 +19,15 @@ import java.io.IOException; @Configuration public class MvcConfiguration extends WebMvcConfigurerAdapter { + @Autowired + @Qualifier("pluginTargetDirectory") + private File pluginTargetDirectory; + @Override public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/login").setViewName("login"); registry.addViewController("/lesson_content").setViewName("lesson_content"); + registry.addViewController("/start.mvc").setViewName("main_new"); } @Bean @@ -33,23 +37,15 @@ public class MvcConfiguration extends WebMvcConfigurerAdapter { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { - Resource resource = new ClassPathResource("/plugin_lessons/plugin_lessons_marker.txt"); - try { - File pluginsDir = resource.getFile().getParentFile(); - registry.addResourceHandler("/plugin_lessons/**").addResourceLocations("file:///" + pluginsDir.toString() + "/"); - } catch (IOException e) { - e.printStackTrace(); - } - + registry.addResourceHandler("/plugin_lessons/**").addResourceLocations("file:///" + pluginTargetDirectory.toString() + "/"); } @Bean - public HammerHead hammerHead(WebgoatContext context) { - return new HammerHead(context); + public HammerHead hammerHead(WebSession webSession) { + return new HammerHead(webSession); } @Bean - //@Scope(value= WebApplicationContext.SCOPE_SESSION) public LabelDebugger labelDebugger() { return new LabelDebugger(); } 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 013f464a2..615cc4122 100644 --- a/webgoat-container/src/main/java/org/owasp/webgoat/WebGoat.java +++ b/webgoat-container/src/main/java/org/owasp/webgoat/WebGoat.java @@ -1,11 +1,25 @@ package org.owasp.webgoat; +import org.owasp.webgoat.plugins.PluginsLoader; +import org.owasp.webgoat.session.Course; +import org.owasp.webgoat.session.WebSession; +import org.owasp.webgoat.session.WebgoatContext; +import org.owasp.webgoat.session.WebgoatProperties; +import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.context.web.SpringBootServletInitializer; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.PropertySource; +import org.springframework.context.annotation.Scope; +import org.springframework.context.annotation.ScopedProxyMode; + +import javax.servlet.ServletContext; +import java.io.File; @SpringBootApplication +@PropertySource("classpath:/webgoat.properties") public class WebGoat extends SpringBootServletInitializer { @Override @@ -17,28 +31,31 @@ public class WebGoat extends SpringBootServletInitializer { SpringApplication.run(WebGoat.class, args); } -// @Bean -// @Autowired -// public TomcatEmbeddedServletContainerFactory servletContainer(final JarScanner jarScanner) { -// TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory(); -// factory.setPort(80); -// factory.setSessionTimeout(10, TimeUnit.MINUTES); -// factory.addContextCustomizers(new TomcatContextCustomizer() { -// @Override -// public void customize(Context context) { -// -// context.setJarScanner(jarScanner); -// } -// }); -// return factory; -// } -// -// @Bean -// public JarScanner getJarScanner() { -// StandardJarScanner jarScanner = new StandardJarScanner(); -// jarScanner.setScanClassPath(true); -// return jarScanner; -// } + @Bean(name = "pluginTargetDirectory") + public File pluginTargetDirectory() { + File tempDir = com.google.common.io.Files.createTempDir(); + tempDir.deleteOnExit(); + return tempDir; + } + @Bean + public PluginsLoader pluginsLoader(@Qualifier("pluginTargetDirectory") File pluginTargetDirectory) { + System.out.println("Plugin target directory: " + pluginTargetDirectory.toString()); + return new PluginsLoader(pluginTargetDirectory); + } + @Bean + @Scope(value = "session", proxyMode = ScopedProxyMode.TARGET_CLASS) + public WebSession webSession(Course course, WebgoatContext webgoatContext, ServletContext context) { + return new WebSession(course, webgoatContext, context); + } + + @Bean + public Course course(PluginsLoader pluginsLoader, WebgoatContext webgoatContext, ServletContext context, + WebgoatProperties webgoatProperties) { + Course course = new Course(webgoatProperties); + course.loadCourses(webgoatContext, context, "/"); + course.loadLessonFromPlugin(pluginsLoader.loadPlugins()); + return course; + } } diff --git a/webgoat-container/src/main/java/org/owasp/webgoat/application/Application.java b/webgoat-container/src/main/java/org/owasp/webgoat/application/Application.java deleted file mode 100644 index b9667b0c8..000000000 --- a/webgoat-container/src/main/java/org/owasp/webgoat/application/Application.java +++ /dev/null @@ -1,108 +0,0 @@ -/* - * To change this license header, choose License Headers in Project Properties. - * To change this template file, choose Tools | Templates - * and open the template in the editor. - */ -package org.owasp.webgoat.application; - -import org.apache.commons.lang3.StringUtils; -import org.apache.commons.lang3.builder.ToStringBuilder; - -/** - * Singleton which is created on context startup - * - * @author rlawson - * @version $Id: $Id - */ -//TODO_NB still necessary? -public class Application { - - private static final Application INSTANCE = new Application(); - - private Application() { - - } - - /** - *

getInstance.

- * - * @return a {@link org.owasp.webgoat.application.Application} object. - */ - public static final Application getInstance() { - return INSTANCE; - } - - private String version = "SNAPSHOT"; - private String build = "local"; - private String name = "WebGoat"; - - /** - *

Getter for the field version.

- * - * @return the version - */ - public String getVersion() { - return version; - } - - /** - *

Setter for the field version.

- * - * @param version the version to set - */ - public void setVersion(String version) { - if (StringUtils.isNotBlank(version)) { - this.version = version; - } - } - - /** - *

Getter for the field build.

- * - * @return the build - */ - public String getBuild() { - return build; - } - - /** - *

Setter for the field build.

- * - * @param build the build to set - */ - public void setBuild(String build) { - if (StringUtils.isNotBlank(build)) { - this.build = build; - } - } - - /** - *

Getter for the field name.

- * - * @return the name - */ - public String getName() { - return name; - } - - /** - *

Setter for the field name.

- * - * @param name the name to set - */ - public void setName(String name) { - if (StringUtils.isNotBlank(name)) { - this.name = name; - } - } - - /** {@inheritDoc} */ - @Override - public String toString() { - return new ToStringBuilder(this). - append("name", name). - append("version", version). - append("build", build). - toString(); - } -} diff --git a/webgoat-container/src/main/java/org/owasp/webgoat/application/WebGoatServletListener.java b/webgoat-container/src/main/java/org/owasp/webgoat/application/WebGoatServletListener.java index 76f41f176..2e202c812 100644 --- a/webgoat-container/src/main/java/org/owasp/webgoat/application/WebGoatServletListener.java +++ b/webgoat-container/src/main/java/org/owasp/webgoat/application/WebGoatServletListener.java @@ -6,7 +6,6 @@ package org.owasp.webgoat.application; import org.owasp.webgoat.lessons.LessonServletMapping; -import org.owasp.webgoat.plugins.PluginsLoader; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.config.BeanDefinition; @@ -17,16 +16,11 @@ import javax.servlet.ServletContext; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import javax.servlet.ServletRegistration; -import java.io.IOException; -import java.io.InputStream; -import java.nio.file.Paths; import java.sql.Driver; import java.sql.DriverManager; import java.sql.SQLException; import java.util.Enumeration; import java.util.Set; -import java.util.jar.Attributes; -import java.util.jar.Manifest; /** * Web application lifecycle listener. @@ -43,10 +37,7 @@ public class WebGoatServletListener implements ServletContextListener { public void contextInitialized(ServletContextEvent sce) { ServletContext context = sce.getServletContext(); context.log("WebGoat is starting"); - setApplicationVariables(context); context.log("Adding extra mappings for lessions"); - - loadPlugins(sce); loadServlets(sce); } @@ -68,12 +59,6 @@ public class WebGoatServletListener implements ServletContextListener { } } - private void loadPlugins(ServletContextEvent sce) { - String pluginPath = sce.getServletContext().getRealPath("plugin_lessons"); - String targetPath = sce.getServletContext().getRealPath("plugin_extracted"); - new PluginsLoader(Paths.get(pluginPath), Paths.get(targetPath)).loadPlugins(); - } - /** {@inheritDoc} */ @Override public void contextDestroyed(ServletContextEvent sce) { @@ -101,21 +86,4 @@ public class WebGoatServletListener implements ServletContextListener { } } } - - private void setApplicationVariables(ServletContext context) { - Application app = Application.getInstance(); - try { - InputStream inputStream = context.getResourceAsStream("/META-INF/MANIFEST.MF"); - Manifest manifest = new Manifest(inputStream); - Attributes attr = manifest.getMainAttributes(); - String name = attr.getValue("Specification-Title"); - String version = attr.getValue("Specification-Version"); - String build = attr.getValue("Implementation-Version"); - app.setName(name); - app.setVersion(version); - app.setBuild(build); - } catch (IOException ioe) { - context.log("Error setting application variables", ioe); - } - } } diff --git a/webgoat-container/src/main/java/org/owasp/webgoat/controller/Start.java b/webgoat-container/src/main/java/org/owasp/webgoat/controller/Start.java deleted file mode 100644 index d2a645d37..000000000 --- a/webgoat-container/src/main/java/org/owasp/webgoat/controller/Start.java +++ /dev/null @@ -1,109 +0,0 @@ -/* - * To change this license header, choose License Headers in Project Properties. - * To change this template file, choose Tools | Templates - * and open the template in the editor. - */ -package org.owasp.webgoat.controller; - -import org.apache.commons.lang3.StringUtils; -import org.owasp.webgoat.application.Application; -import org.owasp.webgoat.session.WebSession; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.security.core.GrantedAuthority; -import org.springframework.security.core.context.SecurityContextHolder; -import org.springframework.stereotype.Controller; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.servlet.ModelAndView; - -import javax.servlet.ServletContext; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpSession; -import java.util.Collection; - -/** - *

Start class.

- * - * @author rlawson - * @version $Id: $Id - */ -@Controller -public class Start { - - final Logger logger = LoggerFactory.getLogger(Start.class); - - private static final String WELCOMED = "welcomed"; - - @Autowired - private ServletContext servletContext; - - /** - *

start.

- * - * @param request a {@link javax.servlet.http.HttpServletRequest} object. - * @return a {@link org.springframework.web.servlet.ModelAndView} object. - */ - @RequestMapping(path = "start.mvc", method = {RequestMethod.GET, RequestMethod.POST}) - public ModelAndView start(HttpServletRequest request) { - - ModelAndView model = new ModelAndView(); - // make sure session is set up correctly - // if not redirect user to login - if (checkWebSession(request.getSession()) == false) { - model.setViewName("redirect:/login.mvc"); - return model; - } - String role = getRole(); - String user = request.getUserPrincipal().getName(); - model.addObject("role", role); - model.addObject("user", user); - - String contactEmail = servletContext.getInitParameter("email"); - model.addObject("contactEmail", contactEmail); - String emailList = servletContext.getInitParameter("emaillist"); - model.addObject("emailList", emailList); - - Application app = Application.getInstance(); - logger.info("Setting application properties: " + app); - model.addObject("version", app.getVersion()); - model.addObject("build", app.getBuild()); - - // if everything ok then go to webgoat UI - model.setViewName("main_new"); - return model; - } - - private String getRole() { - Collection authorities = (Collection) SecurityContextHolder.getContext().getAuthentication().getAuthorities(); - String role = "N/A"; - for (GrantedAuthority authority : authorities) { - authority.getAuthority(); - role = authority.getAuthority(); - role = StringUtils.lowerCase(role); - role = StringUtils.remove(role, "role_"); - break; - } - return role; - } - - /** - *

checkWebSession.

- * - * @param session a {@link javax.servlet.http.HttpSession} object. - * @return a boolean. - */ - public boolean checkWebSession(HttpSession session) { - Object o = session.getAttribute(WebSession.SESSION); - if (o == null) { - logger.error("No valid WebSession object found, has session timed out? [" + session.getId() + "]"); - return false; - } - if (!(o instanceof WebSession)) { - logger.error("Invalid WebSession object found, this is probably a bug! [" + o.getClass() + " | " + session.getId() + "]"); - return false; - } - return true; - } -} diff --git a/webgoat-container/src/main/java/org/owasp/webgoat/lessons/AbstractLesson.java b/webgoat-container/src/main/java/org/owasp/webgoat/lessons/AbstractLesson.java index 9910a2315..bd0cd65df 100644 --- a/webgoat-container/src/main/java/org/owasp/webgoat/lessons/AbstractLesson.java +++ b/webgoat-container/src/main/java/org/owasp/webgoat/lessons/AbstractLesson.java @@ -164,15 +164,11 @@ public abstract class AbstractLesson extends Screen implements Comparable