* Some initial refactoring * Make it one application * Got it working * Fix problem on Windows * Move WebWolf * Move first lesson * Moved all lessons * Fix pom.xml * Fix tests * Add option to initialize a lesson This way we can create content for each user inside a lesson. The initialize method will be called when a new user is created or when a lesson reset happens * Clean up pom.xml files * Remove fetching labels based on language. We only support English at the moment, all the lesson explanations are written in English which makes it very difficult to translate. If we only had labels it would make sense to support multiple languages * Fix SonarLint issues * And move it all to the main project * Fix for documentation paths * Fix pom warnings * Remove PMD as it does not work * Update release notes about refactoring Update release notes about refactoring Update release notes about refactoring * Fix lesson template * Update release notes * Keep it in the same repo in Dockerhub * Update documentation to show how the connection is obtained. Resolves: #1180 * Rename all integration tests * Remove command from Dockerfile * Simplify GitHub actions Currently, we use a separate actions for pull-requests and branch build. This is now consolidated in one action. The PR action triggers always, it now only trigger when the PR is opened and not in draft. Running all platforms on a branch build is a bit too much, it is better to only run all platforms when someone opens a PR. * Remove duplicate entry from release notes * Add explicit registry for base image * Lesson scanner not working when fat jar When running the fat jar we have to take into account we are reading from the jar file and not the filesystem. In this case you cannot use `getFile` for example. * added info in README and fixed release docker * changed base image and added ignore file Co-authored-by: Zubcevic.com <rene@zubcevic.com>
138 lines
5.7 KiB
Java
138 lines
5.7 KiB
Java
package org.owasp.webgoat;
|
|
|
|
import io.restassured.RestAssured;
|
|
import lombok.SneakyThrows;
|
|
import org.hamcrest.CoreMatchers;
|
|
import org.hamcrest.MatcherAssert;
|
|
import org.junit.jupiter.api.AfterEach;
|
|
import org.junit.jupiter.api.BeforeEach;
|
|
import org.junit.jupiter.api.DynamicTest;
|
|
import org.junit.jupiter.api.TestFactory;
|
|
import org.junit.jupiter.api.io.TempDir;
|
|
import org.springframework.security.core.token.Sha512DigestUtils;
|
|
|
|
import java.io.File;
|
|
import java.io.FileOutputStream;
|
|
import java.io.IOException;
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.nio.file.Files;
|
|
import java.nio.file.Path;
|
|
import java.util.Arrays;
|
|
import java.util.Map;
|
|
import java.util.zip.ZipEntry;
|
|
import java.util.zip.ZipOutputStream;
|
|
|
|
import static org.junit.jupiter.api.DynamicTest.dynamicTest;
|
|
|
|
class PathTraversalIT extends IntegrationTest {
|
|
|
|
@TempDir
|
|
Path tempDir;
|
|
|
|
private File fileToUpload = null;
|
|
|
|
@BeforeEach
|
|
@SneakyThrows
|
|
public void init() {
|
|
fileToUpload = Files.createFile(tempDir.resolve("test.jpg")).toFile();
|
|
Files.write(fileToUpload.toPath(), "This is a test".getBytes());
|
|
startLesson("PathTraversal");
|
|
}
|
|
|
|
@TestFactory
|
|
Iterable<DynamicTest> testPathTraversal() {
|
|
return Arrays.asList(
|
|
dynamicTest("assignment 1 - profile upload", () -> assignment1()),
|
|
dynamicTest("assignment 2 - profile upload fix", () -> assignment2()),
|
|
dynamicTest("assignment 3 - profile upload remove user input", () -> assignment3()),
|
|
dynamicTest("assignment 4 - profile upload random pic", () -> assignment4()),
|
|
dynamicTest("assignment 5 - zip slip", () -> assignment5())
|
|
);
|
|
}
|
|
|
|
private void assignment1() throws IOException {
|
|
MatcherAssert.assertThat(
|
|
RestAssured.given()
|
|
.when()
|
|
.relaxedHTTPSValidation()
|
|
.cookie("JSESSIONID", getWebGoatCookie())
|
|
.multiPart("uploadedFile", "test.jpg", Files.readAllBytes(fileToUpload.toPath()))
|
|
.param("fullName", "../John Doe")
|
|
.post(url("/WebGoat/PathTraversal/profile-upload"))
|
|
.then()
|
|
.statusCode(200)
|
|
.extract().path("lessonCompleted"), CoreMatchers.is(true));
|
|
}
|
|
|
|
private void assignment2() throws IOException {
|
|
MatcherAssert.assertThat(
|
|
RestAssured.given()
|
|
.when()
|
|
.relaxedHTTPSValidation()
|
|
.cookie("JSESSIONID", getWebGoatCookie())
|
|
.multiPart("uploadedFileFix", "test.jpg", Files.readAllBytes(fileToUpload.toPath()))
|
|
.param("fullNameFix", "..././John Doe")
|
|
.post(url("/WebGoat/PathTraversal/profile-upload-fix"))
|
|
.then()
|
|
.statusCode(200)
|
|
.extract().path("lessonCompleted"), CoreMatchers.is(true));
|
|
}
|
|
|
|
private void assignment3() throws IOException {
|
|
MatcherAssert.assertThat(
|
|
RestAssured.given()
|
|
.when()
|
|
.relaxedHTTPSValidation()
|
|
.cookie("JSESSIONID", getWebGoatCookie())
|
|
.multiPart("uploadedFileRemoveUserInput", "../test.jpg", Files.readAllBytes(fileToUpload.toPath()))
|
|
.post(url("/WebGoat/PathTraversal/profile-upload-remove-user-input"))
|
|
.then()
|
|
.statusCode(200)
|
|
.extract().path("lessonCompleted"), CoreMatchers.is(true));
|
|
}
|
|
|
|
private void assignment4() throws IOException {
|
|
var uri = "/WebGoat/PathTraversal/random-picture?id=%2E%2E%2F%2E%2E%2Fpath-traversal-secret";
|
|
RestAssured.given().urlEncodingEnabled(false)
|
|
.when()
|
|
.relaxedHTTPSValidation()
|
|
.cookie("JSESSIONID", getWebGoatCookie())
|
|
.get(url(uri))
|
|
.then()
|
|
.statusCode(200)
|
|
.body(CoreMatchers.is("You found it submit the SHA-512 hash of your username as answer"));
|
|
|
|
checkAssignment(url("/WebGoat/PathTraversal/random"), Map.of("secret",
|
|
Sha512DigestUtils.shaHex(this.getUser())), true);
|
|
}
|
|
|
|
private void assignment5() throws IOException {
|
|
var webGoatHome = webGoatServerDirectory() + "PathTraversal/" + this.getUser();
|
|
webGoatHome = webGoatHome.replaceAll("^[a-zA-Z]:", ""); //Remove C: from the home directory on Windows
|
|
|
|
var webGoatDirectory = new File(webGoatHome);
|
|
var zipFile = new File(tempDir.toFile(), "upload.zip");
|
|
try (var zos = new ZipOutputStream(new FileOutputStream(zipFile))) {
|
|
ZipEntry e = new ZipEntry("../../../../../../../../../../" + webGoatDirectory + "/image.jpg");
|
|
zos.putNextEntry(e);
|
|
zos.write("test".getBytes(StandardCharsets.UTF_8));
|
|
}
|
|
MatcherAssert.assertThat(
|
|
RestAssured.given()
|
|
.when()
|
|
.relaxedHTTPSValidation()
|
|
.cookie("JSESSIONID", getWebGoatCookie())
|
|
.multiPart("uploadedFileZipSlip", "upload.zip", Files.readAllBytes(zipFile.toPath()))
|
|
.post(url("/WebGoat/PathTraversal/zip-slip"))
|
|
.then()
|
|
.statusCode(200)
|
|
.extract().path("lessonCompleted"), CoreMatchers.is(true));
|
|
}
|
|
|
|
@AfterEach
|
|
void shutdown() {
|
|
//this will run only once after the list of dynamic tests has run, this is to test if the lesson is marked complete
|
|
checkResults("/PathTraversal");
|
|
}
|
|
}
|