forked from WebGoat/WebGoat
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added macro for asciidoc to produce the WebWolf link dynamically depe…
…nding on configuration
- Loading branch information
Showing
4 changed files
with
68 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
webgoat-container/src/main/java/org/owasp/webgoat/asciidoc/EnvironmentExposure.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package org.owasp.webgoat.asciidoc; | ||
|
||
import org.springframework.beans.BeansException; | ||
import org.springframework.context.ApplicationContext; | ||
import org.springframework.context.ApplicationContextAware; | ||
import org.springframework.core.env.Environment; | ||
import org.springframework.stereotype.Component; | ||
|
||
/** | ||
* Make environment available in the asciidoc code (which you cannot inject because it is handled by the framework) | ||
*/ | ||
@Component | ||
public class EnvironmentExposure implements ApplicationContextAware { | ||
|
||
private static ApplicationContext context; | ||
|
||
public static Environment getEnv() { | ||
return context.getEnvironment(); | ||
} | ||
|
||
@Override | ||
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { | ||
context = applicationContext; | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
webgoat-container/src/main/java/org/owasp/webgoat/asciidoc/WebWolfMacro.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package org.owasp.webgoat.asciidoc; | ||
|
||
import org.asciidoctor.ast.AbstractBlock; | ||
import org.asciidoctor.extension.InlineMacroProcessor; | ||
import org.springframework.core.env.Environment; | ||
import org.springframework.util.StringUtils; | ||
import org.springframework.web.context.request.RequestContextHolder; | ||
import org.springframework.web.context.request.ServletRequestAttributes; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import java.util.Map; | ||
|
||
public class WebWolfMacro extends InlineMacroProcessor { | ||
|
||
public WebWolfMacro(String macroName, Map<String, Object> config) { | ||
super(macroName, config); | ||
} | ||
|
||
@Override | ||
protected String process(AbstractBlock parent, String target, Map<String, Object> attributes) { | ||
Environment env = EnvironmentExposure.getEnv(); | ||
String hostname = determineHost(env.getProperty("webwolf.host"), env.getProperty("webwolf.port")); | ||
return "<a href=\"" + hostname + "\" target=\"_blank\">" + target + "</a>"; | ||
} | ||
|
||
/** | ||
* Look at the remote address from received from the browser first. This way it will also work if you run | ||
* the browser in a Docker container and WebGoat on your local machine. | ||
*/ | ||
private String determineHost(String host, String port) { | ||
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest(); | ||
String ip = request.getRemoteAddr(); | ||
String hostname = StringUtils.hasText(ip) ? ip : host; | ||
return "http://" + hostname + ":" + port + "/WebWolf"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters