-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from DataDog/file-read-vuln
Add arbitrary file read vuln
- Loading branch information
Showing
17 changed files
with
215 additions
and
26 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
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
8 changes: 0 additions & 8 deletions
8
src/main/java/com/datadoghq/workshops/samplevulnerablejavaapp/DomainTestRequest.java
This file was deleted.
Oops, something went wrong.
10 changes: 0 additions & 10 deletions
10
src/main/java/com/datadoghq/workshops/samplevulnerablejavaapp/WebsiteTestRequest.java
This file was deleted.
Oops, something went wrong.
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
2 changes: 0 additions & 2 deletions
2
...n/java/com/datadoghq/workshops/samplevulnerablejavaapp/exception/DomainTestException.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
8 changes: 8 additions & 0 deletions
8
...com/datadoghq/workshops/samplevulnerablejavaapp/exception/FileForbiddenFileException.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,8 @@ | ||
package com.datadoghq.workshops.samplevulnerablejavaapp.exception; | ||
|
||
public class FileForbiddenFileException extends Exception { | ||
public FileForbiddenFileException(String message) { | ||
super(message); | ||
} | ||
} | ||
|
8 changes: 8 additions & 0 deletions
8
...ain/java/com/datadoghq/workshops/samplevulnerablejavaapp/exception/FileReadException.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,8 @@ | ||
package com.datadoghq.workshops.samplevulnerablejavaapp.exception; | ||
|
||
public class FileReadException extends Exception { | ||
public FileReadException(String message) { | ||
super(message); | ||
} | ||
} | ||
|
2 changes: 0 additions & 2 deletions
2
...ava/com/datadoghq/workshops/samplevulnerablejavaapp/exception/InvalidDomainException.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
8 changes: 8 additions & 0 deletions
8
src/main/java/com/datadoghq/workshops/samplevulnerablejavaapp/http/DomainTestRequest.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,8 @@ | ||
package com.datadoghq.workshops.samplevulnerablejavaapp.http; | ||
|
||
import lombok.Data; | ||
|
||
@Data | ||
public class DomainTestRequest { | ||
public String domainName; | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/com/datadoghq/workshops/samplevulnerablejavaapp/http/ViewFileRequest.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,8 @@ | ||
package com.datadoghq.workshops.samplevulnerablejavaapp.http; | ||
|
||
import lombok.Data; | ||
|
||
@Data | ||
public class ViewFileRequest { | ||
public String path; | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/com/datadoghq/workshops/samplevulnerablejavaapp/http/WebsiteTestRequest.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,10 @@ | ||
package com.datadoghq.workshops.samplevulnerablejavaapp.http; | ||
|
||
import lombok.Data; | ||
|
||
@Data | ||
public class WebsiteTestRequest { | ||
public String url; | ||
public String customHeaderKey; | ||
public String customHeaderValue; | ||
} |
3 changes: 1 addition & 2 deletions
3
...evulnerablejavaapp/DomainTestService.java → ...blejavaapp/service/DomainTestService.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
31 changes: 31 additions & 0 deletions
31
src/main/java/com/datadoghq/workshops/samplevulnerablejavaapp/service/FileService.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,31 @@ | ||
package com.datadoghq.workshops.samplevulnerablejavaapp.service; | ||
|
||
import com.datadoghq.workshops.samplevulnerablejavaapp.exception.FileForbiddenFileException; | ||
import com.datadoghq.workshops.samplevulnerablejavaapp.exception.FileReadException; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.io.*; | ||
|
||
@Service | ||
public class FileService { | ||
final static String ALLOWED_PREFIX = "/tmp/files/"; | ||
|
||
public String readFile(String path) throws FileForbiddenFileException, FileReadException { | ||
if(!path.startsWith(ALLOWED_PREFIX)) { | ||
throw new FileForbiddenFileException("You are not allowed to read " + path); | ||
} | ||
try (BufferedReader br = new BufferedReader(new FileReader(path))) { | ||
StringBuilder sb = new StringBuilder(); | ||
String line = br.readLine(); | ||
|
||
while (line != null) { | ||
sb.append(line); | ||
sb.append(System.lineSeparator()); | ||
line = br.readLine(); | ||
} | ||
return sb.toString(); | ||
} catch (IOException e) { | ||
throw new FileReadException(e.getMessage()); | ||
} | ||
} | ||
} |
3 changes: 2 additions & 1 deletion
3
...vulnerablejavaapp/WebsiteTestService.java → ...lejavaapp/service/WebsiteTestService.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
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,69 @@ | ||
<html> | ||
<head> | ||
<!-- Bootstrap --> | ||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.4.1/dist/css/bootstrap.min.css" | ||
integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous"> | ||
|
||
<meta name="viewport" content="width=device-width, initial-scale=1"> | ||
<title>File viewer</title> | ||
</head> | ||
<body> | ||
<div class="container"> | ||
|
||
<div class="page-header"> | ||
<h1>File viewer</h1> | ||
<p class="lead"></p> | ||
</div> | ||
|
||
<div class="row"> | ||
<div class="col"> | ||
<p>View files on this server. <b>For security reasons, you can only access samples files under <code>/tmp/files</code></b>.</p> | ||
|
||
<p>Available files: | ||
<ul> | ||
<li><code>/tmp/files/hello.txt</code></li> | ||
<li><code>/tmp/files/foo.txt</code></li> | ||
</ul> | ||
</p> | ||
</div> | ||
</div> | ||
|
||
|
||
<div class="row"> | ||
<form> | ||
<div class="form-group row"> | ||
<label for="path" class="col-sm-2 col-form-label">File path:</label> | ||
<div class="col-sm-10"> | ||
<input type="text" class="form-control" id="path" placeholder="/tmp/files/something.txt"> | ||
</div> | ||
</div> | ||
|
||
<button type="submit" class="btn btn-primary mb-2">View file</button> | ||
</form> | ||
</div> | ||
|
||
<div class="row"> | ||
<div id="output-container" class="alert alert-secondary hidden" role="alert"> | ||
<pre id="output" style="white-space: pre-wrap"></pre> | ||
</div> | ||
</div> | ||
|
||
<div class="row"> | ||
<div id="error-container" class="alert alert-danger hidden" role="alert"> | ||
<div id="error"></div> | ||
</div> | ||
</div> | ||
</div> | ||
|
||
</body> | ||
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) --> | ||
<script src="https://code.jquery.com/jquery-1.12.4.min.js" | ||
integrity="sha384-nvAa0+6Qg9clwYCGGPpDQLVpLNn0fRaROjHqs13t4Ggj3Ez50XnGQqc/r8MhnRDZ" | ||
crossorigin="anonymous"></script> | ||
<!-- Include all compiled plugins (below), or include individual files as needed --> | ||
<script src="https://cdn.jsdelivr.net/npm/bootstrap@3.4.1/dist/js/bootstrap.min.js" | ||
integrity="sha384-aJ21OjlMXNL5UyIl/XNwTMqvzeRMZH2w8c5cRVpzpU8Y5bApTppSuUkhZXN0VxHd" | ||
crossorigin="anonymous"></script> | ||
|
||
<script src="/js/file.js"></script> | ||
</html> |
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,37 @@ | ||
var outputContainer = document.getElementById('output-container') | ||
var outputElement = document.getElementById('output') | ||
var errorContainer = document.getElementById('error-container') | ||
var errorElement = document.getElementById('error') | ||
|
||
function updateOutput(result) { | ||
// If there is any error, hide it first | ||
errorContainer.classList.add('hidden'); | ||
outputElement.innerText = result; | ||
outputContainer.classList.remove('hidden'); | ||
} | ||
|
||
function handleError(error) { | ||
// If there is any successful output, hide it first | ||
outputContainer.classList.add('hidden'); | ||
errorElement.innerText = error.responseText; | ||
errorContainer.classList.remove('hidden'); | ||
} | ||
|
||
|
||
function submitRequest() { | ||
$.ajax({ | ||
url: '/view-file', | ||
method: 'POST', | ||
contentType: 'application/json', | ||
accept: 'application/json', | ||
data: JSON.stringify({ | ||
'path': document.getElementById('path').value || '' | ||
}), | ||
success: updateOutput, | ||
error: handleError | ||
}) | ||
} | ||
|
||
|
||
var form = document.querySelectorAll('form')[0] | ||
form.addEventListener('submit', function(evt) { evt.preventDefault(); submitRequest(); }) |