Skip to content

Commit

Permalink
feat: show directly requested file in requests overview
Browse files Browse the repository at this point in the history
When a call directly hits a file it is now show up in the requests overview. This helps the user whether an attack from WebGoat actually requested the uploaded file.

Closes: WebGoatgh-1551
  • Loading branch information
nbaars committed Dec 4, 2023
1 parent 3d65152 commit ae261f2
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@

package org.owasp.webgoat.webwolf.requests;

import static java.util.stream.Collectors.toList;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.time.Instant;
Expand Down Expand Up @@ -67,10 +65,10 @@ public ModelAndView get(Authentication authentication) {
var model = new ModelAndView("requests");
String username = (null != authentication) ? authentication.getName() : "anonymous";
var traces =
traceRepository.findAllTraces().stream()
traceRepository.findAll().stream()
.filter(t -> allowedTrace(t, username))
.map(t -> new Tracert(t.getTimestamp(), path(t), toJsonString(t)))
.collect(toList());
.toList();
model.addObject("traces", traces);

return model;
Expand All @@ -93,7 +91,7 @@ private boolean allowedTrace(HttpExchange t, String username) {
}

private String path(HttpExchange t) {
return (String) t.getRequest().getUri().getPath();
return t.getRequest().getUri().getPath();
}

private String toJsonString(HttpExchange t) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@

package org.owasp.webgoat.webwolf.requests;

import static org.owasp.webgoat.webwolf.requests.WebWolfTraceRepository.Exclusion.contains;
import static org.owasp.webgoat.webwolf.requests.WebWolfTraceRepository.Exclusion.endsWith;

import com.google.common.collect.EvictingQueue;
import java.util.ArrayList;
import java.util.List;
Expand All @@ -36,31 +39,50 @@
* @since 8/13/17.
*/
public class WebWolfTraceRepository implements HttpExchangeRepository {
private enum MatchingMode {
CONTAINS,
ENDS_WITH,
EQUALS;
}

record Exclusion(String path, MatchingMode mode) {
public boolean matches(String path) {
return switch (mode) {
case CONTAINS -> path.contains(this.path);
case ENDS_WITH -> path.endsWith(this.path);
case EQUALS -> path.equals(this.path);
};
}

public static Exclusion contains(String exclusionPattern) {
return new Exclusion(exclusionPattern, MatchingMode.CONTAINS);
}

public static Exclusion endsWith(String exclusionPattern) {
return new Exclusion(exclusionPattern, MatchingMode.ENDS_WITH);
}
}

private final EvictingQueue<HttpExchange> traces = EvictingQueue.create(10000);
private final List<String> exclusionList =
private final List<Exclusion> exclusionList =
List.of(
"/tmpdir",
"/home",
"/files",
"/images/",
"/js/",
"/webjars/",
"/requests",
"/css/",
"/mail");
contains("/tmpdir"),
contains("/home"),
endsWith("/files"),
contains("/images/"),
contains("/js/"),
contains("/webjars/"),
contains("/requests"),
contains("/css/"),
contains("/mail"));

@Override
public List<HttpExchange> findAll() {
return List.of();
}

public List<HttpExchange> findAllTraces() {
return new ArrayList<>(traces);
}

private boolean isInExclusionList(String path) {
return exclusionList.stream().anyMatch(e -> path.contains(e));
return exclusionList.stream().anyMatch(e -> e.matches(path));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package org.owasp.webgoat.webwolf.requests;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.net.URI;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.boot.actuate.web.exchanges.HttpExchange;

class WebWolfTraceRepositoryTest {

@Test
@DisplayName("When a user hits a file upload it should be recorded")
void shouldAddFilesRequest() {
HttpExchange httpExchange = mock();
HttpExchange.Request request = mock();
when(httpExchange.getRequest()).thenReturn(request);
when(request.getUri()).thenReturn(URI.create("http://localhost:9090/files/test1234/test.jpg"));
WebWolfTraceRepository repository = new WebWolfTraceRepository();

repository.add(httpExchange);

Assertions.assertThat(repository.findAll()).hasSize(1);
}

@Test
@DisplayName("When a user hits file upload page ('/files') it should be recorded")
void shouldAddNotAddFilesRequestOverview() {
HttpExchange httpExchange = mock();
HttpExchange.Request request = mock();
when(httpExchange.getRequest()).thenReturn(request);
when(request.getUri()).thenReturn(URI.create("http://localhost:9090/files"));
WebWolfTraceRepository repository = new WebWolfTraceRepository();

repository.add(httpExchange);

Assertions.assertThat(repository.findAll()).hasSize(0);
}
}

0 comments on commit ae261f2

Please sign in to comment.