Why this matters
Path traversal attacks exploit server-side request handling to access unauthorized resources. Always validate and sanitize request paths to prevent unauthorized access.
Ensure that all user inputs related to file paths are validated and sanitized to prevent path traversal attacks.
Path traversal attacks exploit server-side request handling to access unauthorized resources. Always validate and sanitize request paths to prevent unauthorized access.
Side-by-side examples engineers can pattern-match during review.
@GetMapping("/user")
public String getUser(@RequestParam(value = "id") String id) {
URL url = new URL("http://example.com/api/user/" + id); // Noncompliant
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
}import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
@GetMapping("/user")
public String getUser(@RequestParam(value = "id") String id) {
String encodedId = URLEncoder.encode(id, StandardCharsets.UTF_8);
URL url = new URL("http://example.com/api/user/?id=" + encodedId);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
}@GetMapping("/user")
public String getUser(@RequestParam(value = "id") String id) {
URL url = new URL("http://example.com/api/user/" + id); // Noncompliant
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
}import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
@GetMapping("/user")
public String getUser(@RequestParam(value = "id") String id) {
String encodedId = URLEncoder.encode(id, StandardCharsets.UTF_8);
URL url = new URL("http://example.com/api/user/?id=" + encodedId);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
}From the same buckets as this rule.