Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update PathTraversalVulnerability.java #471

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,31 +41,35 @@ public class PathTraversalVulnerability {
LogManager.getLogger(PathTraversalVulnerability.class);

private static final String URL_PARAM_KEY = "fileName";

private ResponseEntity<GenericVulnerabilityResponseBean<String>> readFile(
Supplier<Boolean> condition, String fileName) {
if (condition.get()) {
InputStream infoFileStream =
this.getClass().getResourceAsStream("/scripts/PathTraversal/" + fileName);
if (infoFileStream != null) {
try (BufferedReader reader =
new BufferedReader(new InputStreamReader(infoFileStream))) {
String information = reader.readLine();
StringBuilder payload = new StringBuilder();
while (information != null) {
payload.append(information);
information = reader.readLine();
}
return new ResponseEntity<GenericVulnerabilityResponseBean<String>>(
new GenericVulnerabilityResponseBean<>(payload.toString(), true),
HttpStatus.OK);
} catch (IOException e) {
LOGGER.error("Following error occurred: ", e);
}
if (!condition.get()) {
return ResponseEntity.badRequest().body(new GenericVulnerabilityResponseBean<>("Invalid request condition", false));
}

// Preventing Path Traversal and ensuring only allowed filenames are processed.
if (fileName.contains("..") || !ALLOWED_FILE_NAMES.contains(fileName)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this change? we wanted to introduce vulnerability in the application and this will disallow ".." in file name.

LOGGER.error("Attempted access to restricted file: {}", fileName);
return ResponseEntity.status(HttpStatus.FORBIDDEN).body(new GenericVulnerabilityResponseBean<>("Access denied", false));
}

InputStream infoFileStream = this.getClass().getResourceAsStream("/scripts/PathTraversal/" + fileName);
if (infoFileStream == null) {
LOGGER.error("File not found: {}", fileName);
return ResponseEntity.notFound().build();
}

try (BufferedReader reader = new BufferedReader(new InputStreamReader(infoFileStream))) {
StringBuilder payload = new StringBuilder();
String information;
while ((information = reader.readLine()) != null) {
payload.append(information);
}
return ResponseEntity.ok(new GenericVulnerabilityResponseBean<>(payload.toString(), true));
} catch (IOException e) {
LOGGER.error("Error reading file: {}", fileName, e);
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(new GenericVulnerabilityResponseBean<>("Error reading file", false));
}
return new ResponseEntity<GenericVulnerabilityResponseBean<String>>(
new GenericVulnerabilityResponseBean<>(), HttpStatus.OK);
}

@AttackVector(
Expand Down
Loading