Skip to content

Fix path traversal vulnerability in BenchmarkTest00002 - #2

Draft
semgrep-code-fantasyfanfan[bot] wants to merge 1 commit into
masterfrom
semgrep-autofix/1785915048
Draft

Fix path traversal vulnerability in BenchmarkTest00002#2
semgrep-code-fantasyfanfan[bot] wants to merge 1 commit into
masterfrom
semgrep-autofix/1785915048

Conversation

@semgrep-code-fantasyfanfan

Copy link
Copy Markdown

Fix path traversal vulnerability in BenchmarkTest00002 by sanitizing user-controlled cookie input before constructing a file path.

Changes

  • Applied org.apache.commons.io.FilenameUtils.getName() to the param value derived from the cookie before concatenating it with the test files directory path.

Why

The param variable is sourced from a user-controlled cookie value. Without sanitization, an attacker could supply a value like ../../etc/passwd to traverse directories and access or overwrite arbitrary files on the server. FilenameUtils.getName() strips all directory components and traversal sequences, ensuring only a bare filename is used in the resulting path.

Semgrep Finding Details

The application builds a file path from potentially untrusted data, which can lead to a path traversal vulnerability. An attacker can manipulate the path which the application uses to access files. If the application does not validate user input and sanitize file paths, sensitive files such as configuration or user data can be accessed, potentially creating or overwriting files. To prevent this vulnerability, validate and sanitize any input that is used to create references to file paths. Also, enforce strict file access controls. For example, choose privileges allowing public-facing applications to access only the required files. In Java, you may also consider using a utility method such as org.apache.commons.io.FilenameUtils.getName(...) to only retrieve the file name from the path.

635648771@qq.com requested this Autofix PR for this finding from the detection rule java.servlets.security.httpservlet-path-traversal-deepsemgrep.httpservlet-path-traversal-deepsemgrep.


⚠️ Review carefully before merging. This PR was generated by AI and may cause breaking changes or introduce new vulnerabilities.

Fix path traversal vulnerability in BenchmarkTest00002 by sanitizing user-controlled cookie input before constructing a file path.

## Changes
- Applied `org.apache.commons.io.FilenameUtils.getName()` to the `param` value derived from the cookie before concatenating it with the test files directory path.

## Why
The `param` variable is sourced from a user-controlled cookie value. Without sanitization, an attacker could supply a value like `../../etc/passwd` to traverse directories and access or overwrite arbitrary files on the server. `FilenameUtils.getName()` strips all directory components and traversal sequences, ensuring only a bare filename is used in the resulting path.

## Semgrep Finding Details
The application builds a file path from potentially untrusted data, which can lead to a path traversal vulnerability. An attacker can manipulate the path which the application uses to access files. If the application does not validate user input and sanitize file paths, sensitive files such as configuration or user data can be accessed, potentially creating or overwriting files. To prevent this vulnerability, validate and sanitize any input that is used to create references to file paths. Also, enforce strict file access controls. For example, choose privileges allowing public-facing applications to access only the required files. In Java, you may also consider using a utility method such as `org.apache.commons.io.FilenameUtils.getName(...)` to only retrieve the file name from the path.

635648771@qq.com requested this Autofix PR for [this finding](https://semgrep.dev/orgs/635648771_personal_org/findings/911924654) from the detection rule [java.servlets.security.httpservlet-path-traversal-deepsemgrep.httpservlet-path-traversal-deepsemgrep](https://semgrep.dev/r/java.servlets.security.httpservlet-path-traversal-deepsemgrep.httpservlet-path-traversal-deepsemgrep).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants